From 81d0922582d808f373d90a3acedf326cc67db0c3 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Mon, 26 Nov 2018 14:19:43 +0100 Subject: [PATCH] lncli: add cancel invoice command --- cmd/lncli/invoicesrpc_active.go | 82 ++++++++++++++++++++++++++++++++ cmd/lncli/invoicesrpc_default.go | 10 ++++ cmd/lncli/main.go | 1 + 3 files changed, 93 insertions(+) create mode 100644 cmd/lncli/invoicesrpc_active.go create mode 100644 cmd/lncli/invoicesrpc_default.go diff --git a/cmd/lncli/invoicesrpc_active.go b/cmd/lncli/invoicesrpc_active.go new file mode 100644 index 0000000000..dc2330ad6e --- /dev/null +++ b/cmd/lncli/invoicesrpc_active.go @@ -0,0 +1,82 @@ +// +build invoicesrpc + +package main + +import ( + "context" + "encoding/hex" + "fmt" + + "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" + "github.com/urfave/cli" +) + +// invoicesCommands will return nil for non-invoicesrpc builds. +func invoicesCommands() []cli.Command { + return []cli.Command{ + cancelInvoiceCommand, + } +} + +func getInvoicesClient(ctx *cli.Context) (invoicesrpc.InvoicesClient, func()) { + conn := getClientConn(ctx, false) + + cleanUp := func() { + conn.Close() + } + + return invoicesrpc.NewInvoicesClient(conn), cleanUp +} + +var cancelInvoiceCommand = cli.Command{ + Name: "cancelinvoice", + Category: "Payments", + Usage: "Cancels a (hold) invoice", + Description: ` + Todo.`, + ArgsUsage: "paymenthash", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "paymenthash", + Usage: "the hex-encoded payment hash (32 byte) for which the " + + "corresponding invoice will be canceled.", + }, + }, + Action: actionDecorator(cancelInvoice), +} + +func cancelInvoice(ctx *cli.Context) error { + var ( + paymentHash []byte + err error + ) + + client, cleanUp := getInvoicesClient(ctx) + defer cleanUp() + + args := ctx.Args() + + switch { + case ctx.IsSet("paymenthash"): + paymentHash, err = hex.DecodeString(ctx.String("paymenthash")) + case args.Present(): + paymentHash, err = hex.DecodeString(args.First()) + } + + if err != nil { + return fmt.Errorf("unable to parse preimage: %v", err) + } + + invoice := &invoicesrpc.CancelInvoiceMsg{ + PaymentHash: paymentHash, + } + + resp, err := client.CancelInvoice(context.Background(), invoice) + if err != nil { + return err + } + + printJSON(resp) + + return nil +} diff --git a/cmd/lncli/invoicesrpc_default.go b/cmd/lncli/invoicesrpc_default.go new file mode 100644 index 0000000000..570dfa6941 --- /dev/null +++ b/cmd/lncli/invoicesrpc_default.go @@ -0,0 +1,10 @@ +// +build !invoicesrpc + +package main + +import "github.com/urfave/cli" + +// invoicesCommands will return nil for non-invoicesrpc builds. +func invoicesCommands() []cli.Command { + return nil +} diff --git a/cmd/lncli/main.go b/cmd/lncli/main.go index b5c2db5b84..6a930c5f55 100644 --- a/cmd/lncli/main.go +++ b/cmd/lncli/main.go @@ -298,6 +298,7 @@ func main() { // Add any extra autopilot commands determined by build flags. app.Commands = append(app.Commands, autopilotCommands()...) + app.Commands = append(app.Commands, invoicesCommands()...) if err := app.Run(os.Args); err != nil { fatal(err)