Skip to content

Commit c5dc10c

Browse files
committed
cmd/litcli: add super macaroon helper commands
In this commit, a `supermacrootkey` helper command is added which lets a user generate a random root key ID for a super macaroon along with an `issupermacaroon` command that allows a users to easily confirm if a macaroon is considered a super macaroon or not. Neither of these commands require a connection to LiTd.
1 parent 0428731 commit c5dc10c

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

cmd/litcli/helpers.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"crypto/rand"
6+
"encoding/hex"
7+
"encoding/json"
8+
"os"
9+
10+
"github.com/lightninglabs/lightning-terminal/session"
11+
"github.com/urfave/cli"
12+
)
13+
14+
// helperCommands are commands that do not require a connection to LiTd to
15+
// return a response.
16+
var helperCommands = cli.Command{
17+
Name: "helper",
18+
Usage: "helper commands",
19+
Category: "Helper",
20+
Subcommands: []cli.Command{
21+
generateSuperMacRootIDCmd,
22+
isSuperMacaroonCmd,
23+
},
24+
}
25+
26+
// generateSuperMacRootIDCmd is a command that can be used to generate a root
27+
// key ID for a super macaroon. A suffix may be specified.
28+
var generateSuperMacRootIDCmd = cli.Command{
29+
Name: "supermacrootkey",
30+
Usage: "Generate a valid super macaroon root key ID from scratch " +
31+
"or from a given root key ID suffix",
32+
Description: `
33+
This command can be used to generate a valid super macaroon root key ID
34+
from scratch or from a given root key ID suffix.
35+
`,
36+
Action: superMacRootKey,
37+
Flags: []cli.Flag{
38+
cli.StringFlag{
39+
Name: "root_key_suffix",
40+
Usage: "A 4-byte suffix to use in the construction " +
41+
"of the root key ID. If not provided, then a " +
42+
"random one will be generated. This must be " +
43+
"specified as a hex string using a maximum of " +
44+
"8 characters.",
45+
},
46+
},
47+
}
48+
49+
// superMacRootKey generates a super macaroon root key ID.
50+
func superMacRootKey(ctx *cli.Context) error {
51+
var suffix [4]byte
52+
53+
if ctx.IsSet("root_key_suffix") {
54+
suffixBytes, err := hex.DecodeString(
55+
ctx.String("root_key_suffix"),
56+
)
57+
if err != nil {
58+
return err
59+
}
60+
61+
copy(suffix[:], suffixBytes)
62+
} else {
63+
_, err := rand.Read(suffix[:])
64+
if err != nil {
65+
return err
66+
}
67+
}
68+
69+
id := session.NewSuperMacaroonRootKeyID(suffix)
70+
71+
printJSON(struct {
72+
RootKeyID uint64 `json:"root_key_id"`
73+
}{
74+
RootKeyID: id,
75+
})
76+
77+
return nil
78+
}
79+
80+
// isSuperMacaronoCmd is a command that a user can run in order to check if
81+
// a macaroon is a super macaroon.
82+
var isSuperMacaroonCmd = cli.Command{
83+
Name: "issupermacaroon",
84+
Usage: "Prints 'true' if the given macaroon is a super macaroon",
85+
Description: `
86+
This command can be used to verify if a macaroon is considered a
87+
super macaroon.
88+
`,
89+
Action: isSuperMacaroon,
90+
Flags: []cli.Flag{
91+
cli.StringFlag{
92+
Name: "mac",
93+
Usage: "The hex-encoded macaroon",
94+
Required: true,
95+
},
96+
},
97+
}
98+
99+
// isSuperMacaroon checks if the users given macaroon is considered a super
100+
// macaroon.
101+
func isSuperMacaroon(ctx *cli.Context) error {
102+
isSuperMac := session.IsSuperMacaroon(ctx.String("mac"))
103+
104+
printJSON(struct {
105+
IsSuperMacaroon bool `json:"is_super_macaroon"`
106+
}{
107+
IsSuperMacaroon: isSuperMac,
108+
})
109+
110+
return nil
111+
}
112+
113+
// printJSON marshals the given interface as a json byte slice, formats it to
114+
// use easy to read indentation and writes it as a string to standard out.
115+
func printJSON(resp interface{}) {
116+
b, err := json.Marshal(resp)
117+
if err != nil {
118+
fatal(err)
119+
}
120+
121+
var out bytes.Buffer
122+
json.Indent(&out, b, "", "\t")
123+
out.WriteString("\n")
124+
out.WriteTo(os.Stdout)
125+
}

cmd/litcli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func main() {
7676
app.Commands = append(app.Commands, privacyMapCommands)
7777
app.Commands = append(app.Commands, autopilotCommands)
7878
app.Commands = append(app.Commands, litCommands...)
79+
app.Commands = append(app.Commands, helperCommands)
7980

8081
err := app.Run(os.Args)
8182
if err != nil {

0 commit comments

Comments
 (0)