Skip to content

Commit 8527969

Browse files
committed
accounts: remove accountFromMacaroon helper
We only use it in one place and we can just handle the unwrap of the option there.
1 parent 4a5259e commit 8527969

File tree

1 file changed

+8
-27
lines changed

1 file changed

+8
-27
lines changed

accounts/interceptor.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (s *InterceptorService) Intercept(ctx context.Context,
8181
return mid.RPCErrString(req, "error parsing macaroon: %v", err)
8282
}
8383

84-
acctID, err := accountFromMacaroon(mac)
84+
acctID, err := IDFromCaveats(mac.Caveats())
8585
if err != nil {
8686
return mid.RPCErrString(
8787
req, "error parsing account from macaroon: %v", err,
@@ -91,15 +91,17 @@ func (s *InterceptorService) Intercept(ctx context.Context,
9191
// No account lock in the macaroon, something's weird. The interceptor
9292
// wouldn't have been triggered if there was no caveat, so we do expect
9393
// a macaroon here.
94-
if acctID == nil {
95-
return mid.RPCErrString(req, "expected account ID in "+
96-
"macaroon caveat")
94+
accountID, err := acctID.UnwrapOrErr(
95+
fmt.Errorf("expected account ID in macaroon caveat"),
96+
)
97+
if err != nil {
98+
return mid.RPCErr(req, err)
9799
}
98100

99-
acct, err := s.Account(ctx, *acctID)
101+
acct, err := s.Account(ctx, accountID)
100102
if err != nil {
101103
return mid.RPCErrString(
102-
req, "error getting account %x: %v", acctID[:], err,
104+
req, "error getting account %x: %v", accountID[:], err,
103105
)
104106
}
105107

@@ -208,27 +210,6 @@ func parseRPCMessage(msg *lnrpc.RPCMessage) (proto.Message, error) {
208210
return parsedMsg, nil
209211
}
210212

211-
// accountFromMacaroon attempts to extract an account ID from the custom account
212-
// caveat in the macaroon.
213-
func accountFromMacaroon(mac *macaroon.Macaroon) (*AccountID, error) {
214-
if mac == nil {
215-
return nil, nil
216-
}
217-
218-
// Extract the account caveat from the macaroon.
219-
accountID, err := IDFromCaveats(mac.Caveats())
220-
if err != nil {
221-
return nil, err
222-
}
223-
224-
var id *AccountID
225-
accountID.WhenSome(func(aID AccountID) {
226-
id = &aID
227-
})
228-
229-
return id, nil
230-
}
231-
232213
// CaveatFromID creates a custom caveat that can be used to bind a macaroon to
233214
// a certain account.
234215
func CaveatFromID(id AccountID) macaroon.Caveat {

0 commit comments

Comments
 (0)