Skip to content

Commit ec77c54

Browse files
committed
cmd/litcli: add bakesupermacaroon command
1 parent 29b1126 commit ec77c54

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

cmd/litcli/proxy.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package main
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"encoding/binary"
7+
"encoding/hex"
58
"fmt"
9+
"os"
610

711
"github.com/lightninglabs/lightning-terminal/litrpc"
12+
"github.com/lightningnetwork/lnd/lncfg"
813
"github.com/urfave/cli"
914
)
1015

@@ -22,6 +27,29 @@ var litCommands = []cli.Command{
2227
Category: "LiT",
2328
Action: getInfo,
2429
},
30+
{
31+
Name: "bakesupermacaroon",
32+
Usage: "Bake a new super macaroon with all of LiT's active " +
33+
"permissions.",
34+
Category: "LiT",
35+
Action: bakeSuperMacaroon,
36+
Flags: []cli.Flag{
37+
cli.StringFlag{
38+
Name: "root_key_suffix",
39+
Usage: "A 4-byte suffix to use in the " +
40+
"construction of the root key ID. " +
41+
"If not provided, then a random one " +
42+
"will be generated. This must be " +
43+
"specified as a hex string using a " +
44+
"maximum of 8 characters.",
45+
},
46+
cli.StringFlag{
47+
Name: "save_to",
48+
Usage: "save returned admin macaroon to " +
49+
"this file",
50+
},
51+
},
52+
},
2553
}
2654

2755
func getInfo(ctx *cli.Context) error {
@@ -61,3 +89,63 @@ func shutdownLit(ctx *cli.Context) error {
6189

6290
return nil
6391
}
92+
93+
func bakeSuperMacaroon(ctx *cli.Context) error {
94+
var suffixBytes [4]byte
95+
if ctx.IsSet("root_key_suffix") {
96+
suffixHex, err := hex.DecodeString(
97+
ctx.String("root_key_suffix"),
98+
)
99+
if err != nil {
100+
return err
101+
}
102+
103+
copy(suffixBytes[:], suffixHex)
104+
} else {
105+
_, err := rand.Read(suffixBytes[:])
106+
if err != nil {
107+
return err
108+
}
109+
}
110+
suffix := binary.BigEndian.Uint32(suffixBytes[:])
111+
112+
clientConn, cleanup, err := connectClient(ctx)
113+
if err != nil {
114+
return err
115+
}
116+
defer cleanup()
117+
client := litrpc.NewProxyClient(clientConn)
118+
119+
ctxb := context.Background()
120+
resp, err := client.BakeSuperMacaroon(
121+
ctxb, &litrpc.BakeSuperMacaroonRequest{
122+
RootKeyIdSuffix: suffix,
123+
},
124+
)
125+
if err != nil {
126+
return err
127+
}
128+
129+
// If the user specified the optional --save_to parameter, we'll save
130+
// the macaroon to that file.
131+
if ctx.IsSet("save_to") {
132+
macSavePath := lncfg.CleanAndExpandPath(ctx.String("save_to"))
133+
superMacBytes, err := hex.DecodeString(resp.Macaroon)
134+
if err != nil {
135+
return err
136+
}
137+
138+
err = os.WriteFile(macSavePath, superMacBytes, 0644)
139+
if err != nil {
140+
_ = os.Remove(macSavePath)
141+
return err
142+
}
143+
fmt.Printf("Super macaroon saved to %s\n", macSavePath)
144+
145+
return nil
146+
}
147+
148+
printRespJSON(resp)
149+
150+
return nil
151+
}

0 commit comments

Comments
 (0)