@@ -2,7 +2,6 @@ package client
22
33import (
44 "bytes"
5- "crypto/ed25519"
65 "encoding/base64"
76 "encoding/json"
87 "fmt"
@@ -11,44 +10,83 @@ import (
1110 "time"
1211
1312 "github.com/pkg/errors"
13+ "github.com/vedhavyas/go-subkey/v2"
1414)
1515
1616var ErrorAccountNotFround = fmt .Errorf ("failed to get requested account from node regiatrar" )
1717
18- func (c RegistrarClient ) CreateAccount (relays []string , rmbEncKey string ) (account Account , err error ) {
18+ func (c * RegistrarClient ) CreateAccount (relays []string , rmbEncKey string ) (account Account , mnemonic string , err error ) {
1919 return c .createAccount (relays , rmbEncKey )
2020}
2121
22- func (c RegistrarClient ) GetAccount (id uint64 ) (account Account , err error ) {
22+ func (c * RegistrarClient ) GetAccount (id uint64 ) (account Account , err error ) {
2323 return c .getAccount (id )
2424}
2525
26- func (c RegistrarClient ) GetAccountByPK (pk []byte ) (account Account , err error ) {
26+ func (c * RegistrarClient ) GetAccountByPK (pk []byte ) (account Account , err error ) {
2727 return c .getAccountByPK (pk )
2828}
2929
30- func (c RegistrarClient ) UpdateAccount (opts ... UpdateAccountOpts ) (err error ) {
30+ func (c * RegistrarClient ) UpdateAccount (opts ... UpdateAccountOpts ) (err error ) {
3131 return c .updateAccount (opts )
3232}
3333
34- func (c RegistrarClient ) EnsureAccount (relays []string , rmbEncKey string ) (account Account , err error ) {
34+ type accountCfg struct {
35+ relays []string
36+ rmbEncKey string
37+ }
38+
39+ type (
40+ UpdateAccountOpts func (* accountCfg )
41+ )
42+
43+ func UpdateAccountWithRelays (relays []string ) UpdateAccountOpts {
44+ return func (n * accountCfg ) {
45+ n .relays = relays
46+ }
47+ }
48+
49+ func UpdateAccountWithRMBEncKey (rmbEncKey string ) UpdateAccountOpts {
50+ return func (n * accountCfg ) {
51+ n .rmbEncKey = rmbEncKey
52+ }
53+ }
54+
55+ func (c * RegistrarClient ) EnsureAccount (relays []string , rmbEncKey string ) (account Account , err error ) {
3556 return c .ensureAccount (relays , rmbEncKey )
3657}
3758
38- func (c * RegistrarClient ) createAccount (relays []string , rmbEncKey string ) (account Account , err error ) {
59+ func (c * RegistrarClient ) createAccount (relays []string , rmbEncKey string ) (account Account , mnemonic string , err error ) {
3960 url , err := url .JoinPath (c .baseURL , "accounts" )
4061 if err != nil {
41- return account , errors .Wrap (err , "failed to construct registrar url" )
62+ return account , mnemonic , errors .Wrap (err , "failed to construct registrar url" )
4263 }
4364
44- timestamp := time .Now ().Unix ()
45- publicKeyBase64 := base64 .StdEncoding .EncodeToString (c .keyPair .publicKey )
65+ var keyPair subkey.KeyPair
66+ if len (c .mnemonic ) != 0 {
67+ mnemonic = c .mnemonic
68+ keyPair , err = parseKeysFromMnemonicOrSeed (c .mnemonic )
69+ } else {
70+ mnemonic , keyPair , err = generateNewMnemonic ()
71+ }
72+ if err != nil {
73+ return account , mnemonic , err
74+ }
4675
76+ c .keyPair = keyPair
77+ c .mnemonic = mnemonic
78+
79+ publicKeyBase64 := base64 .StdEncoding .EncodeToString (c .keyPair .Public ())
80+
81+ timestamp := time .Now ().Unix ()
4782 challenge := []byte (fmt .Sprintf ("%d:%v" , timestamp , publicKeyBase64 ))
48- signature := ed25519 .Sign (c .keyPair .privateKey , challenge )
83+ signature , err := keyPair .Sign (challenge )
84+ if err != nil {
85+ return account , mnemonic , errors .Wrap (err , "failed to sign account creation request" )
86+ }
4987
5088 data := map [string ]any {
51- "public_key" : c .keyPair .publicKey ,
89+ "public_key" : c .keyPair .Public () ,
5290 "signature" : signature ,
5391 "timestamp" : timestamp ,
5492 "rmb_enc_key" : rmbEncKey ,
@@ -58,17 +96,17 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (acco
5896 var body bytes.Buffer
5997 err = json .NewEncoder (& body ).Encode (data )
6098 if err != nil {
61- return account , errors .Wrap (err , "failed to parse request body" )
99+ return account , mnemonic , errors .Wrap (err , "failed to parse request body" )
62100 }
63101
64102 resp , err := c .httpClient .Post (url , "application/json" , & body )
65103 if err != nil {
66- return account , errors .Wrap (err , "failed to send request to the registrar" )
104+ return account , mnemonic , errors .Wrap (err , "failed to send request to the registrar" )
67105 }
68106
69107 if resp .StatusCode != http .StatusCreated {
70108 err = parseResponseError (resp .Body )
71- return account , errors .Wrapf (err , "failed to create account with status %s" , resp .Status )
109+ return account , mnemonic , errors .Wrapf (err , "failed to create account with status %s" , resp .Status )
72110 }
73111 defer resp .Body .Close ()
74112
@@ -78,7 +116,7 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (acco
78116 return
79117}
80118
81- func (c RegistrarClient ) getAccount (id uint64 ) (account Account , err error ) {
119+ func (c * RegistrarClient ) getAccount (id uint64 ) (account Account , err error ) {
82120 url , err := url .JoinPath (c .baseURL , "accounts" )
83121 if err != nil {
84122 return account , errors .Wrap (err , "failed to construct registrar url" )
@@ -116,7 +154,7 @@ func (c RegistrarClient) getAccount(id uint64) (account Account, err error) {
116154 return
117155}
118156
119- func (c RegistrarClient ) getAccountByPK (pk []byte ) (account Account , err error ) {
157+ func (c * RegistrarClient ) getAccountByPK (pk []byte ) (account Account , err error ) {
120158 url , err := url .JoinPath (c .baseURL , "accounts" )
121159 if err != nil {
122160 return account , errors .Wrap (err , "failed to construct registrar url" )
@@ -157,7 +195,7 @@ func (c RegistrarClient) getAccountByPK(pk []byte) (account Account, err error)
157195 return account , err
158196}
159197
160- func (c RegistrarClient ) updateAccount (opts []UpdateAccountOpts ) (err error ) {
198+ func (c * RegistrarClient ) updateAccount (opts []UpdateAccountOpts ) (err error ) {
161199 err = c .ensureTwinID ()
162200 if err != nil {
163201 return errors .Wrap (err , "failed to ensure twin id" )
@@ -180,7 +218,11 @@ func (c RegistrarClient) updateAccount(opts []UpdateAccountOpts) (err error) {
180218 return
181219 }
182220
183- req .Header .Set ("X-Auth" , c .signRequest (time .Now ().Unix ()))
221+ authHeader , err := c .signRequest (time .Now ().Unix ())
222+ if err != nil {
223+ return errors .Wrap (err , "failed to sign request" )
224+ }
225+ req .Header .Set ("X-Auth" , authHeader )
184226 req .Header .Set ("Content-Type" , "application/json" )
185227
186228 resp , err := c .httpClient .Do (req )
@@ -200,44 +242,20 @@ func (c RegistrarClient) updateAccount(opts []UpdateAccountOpts) (err error) {
200242 return
201243}
202244
203- type accountCfg struct {
204- relays []string
205- rmbEncKey string
206- }
207-
208- type (
209- UpdateAccountOpts func (* accountCfg )
210- )
211-
212- func UpdateAccountWithRelays (relays []string ) UpdateAccountOpts {
213- return func (n * accountCfg ) {
214- n .relays = relays
215- }
216- }
217-
218- func UpdateAccountWithRMBEncKey (rmbEncKey string ) UpdateAccountOpts {
219- return func (n * accountCfg ) {
220- n .rmbEncKey = rmbEncKey
221- }
222- }
223-
224- func (c RegistrarClient ) ensureAccount (relays []string , rmbEncKey string ) (account Account , err error ) {
225- account , err = c .GetAccountByPK (c .keyPair .publicKey )
245+ func (c * RegistrarClient ) ensureAccount (relays []string , rmbEncKey string ) (account Account , err error ) {
246+ account , err = c .GetAccountByPK (c .keyPair .Public ())
226247 if errors .Is (err , ErrorAccountNotFround ) {
227- return c .CreateAccount (relays , rmbEncKey )
228- } else if err != nil {
229- return account , errors .Wrap (err , "failed to get account from the registrar" )
248+ account , _ , err = c .CreateAccount (relays , rmbEncKey )
230249 }
231-
232- return
250+ return account , err
233251}
234252
235253func (c * RegistrarClient ) ensureTwinID () error {
236254 if c .twinID != 0 {
237255 return nil
238256 }
239257
240- twin , err := c .getAccountByPK (c .keyPair .publicKey )
258+ twin , err := c .getAccountByPK (c .keyPair .Public () )
241259 if err != nil {
242260 return errors .Wrap (err , "failed to get the account of the node, registrar client was not set up properly" )
243261 }
0 commit comments