From 01bc36ca41a4e4f1eb1cd063fbc19ac9bbffaa3f Mon Sep 17 00:00:00 2001 From: Viktor Torstensson Date: Wed, 1 Oct 2025 15:13:44 +0200 Subject: [PATCH] multi: remove `AddActionReq` `MacaroonIdentifier` As the `MacaroonRootKeyID` field of the `AddActionReq` struct also contains the 4 bytes of the `MacaroonIdentifier`, we change all call sites to instead use the last 4 bytes of the `MacaroonRootKeyID` field. As the `MacaroonIdentifier` field therefore becomes redundant, we also remove it. --- firewall/request_logger.go | 17 +++++--------- firewalldb/actions.go | 9 ++------ firewalldb/actions_kvdb.go | 8 +++---- firewalldb/actions_sql.go | 13 ++++------- firewalldb/actions_test.go | 45 ++++++++++++++++++-------------------- session_rpcserver.go | 4 ++-- 6 files changed, 38 insertions(+), 58 deletions(-) diff --git a/firewall/request_logger.go b/firewall/request_logger.go index df429213..2af27831 100644 --- a/firewall/request_logger.go +++ b/firewall/request_logger.go @@ -9,7 +9,6 @@ import ( "github.com/lightninglabs/lightning-terminal/firewalldb" litmac "github.com/lightninglabs/lightning-terminal/macaroons" mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware" - "github.com/lightninglabs/lightning-terminal/session" "github.com/lightningnetwork/lnd/fn" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/macaroons" @@ -184,8 +183,7 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo, withPayloadData bool) error { var ( - rootKeyID fn.Option[uint64] - macaroonID fn.Option[[4]byte] + rootKeyID fn.Option[uint64] ) if ri.Macaroon != nil { @@ -198,19 +196,14 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo, return fmt.Errorf("could not extract root key ID from "+ "macaroon: %w", err) } - - macID := session.IDFromMacRootKeyID(fullRootKeyID) - rootKeyID = fn.Some(fullRootKeyID) - macaroonID = fn.Some([4]byte(macID)) } actionReq := &firewalldb.AddActionReq{ - SessionID: ri.SessionID, - AccountID: ri.AccountID, - MacaroonIdentifier: macaroonID, - MacaroonRootKeyID: rootKeyID, - RPCMethod: ri.URI, + SessionID: ri.SessionID, + AccountID: ri.AccountID, + MacaroonRootKeyID: rootKeyID, + RPCMethod: ri.URI, } if withPayloadData { diff --git a/firewalldb/actions.go b/firewalldb/actions.go index d9349b38..ddaae65b 100644 --- a/firewalldb/actions.go +++ b/firewalldb/actions.go @@ -34,11 +34,6 @@ const ( // It contains all the information that is needed to create a new Action in the // ActionStateInit State. type AddActionReq struct { - // MacaroonIdentifier is a 4 byte identifier created from the last 4 - // bytes of the root key ID of the macaroon used to perform the action. - // If no macaroon was used for the action, then this will not be set. - MacaroonIdentifier fn.Option[[4]byte] - // MacaroonRootKeyID is the uint64 / full 8 bytes of the root key ID of // the macaroon used to perform the action. // If no macaroon was used for the action, then this will not be set. @@ -52,8 +47,8 @@ type AddActionReq struct { // action was performed with. // // NOTE: for our BoltDB impl, this is not persisted in any way, and we - // populate it by casting the macaroon ID to a session.ID and so is not - // guaranteed to be linked to an existing session. + // populate it by casting the MacaroonRootKeyID to a session.ID and so + // is not guaranteed to be linked to an existing session. SessionID fn.Option[session.ID] // AccountID holds the optional account ID of the account that this diff --git a/firewalldb/actions_kvdb.go b/firewalldb/actions_kvdb.go index 24047f8f..75dc3042 100644 --- a/firewalldb/actions_kvdb.go +++ b/firewalldb/actions_kvdb.go @@ -59,10 +59,11 @@ func (db *BoltDB) AddAction(ctx context.Context, req *AddActionReq) (ActionLocator, error) { // If no macaroon is provided, then an empty 4-byte array is used as the - // macaroon ID. + // macaroon ID. Note that the kvdb implementation only stores the last + // 4 bytes of the macaroon root key ID. var macaroonID [4]byte - req.MacaroonIdentifier.WhenSome(func(id [4]byte) { - macaroonID = id + req.MacaroonRootKeyID.WhenSome(func(rootID uint64) { + macaroonID = session.IDFromMacRootKeyID(rootID) }) // If the new action links to a session, the session must exist. @@ -601,7 +602,6 @@ func DeserializeAction(r io.Reader, sessionID session.ID) (*Action, error) { // the first 4 bytes with zeroes. rootKeyID := uint64(binary.BigEndian.Uint32(sessionID[:])) - action.MacaroonIdentifier = fn.Some([4]byte(sessionID)) action.MacaroonRootKeyID = fn.Some(rootKeyID) action.SessionID = fn.Some(sessionID) action.ActorName = string(actor) diff --git a/firewalldb/actions_sql.go b/firewalldb/actions_sql.go index e575de4e..9e2fa63b 100644 --- a/firewalldb/actions_sql.go +++ b/firewalldb/actions_sql.go @@ -400,20 +400,15 @@ func unmarshalAction(ctx context.Context, db SQLActionQueries, // Note that we export the full 8 byte macaroon root key ID in the sql // actions DB, while the kvdb version persists and exports stored the // last 4 bytes only. - var macID fn.Option[[4]byte] var macRootKeyID fn.Option[uint64] - if len(dbAction.MacaroonIdentifier) >= 4 { - dbMacID := dbAction.MacaroonIdentifier - macID = fn.Some([4]byte(dbMacID[len(dbMacID)-4:])) - - if len(dbAction.MacaroonIdentifier) >= 8 { - macRootKeyID = fn.Some(binary.BigEndian.Uint64(dbMacID)) - } + if len(dbAction.MacaroonIdentifier) >= 8 { + macRootKeyID = fn.Some( + binary.BigEndian.Uint64(dbAction.MacaroonIdentifier), + ) } return &Action{ AddActionReq: AddActionReq{ - MacaroonIdentifier: macID, MacaroonRootKeyID: macRootKeyID, AccountID: legacyAcctID, SessionID: legacySessID, diff --git a/firewalldb/actions_test.go b/firewalldb/actions_test.go index 54256ab2..b3ee2f78 100644 --- a/firewalldb/actions_test.go +++ b/firewalldb/actions_test.go @@ -66,7 +66,6 @@ func TestActionStorage(t *testing.T) { action1Req := &AddActionReq{ SessionID: fn.Some(sess1.ID), AccountID: fn.Some(acct1.ID), - MacaroonIdentifier: fn.Some([4]byte(sess1.ID)), MacaroonRootKeyID: fn.Some(sess1RootKeyID), ActorName: "Autopilot", FeatureName: "auto-fees", @@ -86,15 +85,14 @@ func TestActionStorage(t *testing.T) { sess2RootKeyID := litmac.NewSuperMacaroonRootKeyID(sess2.ID) action2Req := &AddActionReq{ - SessionID: fn.Some(sess2.ID), - MacaroonIdentifier: fn.Some([4]byte(sess2.ID)), - MacaroonRootKeyID: fn.Some(sess2RootKeyID), - ActorName: "Autopilot", - FeatureName: "rebalancer", - Trigger: "channels not balanced", - Intent: "balance", - RPCMethod: "SendToRoute", - RPCParamsJson: []byte("hops, amount"), + SessionID: fn.Some(sess2.ID), + MacaroonRootKeyID: fn.Some(sess2RootKeyID), + ActorName: "Autopilot", + FeatureName: "rebalancer", + Trigger: "channels not balanced", + Intent: "balance", + RPCMethod: "SendToRoute", + RPCParamsJson: []byte("hops, amount"), } action2 := &Action{ @@ -223,7 +221,6 @@ func TestListActions(t *testing.T) { sessRootKeyID := litmac.NewSuperMacaroonRootKeyID(sessionID) actionReq := &AddActionReq{ - MacaroonIdentifier: fn.Some(sessionID), MacaroonRootKeyID: fn.Some(sessRootKeyID), ActorName: "Autopilot", FeatureName: fmt.Sprintf("%d", actionIds), @@ -246,11 +243,13 @@ func TestListActions(t *testing.T) { assertActions := func(dbActions []*Action, al []*action) { require.Len(t, dbActions, len(al)) for i, a := range al { - mID, err := dbActions[i].MacaroonIdentifier.UnwrapOrErr( - fmt.Errorf("macaroon identifier is none"), + rID, err := dbActions[i].MacaroonRootKeyID.UnwrapOrErr( + fmt.Errorf("macaroon root key is none"), ) require.NoError(t, err) - require.EqualValues(t, a.sessionID, mID) + require.EqualValues( + t, a.sessionID, session.IDFromMacRootKeyID(rID), + ) require.Equal(t, a.actionID, dbActions[i].FeatureName) } } @@ -438,7 +437,6 @@ func TestListGroupActions(t *testing.T) { action1Req := &AddActionReq{ SessionID: fn.Some(sess1.ID), - MacaroonIdentifier: fn.Some([4]byte(sess1.ID)), MacaroonRootKeyID: fn.Some(sess1RootKeyID), ActorName: "Autopilot", FeatureName: "auto-fees", @@ -458,15 +456,14 @@ func TestListGroupActions(t *testing.T) { sess2RootKeyID := litmac.NewSuperMacaroonRootKeyID(sess2.ID) action2Req := &AddActionReq{ - SessionID: fn.Some(sess2.ID), - MacaroonIdentifier: fn.Some([4]byte(sess2.ID)), - MacaroonRootKeyID: fn.Some(sess2RootKeyID), - ActorName: "Autopilot", - FeatureName: "rebalancer", - Trigger: "channels not balanced", - Intent: "balance", - RPCMethod: "SendToRoute", - RPCParamsJson: []byte("hops, amount"), + SessionID: fn.Some(sess2.ID), + MacaroonRootKeyID: fn.Some(sess2RootKeyID), + ActorName: "Autopilot", + FeatureName: "rebalancer", + Trigger: "channels not balanced", + Intent: "balance", + RPCMethod: "SendToRoute", + RPCParamsJson: []byte("hops, amount"), } action2 := &Action{ diff --git a/session_rpcserver.go b/session_rpcserver.go index b88cea05..6bebb73d 100644 --- a/session_rpcserver.go +++ b/session_rpcserver.go @@ -818,8 +818,8 @@ func (s *sessionRpcServer) ListActions(ctx context.Context, }) var macID [4]byte - a.MacaroonIdentifier.WhenSome(func(id [4]byte) { - macID = id + a.MacaroonRootKeyID.WhenSome(func(rootID uint64) { + macID = session.IDFromMacRootKeyID(rootID) }) resp[i] = &litrpc.Action{