Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

ECDH for spontaneous payments and offline vending machines

6 messages, Jun 9 to 11, 2019. Started by Stepan Snigirev.

Stepan Snigirev (3), ZmnSCPxj (3)

Permalink First message in the Wayback Machine

Stepan Snigirev permalink original

Hi list,

In the last couple weeks we came up with two ideas that you might find interesting. One is how to do spontaneous lightning payments to a node only with the knowledge of the node id, and the second one is how to generate invoices for the offline vending machine with arbitrary amounts without exposing node secrets to it. Both tricks use ECDH key agreement but in a slightly different way.

# Spontaneous payments.

The idea is to send the payment to a friend only with the knowledge of his node id. In this situation the node id acts similar to a bitcoin address for lightning.

When we create an onion packet for the target node we calculate a common secret with ECDH ( x-coordinate of `k * N` where `k` is a ephemeral key and `N` is the node id). We can use the double-sha256 of this common secret as a payment hash for the htlc and a single sha256 of this secret will be the preimage.

Christian Decker was very kind and implemented this feature as a plugin for c-lightning (https://github.com/cdecker/lightning/tree/stepan-pay) and it looks like the changes to be made are pretty small.

IMHO it would be nice to have such functionality and it can be used for payments between friends or for donations to charity organisations.

# Offline invoice generation

Some time ago there was a discussion about offline vending machines, the idea was to pre-load a vending machine with invoices, display invoices to the user and ask for the preimage as a payment confirmation. When all invoices are used or expired we refill it with the new ones. It is great but it doesn't work if the payment amount is variable (pay for volume or time).

An alternative could be to share the node private key with the vending machines to generate invoices on the fly, but it is a security hole. There is a trick though how generate invoices that our online node could claim without exposing any secrets to the vending machine.

Vending machine generates an ephemeral key `k` and corresponding ephemeral node id `K`. It generates an invoice signed with this key. It also includes a routing information that the payment should go through our online node `N`. As a preimage we use `hmac-sha256(x, amount)` where `x` is a secret key from ECDH(K, N) - (x-coordinate of `k * N`). We also put the amount into the 8-byte short channel id in the routing information to ensure that the node can calculate the preimage even if the user is willing to pay a tip and increases the amount of the htlc.

When our online node is asked to route a payment to an unknown node `K` it tries to generate the preimage. It calculates the ECDH secret `x` (x-coordinate of `n * K` where `n` is a private key corresponding to its node id) and calculates the preimage from the amount encoded in the short channel id and the secret x. If the preimage matches the payment hash it can claim the money (also checking that htlc amount is greater or equal the amount encoded in the channel id).

This construction allows to generate invoices on a vending machine or a web server without ever talking to the node itself. In contrast to the spontaneous payments described above the payer has no idea about the preimage.

I don't suggest to include this in the node implementation as is, but having a plugin / add-on with such functionality could be interesting.

Cheers, Stepan.

ZmnSCPxj in reply to Stepan Snigirev permalink original

Good morning Stepan,

These are very good developments and I applaud them.

Quoted text (1 line)
> Vending machine generates an ephemeral key `k` and corresponding ephemeral node id `K`. It generates an invoice signed with this key. It also includes a routing information that the payment should go through our online node `N`. As a preimage we use `hmac-sha256(x, amount)` where `x` is a secret key from ECDH(K, N) - (x-coordinate of `k * N`). We also put the amount into the 8-byte short channel id in the routing information to ensure that the node can calculate the preimage even if the user is willing to pay a tip and increases the amount of the htlc.

Just to be clear: each time the vending node needs a new invoice, it generates a new ephemeral key `k`?

Regards, ZmnSCPxj

Stepan Snigirev in reply to ZmnSCPxj permalink original

Hi ZmnSCPxj,

Quoted text (2 lines)
> Just to be clear: each time the vending node needs a new invoice, it
generates a new ephemeral key `k`?

Yes, the vending machine generates a new `k` for every invoice.

The same technique can be used for spontaneous payments as well and in this case we can also provide additional information to the node about the payment. For example, let's say I have my digitalocean account with some user id. If I want to top-up my account with certain amount I could just generate an invoice myself and pay it, and I could also include my ID (or login) into the short channel id. The node would see this information and top-up my account with the money I just sent.

Effectively this routing trick allows including a "payment purpose" by misusing the short channel id. It is a workaround as it uses the data field not according to the purpose and has a limit of 8 bytes, so I would prefer to have some metadata field in the invoice that should be passed to the payee in the onion packet instead, but this requires changes in the bolt 11 and as far as I understand something like that is already on the roadmap.

Stepan.

ZmnSCPxj in reply to Stepan Snigirev permalink original

Good morning Stepan,

Quoted text (9 lines)
> # Offline invoice generation
>
> Some time ago there was a discussion about offline vending machines, the idea was to pre-load a vending machine with invoices, display invoices to the user and ask for the preimage as a payment confirmation. When all invoices are used or expired we refill it with the new ones. It is great but it doesn't work if the payment amount is variable (pay for volume or time).
>
> An alternative could be to share the node private key with the vending machines to generate invoices on the fly, but it is a security hole. There is a trick though how generate invoices that our online node could claim without exposing any secrets to the vending machine.
>
> Vending machine generates an ephemeral key `k` and corresponding ephemeral node id `K`. It generates an invoice signed with this key. It also includes a routing information that the payment should go through our online node `N`. As a preimage we use `hmac-sha256(x, amount)` where `x` is a secret key from ECDH(K, N) - (x-coordinate of `k * N`). We also put the amount into the 8-byte short channel id in the routing information to ensure that the node can calculate the preimage even if the user is willing to pay a tip and increases the amount of the htlc.
>
> When our online node is asked to route a payment to an unknown node `K` it tries to generate the preimage. It calculates the ECDH secret `x` (x-coordinate of `n * K` where `n` is a private key corresponding to its node id) and calculates the preimage from the amount encoded in the short channel id and the secret x. If the preimage matches the payment hash it can claim the money (also checking that htlc amount is greater or equal the amount encoded in the channel id).

Reviewing BOLT #4, sadly it seems not to work...?

Quoted text (6 lines)
> 1. type: `per_hop` (for `realm` 0)
> 2. data:
>    * [`8`:`short_channel_id`]
>    * [`8`:`amt_to_forward`]
>    * [`4`:`outgoing_cltv_value`]
>    * [`12`:`padding`]

There is no way to inform your online node what `K` is, since what is in the `per_hop` does not include `K`. Instead, the `short_channel_id` is intended to inform the forwarding node which is the next hop, using only 8 bytes; presumably the forwarding node has a table mapping short channel IDs to actual peers it has. The online node in your proposal is unable to extract `K`, a the next-node-ID is never transmitted.

For the donation case, it seems possible, as the donator is the sender and can arrange to use the shared secret as the preimage. But for the invoice-generation case, the invoice-generator is not the sender, and the sender might not know how to properly inform the shared secret.

Maybe later, when we use "rendezvous routing" (and provide hooks into BOLT #11 for it), it may become possible to perform this trick (as we will use node IDs in rendezvous routing, rather than `short_channel_id`). Then the online node can, if unable to rendezvous-forward to a node on its routemap, check if the "next node ID" in the rendezvous routing sub-route can be used as to compute a shared secret that is the preimage. https://github.com/lightningnetwork/lightning-rfc/pull/611

Regards, ZmnSCPxj

Stepan Snigirev in reply to ZmnSCPxj permalink original

Hi ZmnSCPxj,

Quoted text (2 lines)
> The online node in your proposal is unable to extract `K`, a the
next-node-ID is never transmitted.

Then currently for the vending machines we can use a pre-shared common secret set up at installation time (may be unique for every machine) and use it for preimage generation:

preimage = hmac(common_secret, amount | nonce)

We can pass this nonce as a channel id. This is exactly what we did for the hackathon project. Such invoice has to be paid with exact amount, there is no way to include a tip - otherwise preimage generation will fail. Alternatively, for donation payments we can drop amount from preimage generation.

Online node can try to generate the preimage for every htlc offer with all known common secrets and the channel ID, and if it fails - try to route the payment as usual.

A set of common secrets also helps to determine the source of the payment, in order to avoid bruteforce we can use first byte (or varint) of channel id as a secret id (as soon as we have enough space left for nonce)

Stepan.

ZmnSCPxj in reply to Stepan Snigirev permalink original

Good morning Stepan,

Quoted text (9 lines)
> Hi ZmnSCPxj,
>
> > The online node in your proposal is unable to extract `K`, a the next-node-ID is never transmitted.
>
> Then currently for the vending machines we can use a pre-shared common secret set up at installation time (may be unique for every machine) and use it for preimage generation:
>
> preimage = hmac(common_secret, amount | nonce)
>
> We can pass this nonce as a channel id. This is exactly what we did for the hackathon project. Such invoice has to be paid with exact amount, there is no way to include a tip - otherwise preimage generation will fail.

I observe that any tip worth its name would be a positive delta over the invoice face value.

Thus, if you want to accept tips up to (say) 25% above the invoice face value, you can just iterate from `amount_to_forward` down to `0.8 * amount_to_forward` and see if it yields a preimage that matches the payment hash.

This may require some grinding; but remember that you have several blocks in which you can compute this.

This is particularly relevant for older versions of C-lightning which implemented value randomization (i.e. adding a small random tip in an attempt to fool analysts from determining the actual value being paid --- BOLT spec specifically allows this behavior). (This was inadvertently removed/not implemented in newer versions after the pay algorithm was moved to a plugin; since nobody but me was interested in value randomization anyway, I decided not to reimplement it until somebody else asks for it)

Quoted text (1 line)
> Online node can try to generate the preimage for every htlc offer with all known common secrets and the channel ID, and if it fails - try to route the payment as usual.

It is probably faster on most implementations to look up the channel ID in some hash table first, i.e. try to route first before generating preimages. Of course, 64-bit `short_channel_id` as nonce is likely to hit a "real" channel ID, but you can avoid this by restricting the range to far-future blockheights and/or before the `when_lightning_became_cool` blockheight.

Regards, ZmnSCPxj