|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/client" |
| 8 | +) |
| 9 | + |
| 10 | +var urls = map[string]string{ |
| 11 | + "dev": "https://registrar.dev4.grid.tf/v1", |
| 12 | + "qa": "https://registrar.qa4.grid.tf/v1", |
| 13 | + "test": "https://registrar.test4.grid.tf/v1", |
| 14 | + "main": "https://registrar.prod4.grid.tf/v1", |
| 15 | +} |
| 16 | + |
| 17 | +func CreaeteAccount(network string, relays []string, rmbEncKey string, mnemonicOrSeed ...string) (account client.Account, mnemonic string, err error) { |
| 18 | + u, ok := urls[network] |
| 19 | + if !ok { |
| 20 | + return account, "", fmt.Errorf("invalid network %s", network) |
| 21 | + } |
| 22 | + |
| 23 | + var cli client.RegistrarClient |
| 24 | + cli, err = client.NewRegistrarClient(u, mnemonicOrSeed...) |
| 25 | + if err != nil { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + return cli.CreateAccount(relays, rmbEncKey) |
| 30 | +} |
| 31 | + |
| 32 | +func GetAccount(network string, twinID uint64, pk string) (account client.Account, err error) { |
| 33 | + u, ok := urls[network] |
| 34 | + if !ok { |
| 35 | + return account, fmt.Errorf("invalid network %s", network) |
| 36 | + } |
| 37 | + |
| 38 | + publicKey, err := hex.DecodeString(pk) |
| 39 | + if err != nil { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + cli, err := client.NewRegistrarClient(u) |
| 44 | + if err != nil { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if twinID != 0 { |
| 49 | + return cli.GetAccount(twinID) |
| 50 | + } else if len(publicKey) != 0 { |
| 51 | + return cli.GetAccountByPK(publicKey) |
| 52 | + } |
| 53 | + |
| 54 | + return account, fmt.Errorf("you need to provide either twin id or public key to load an account") |
| 55 | +} |
| 56 | + |
| 57 | +func UpdateAccount(mnemonic string, network string, relays []string, rmbEncKey string) (err error) { |
| 58 | + u, ok := urls[network] |
| 59 | + if !ok { |
| 60 | + return fmt.Errorf("invalid network %s", network) |
| 61 | + } |
| 62 | + |
| 63 | + if len(mnemonic) == 0 { |
| 64 | + return fmt.Errorf("can not initialize registrar client with no mnemonic") |
| 65 | + } |
| 66 | + |
| 67 | + cli, err := client.NewRegistrarClient(u, mnemonic) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + var opts []client.UpdateAccountOpts |
| 73 | + if len(relays) > 0 { |
| 74 | + opts = append(opts, client.UpdateAccountWithRelays(relays)) |
| 75 | + } |
| 76 | + |
| 77 | + if len(rmbEncKey) > 0 { |
| 78 | + opts = append(opts, client.UpdateAccountWithRMBEncKey(rmbEncKey)) |
| 79 | + } |
| 80 | + |
| 81 | + return cli.UpdateAccount(opts...) |
| 82 | +} |
0 commit comments