Skip to content

Commit 17458b8

Browse files
committed
cmd/litcli: add bakesupermacaroon command
1 parent 250f21c commit 17458b8

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

cmd/litcli/proxy.go

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

0 commit comments

Comments
 (0)