2025-02-09 10:37:40 +02:00
|
|
|
package macaroons
|
|
|
|
|
|
|
|
|
|
import (
|
2025-02-09 10:47:26 +02:00
|
|
|
"context"
|
2025-02-09 10:37:40 +02:00
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
"gopkg.in/macaroon-bakery.v2/bakery"
|
|
|
|
|
"gopkg.in/macaroon.v2"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-09 10:47:26 +02:00
|
|
|
// Baker is a function type for baking a super macaroon.
|
|
|
|
|
type Baker func(ctx context.Context, rootKeyID uint64,
|
|
|
|
|
perms []bakery.Op, caveats []macaroon.Caveat) (string, error)
|
|
|
|
|
|
2025-02-09 10:37:40 +02:00
|
|
|
// RootKeyIDFromMacaroon extracts the root key ID of the passed macaroon.
|
|
|
|
|
func RootKeyIDFromMacaroon(mac *macaroon.Macaroon) (uint64, error) {
|
|
|
|
|
rawID := mac.Id()
|
|
|
|
|
if rawID[0] != byte(bakery.LatestVersion) {
|
|
|
|
|
return 0, fmt.Errorf("mac id is not on the latest version")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decodedID := &lnrpc.MacaroonId{}
|
|
|
|
|
idProto := rawID[1:]
|
|
|
|
|
err := proto.Unmarshal(idProto, decodedID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The storage ID is a string representation of a 64-bit unsigned
|
|
|
|
|
// number.
|
|
|
|
|
return strconv.ParseUint(string(decodedID.StorageId), 10, 64)
|
|
|
|
|
}
|