|
| 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 | +} |
0 commit comments