Lightning Keysend

Omoniyi
Coinmonks
Published in
4 min readMay 5, 2022

--

What is lightning?

For Bitcoin to process transactions like other payment processors like MasterCard, Visa, and others, it needs to scale. Every transaction that occurs on the Bitcoin network needs to be relayed, mined, and confirmed, which serves as an impediment for the Bitcoin network to scale and compete with other payment processors. The race to solve this scalability issue birthed the Lightning Network. Lightning is a “layer 2” solution built on top of the Bitcoin blockchain that uses payment channels to make Bitcoin transact efficiently. With the Lightning Network, two parties can send and receive payments from each other at a faster and cheaper rate.

What is Keysend?

To make a Lightning payment, the payee needs to generate a BOLT 11 invoice and then send it to the payer by email, displaying it as a QR code or other means. You can think of this invoice as a Bitcoin address in the Bitcoin network, even though it works substantially differently. The main difficulty is when the receiver can’t interact with the payer (e.g., because he doesn’t know who/when he’s getting paid, like anonymous donations, or when the receiver doesn’t have a channel to speak with the payer). Another is if the payee needs to accept reoccurring payments with varying amounts. This is where Lightning Keysend shines. Keysend removes the need for invoices when making payments. Instead, the receiver of the funds shares their node ID (a public key) once, and then the payer can pay any amount at any time in the future. Keysend doesn’t require changes to the network, but the receiving node needs to have Keysend support enabled.

Why did I choose keysend?

Following the features of my application, I needed to send payments to users after each bet win. Asking users to generate an invoice for every withdrawal will give a bad experience and slow down the betting process. After reading about Keysend in the book Mastering the Lightning Network, I decided it was a good way for me to push payments to users without having them generate invoices.

Bet App built on Ligthning keysend

What is my experience with keysend?

After surfing the internet for days, I just couldn’t find any code that actually does what I intended to do. Even the lnrpc library I used didn’t work for keysend as it couldn’t marshal response for keysend payment. Neither was the lightning documentation precise about keysend implementation.

With Keysend, the payer wraps the preimage in the hash, so only the payee can read it when the payment onion reaches him or her. A preimage is added to custom records using 5482373484. If you’re using LND locally, the .conf file in the application options needs to be set to accept-keysend=1 on the receiver (payee) node for keysend to work. See what’s behind the — keysend flag of lndcli sendpayment here.

How is data sent in keysend?

In BOLT 11 invoice-based Lightning payment, the payee picks a random number called the preimage or payment secret and then applies a cryptographic hash to it, and then gives the invoice, which includes the payment hash, to the payer.

For a Keysend payment, the payer picks the preimage, includes it within the onion wrapper to the receiver, and routes the HTLC to the receiver. The receiver then decrypts the onion payload and uses the included preimage to settle the payment. The onion-wrapped data is transmitted along a route that is hidden from the nodes moving the payment. This data package gets incrementally unwrapped as the payment travels along with hops to its final destination.

Below is my typescript keysend code snippet, visit here for the full code:

Keysend payment via REST API

What are the shortcomings of using Keysend?

One of the major disadvantages of using keysend is the inability of the payer to prove that they paid due to the payer’s picking the preimage. As a result, the payee can deny payment. Also, Lightnin NodeID (pubKey) can leak lots of information, such as your peers, fee rate, and so on, about the node. You may want to be cautious with whom you share that. Even though the BOLT 11 invoice can be decoded to expose your pubKey too

Conclusion

For Keysend to work, the receiver needs to manually enable this feature. It removes the complexity of having the receiver share an invoice of a specific amount before payment can be made from the payment flow. It allows any node to send a payment to any other node without requiring an invoice. Keysend excels in some kinds of payments, like donations.

I hope this helps understand and implement Keysend. If you find this post useful, do share it with your friends on Facebook and Twitter!

Also, Read

--

--