@@ -2,9 +2,14 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "crypto/rand"
6
+ "encoding/binary"
7
+ "encoding/hex"
5
8
"fmt"
9
+ "os"
6
10
7
11
"github.com/lightninglabs/lightning-terminal/litrpc"
12
+ "github.com/lightningnetwork/lnd/lncfg"
8
13
"github.com/urfave/cli"
9
14
)
10
15
@@ -22,6 +27,29 @@ var litCommands = []cli.Command{
22
27
Category : "LiT" ,
23
28
Action : getInfo ,
24
29
},
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
+ },
25
53
}
26
54
27
55
func getInfo (ctx * cli.Context ) error {
@@ -61,3 +89,63 @@ func shutdownLit(ctx *cli.Context) error {
61
89
62
90
return nil
63
91
}
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