Skip to content

Commit

Permalink
lncli: add cancel invoice command
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Feb 3, 2019
1 parent c218675 commit 81d0922
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
82 changes: 82 additions & 0 deletions 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
}
10 changes: 10 additions & 0 deletions 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
}
1 change: 1 addition & 0 deletions cmd/lncli/main.go
Expand Up @@ -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)
Expand Down

0 comments on commit 81d0922

Please sign in to comment.