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.
This commit is contained in:
Viktor Torstensson 2025-10-01 15:13:44 +02:00
parent 651cc678fc
commit 01bc36ca41
No known key found for this signature in database
GPG key ID: 961CC8259AE675D4
6 changed files with 38 additions and 58 deletions

View file

@ -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 {

View file

@ -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

View file

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

View file

@ -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,

View file

@ -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{

View file

@ -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{