lncli: add settle invoice command

This commit is contained in:
Joost Jager 2019-02-10 21:03:39 +01:00
parent f450929b65
commit f6d67945dc
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -7,9 +7,10 @@ import (
"encoding/hex"
"fmt"
"strconv"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/urfave/cli"
"strconv"
)
// invoicesCommands will return nil for non-invoicesrpc builds.
@ -17,6 +18,7 @@ func invoicesCommands() []cli.Command {
return []cli.Command{
cancelInvoiceCommand,
addHoldInvoiceCommand,
settleInvoiceCommand,
}
}
@ -30,6 +32,60 @@ func getInvoicesClient(ctx *cli.Context) (invoicesrpc.InvoicesClient, func()) {
return invoicesrpc.NewInvoicesClient(conn), cleanUp
}
var settleInvoiceCommand = cli.Command{
Name: "settleinvoice",
Category: "Payments",
Usage: "Reveal a preimage and use it to settle the corresponding invoice.",
Description: `
Todo.`,
ArgsUsage: "preimage",
Flags: []cli.Flag{
cli.StringFlag{
Name: "preimage",
Usage: "the hex-encoded preimage (32 byte) which will " +
"allow settling an incoming HTLC payable to this " +
"preimage.",
},
},
Action: actionDecorator(settleInvoice),
}
func settleInvoice(ctx *cli.Context) error {
var (
preimage []byte
err error
)
client, cleanUp := getInvoicesClient(ctx)
defer cleanUp()
args := ctx.Args()
switch {
case ctx.IsSet("preimage"):
preimage, err = hex.DecodeString(ctx.String("preimage"))
case args.Present():
preimage, err = hex.DecodeString(args.First())
}
if err != nil {
return fmt.Errorf("unable to parse preimage: %v", err)
}
invoice := &invoicesrpc.SettleInvoiceMsg{
Preimage: preimage,
}
resp, err := client.SettleInvoice(context.Background(), invoice)
if err != nil {
return err
}
printJSON(resp)
return nil
}
var cancelInvoiceCommand = cli.Command{
Name: "cancelinvoice",
Category: "Payments",