Skip to content

Commit fa62caa

Browse files
committed
cmd/loop: add command to list tokens
1 parent 47bf510 commit fa62caa

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

cmd/loop/lsat.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/hex"
7+
"fmt"
8+
"time"
9+
10+
"github.com/lightninglabs/loop/looprpc"
11+
"github.com/lightninglabs/loop/lsat"
12+
"github.com/urfave/cli"
13+
"gopkg.in/macaroon.v2"
14+
)
15+
16+
type printableToken struct {
17+
ID string `json:"id"`
18+
ValidUntil string `json:"valid_until"`
19+
BaseMacaroon string `json:"base_macaroon"`
20+
PaymentHash string `json:"payment_hash"`
21+
PaymentPreimage string `json:"payment_preimage"`
22+
AmountPaid int64 `json:"amount_paid_msat"`
23+
RoutingFeePaid int64 `json:"routing_fee_paid_msat"`
24+
TimeCreated string `json:"time_created"`
25+
Expired bool `json:"expired"`
26+
FileName string `json:"file_name"`
27+
}
28+
29+
var listAuthCommand = cli.Command{
30+
Name: "listauth",
31+
Usage: "list all LSAT tokens",
32+
Description: "Shows a list of all LSAT tokens that loopd has paid for",
33+
Action: listAuth,
34+
}
35+
36+
func listAuth(ctx *cli.Context) error {
37+
client, cleanup, err := getClient(ctx)
38+
if err != nil {
39+
return err
40+
}
41+
defer cleanup()
42+
43+
resp, err := client.GetLsatTokens(
44+
context.Background(), &looprpc.TokensRequest{},
45+
)
46+
if err != nil {
47+
return err
48+
}
49+
50+
tokens := make([]*printableToken, len(resp.Tokens))
51+
for i, t := range resp.Tokens {
52+
53+
mac := &macaroon.Macaroon{}
54+
err := mac.UnmarshalBinary(t.BaseMacaroon)
55+
if err != nil {
56+
return fmt.Errorf("unable to unmarshal macaroon: %v",
57+
err)
58+
}
59+
id, err := lsat.DecodeIdentifier(bytes.NewReader(mac.Id()))
60+
if err != nil {
61+
return fmt.Errorf("unable to decode macaroon ID: %v",
62+
err)
63+
}
64+
tokens[i] = &printableToken{
65+
ID: hex.EncodeToString(id.TokenID[:]),
66+
ValidUntil: "",
67+
BaseMacaroon: hex.EncodeToString(t.BaseMacaroon),
68+
PaymentHash: hex.EncodeToString(t.PaymentHash),
69+
PaymentPreimage: hex.EncodeToString(t.PaymentPreimage),
70+
AmountPaid: t.AmountPaidMsat,
71+
RoutingFeePaid: t.RoutingFeePaidMsat,
72+
TimeCreated: time.Unix(t.TimeCreated, 0).Format(
73+
time.RFC3339,
74+
),
75+
Expired: t.Expired,
76+
FileName: t.StorageName,
77+
}
78+
}
79+
80+
printJSON(tokens)
81+
return nil
82+
}

cmd/loop/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"errors"
57
"fmt"
68
"os"
@@ -30,6 +32,21 @@ var (
3032
defaultSwapWaitTime = 30 * time.Minute
3133
)
3234

35+
func printJSON(resp interface{}) {
36+
b, err := json.Marshal(resp)
37+
if err != nil {
38+
fatal(err)
39+
}
40+
41+
var out bytes.Buffer
42+
err = json.Indent(&out, b, "", "\t")
43+
if err != nil {
44+
fatal(err)
45+
}
46+
out.WriteString("\n")
47+
_, _ = out.WriteTo(os.Stdout)
48+
}
49+
3350
func printRespJSON(resp proto.Message) {
3451
jsonMarshaler := &jsonpb.Marshaler{
3552
EmitDefaults: true,
@@ -65,7 +82,7 @@ func main() {
6582
}
6683
app.Commands = []cli.Command{
6784
loopOutCommand, loopInCommand, termsCommand,
68-
monitorCommand, quoteCommand,
85+
monitorCommand, quoteCommand, listAuthCommand,
6986
}
7087

7188
err := app.Run(os.Args)

0 commit comments

Comments
 (0)