From 0e07699550fcdea82143d94dec37530987f90be9 Mon Sep 17 00:00:00 2001 From: rajeshnair2k Date: Fri, 27 Oct 2017 18:39:54 -0400 Subject: [PATCH] lncli: add a convenience command for paying an invoice Fixes #324. --- cmd/lncli/commands.go | 46 +++++++++++++++++++++++++++++++++++++++---- cmd/lncli/main.go | 1 + 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/cmd/lncli/commands.go b/cmd/lncli/commands.go index f12b73e0..5fc3fc87 100644 --- a/cmd/lncli/commands.go +++ b/cmd/lncli/commands.go @@ -834,16 +834,13 @@ var sendPaymentCommand = cli.Command{ }, cli.StringFlag{ Name: "pay_req", - Usage: "a bech32 encoded payment request to fulfill", + Usage: "a zpay32 encoded payment request to fulfill", }, }, Action: sendPayment, } func sendPayment(ctx *cli.Context) error { - client, cleanUp := getClient(ctx) - defer cleanUp() - // Show command help if no arguments provieded if ctx.NArg() == 0 && ctx.NumFlags() == 0 { cli.ShowCommandHelp(ctx, "sendpayment") @@ -922,6 +919,13 @@ func sendPayment(ctx *cli.Context) error { } } + return sendPaymentRequest(ctx, req) +} + +func sendPaymentRequest(ctx *cli.Context, req *lnrpc.SendRequest) error { + client, cleanUp := getClient(ctx) + defer cleanUp() + paymentStream, err := client.SendPayment(context.Background()) if err != nil { return err @@ -951,6 +955,40 @@ func sendPayment(ctx *cli.Context) error { return nil } +var payInvoiceCommand = cli.Command{ + Name: "payinvoice", + Usage: "pay an invoice over lightning", + ArgsUsage: "pay_req", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "pay_req", + Usage: "a zpay32 encoded payment request to fulfill", + }, + }, + Action: payInvoice, +} + +func payInvoice(ctx *cli.Context) error { + args := ctx.Args() + + var payReq string + + switch { + case ctx.IsSet("pay_req"): + payReq = ctx.String("pay_req") + case args.Present(): + payReq = args.First() + default: + return fmt.Errorf("pay_req argument missing") + } + + req := &lnrpc.SendRequest{ + PaymentRequest: payReq, + } + + return sendPaymentRequest(ctx, req) +} + var addInvoiceCommand = cli.Command{ Name: "addinvoice", Usage: "add a new invoice.", diff --git a/cmd/lncli/main.go b/cmd/lncli/main.go index 8b1ebfc0..25343959 100644 --- a/cmd/lncli/main.go +++ b/cmd/lncli/main.go @@ -171,6 +171,7 @@ func main() { getInfoCommand, pendingChannelsCommand, sendPaymentCommand, + payInvoiceCommand, addInvoiceCommand, lookupInvoiceCommand, listInvoicesCommand,