lncli: add confirmPayreq used when paying

This commit is contained in:
f 2018-06-27 22:02:01 -07:00
parent 8379bbaa9b
commit 0ae268397b

@ -1702,6 +1702,10 @@ var sendPaymentCommand = cli.Command{
Name: "final_cltv_delta", Name: "final_cltv_delta",
Usage: "the number of blocks the last hop has to reveal the preimage", Usage: "the number of blocks the last hop has to reveal the preimage",
}, },
cli.BoolFlag{
Name: "force, f",
Usage: "will skip payment request confirmation",
},
}, },
Action: sendPayment, Action: sendPayment,
} }
@ -1732,7 +1736,30 @@ func retrieveFeeLimit(ctx *cli.Context) (*lnrpc.FeeLimit, error) {
return nil, nil return nil, nil
} }
func confirmPayReq(client lnrpc.LightningClient, payReq string) error {
ctxb := context.Background()
req := &lnrpc.PayReqString{PayReq: payReq}
resp, err := client.DecodePayReq(ctxb, req)
if err != nil {
return err
}
fmt.Printf("Description: %v\n", resp.GetDescription())
fmt.Printf("Amount (in satoshis): %v\n", resp.GetNumSatoshis())
fmt.Printf("Destination: %v\n", resp.GetDestination())
confirm := promptForConfirmation("Confirm payment (yes/no): ")
if !confirm {
return fmt.Errorf("payment not confirmed")
}
return nil
}
func sendPayment(ctx *cli.Context) error { func sendPayment(ctx *cli.Context) error {
client, cleanUp := getClient(ctx)
defer cleanUp()
// Show command help if no arguments provided // Show command help if no arguments provided
if ctx.NArg() == 0 && ctx.NumFlags() == 0 { if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
cli.ShowCommandHelp(ctx, "sendpayment") cli.ShowCommandHelp(ctx, "sendpayment")
@ -1750,13 +1777,19 @@ func sendPayment(ctx *cli.Context) error {
// If a payment request was provided, we can exit early since all of the // If a payment request was provided, we can exit early since all of the
// details of the payment are encoded within the request. // details of the payment are encoded within the request.
if ctx.IsSet("pay_req") { if ctx.IsSet("pay_req") {
if !ctx.Bool("force") {
err = confirmPayReq(client, ctx.String("pay_req"))
if err != nil {
return err
}
}
req := &lnrpc.SendRequest{ req := &lnrpc.SendRequest{
PaymentRequest: ctx.String("pay_req"), PaymentRequest: ctx.String("pay_req"),
Amt: ctx.Int64("amt"), Amt: ctx.Int64("amt"),
FeeLimit: feeLimit, FeeLimit: feeLimit,
} }
return sendPaymentRequest(ctx, req) return sendPaymentRequest(client, req)
} }
var ( var (
@ -1835,13 +1868,10 @@ func sendPayment(ctx *cli.Context) error {
} }
} }
return sendPaymentRequest(ctx, req) return sendPaymentRequest(client, req)
} }
func sendPaymentRequest(ctx *cli.Context, req *lnrpc.SendRequest) error { func sendPaymentRequest(client lnrpc.LightningClient, req *lnrpc.SendRequest) error {
client, cleanUp := getClient(ctx)
defer cleanUp()
paymentStream, err := client.SendPayment(context.Background()) paymentStream, err := client.SendPayment(context.Background())
if err != nil { if err != nil {
return err return err
@ -1896,12 +1926,18 @@ var payInvoiceCommand = cli.Command{
Usage: "percentage of the payment's amount used as the" + Usage: "percentage of the payment's amount used as the" +
"maximum fee allowed when sending the payment", "maximum fee allowed when sending the payment",
}, },
cli.BoolFlag{
Name: "force, f",
Usage: "will skip payment request confirmation",
},
}, },
Action: actionDecorator(payInvoice), Action: actionDecorator(payInvoice),
} }
func payInvoice(ctx *cli.Context) error { func payInvoice(ctx *cli.Context) error {
args := ctx.Args() args := ctx.Args()
client, cleanUp := getClient(ctx)
defer cleanUp()
var payReq string var payReq string
switch { switch {
@ -1918,13 +1954,20 @@ func payInvoice(ctx *cli.Context) error {
return err return err
} }
if !ctx.Bool("force") {
err = confirmPayReq(client, payReq)
if err != nil {
return err
}
}
req := &lnrpc.SendRequest{ req := &lnrpc.SendRequest{
PaymentRequest: payReq, PaymentRequest: payReq,
Amt: ctx.Int64("amt"), Amt: ctx.Int64("amt"),
FeeLimit: feeLimit, FeeLimit: feeLimit,
} }
return sendPaymentRequest(ctx, req) return sendPaymentRequest(client, req)
} }
var sendToRouteCommand = cli.Command{ var sendToRouteCommand = cli.Command{