@@ -112,16 +112,13 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
112
112
func (s * RPCServer ) UpdateAccount (_ context.Context ,
113
113
req * litrpc.UpdateAccountRequest ) (* litrpc.Account , error ) {
114
114
115
- log .Infof ("[updateaccount] id=%s, balance=%d, expiration=%d" , req . Id ,
116
- req .AccountBalance , req .ExpirationDate )
115
+ log .Infof ("[updateaccount] id=%s, label=%v, balance=%d, expiration=%d" ,
116
+ req .Id , req . Label , req . AccountBalance , req .ExpirationDate )
117
117
118
- // Account ID is always a hex string, convert it to our account ID type.
119
- var accountID AccountID
120
- decoded , err := hex .DecodeString (req .Id )
118
+ accountID , err := s .findAccount (req .Id , req .Label )
121
119
if err != nil {
122
- return nil , fmt . Errorf ( "error decoding account ID: %v" , err )
120
+ return nil , err
123
121
}
124
- copy (accountID [:], decoded )
125
122
126
123
// Ask the service to update the account.
127
124
account , err := s .service .UpdateAccount (
@@ -165,15 +162,12 @@ func (s *RPCServer) RemoveAccount(_ context.Context,
165
162
req * litrpc.RemoveAccountRequest ) (* litrpc.RemoveAccountResponse ,
166
163
error ) {
167
164
168
- log .Infof ("[removeaccount] id=%v" , req .Id )
165
+ log .Infof ("[removeaccount] id=%v, label=%v " , req .Id , req . Label )
169
166
170
- // Account ID is always a hex string, convert it to our account ID type.
171
- var accountID AccountID
172
- decoded , err := hex .DecodeString (req .Id )
167
+ accountID , err := s .findAccount (req .Id , req .Label )
173
168
if err != nil {
174
- return nil , fmt . Errorf ( "error decoding account ID: %v" , err )
169
+ return nil , err
175
170
}
176
- copy (accountID [:], decoded )
177
171
178
172
// Now remove the account.
179
173
err = s .service .RemoveAccount (accountID )
@@ -184,6 +178,49 @@ func (s *RPCServer) RemoveAccount(_ context.Context,
184
178
return & litrpc.RemoveAccountResponse {}, nil
185
179
}
186
180
181
+ // findAccount finds an account by its ID or label.
182
+ func (s * RPCServer ) findAccount (id string , label string ) (AccountID , error ) {
183
+ switch {
184
+ case id != "" && label != "" :
185
+ return AccountID {}, fmt .Errorf ("either account ID or label " +
186
+ "must be specified, not both" )
187
+
188
+ case id != "" :
189
+ // Account ID is always a hex string, convert it to our account
190
+ // ID type.
191
+ var accountID AccountID
192
+ decoded , err := hex .DecodeString (id )
193
+ if err != nil {
194
+ return AccountID {}, fmt .Errorf ("error decoding " +
195
+ "account ID: %v" , err )
196
+ }
197
+ copy (accountID [:], decoded )
198
+
199
+ return accountID , nil
200
+
201
+ case label != "" :
202
+ // We need to find the account by its label.
203
+ accounts , err := s .service .Accounts ()
204
+ if err != nil {
205
+ return AccountID {}, fmt .Errorf ("unable to list " +
206
+ "accounts: %w" , err )
207
+ }
208
+
209
+ for _ , acct := range accounts {
210
+ if acct .Label == label {
211
+ return acct .ID , nil
212
+ }
213
+ }
214
+
215
+ return AccountID {}, fmt .Errorf ("unable to find account " +
216
+ "with label '%s'" , label )
217
+
218
+ default :
219
+ return AccountID {}, fmt .Errorf ("either account ID or label " +
220
+ "must be specified" )
221
+ }
222
+ }
223
+
187
224
// marshalAccount converts an account into its RPC counterpart.
188
225
func marshalAccount (acct * OffChainBalanceAccount ) * litrpc.Account {
189
226
rpcAccount := & litrpc.Account {
0 commit comments