From d978b55701472f8b33e566976c6df19489c2bcd9 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 14 Jan 2020 09:33:35 +0100 Subject: [PATCH] lncli: add custom data payment flag --- cmd/lncli/commands.go | 53 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/cmd/lncli/commands.go b/cmd/lncli/commands.go index 1af69696..6ef0aad8 100644 --- a/cmd/lncli/commands.go +++ b/cmd/lncli/commands.go @@ -2091,6 +2091,14 @@ var ( Usage: "pubkey of the last hop (penultimate node in the path) " + "to route through for this payment", } + + dataFlag = cli.StringFlag{ + Name: "data", + Usage: "attach custom data to the payment. The required " + + "format is: =,=" + + ",.. For example: --data 3438382=0a21ff. " + + "Custom record ids start from 65536.", + } ) // paymentFlags returns common flags for sendpayment and payinvoice. @@ -2127,6 +2135,7 @@ func paymentFlags() []cli.Flag { Name: "allow_self_payment", Usage: "allow sending a circular payment to self", }, + dataFlag, } } @@ -2270,8 +2279,9 @@ func sendPayment(ctx *cli.Context) error { } req := &lnrpc.SendRequest{ - Dest: destNode, - Amt: amount, + Dest: destNode, + Amt: amount, + DestCustomRecords: make(map[uint64][]byte), } var rHash []byte @@ -2286,9 +2296,10 @@ func sendPayment(ctx *cli.Context) error { return err } - req.DestCustomRecords = map[uint64][]byte{ - record.KeySendType: preimage[:], - } + // Set the preimage. If the user supplied a preimage with the + // data flag, the preimage that is set here will be overwritten + // later. + req.DestCustomRecords[record.KeySendType] = preimage[:] hash := preimage.Hash() rHash = hash[:] @@ -2355,6 +2366,33 @@ func sendPaymentRequest(ctx *cli.Context, req *lnrpc.SendRequest) error { req.AllowSelfPayment = ctx.Bool("allow_self_payment") + // Parse custom data records. + data := ctx.String(dataFlag.Name) + if data != "" { + records := strings.Split(data, ",") + for _, r := range records { + kv := strings.Split(r, "=") + if len(kv) != 2 { + return errors.New("invalid data format: " + + "multiple equal signs in record") + } + + recordID, err := strconv.ParseUint(kv[0], 10, 64) + if err != nil { + return fmt.Errorf("invalid data format: %v", + err) + } + + hexValue, err := hex.DecodeString(kv[1]) + if err != nil { + return fmt.Errorf("invalid data format: %v", + err) + } + + req.DestCustomRecords[recordID] = hexValue + } + } + amt := req.Amt if req.PaymentRequest != "" { @@ -2434,8 +2472,9 @@ func payInvoice(ctx *cli.Context) error { } req := &lnrpc.SendRequest{ - PaymentRequest: payReq, - Amt: ctx.Int64("amt"), + PaymentRequest: payReq, + Amt: ctx.Int64("amt"), + DestCustomRecords: make(map[uint64][]byte), } return sendPaymentRequest(ctx, req)