Skip to content

Commit 832fbad

Browse files
committed
lit+macaroons: move BakeSuperMacaroons helper
Move this helper to the new macaroons package.
1 parent 949c0bf commit 832fbad

File tree

2 files changed

+53
-52
lines changed

2 files changed

+53
-52
lines changed

macaroons/super_mac.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"context"
66
"encoding/binary"
77
"encoding/hex"
8+
"errors"
89

10+
"github.com/lightningnetwork/lnd/lnrpc"
911
"gopkg.in/macaroon-bakery.v2/bakery"
1012
"gopkg.in/macaroon.v2"
1113
)
@@ -68,3 +70,51 @@ func isSuperMacaroonRootKeyID(rootKeyID uint64) bool {
6870
binary.BigEndian.PutUint64(rootKeyBytes, rootKeyID)
6971
return bytes.HasPrefix(rootKeyBytes, SuperMacaroonRootKeyPrefix[:])
7072
}
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+
}

terminal.go

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ import (
6969
"google.golang.org/grpc/test/bufconn"
7070
"google.golang.org/protobuf/encoding/protojson"
7171
"gopkg.in/macaroon-bakery.v2/bakery"
72-
"gopkg.in/macaroon.v2"
7372
)
7473

7574
const (
@@ -433,7 +432,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
433432
superMacBaker := func(ctx context.Context, rootKeyID uint64,
434433
recipe *session.MacaroonRecipe) (string, error) {
435434

436-
return BakeSuperMacaroon(
435+
return litmac.BakeSuperMacaroon(
437436
ctx, g.basicClient, rootKeyID,
438437
recipe.Permissions, recipe.Caveats,
439438
)
@@ -665,7 +664,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
665664

666665
rootKeyID := litmac.NewSuperMacaroonRootKeyID(suffixBytes)
667666

668-
return BakeSuperMacaroon(
667+
return litmac.BakeSuperMacaroon(
669668
ctx, g.basicClient, rootKeyID,
670669
g.permsMgr.ActivePermissions(readOnly), nil,
671670
)
@@ -953,7 +952,7 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
953952
// Create a super macaroon that can be used to control lnd,
954953
// faraday, loop, and pool, all at the same time.
955954
log.Infof("Baking internal super macaroon")
956-
superMacaroon, err := BakeSuperMacaroon(
955+
superMacaroon, err := litmac.BakeSuperMacaroon(
957956
ctx, g.basicClient, litmac.NewSuperMacaroonRootKeyID(
958957
[4]byte{},
959958
),
@@ -1845,54 +1844,6 @@ func (g *LightningTerminal) initSubServers() error {
18451844
return nil
18461845
}
18471846

1848-
// BakeSuperMacaroon uses the lnd client to bake a macaroon that can include
1849-
// permissions for multiple daemons.
1850-
func BakeSuperMacaroon(ctx context.Context, lnd lnrpc.LightningClient,
1851-
rootKeyID uint64, perms []bakery.Op, caveats []macaroon.Caveat) (string,
1852-
error) {
1853-
1854-
if lnd == nil {
1855-
return "", errors.New("lnd not yet connected")
1856-
}
1857-
1858-
req := &lnrpc.BakeMacaroonRequest{
1859-
Permissions: make(
1860-
[]*lnrpc.MacaroonPermission, len(perms),
1861-
),
1862-
AllowExternalPermissions: true,
1863-
RootKeyId: rootKeyID,
1864-
}
1865-
for idx, perm := range perms {
1866-
req.Permissions[idx] = &lnrpc.MacaroonPermission{
1867-
Entity: perm.Entity,
1868-
Action: perm.Action,
1869-
}
1870-
}
1871-
1872-
res, err := lnd.BakeMacaroon(ctx, req)
1873-
if err != nil {
1874-
return "", err
1875-
}
1876-
1877-
mac, err := litmac.ParseMacaroon(res.Macaroon)
1878-
if err != nil {
1879-
return "", err
1880-
}
1881-
1882-
for _, caveat := range caveats {
1883-
if err := mac.AddFirstPartyCaveat(caveat.Id); err != nil {
1884-
return "", err
1885-
}
1886-
}
1887-
1888-
macBytes, err := mac.MarshalBinary()
1889-
if err != nil {
1890-
return "", err
1891-
}
1892-
1893-
return hex.EncodeToString(macBytes), err
1894-
}
1895-
18961847
// allowCORS wraps the given http.Handler with a function that adds the
18971848
// Access-Control-Allow-Origin header to the response.
18981849
func allowCORS(handler http.Handler, origins []string) http.Handler {

0 commit comments

Comments
 (0)