@@ -50,8 +50,8 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
50
50
req * litrpc.CreateAccountRequest ) (* litrpc.CreateAccountResponse ,
51
51
error ) {
52
52
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 )
55
55
56
56
var (
57
57
balanceMsat lnwire.MilliSatoshi
@@ -70,9 +70,11 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
70
70
balanceMsat = lnwire .NewMSatFromSatoshis (balance )
71
71
72
72
// 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
+ )
74
76
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 )
76
78
}
77
79
78
80
var rootKeyIdSuffix [4 ]byte
@@ -91,12 +93,12 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
91
93
}},
92
94
})
93
95
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 )
95
97
}
96
98
97
99
macBytes , err := hex .DecodeString (macHex )
98
100
if err != nil {
99
- return nil , fmt .Errorf ("error decoding account macaroon: %v " ,
101
+ return nil , fmt .Errorf ("error decoding account macaroon: %w " ,
100
102
err )
101
103
}
102
104
@@ -110,16 +112,13 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
110
112
func (s * RPCServer ) UpdateAccount (_ context.Context ,
111
113
req * litrpc.UpdateAccountRequest ) (* litrpc.Account , error ) {
112
114
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 )
115
117
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 )
119
119
if err != nil {
120
- return nil , fmt . Errorf ( "error decoding account ID: %v" , err )
120
+ return nil , err
121
121
}
122
- copy (accountID [:], decoded )
123
122
124
123
// Ask the service to update the account.
125
124
account , err := s .service .UpdateAccount (
@@ -142,7 +141,7 @@ func (s *RPCServer) ListAccounts(context.Context,
142
141
// Retrieve all accounts from the macaroon account store.
143
142
accts , err := s .service .Accounts ()
144
143
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 )
146
145
}
147
146
148
147
// Map the response into the proper response type and return it.
@@ -158,30 +157,89 @@ func (s *RPCServer) ListAccounts(context.Context,
158
157
}, nil
159
158
}
160
159
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
+
161
179
// RemoveAccount removes the given account from the account database.
162
180
func (s * RPCServer ) RemoveAccount (_ context.Context ,
163
181
req * litrpc.RemoveAccountRequest ) (* litrpc.RemoveAccountResponse ,
164
182
error ) {
165
183
166
- log .Infof ("[removeaccount] id=%v" , req .Id )
184
+ log .Infof ("[removeaccount] id=%v, label=%v " , req .Id , req . Label )
167
185
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 )
171
187
if err != nil {
172
- return nil , fmt . Errorf ( "error decoding account ID: %v" , err )
188
+ return nil , err
173
189
}
174
- copy (accountID [:], decoded )
175
190
176
191
// Now remove the account.
177
192
err = s .service .RemoveAccount (accountID )
178
193
if err != nil {
179
- return nil , fmt .Errorf ("error removing account: %v " , err )
194
+ return nil , fmt .Errorf ("error removing account: %w " , err )
180
195
}
181
196
182
197
return & litrpc.RemoveAccountResponse {}, nil
183
198
}
184
199
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
+
185
243
// marshalAccount converts an account into its RPC counterpart.
186
244
func marshalAccount (acct * OffChainBalanceAccount ) * litrpc.Account {
187
245
rpcAccount := & litrpc.Account {
@@ -196,6 +254,7 @@ func marshalAccount(acct *OffChainBalanceAccount) *litrpc.Account {
196
254
Payments : make (
197
255
[]* litrpc.AccountPayment , 0 , len (acct .Payments ),
198
256
),
257
+ Label : acct .Label ,
199
258
}
200
259
201
260
for hash := range acct .Invoices {
0 commit comments