@@ -26,26 +26,26 @@ func (c RegistrarClient) GetAccountByPK(pk []byte) (account Account, err error)
26
26
return c .getAccountByPK (pk )
27
27
}
28
28
29
- func (c RegistrarClient ) UpdateAccount (relays [] string , rmbEncKey string ) (err error ) {
30
- return c .updateAccount (relays , rmbEncKey )
29
+ func (c RegistrarClient ) UpdateAccount (opts ... UpdateAccountOpts ) (err error ) {
30
+ return c .updateAccount (opts )
31
31
}
32
32
33
33
func (c RegistrarClient ) EnsureAccount (pk []byte , relays []string , rmbEncKey string ) (account Account , err error ) {
34
34
return c .ensureAccount (pk , relays , rmbEncKey )
35
35
}
36
36
37
- func (c * RegistrarClient ) createAccount (relays []string , rmbEncKey string ) (result Account , err error ) {
37
+ func (c * RegistrarClient ) createAccount (relays []string , rmbEncKey string ) (account Account , err error ) {
38
38
url , err := url .JoinPath (c .baseURL , "accounts" )
39
39
if err != nil {
40
- return result , errors .Wrap (err , "failed to construct registrar url" )
40
+ return account , errors .Wrap (err , "failed to construct registrar url" )
41
41
}
42
42
43
43
publicKeyBase64 := base64 .StdEncoding .EncodeToString (c .keyPair .publicKey )
44
44
45
45
timestamp := time .Now ().Unix ()
46
46
signature := c .signRequest (timestamp )
47
47
48
- account := map [string ]any {
48
+ data := map [string ]any {
49
49
"public_key" : publicKeyBase64 ,
50
50
"signature" : signature ,
51
51
"timestamp" : timestamp ,
@@ -54,25 +54,25 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (resu
54
54
}
55
55
56
56
var body bytes.Buffer
57
- err = json .NewEncoder (& body ).Encode (account )
57
+ err = json .NewEncoder (& body ).Encode (data )
58
58
if err != nil {
59
- return result , errors .Wrap (err , "failed to parse request body" )
59
+ return account , errors .Wrap (err , "failed to parse request body" )
60
60
}
61
61
62
62
resp , err := c .httpClient .Post (url , "application/json" , & body )
63
63
if err != nil {
64
- return result , errors .Wrap (err , "failed to send request to the registrar" )
64
+ return account , errors .Wrap (err , "failed to send request to the registrar" )
65
65
}
66
66
67
67
if resp .StatusCode != http .StatusCreated {
68
68
err = parseResponseError (resp .Body )
69
- return result , errors .Wrapf (err , "failed to create account with status %s" , resp .Status )
69
+ return account , errors .Wrapf (err , "failed to create account with status %s" , resp .Status )
70
70
}
71
71
defer resp .Body .Close ()
72
72
73
- err = json .NewDecoder (resp .Body ).Decode (& result )
73
+ err = json .NewDecoder (resp .Body ).Decode (& account )
74
74
75
- c .twinID = result .TwinID
75
+ c .twinID = account .TwinID
76
76
return
77
77
}
78
78
@@ -114,24 +114,57 @@ func (c RegistrarClient) getAccount(id uint64) (account Account, err error) {
114
114
return
115
115
}
116
116
117
- func (c RegistrarClient ) updateAccount ( relays []string , rmbEncKey string ) (err error ) {
118
- url , err := url .JoinPath (c .baseURL , "accounts" , fmt . Sprint ( c . twinID ) )
117
+ func (c RegistrarClient ) getAccountByPK ( pk []byte ) (account Account , err error ) {
118
+ url , err := url .JoinPath (c .baseURL , "accounts" )
119
119
if err != nil {
120
- return errors .Wrap (err , "failed to construct registrar url" )
120
+ return account , errors .Wrap (err , "failed to construct registrar url" )
121
+ }
122
+
123
+ publicKeyBase64 := base64 .StdEncoding .EncodeToString (pk )
124
+
125
+ req , err := http .NewRequest ("GET" , url , nil )
126
+ if err != nil {
127
+ return account , err
128
+ }
129
+
130
+ q := req .URL .Query ()
131
+ q .Add ("public_key" , publicKeyBase64 )
132
+ req .URL .RawQuery = q .Encode ()
133
+
134
+ resp , err := c .httpClient .Do (req )
135
+ if err != nil {
136
+ return account , err
121
137
}
122
138
123
- acc := map [string ]any {}
139
+ if resp == nil {
140
+ return account , errors .New ("no response received" )
141
+ }
142
+ defer resp .Body .Close ()
124
143
125
- if len ( relays ) != 0 {
126
- acc [ "relays" ] = relays
144
+ if resp . StatusCode == http . StatusNotFound {
145
+ return account , ErrorAccountNotFround
127
146
}
128
147
129
- if len (rmbEncKey ) != 0 {
130
- acc ["rmb_enc_key" ] = rmbEncKey
148
+ if resp .StatusCode != http .StatusOK {
149
+ err = parseResponseError (resp .Body )
150
+ return account , errors .Wrapf (err , "failed to get account by public_key with status code %s" , resp .Status )
151
+ }
152
+
153
+ err = json .NewDecoder (resp .Body ).Decode (& account )
154
+
155
+ return account , err
156
+ }
157
+
158
+ func (c RegistrarClient ) updateAccount (opts []UpdateAccountOpts ) (err error ) {
159
+ url , err := url .JoinPath (c .baseURL , "accounts" , fmt .Sprint (c .twinID ))
160
+ if err != nil {
161
+ return errors .Wrap (err , "failed to construct registrar url" )
131
162
}
132
163
133
164
var body bytes.Buffer
134
- err = json .NewEncoder (& body ).Encode (acc )
165
+ data := parseUpdateAccountOpts (opts )
166
+
167
+ err = json .NewEncoder (& body ).Encode (data )
135
168
if err != nil {
136
169
return errors .Wrap (err , "failed to parse request body" )
137
170
}
@@ -162,45 +195,25 @@ func (c RegistrarClient) updateAccount(relays []string, rmbEncKey string) (err e
162
195
return
163
196
}
164
197
165
- func (c RegistrarClient ) getAccountByPK (pk []byte ) (account Account , err error ) {
166
- url , err := url .JoinPath (c .baseURL , "accounts" )
167
- if err != nil {
168
- return account , errors .Wrap (err , "failed to construct registrar url" )
169
- }
170
-
171
- publicKeyBase64 := base64 .StdEncoding .EncodeToString (pk )
172
-
173
- req , err := http .NewRequest ("GET" , url , nil )
174
- if err != nil {
175
- return account , err
176
- }
177
-
178
- q := req .URL .Query ()
179
- q .Add ("public_key" , publicKeyBase64 )
180
- req .URL .RawQuery = q .Encode ()
181
-
182
- resp , err := c .httpClient .Do (req )
183
- if err != nil {
184
- return account , err
185
- }
198
+ type accountCfg struct {
199
+ relays []string
200
+ rmbEncKey string
201
+ }
186
202
187
- if resp == nil {
188
- return account , errors .New ("no response received" )
189
- }
190
- defer resp .Body .Close ()
203
+ type (
204
+ UpdateAccountOpts func (* accountCfg )
205
+ )
191
206
192
- if resp .StatusCode == http .StatusNotFound {
193
- return account , ErrorAccountNotFround
207
+ func UpdateAccountWithRelays (relays []string ) UpdateAccountOpts {
208
+ return func (n * accountCfg ) {
209
+ n .relays = relays
194
210
}
211
+ }
195
212
196
- if resp . StatusCode != http . StatusOK {
197
- err = parseResponseError ( resp . Body )
198
- return account , errors . Wrapf ( err , "failed to get account by public_key with status code %s" , resp . Status )
213
+ func UpdateAccountWithRMBEncKey ( rmbEncKey string ) UpdateAccountOpts {
214
+ return func ( n * accountCfg ) {
215
+ n . rmbEncKey = rmbEncKey
199
216
}
200
-
201
- err = json .NewDecoder (resp .Body ).Decode (& account )
202
-
203
- return account , err
204
217
}
205
218
206
219
func (c RegistrarClient ) ensureAccount (pk []byte , relays []string , rmbEncKey string ) (account Account , err error ) {
@@ -227,3 +240,26 @@ func (c *RegistrarClient) ensureTwinID() error {
227
240
c .twinID = twin .TwinID
228
241
return nil
229
242
}
243
+
244
+ func parseUpdateAccountOpts (opts []UpdateAccountOpts ) map [string ]any {
245
+ cfg := accountCfg {
246
+ rmbEncKey : "" ,
247
+ relays : []string {},
248
+ }
249
+
250
+ for _ , opt := range opts {
251
+ opt (& cfg )
252
+ }
253
+
254
+ data := map [string ]any {}
255
+
256
+ if len (cfg .relays ) != 0 {
257
+ data ["relays" ] = cfg .relays
258
+ }
259
+
260
+ if len (cfg .rmbEncKey ) != 0 {
261
+ data ["rmb_enc_key" ] = cfg .rmbEncKey
262
+ }
263
+
264
+ return data
265
+ }
0 commit comments