@@ -50,8 +50,8 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
5050 req * litrpc.CreateAccountRequest ) (* litrpc.CreateAccountResponse ,
5151 error ) {
5252
53- log .Infof ("[createaccount] balance=%d, expiration=%d" ,
54- req .AccountBalance , req .ExpirationDate )
53+ log .Infof ("[createaccount] label=%v, balance=%d, expiration=%d" ,
54+ req .Label , req . AccountBalance , req .ExpirationDate )
5555
5656 var (
5757 balanceMsat lnwire.MilliSatoshi
@@ -70,9 +70,11 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
7070 balanceMsat = lnwire .NewMSatFromSatoshis (balance )
7171
7272 // Create the actual account in the macaroon account store.
73- account , err := s .service .NewAccount (balanceMsat , expirationDate )
73+ account , err := s .service .NewAccount (
74+ balanceMsat , expirationDate , req .Label ,
75+ )
7476 if err != nil {
75- return nil , fmt .Errorf ("unable to create account: %v " , err )
77+ return nil , fmt .Errorf ("unable to create account: %w " , err )
7678 }
7779
7880 var rootKeyIdSuffix [4 ]byte
@@ -91,12 +93,12 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
9193 }},
9294 })
9395 if err != nil {
94- return nil , fmt .Errorf ("error baking account macaroon: %v " , err )
96+ return nil , fmt .Errorf ("error baking account macaroon: %w " , err )
9597 }
9698
9799 macBytes , err := hex .DecodeString (macHex )
98100 if err != nil {
99- return nil , fmt .Errorf ("error decoding account macaroon: %v " ,
101+ return nil , fmt .Errorf ("error decoding account macaroon: %w " ,
100102 err )
101103 }
102104
@@ -110,16 +112,13 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
110112func (s * RPCServer ) UpdateAccount (_ context.Context ,
111113 req * litrpc.UpdateAccountRequest ) (* litrpc.Account , error ) {
112114
113- log .Infof ("[updateaccount] id=%s, balance=%d, expiration=%d" , req . Id ,
114- 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 )
115117
116- // Account ID is always a hex string, convert it to our account ID type.
117- var accountID AccountID
118- decoded , err := hex .DecodeString (req .Id )
118+ accountID , err := s .findAccount (req .Id , req .Label )
119119 if err != nil {
120- return nil , fmt . Errorf ( "error decoding account ID: %v" , err )
120+ return nil , err
121121 }
122- copy (accountID [:], decoded )
123122
124123 // Ask the service to update the account.
125124 account , err := s .service .UpdateAccount (
@@ -142,7 +141,7 @@ func (s *RPCServer) ListAccounts(context.Context,
142141 // Retrieve all accounts from the macaroon account store.
143142 accts , err := s .service .Accounts ()
144143 if err != nil {
145- return nil , fmt .Errorf ("unable to list accounts: %v " , err )
144+ return nil , fmt .Errorf ("unable to list accounts: %w " , err )
146145 }
147146
148147 // Map the response into the proper response type and return it.
@@ -158,30 +157,89 @@ func (s *RPCServer) ListAccounts(context.Context,
158157 }, nil
159158}
160159
160+ // AccountInfo returns the account with the given ID or label.
161+ func (s * RPCServer ) AccountInfo (_ context.Context ,
162+ req * litrpc.AccountInfoRequest ) (* litrpc.Account , error ) {
163+
164+ log .Infof ("[accountinfo] id=%v, label=%v" , req .Id , req .Label )
165+
166+ accountID , err := s .findAccount (req .Id , req .Label )
167+ if err != nil {
168+ return nil , err
169+ }
170+
171+ dbAccount , err := s .service .Account (accountID )
172+ if err != nil {
173+ return nil , fmt .Errorf ("error retrieving account: %w" , err )
174+ }
175+
176+ return marshalAccount (dbAccount ), nil
177+ }
178+
161179// RemoveAccount removes the given account from the account database.
162180func (s * RPCServer ) RemoveAccount (_ context.Context ,
163181 req * litrpc.RemoveAccountRequest ) (* litrpc.RemoveAccountResponse ,
164182 error ) {
165183
166- log .Infof ("[removeaccount] id=%v" , req .Id )
184+ log .Infof ("[removeaccount] id=%v, label=%v " , req .Id , req . Label )
167185
168- // Account ID is always a hex string, convert it to our account ID type.
169- var accountID AccountID
170- decoded , err := hex .DecodeString (req .Id )
186+ accountID , err := s .findAccount (req .Id , req .Label )
171187 if err != nil {
172- return nil , fmt . Errorf ( "error decoding account ID: %v" , err )
188+ return nil , err
173189 }
174- copy (accountID [:], decoded )
175190
176191 // Now remove the account.
177192 err = s .service .RemoveAccount (accountID )
178193 if err != nil {
179- return nil , fmt .Errorf ("error removing account: %v " , err )
194+ return nil , fmt .Errorf ("error removing account: %w " , err )
180195 }
181196
182197 return & litrpc.RemoveAccountResponse {}, nil
183198}
184199
200+ // findAccount finds an account by its ID or label.
201+ func (s * RPCServer ) findAccount (id string , label string ) (AccountID , error ) {
202+ switch {
203+ case id != "" && label != "" :
204+ return AccountID {}, fmt .Errorf ("either account ID or label " +
205+ "must be specified, not both" )
206+
207+ case id != "" :
208+ // Account ID is always a hex string, convert it to our account
209+ // ID type.
210+ var accountID AccountID
211+ decoded , err := hex .DecodeString (id )
212+ if err != nil {
213+ return AccountID {}, fmt .Errorf ("error decoding " +
214+ "account ID: %w" , err )
215+ }
216+ copy (accountID [:], decoded )
217+
218+ return accountID , nil
219+
220+ case label != "" :
221+ // We need to find the account by its label.
222+ accounts , err := s .service .Accounts ()
223+ if err != nil {
224+ return AccountID {}, fmt .Errorf ("unable to list " +
225+ "accounts: %w" , err )
226+ }
227+
228+ for _ , acct := range accounts {
229+ if acct .Label == label {
230+ return acct .ID , nil
231+ }
232+ }
233+
234+ return AccountID {}, fmt .Errorf ("unable to find account " +
235+ "with label '%s'" , label )
236+
237+ default :
238+ return AccountID {}, fmt .Errorf ("either account ID or label " +
239+ "must be specified" )
240+ }
241+ }
242+
185243// marshalAccount converts an account into its RPC counterpart.
186244func marshalAccount (acct * OffChainBalanceAccount ) * litrpc.Account {
187245 rpcAccount := & litrpc.Account {
@@ -196,6 +254,7 @@ func marshalAccount(acct *OffChainBalanceAccount) *litrpc.Account {
196254 Payments : make (
197255 []* litrpc.AccountPayment , 0 , len (acct .Payments ),
198256 ),
257+ Label : acct .Label ,
199258 }
200259
201260 for hash := range acct .Invoices {
0 commit comments