|
| 1 | +package macaroons |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/binary" |
| 7 | + "encoding/hex" |
| 8 | + "errors" |
| 9 | + |
| 10 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 11 | + "gopkg.in/macaroon-bakery.v2/bakery" |
| 12 | + "gopkg.in/macaroon.v2" |
| 13 | +) |
| 14 | + |
| 15 | +// SuperMacaroonRootKeyPrefix is the prefix we set on a super macaroon's root |
| 16 | +// key to clearly mark it as such. |
| 17 | +var SuperMacaroonRootKeyPrefix = [4]byte{0xFF, 0xEE, 0xDD, 0xCC} |
| 18 | + |
| 19 | +// SuperMacaroonValidator is a function type for validating a super macaroon. |
| 20 | +type SuperMacaroonValidator func(ctx context.Context, |
| 21 | + superMacaroon []byte, requiredPermissions []bakery.Op, |
| 22 | + fullMethod string) error |
| 23 | + |
| 24 | +// NewSuperMacaroonRootKeyID returns a new macaroon root key ID that has the |
| 25 | +// prefix to mark it as a super macaroon root key. |
| 26 | +func NewSuperMacaroonRootKeyID(id [4]byte) uint64 { |
| 27 | + rootKeyBytes := make([]byte, 8) |
| 28 | + copy(rootKeyBytes[:], SuperMacaroonRootKeyPrefix[:]) |
| 29 | + copy(rootKeyBytes[4:], id[:]) |
| 30 | + |
| 31 | + return binary.BigEndian.Uint64(rootKeyBytes) |
| 32 | +} |
| 33 | + |
| 34 | +// ParseMacaroon parses a hex encoded macaroon into its native struct. |
| 35 | +func ParseMacaroon(macHex string) (*macaroon.Macaroon, error) { |
| 36 | + macBytes, err := hex.DecodeString(macHex) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + mac := &macaroon.Macaroon{} |
| 42 | + if err := mac.UnmarshalBinary(macBytes); err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + return mac, nil |
| 47 | +} |
| 48 | + |
| 49 | +// IsSuperMacaroon returns true if the given hex encoded macaroon is a super |
| 50 | +// macaroon baked by LiT which can be identified by its root key ID. |
| 51 | +func IsSuperMacaroon(macHex string) bool { |
| 52 | + mac, err := ParseMacaroon(macHex) |
| 53 | + if err != nil { |
| 54 | + return false |
| 55 | + } |
| 56 | + |
| 57 | + rootKeyID, err := RootKeyIDFromMacaroon(mac) |
| 58 | + if err != nil { |
| 59 | + return false |
| 60 | + } |
| 61 | + |
| 62 | + return isSuperMacaroonRootKeyID(rootKeyID) |
| 63 | +} |
| 64 | + |
| 65 | +// isSuperMacaroonRootKeyID returns true if the given macaroon root key ID (also |
| 66 | +// known as storage ID) is a super macaroon, which can be identified by its |
| 67 | +// first 4 bytes. |
| 68 | +func isSuperMacaroonRootKeyID(rootKeyID uint64) bool { |
| 69 | + rootKeyBytes := make([]byte, 8) |
| 70 | + binary.BigEndian.PutUint64(rootKeyBytes, rootKeyID) |
| 71 | + return bytes.HasPrefix(rootKeyBytes, SuperMacaroonRootKeyPrefix[:]) |
| 72 | +} |
| 73 | + |
| 74 | +// BakeSuperMacaroon uses the lnd client to bake a macaroon that can include |
| 75 | +// permissions for multiple daemons. |
| 76 | +func BakeSuperMacaroon(ctx context.Context, lnd lnrpc.LightningClient, |
| 77 | + rootKeyID uint64, perms []bakery.Op, caveats []macaroon.Caveat) (string, |
| 78 | + error) { |
| 79 | + |
| 80 | + if lnd == nil { |
| 81 | + return "", errors.New("lnd not yet connected") |
| 82 | + } |
| 83 | + |
| 84 | + req := &lnrpc.BakeMacaroonRequest{ |
| 85 | + Permissions: make( |
| 86 | + []*lnrpc.MacaroonPermission, len(perms), |
| 87 | + ), |
| 88 | + AllowExternalPermissions: true, |
| 89 | + RootKeyId: rootKeyID, |
| 90 | + } |
| 91 | + for idx, perm := range perms { |
| 92 | + req.Permissions[idx] = &lnrpc.MacaroonPermission{ |
| 93 | + Entity: perm.Entity, |
| 94 | + Action: perm.Action, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + res, err := lnd.BakeMacaroon(ctx, req) |
| 99 | + if err != nil { |
| 100 | + return "", err |
| 101 | + } |
| 102 | + |
| 103 | + mac, err := ParseMacaroon(res.Macaroon) |
| 104 | + if err != nil { |
| 105 | + return "", err |
| 106 | + } |
| 107 | + |
| 108 | + for _, caveat := range caveats { |
| 109 | + if err := mac.AddFirstPartyCaveat(caveat.Id); err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + macBytes, err := mac.MarshalBinary() |
| 115 | + if err != nil { |
| 116 | + return "", err |
| 117 | + } |
| 118 | + |
| 119 | + return hex.EncodeToString(macBytes), err |
| 120 | +} |
0 commit comments