Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lnrpc+lncli: add send support for custom data records #3900

Merged
merged 1 commit into from
Jan 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions cmd/lncli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -2127,6 +2135,7 @@ func paymentFlags() []cli.Flag {
Name: "allow_self_payment",
Usage: "allow sending a circular payment to self",
},
dataFlag,
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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[:]
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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)
Expand Down