cmd/lncli: always parse the pay_addr field for sendpayment

In this commit, we fix a bug that would cause attempts to re-use an AMP
invoice to fail. Without this commit, we would only attempt to parse the
payment addr if no invoice was specified, so a user manually specifying the
pubkey of the detonation. The fix is straight forward: always parse the
`pay_addr` field as the user may be attempting to re-use an AMP invoice w/o
open coding each of the sections.
This commit is contained in:
Olaoluwa Osuntokun 2021-07-22 15:23:06 -07:00
parent 4c010e803e
commit 6e3b33629c
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306

View File

@ -235,6 +235,31 @@ func confirmPayReq(resp *lnrpc.PayReq, amt, feeLimit int64) error {
return nil
}
func parsePayAddr(ctx *cli.Context) ([]byte, error) {
var (
payAddr []byte
err error
)
switch {
case ctx.IsSet("pay_addr"):
payAddr, err = hex.DecodeString(ctx.String("pay_addr"))
case ctx.Args().Present():
payAddr, err = hex.DecodeString(ctx.Args().First())
}
if err != nil {
return nil, err
}
// payAddr may be not required if it's a legacy invoice.
if len(payAddr) != 0 && len(payAddr) != 32 {
return nil, fmt.Errorf("payment addr must be exactly 32 "+
"bytes, is instead %v", len(payAddr))
}
return payAddr, nil
}
func sendPayment(ctx *cli.Context) error {
// Show command help if no arguments provided
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
@ -242,6 +267,8 @@ func sendPayment(ctx *cli.Context) error {
return nil
}
args := ctx.Args()
// If a payment request was provided, we can exit early since all of the
// details of the payment are encoded within the request.
if ctx.IsSet("pay_req") {
@ -250,6 +277,16 @@ func sendPayment(ctx *cli.Context) error {
Amt: ctx.Int64("amt"),
}
// We'll attempt to parse a payment address as well, given that
// if the user is using an AMP invoice, then they may be trying
// to specify that value manually.
payAddr, err := parsePayAddr(ctx)
if err != nil {
return err
}
req.PaymentAddr = payAddr
return sendPaymentRequest(ctx, req)
}
@ -259,8 +296,6 @@ func sendPayment(ctx *cli.Context) error {
err error
)
args := ctx.Args()
switch {
case ctx.IsSet("dest"):
destNode, err = hex.DecodeString(ctx.String("dest"))
@ -352,22 +387,11 @@ func sendPayment(ctx *cli.Context) error {
req.FinalCltvDelta = int32(delta)
}
var payAddr []byte
switch {
case ctx.IsSet("pay_addr"):
payAddr, err = hex.DecodeString(ctx.String("pay_addr"))
case args.Present():
payAddr, err = hex.DecodeString(args.First())
}
payAddr, err := parsePayAddr(ctx)
if err != nil {
return err
}
// payAddr may be not required if it's a legacy invoice.
if len(payAddr) != 0 && len(payAddr) != 32 {
return fmt.Errorf("payment addr must be exactly 32 "+
"bytes, is instead %v", len(payAddr))
}
req.PaymentAddr = payAddr
return sendPaymentRequest(ctx, req)