@@ -2,7 +2,6 @@ package client
2
2
3
3
import (
4
4
"bytes"
5
- "crypto/ed25519"
6
5
"encoding/base64"
7
6
"encoding/json"
8
7
"fmt"
@@ -11,44 +10,83 @@ import (
11
10
"time"
12
11
13
12
"github.com/pkg/errors"
13
+ "github.com/vedhavyas/go-subkey/v2"
14
14
)
15
15
16
16
var ErrorAccountNotFround = fmt .Errorf ("failed to get requested account from node regiatrar" )
17
17
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 ) {
19
19
return c .createAccount (relays , rmbEncKey )
20
20
}
21
21
22
- func (c RegistrarClient ) GetAccount (id uint64 ) (account Account , err error ) {
22
+ func (c * RegistrarClient ) GetAccount (id uint64 ) (account Account , err error ) {
23
23
return c .getAccount (id )
24
24
}
25
25
26
- func (c RegistrarClient ) GetAccountByPK (pk []byte ) (account Account , err error ) {
26
+ func (c * RegistrarClient ) GetAccountByPK (pk []byte ) (account Account , err error ) {
27
27
return c .getAccountByPK (pk )
28
28
}
29
29
30
- func (c RegistrarClient ) UpdateAccount (opts ... UpdateAccountOpts ) (err error ) {
30
+ func (c * RegistrarClient ) UpdateAccount (opts ... UpdateAccountOpts ) (err error ) {
31
31
return c .updateAccount (opts )
32
32
}
33
33
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 ) {
35
56
return c .ensureAccount (relays , rmbEncKey )
36
57
}
37
58
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 ) {
39
60
url , err := url .JoinPath (c .baseURL , "accounts" )
40
61
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" )
42
63
}
43
64
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
+ }
46
75
76
+ c .keyPair = keyPair
77
+ c .mnemonic = mnemonic
78
+
79
+ publicKeyBase64 := base64 .StdEncoding .EncodeToString (c .keyPair .Public ())
80
+
81
+ timestamp := time .Now ().Unix ()
47
82
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
+ }
49
87
50
88
data := map [string ]any {
51
- "public_key" : c .keyPair .publicKey ,
89
+ "public_key" : c .keyPair .Public () ,
52
90
"signature" : signature ,
53
91
"timestamp" : timestamp ,
54
92
"rmb_enc_key" : rmbEncKey ,
@@ -58,17 +96,17 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (acco
58
96
var body bytes.Buffer
59
97
err = json .NewEncoder (& body ).Encode (data )
60
98
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" )
62
100
}
63
101
64
102
resp , err := c .httpClient .Post (url , "application/json" , & body )
65
103
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" )
67
105
}
68
106
69
107
if resp .StatusCode != http .StatusCreated {
70
108
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 )
72
110
}
73
111
defer resp .Body .Close ()
74
112
@@ -78,7 +116,7 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (acco
78
116
return
79
117
}
80
118
81
- func (c RegistrarClient ) getAccount (id uint64 ) (account Account , err error ) {
119
+ func (c * RegistrarClient ) getAccount (id uint64 ) (account Account , err error ) {
82
120
url , err := url .JoinPath (c .baseURL , "accounts" )
83
121
if err != nil {
84
122
return account , errors .Wrap (err , "failed to construct registrar url" )
@@ -116,7 +154,7 @@ func (c RegistrarClient) getAccount(id uint64) (account Account, err error) {
116
154
return
117
155
}
118
156
119
- func (c RegistrarClient ) getAccountByPK (pk []byte ) (account Account , err error ) {
157
+ func (c * RegistrarClient ) getAccountByPK (pk []byte ) (account Account , err error ) {
120
158
url , err := url .JoinPath (c .baseURL , "accounts" )
121
159
if err != nil {
122
160
return account , errors .Wrap (err , "failed to construct registrar url" )
@@ -157,7 +195,7 @@ func (c RegistrarClient) getAccountByPK(pk []byte) (account Account, err error)
157
195
return account , err
158
196
}
159
197
160
- func (c RegistrarClient ) updateAccount (opts []UpdateAccountOpts ) (err error ) {
198
+ func (c * RegistrarClient ) updateAccount (opts []UpdateAccountOpts ) (err error ) {
161
199
err = c .ensureTwinID ()
162
200
if err != nil {
163
201
return errors .Wrap (err , "failed to ensure twin id" )
@@ -180,7 +218,11 @@ func (c RegistrarClient) updateAccount(opts []UpdateAccountOpts) (err error) {
180
218
return
181
219
}
182
220
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 )
184
226
req .Header .Set ("Content-Type" , "application/json" )
185
227
186
228
resp , err := c .httpClient .Do (req )
@@ -200,44 +242,20 @@ func (c RegistrarClient) updateAccount(opts []UpdateAccountOpts) (err error) {
200
242
return
201
243
}
202
244
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 ())
226
247
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 )
230
249
}
231
-
232
- return
250
+ return account , err
233
251
}
234
252
235
253
func (c * RegistrarClient ) ensureTwinID () error {
236
254
if c .twinID != 0 {
237
255
return nil
238
256
}
239
257
240
- twin , err := c .getAccountByPK (c .keyPair .publicKey )
258
+ twin , err := c .getAccountByPK (c .keyPair .Public () )
241
259
if err != nil {
242
260
return errors .Wrap (err , "failed to get the account of the node, registrar client was not set up properly" )
243
261
}
0 commit comments