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) " +
"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.
@ -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)