Skip to content

Commit

Permalink
cmd/litcli: add new decodeassetinvoice command
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Dec 17, 2024
1 parent 0d012f3 commit 4b42f01
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cmd/litcli/ln.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var lnCommands = []cli.Command{
sendPaymentCommand,
payInvoiceCommand,
addInvoiceCommand,
decodeAssetInvoiceCommand,
},
},
}
Expand Down Expand Up @@ -639,3 +640,67 @@ func addInvoice(ctx *cli.Context) error {

return nil
}

var decodeAssetInvoiceCommand = cli.Command{
Name: "decodeassetinvoice",
Category: "Payments",
Usage: "Decodes an LN invoice and displays the invoice's amount in asset " +
"units specified by an asset ID",
Description: `
This command can be used to display the information encoded in an invoice.
Given a chosen asset_id, the invoice's amount expressed in units of the asset
will be displayed.
Other information such as the decimal display of an asset, and the asset
group information (if applicable) are also shown.
`,
ArgsUsage: "--pay_req=X --asset_id=X",
Flags: []cli.Flag{
cli.StringFlag{
Name: "pay_req",
Usage: "a zpay32 encoded payment request to fulfill",
},
assetIDFlag,
},
Action: decodeAssetInvoice,
}

func decodeAssetInvoice(ctx *cli.Context) error {
ctxb := context.Background()

switch {
case !ctx.IsSet("pay_req"):
return fmt.Errorf("pay_req argument missing")
case !ctx.IsSet(assetIDFlag.Name):
return fmt.Errorf("the --asset_id flag must be set")
}

payReq := ctx.String("pay_req")

assetIDStr := ctx.String(assetIDFlag.Name)
assetIDBytes, err := hex.DecodeString(assetIDStr)
if err != nil {
return fmt.Errorf("unable to decode assetID: %v", err)
}

tapdConn, cleanup, err := connectSuperMacClient(ctx)
if err != nil {
return fmt.Errorf("unable to make rpc con: %w", err)
}
defer cleanup()

channelsClient := tchrpc.NewTaprootAssetChannelsClient(tapdConn)
resp, err := channelsClient.DecodeAssetPayReq(ctxb, &tchrpc.AssetPayReqString{
AssetId: assetIDBytes,
PayReqString: &lnrpc.PayReqString{
PayReq: payReq,
},
})
if err != nil {
return fmt.Errorf("error adding invoice: %w", err)
}

printRespJSON(resp)

return nil
}

0 comments on commit 4b42f01

Please sign in to comment.