Skip to content

Commit 1e08f4a

Browse files
committed
multi: allow account update and removal by label
1 parent 138d33b commit 1e08f4a

File tree

8 files changed

+246
-64
lines changed

8 files changed

+246
-64
lines changed

accounts/rpcserver.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,13 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
112112
func (s *RPCServer) UpdateAccount(_ context.Context,
113113
req *litrpc.UpdateAccountRequest) (*litrpc.Account, error) {
114114

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)
117117

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)
121119
if err != nil {
122-
return nil, fmt.Errorf("error decoding account ID: %v", err)
120+
return nil, err
123121
}
124-
copy(accountID[:], decoded)
125122

126123
// Ask the service to update the account.
127124
account, err := s.service.UpdateAccount(
@@ -165,15 +162,12 @@ func (s *RPCServer) RemoveAccount(_ context.Context,
165162
req *litrpc.RemoveAccountRequest) (*litrpc.RemoveAccountResponse,
166163
error) {
167164

168-
log.Infof("[removeaccount] id=%v", req.Id)
165+
log.Infof("[removeaccount] id=%v, label=%v", req.Id, req.Label)
169166

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)
173168
if err != nil {
174-
return nil, fmt.Errorf("error decoding account ID: %v", err)
169+
return nil, err
175170
}
176-
copy(accountID[:], decoded)
177171

178172
// Now remove the account.
179173
err = s.service.RemoveAccount(accountID)
@@ -184,6 +178,49 @@ func (s *RPCServer) RemoveAccount(_ context.Context,
184178
return &litrpc.RemoveAccountResponse{}, nil
185179
}
186180

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+
187224
// marshalAccount converts an account into its RPC counterpart.
188225
func marshalAccount(acct *OffChainBalanceAccount) *litrpc.Account {
189226
rpcAccount := &litrpc.Account{

app/src/types/generated/lit-accounts_pb.d.ts

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/types/generated/lit-accounts_pb.js

Lines changed: 56 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

litrpc/lit-accounts.pb.go

Lines changed: 67 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)