lncli: add custom data payment flag

This commit is contained in:
Joost Jager 2020-01-14 09:33:35 +01:00
parent daa08be62a
commit d978b55701
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -2091,6 +2091,14 @@ var (
Usage: "pubkey of the last hop (penultimate node in the path) " + Usage: "pubkey of the last hop (penultimate node in the path) " +
"to route through for this payment", "to route through for this payment",
} }
dataFlag = cli.StringFlag{
Name: "data",
Usage: "attach custom data to the payment. The required " +
"format is: <record_id>=<hex_value>,<record_id>=" +
"<hex_value>,.. For example: --data 3438382=0a21ff. " +
"Custom record ids start from 65536.",
}
) )
// paymentFlags returns common flags for sendpayment and payinvoice. // paymentFlags returns common flags for sendpayment and payinvoice.
@ -2127,6 +2135,7 @@ func paymentFlags() []cli.Flag {
Name: "allow_self_payment", Name: "allow_self_payment",
Usage: "allow sending a circular payment to self", Usage: "allow sending a circular payment to self",
}, },
dataFlag,
} }
} }
@ -2272,6 +2281,7 @@ func sendPayment(ctx *cli.Context) error {
req := &lnrpc.SendRequest{ req := &lnrpc.SendRequest{
Dest: destNode, Dest: destNode,
Amt: amount, Amt: amount,
DestCustomRecords: make(map[uint64][]byte),
} }
var rHash []byte var rHash []byte
@ -2286,9 +2296,10 @@ func sendPayment(ctx *cli.Context) error {
return err return err
} }
req.DestCustomRecords = map[uint64][]byte{ // Set the preimage. If the user supplied a preimage with the
record.KeySendType: preimage[:], // data flag, the preimage that is set here will be overwritten
} // later.
req.DestCustomRecords[record.KeySendType] = preimage[:]
hash := preimage.Hash() hash := preimage.Hash()
rHash = hash[:] rHash = hash[:]
@ -2355,6 +2366,33 @@ func sendPaymentRequest(ctx *cli.Context, req *lnrpc.SendRequest) error {
req.AllowSelfPayment = ctx.Bool("allow_self_payment") 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 amt := req.Amt
if req.PaymentRequest != "" { if req.PaymentRequest != "" {
@ -2436,6 +2474,7 @@ func payInvoice(ctx *cli.Context) error {
req := &lnrpc.SendRequest{ req := &lnrpc.SendRequest{
PaymentRequest: payReq, PaymentRequest: payReq,
Amt: ctx.Int64("amt"), Amt: ctx.Int64("amt"),
DestCustomRecords: make(map[uint64][]byte),
} }
return sendPaymentRequest(ctx, req) return sendPaymentRequest(ctx, req)