Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

Recovery of Lightning channels without backups

16 messages, Dec 7, 2020, to May 3, 2021. Started by Lloyd Fournier.

Lloyd Fournier (8), Rusty Russell (5), David A. Harding (2), Gijs van Dam

Permalink First message in the Wayback Machine

Lloyd Fournier permalink original

Hi list,

I've been considering the problem of recovering lightning channels after losing channel state in a boating accident. The modern way of doing this seems to be "static channel backups" -- these are essentially lists of channel ids and the nodes you had the channels with.

The idea is that with this backup you can remember who you had channels with, connect to them and ask them to force close the channel (can someone link me the concrete protocol messages you send to do this?).

It occurred to me that if the lightning protocol were changed slightly you could do this without the channel backup at all. Consider the open channel message and its two fields `funding_pubkey` and `temporary_channel_id`.

https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md#the-open_channel-message

The reason `temporary_channel_id` is necessarily "temporary" is because we don't know the other party's `funding_pubkey`. Instead I propose using deterministically randomized version of the node's static public key as the `funding_pubkey` so we know it up front. Specifically, when opening a channel with a remote node, do a Diffie-Hellman operation with their public key and use the resulting shared secret to deterministically produce scalars r1 and r2 and use 2-of-2(r1*G + P_local, r2*P_remote) as the script pubkey of funding output. Now we know what the funding output would look like with any node without interacting with them. Of course, to open multiple channels with the same node we would have to generate a new shared secret from each one by hashing a counter.

Now that we can figure out what our funding outputs with each node look like non-interactively, when we lose our channel state we can find them in the UTXO set as long as we can recreate a list of node's we may have had channels with. For most non-enterprise users (such as myself) this can be done automatically because we exclusively have channels with well known reliable public nodes whose public keys can be discovered through routing gossip or from public indexes. Therefore if I can just restore my node's keypair from my seed I should be able to scan the network for nodes and figure out if I have channels open with them and then carry out the recovery process. From my perspective this seems to be a rather nice improvement in user experience.

Thoughts?

Cheers,

LL

Gijs van Dam in reply to Lloyd Fournier permalink original

Hey Lloyd (and others),

I have just been working on something similar. I came up with the following plan of attack that works within the current protocol:

Create a static channel backup after the fact. I have dubbed this a
synthetic static channel backup. I only use it to trigger the data loss
protection protocol.
By restoring this synthetic SCB a `channel_reestablish` is being sent to
the remote peer. This `channel_reestablish`contains the
`next_commitment_number`and the `next_revocation_number` both set to zero.
This triggers the remote peer to force close the channel dropping its
current commitment transaction to the chain. Using the
`per_commitment_point` received from the remote peer you can now derive the
private key needed for sweeping your funds, using
privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)

We use the basepoint_secret derived (in LND) with

```
basePointSecret, err := keyRing.DerivePrivKey(keychain.KeyDescriptor{
  KeyLocator: keychain.KeyLocator{
    Family: keychain.KeyFamilyPaymentBase,
    Index:  0, // Same commitment index as used by the remote node
  },
})
```

I have got this somewhat working in a test setup. (I probably have overlooked some details). I would be interested in hearing Sjors Provoost on this, since his question and answer on this topic over on the Bitcoin StackExchange was really helpful.[1] He lost his backup in a boating accident as well. (What is it with you guys and boating? :) )

Your approach covers more cases, and my approach only fits certain edge cases. But my approach is possible today, without changes to the Lightning protocol.

Regards,

Gijs

[1]: https://bitcoin.stackexchange.com/questions/90196/how-to-recover-funds-from-a-force-closed-lnd-lightning-channel/90719#90719

On Mon, Dec 7, 2020 at 8:32 AM Lloyd Fournier <lloyd.fourn at gmail.com> wrote:

Quoted text (52 lines)
> Hi list,
>
> I've been considering the problem of recovering lightning channels after
> losing channel state in a boating accident. The modern way of doing this
> seems to be "static channel backups" -- these are essentially lists of
> channel ids and the nodes you had the channels with.
>
> The idea is that with this backup you can remember who you had channels
> with, connect to them and ask them to force close the channel (can someone
> link me the concrete protocol messages you send to do this?).
>
> It occurred to me that if the lightning protocol were changed slightly you
> could do this without the channel backup at all. Consider the open channel
> message and its two fields `funding_pubkey` and `temporary_channel_id`.
>
>
> https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md#the-open_channel-message
>
> The reason `temporary_channel_id` is necessarily "temporary" is because we
> don't know the other party's `funding_pubkey`. Instead I propose using
> deterministically randomized version of the node's static public key as the
> `funding_pubkey` so we know it up front. Specifically, when opening a
> channel with a remote node, do a Diffie-Hellman operation with their public
> key and use the resulting shared secret to deterministically produce
> scalars r1 and r2 and use 2-of-2(r1*G + P_local, r2*P_remote) as the script
> pubkey of funding output. Now we know what the funding output would look
> like with any node without interacting with them. Of course, to open
> multiple channels with the same node we would have to generate a new shared
> secret from each one by hashing a counter.
>
> Now that we can figure out what our funding outputs with each node look
> like non-interactively, when we lose our channel state we can find them in
> the UTXO set as long as we can recreate a list of node's we may have had
> channels with. For most non-enterprise users (such as myself) this can be
> done automatically because we exclusively have channels with well known
> reliable public nodes whose public keys can be discovered through routing
> gossip or from public indexes. Therefore if I can just restore my node's
> keypair from my seed I should be able to scan the network for nodes and
> figure out if I have channels open with them and then carry out the
> recovery process. From my perspective this seems to be a rather nice
> improvement in user experience.
>
> Thoughts?
>
> Cheers,
>
> LL
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Lloyd Fournier in reply to Gijs van Dam permalink original
Quoted text (12 lines)
> Create a static channel backup after the fact. I have dubbed this a
> synthetic static channel backup. I only use it to trigger the data loss
> protection protocol.
> By restoring this synthetic SCB a `channel_reestablish` is being sent to
> the remote peer. This `channel_reestablish`contains the
> `next_commitment_number`and the `next_revocation_number` both set to zero.
> This triggers the remote peer to force close the channel dropping its
> current commitment transaction to the chain. Using the
> `per_commitment_point` received from the remote peer you can now derive the
> private key needed for sweeping your funds, using
> privkey = basepoint_secret + SHA256(per_commitment_point || basepoint)
>

Thanks Gijs for describing how this works I wasn't quite sure.

The thing I dream of is being able to securely restore my layer 1 and 2 funds with just my seed. There was discussion of this idea in last lightning dev meeting: https://github.com/lightningnetwork/lightning-rfc/issues/821#issuecomment-740161185 A few of the concerns were:

1. You have to remember the counter

The intention here is to not have to remember any counter. Just as in BIP32 you just scan with some allowance for gaps. roasbeef correctly points out that this may be more or less difficult depending on your node setup and whether you can ballpark how long ago your funding transactions were put down. However since you are using this when you have lost your channel states with no static channel backups I think it still provides a very realistic chance of recovering a significant chunk of your funds.

Another point ariard made is that you only have to find one of the channels with a peer to find all of them with a peer if there was some kind of "list channels" message request.

2. It only works for public nodes who can be discovered

Correct. For my use of LN so far I am always connecting to public nodes so it would likely work well for me. I think this is true for most unsophisticated users who are most likely to lose their channels with no backups.

If each peer allowed you to store some encrypted data with them then finding one honest public peer through the chain could potentially let you find all other peers (even the private ones).

3. We might not want to encourage doing channel recovery by asking the node to force close channel

I agree. "PLEASE BLACKMAIL ME" is not a bad characterization of the channel backup interaction Gijs describes above. I believe there is a lot of room for improvement here. First you have to find the channels though!

LL

Rusty Russell in reply to Lloyd Fournier permalink original

Lloyd Fournier <lloyd.fourn at gmail.com> writes:

Quoted text (10 lines)
> Hi list,
>
> I've been considering the problem of recovering lightning channels after
> losing channel state in a boating accident. The modern way of doing this
> seems to be "static channel backups" -- these are essentially lists of
> channel ids and the nodes you had the channels with.
>
> The idea is that with this backup you can remember who you had channels
> with, connect to them and ask them to force close the channel (can someone
> link me the concrete protocol messages you send to do this?).

It's in BOLT #2:

1. type: 136 (`channel_reestablish`)
2. data:
   * [`channel_id`:`channel_id`]
   * [`u64`:`next_commitment_number`]
   * [`u64`:`next_revocation_number`]
   * [`32*byte`:`your_last_per_commitment_secret`]
   * [`point`:`my_current_per_commitment_point`]

The `your_last_per_commitment_secret` lets Bob prove to Alice that he's from The Future, and prior to `option_static_remotekey` my_current_per_commitment_point allowed Alice to derive the script used for her output.

Quoted text (3 lines)
> It occurred to me that if the lightning protocol were changed slightly you
> could do this without the channel backup at all. Consider the open channel
> message and its two fields `funding_pubkey` and `temporary_channel_id`.
Hmm, we had a proposal (for DF) to get rid of the temporary_channel_id
by using 'SHA256(lesser-revocation-basepoint ||
greater-revocation-basepoint)' as the channel_id, which is known
from the start.  I don't see it in the DF spec PR, but it's in
c-lightning.
Quoted text (13 lines)
> https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md#the-open_channel-message
>
> The reason `temporary_channel_id` is necessarily "temporary" is because we
> don't know the other party's `funding_pubkey`. Instead I propose using
> deterministically randomized version of the node's static public key as the
> `funding_pubkey` so we know it up front. Specifically, when opening a
> channel with a remote node, do a Diffie-Hellman operation with their public
> key and use the resulting shared secret to deterministically produce
> scalars r1 and r2 and use 2-of-2(r1*G + P_local, r2*P_remote) as the script
> pubkey of funding output. Now we know what the funding output would look
> like with any node without interacting with them. Of course, to open
> multiple channels with the same node we would have to generate a new shared
> secret from each one by hashing a counter.
Say r1=SHA256(ss || counter || 0), r2 = SHA256(ss || counter || 1)?

Nice work. This would be a definite recovery win. We should add this to the DF spec, because Lisa was almost finished implmenting it, so it's clearly due for a change!

Quoted text (11 lines)
> Now that we can figure out what our funding outputs with each node look
> like non-interactively, when we lose our channel state we can find them in
> the UTXO set as long as we can recreate a list of node's we may have had
> channels with. For most non-enterprise users (such as myself) this can be
> done automatically because we exclusively have channels with well known
> reliable public nodes whose public keys can be discovered through routing
> gossip or from public indexes. Therefore if I can just restore my node's
> keypair from my seed I should be able to scan the network for nodes and
> figure out if I have channels open with them and then carry out the
> recovery process. From my perspective this seems to be a rather nice
> improvement in user experience.

Note that in practice you can have nodes reconnecting to you.

We've long thought about peers supplying some storage for each other, so you can spray out (encrypted) backups that way. It's actually a fairly trivial addition; your scheme makes for much less data to store.

Cheers! Rusty.

Lloyd Fournier in reply to Rusty Russell permalink original

On Wed, Dec 9, 2020 at 4:26 PM Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (7 lines)
>
> Say r1=SHA256(ss || counter || 0), r2 = SHA256(ss || counter || 1)?
>
> Nice work.  This would be a definite recovery win.  We should add this
> to the DF spec, because Lisa was almost finished implmenting it, so it's
> clearly due for a change!
>

Yes that's certainly a fine way to do it. I was also thinking you could eliminate all "basepoints" (not just funding pubkey) using something like this. i.e. just use the node pubkey as the "basepoint" for everything and randomize it using the shared secret for each purpose.

Note that in practice you can have nodes reconnecting to you.

Quoted text (1 line)
>

Hmm, this is a good point. I was incorrectly assuming the best way to implement "recovery mode" would be to never accept incoming connections.

Quoted text (4 lines)
We've long thought about peers supplying some storage for each other, so
> you can spray out (encrypted) backups that way.  It's actually a fairly
> trivial addition; your scheme makes for much less data to store.
>

I've also been thinking about the best way to go about this. If you're able to encrypt some backups to different places then all you have to do is find one of your honest channel partners using this method and then get encrypted hints to find them all.

Cheers,

LL

Rusty Russell in reply to Lloyd Fournier permalink original

Lloyd Fournier <lloyd.fourn at gmail.com> writes:

Quoted text (15 lines)
> On Wed, Dec 9, 2020 at 4:26 PM Rusty Russell <rusty at rustcorp.com.au> wrote:
>
>>
>> Say r1=SHA256(ss || counter || 0), r2 = SHA256(ss || counter || 1)?
>>
>> Nice work.  This would be a definite recovery win.  We should add this
>> to the DF spec, because Lisa was almost finished implmenting it, so it's
>> clearly due for a change!
>>
>
> Yes that's certainly a fine way to do it.
> I was also thinking you could eliminate all "basepoints" (not just funding
> pubkey) using something like this. i.e. just use the node pubkey as the
> "basepoint" for everything and randomize it using the shared secret for
> each purpose.

OK, I tried to spec this out, to implement it. One issue is that you now can't sign the commitment_tx (or htlc_tx) without knowing the node's secret key (or, equivalently, knowing the tweaked key and being able to use the derivation scheme to untweak it).

c-lightning currently does a round-trip to the signing daemon for this already, but it'd be nice to avoid requiring it.

So I somewhat reluctantly added `commit_basepoint` from which the others
are derived: an implementation can use some hardened derivation from its
privkey (e.g. SHA256(node_privkey || ss || counter)) to create
this in a deterministic but still private manner.

Or we could just leave all the other points in and just replace funding_pubkey.

Cheers, Rusty.

Lloyd Fournier in reply to Rusty Russell permalink original

Hi Rusty,

On Tue, 20 Apr 2021 at 10:55, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (24 lines)
> Lloyd Fournier <lloyd.fourn at gmail.com> writes:
> > On Wed, Dec 9, 2020 at 4:26 PM Rusty Russell <rusty at rustcorp.com.au>
> wrote:
> >
> >>
> >> Say r1=SHA256(ss || counter || 0), r2 = SHA256(ss || counter || 1)?
> >>
> >> Nice work.  This would be a definite recovery win.  We should add this
> >> to the DF spec, because Lisa was almost finished implmenting it, so it's
> >> clearly due for a change!
> >>
> >
> > Yes that's certainly a fine way to do it.
> > I was also thinking you could eliminate all "basepoints" (not just
> funding
> > pubkey) using something like this. i.e. just use the node pubkey as the
> > "basepoint" for everything and randomize it using the shared secret for
> > each purpose.
>
> OK, I tried to spec this out, to implement it.  One issue is that you
> now can't sign the commitment_tx (or htlc_tx) without knowing the node's
> secret key (or, equivalently, knowing the tweaked key and being able to
> use the derivation scheme to untweak it).
>

Using node secret key to sign the commitment_tx seems like something you will have to accept to introduce this feature. For the idea to work it has to be some public key that is known by others and gossiped through the network. Of course you could extend the information that is gossiped about a node to include a "commit_tx_point" but the nodeid seems the more natural choice.

Quoted text (11 lines)
> c-lightning currently does a round-trip to the signing daemon for this
> already, but it'd be nice to avoid requiring it.
>
> So I somewhat reluctantly added `commit_basepoint` from which the others
> are derived: an implementation can use some hardened derivation from its
> privkey (e.g. SHA256(node_privkey || ss || counter)) to create
> this in a deterministic but still private manner.
>
> Or we could just leave all the other points in and just replace
> funding_pubkey.
>

Another approach is to do things in "soft-fork" like manner. Each node that wants to offer this feature sets their funding_pubkey to a specified DH tweak of the nodeid. Nodes that want backup-free channel recovery can just refuse to carry on the funding protocol if the funding_pubkey is not set the way it wanted.

Quoted text (1 line)
>From my pruisit crypto point of view having only one public key is nice but

I'm not sure how it impacts things architecturally and other protocols like watchtowers.

Cheers,

LL

Rusty Russell in reply to Lloyd Fournier permalink original

Lloyd Fournier <lloyd.fourn at gmail.com> writes:

Quoted text (35 lines)
> Hi Rusty,
>
> On Tue, 20 Apr 2021 at 10:55, Rusty Russell <rusty at rustcorp.com.au> wrote:
>
>> Lloyd Fournier <lloyd.fourn at gmail.com> writes:
>> > On Wed, Dec 9, 2020 at 4:26 PM Rusty Russell <rusty at rustcorp.com.au>
>> wrote:
>> >
>> >>
>> >> Say r1=SHA256(ss || counter || 0), r2 = SHA256(ss || counter || 1)?
>> >>
>> >> Nice work.  This would be a definite recovery win.  We should add this
>> >> to the DF spec, because Lisa was almost finished implmenting it, so it's
>> >> clearly due for a change!
>> >>
>> >
>> > Yes that's certainly a fine way to do it.
>> > I was also thinking you could eliminate all "basepoints" (not just
>> funding
>> > pubkey) using something like this. i.e. just use the node pubkey as the
>> > "basepoint" for everything and randomize it using the shared secret for
>> > each purpose.
>>
>> OK, I tried to spec this out, to implement it.  One issue is that you
>> now can't sign the commitment_tx (or htlc_tx) without knowing the node's
>> secret key (or, equivalently, knowing the tweaked key and being able to
>> use the derivation scheme to untweak it).
>>
>
> Using node secret key to sign the commitment_tx seems like something you
> will have to accept to introduce this feature. For the idea to work it has
> to be some public key that is known by others and gossiped through the
> network. Of course you could extend the information that is gossiped about
> a node to include a "commit_tx_point" but the nodeid seems the more natural
> choice.

Duh, yes, of course you need the funding_key secret to sign the commitment tx.

But you really don't want to access the `remote_pubkey` (which in a modern option_static_remotekey world is simply the payment_basepoint). It's generally considered good practice *not* to have this accessible to your lightning node at all.

Quoted text (17 lines)
>> c-lightning currently does a round-trip to the signing daemon for this
>> already, but it'd be nice to avoid requiring it.
>>
>> So I somewhat reluctantly added `commit_basepoint` from which the others
>> are derived: an implementation can use some hardened derivation from its
>> privkey (e.g. SHA256(node_privkey || ss || counter)) to create
>> this in a deterministic but still private manner.
>>
>> Or we could just leave all the other points in and just replace
>> funding_pubkey.
>>
>
> Another approach is to do things in "soft-fork" like manner.
> Each node that wants to offer this feature sets their funding_pubkey to a
> specified DH tweak of the nodeid. Nodes that want backup-free channel
> recovery can just refuse to carry on the funding protocol if the
> funding_pubkey is not set the way it wanted.

Yeah, you can totally do this in an opt-in manner, except it doesn't work unless your peer does it too. Since we expect everyone to want to do this, it's clearer to force everyone to calculate this and not have redundant and confusing fields in the message.

Quoted text (3 lines)
>>From my pruisit crypto point of view having only one public key is nice but
> I'm not sure how it impacts things architecturally and other protocols like
> watchtowers.

They can operate exactly like the existing scheme, AFAICT.

Here's the spec diff (based on dual-funding, since it's easier to simply hard change). Please check my EC math! :)

diff --git a/02-peer-protocol.md b/02-peer-protocol.md
index fbc56c8..1114068 100644
--- a/02-peer-protocol.md
+++ b/02-peer-protocol.md
@@ -867,11 +867,9 @@ This message initiates the v2 channel establishment workflow.
    * [`u16`:`to_self_delay`]
    * [`u16`:`max_accepted_htlcs`]
    * [`u32`:`locktime`]
-   * [`point`:`funding_pubkey`]
+   * [`u64`:`generation`]
    * [`point`:`revocation_basepoint`]
    * [`point`:`payment_basepoint`]
-   * [`point`:`delayed_payment_basepoint`]
-   * [`point`:`htlc_basepoint`]
    * [`point`:`first_per_commitment_point`]
    * [`byte`:`channel_flags`]
    * [`opening_tlvs`:`tlvs`]
@@ -895,13 +893,16 @@ If nodes have negotiated `option_dual_fund`:
 The sending node:
   - MUST set `funding_feerate_perkw` to the feerate for this transaction
-  - MUST ensure `temporary_channel_id` is unique from any
-    other channel ID with the same peer.
+  - MUST set `generation` to a number greater than any previous
+    `generation` it has sent to this receiving node which has reached
+    `commitment_signed`.
+  - SHOULD set `generation` to the lowest number which meets this requirement.
 The receiving node:
   - MAY fail the negotiation if:
     - the `locktime` is unacceptable
     - the `funding_feerate_per_kw` is unacceptable
+    - the `generation` exceeds expectation by more than the maximum it would scan for recovery.

#### Rationale `channel_id` for the `open_channel2` MUST be derived using a zero-d out @@ -926,6 +927,13 @@ Instead, the channel reserve is fixed at 1% of the total channel balance rounded down to the nearest whole satoshi or the `dust_limit_satoshis`, whichever is greater.

+`generation` is a number which is used to generate the points used for +this pair of peers, with the aim of allowing automatic onchain +scanning for channels if all other information is lost. Since this +scan would presumably only try a limited number of generations, it is +best if this number is low, but it also needs to change for each +successive channel between the peers, to avoid obvious fingerprinting. + Note that `push_msat` has been omitted.

 ### The `accept_channel2` Message
@@ -943,11 +951,9 @@ acceptance of the new channel.
     * [`u32`:`minimum_depth`]
     * [`u16`:`to_self_delay`]
     * [`u16`:`max_accepted_htlcs`]
-    * [`point`:`funding_pubkey`]
+    * [`u64`:`generation`]
     * [`point`:`revocation_basepoint`]
     * [`point`:`payment_basepoint`]
-    * [`point`:`delayed_payment_basepoint`]
-    * [`point`:`htlc_basepoint`]
     * [`point`:`first_per_commitment_point`]
     * [`accept_tlvs`:`tlvs`]

@@ -967,6 +973,10 @@ additions.

 The accepting node:
     - MAY respond with a `funding_satoshis` value of zero.
+    - MUST set `generation` to a number greater than any previous
+      `generation` it has sent to this receiving node which has reached
+      `commitment_signed`.
+    - SHOULD set `generation` to the lowest number which meets this requirement.

#### Rationale

@@ -985,6 +995,31 @@ Funding composition for channel establishment v2 makes use of the [Interactive Transaction Construction](#interactive-transaction-construction) protocol, with the following additional caveats.

+#### Point Derivation
+
+The `funding_pubkey` and basepoints are derived from the two
+`node_id`s and the higher of the two `generation` values; the
+`payment_basepoint` is supplied directly.
+
+Derivation is done as follows:
+
+1. Start with two node ids, `N1` and `N2` (`N1` is the lesser of the
+   two SEC1-encoded compressed public keys, `N2` the greater).
+2. Derive a shared secret, `SS`, using ECDH on `N1` and `N2`.
+3. Define tweaks `T` for each peer, using `SHA256(SS || generation || node_id || name)`, where:
+   1. `generation` is the `u64` larger of the two `generation` fields from `open_channel2` and `accept_channel2`.
+   2. `node_id` is the SEC1-encoded compressed public key of the peer.
+   3. `name` is a non-terminated ASCII string, e.g. `htlc` is the four bytes
+   `0x68 0x74 0x6C 0x63`
+4. The `funding_pubkey` is defined as the `node_id` + G*T(`funding`).
+5. The `delayed_payment_basepoint` is defined as `node_id` + G*T(`delayed_payment`).
+6. The `htlc_basepoint` is defined as the `node_id` + G*T(`htlc`).
+
+If the secret for `payment_basepoint` is derived in a similar manner,
+it too can be easily recovered from just the `generation`, node key
+and peer `node_id`.  However, it may also point to an address for a
+completely separate system (e.g. cold storage), so it is specified
+explicitly in the protocol.

#### The `tx_add_input` Message

Rusty Russell in reply to Rusty Russell permalink original

OK, I'm now leaning *against* this method.

1. It removes the ability to update a channel without access to the node's
   secret key.  At the moment the node secret key is only needed for
   gossip and to DH to set up a new peer connection.  c-lightning does
   not use this for now (we keep the per-channel keys in the HSM too),
   but it would be a perfectly acceptable tradeoff not to do this.
2. It doesn't get rid of temporary_channel_id, since we don't know
   the generation_number until both sides have sent it.  We have a
   workaround for this already in dual-funding anyway.
3. Because we need a generation counter, it's not quite as easily
   scannable as you'd hope (the "gap" problem).

I think the "encrypted blob served by peers", even in a very naive way, offers another way to do this, though it requires the assumption that at least one peer is honest.

Damn, because it was so clever!

Thoughts? Rusty.

Rusty Russell <rusty at rustcorp.com.au> writes:

Quoted text (183 lines)
> Lloyd Fournier <lloyd.fourn at gmail.com> writes:
>> Hi Rusty,
>>
>> On Tue, 20 Apr 2021 at 10:55, Rusty Russell <rusty at rustcorp.com.au> wrote:
>>
>>> Lloyd Fournier <lloyd.fourn at gmail.com> writes:
>>> > On Wed, Dec 9, 2020 at 4:26 PM Rusty Russell <rusty at rustcorp.com.au>
>>> wrote:
>>> >
>>> >>
>>> >> Say r1=SHA256(ss || counter || 0), r2 = SHA256(ss || counter || 1)?
>>> >>
>>> >> Nice work.  This would be a definite recovery win.  We should add this
>>> >> to the DF spec, because Lisa was almost finished implmenting it, so it's
>>> >> clearly due for a change!
>>> >>
>>> >
>>> > Yes that's certainly a fine way to do it.
>>> > I was also thinking you could eliminate all "basepoints" (not just
>>> funding
>>> > pubkey) using something like this. i.e. just use the node pubkey as the
>>> > "basepoint" for everything and randomize it using the shared secret for
>>> > each purpose.
>>>
>>> OK, I tried to spec this out, to implement it.  One issue is that you
>>> now can't sign the commitment_tx (or htlc_tx) without knowing the node's
>>> secret key (or, equivalently, knowing the tweaked key and being able to
>>> use the derivation scheme to untweak it).
>>>
>>
>> Using node secret key to sign the commitment_tx seems like something you
>> will have to accept to introduce this feature. For the idea to work it has
>> to be some public key that is known by others and gossiped through the
>> network. Of course you could extend the information that is gossiped about
>> a node to include a "commit_tx_point" but the nodeid seems the more natural
>> choice.
>
> Duh, yes, of course you need the funding_key secret to sign the
> commitment tx.
>
> But you really don't want to access the `remote_pubkey` (which in a
> modern option_static_remotekey world is simply the payment_basepoint).
> It's generally considered good practice *not* to have this accessible to
> your lightning node at all.
>
>>> c-lightning currently does a round-trip to the signing daemon for this
>>> already, but it'd be nice to avoid requiring it.
>>>
>>> So I somewhat reluctantly added `commit_basepoint` from which the others
>>> are derived: an implementation can use some hardened derivation from its
>>> privkey (e.g. SHA256(node_privkey || ss || counter)) to create
>>> this in a deterministic but still private manner.
>>>
>>> Or we could just leave all the other points in and just replace
>>> funding_pubkey.
>>>
>>
>> Another approach is to do things in "soft-fork" like manner.
>> Each node that wants to offer this feature sets their funding_pubkey to a
>> specified DH tweak of the nodeid. Nodes that want backup-free channel
>> recovery can just refuse to carry on the funding protocol if the
>> funding_pubkey is not set the way it wanted.
>
> Yeah, you can totally do this in an opt-in manner, except it doesn't
> work unless your peer does it too.  Since we expect everyone to want to
> do this, it's clearer to force everyone to calculate this and not have
> redundant and confusing fields in the message.
>
>>>From my pruisit crypto point of view having only one public key is nice but
>> I'm not sure how it impacts things architecturally and other protocols like
>> watchtowers.
>
> They can operate exactly like the existing scheme, AFAICT.
>
> Here's the spec diff (based on dual-funding, since it's easier to simply
> hard change).  Please check my EC math! :)
>
> diff --git a/02-peer-protocol.md b/02-peer-protocol.md
> index fbc56c8..1114068 100644
> --- a/02-peer-protocol.md
> +++ b/02-peer-protocol.md
> @@ -867,11 +867,9 @@ This message initiates the v2 channel establishment workflow.
>     * [`u16`:`to_self_delay`]
>     * [`u16`:`max_accepted_htlcs`]
>     * [`u32`:`locktime`]
> -   * [`point`:`funding_pubkey`]
> +   * [`u64`:`generation`]
>     * [`point`:`revocation_basepoint`]
>     * [`point`:`payment_basepoint`]
> -   * [`point`:`delayed_payment_basepoint`]
> -   * [`point`:`htlc_basepoint`]
>     * [`point`:`first_per_commitment_point`]
>     * [`byte`:`channel_flags`]
>     * [`opening_tlvs`:`tlvs`]
> @@ -895,13 +893,16 @@ If nodes have negotiated `option_dual_fund`:
>
>  The sending node:
>    - MUST set `funding_feerate_perkw` to the feerate for this transaction
> -  - MUST ensure `temporary_channel_id` is unique from any
> -    other channel ID with the same peer.
> +  - MUST set `generation` to a number greater than any previous
> +    `generation` it has sent to this receiving node which has reached
> +    `commitment_signed`.
> +  - SHOULD set `generation` to the lowest number which meets this requirement.
>
>  The receiving node:
>    - MAY fail the negotiation if:
>      - the `locktime` is unacceptable
>      - the `funding_feerate_per_kw` is unacceptable
> +    - the `generation` exceeds expectation by more than the maximum it would scan for recovery.
>
>  #### Rationale
>  `channel_id` for the `open_channel2` MUST be derived using a zero-d out
> @@ -926,6 +927,13 @@ Instead, the channel reserve is fixed at 1% of the total channel balance
>  rounded down to the nearest whole satoshi or the `dust_limit_satoshis`,
>  whichever is greater.
>
> +`generation` is a number which is used to generate the points used for
> +this pair of peers, with the aim of allowing automatic onchain
> +scanning for channels if all other information is lost.  Since this
> +scan would presumably only try a limited number of generations, it is
> +best if this number is low, but it also needs to change for each
> +successive channel between the peers, to avoid obvious fingerprinting.
> +
>  Note that `push_msat` has been omitted.
>
>  ### The `accept_channel2` Message
> @@ -943,11 +951,9 @@ acceptance of the new channel.
>      * [`u32`:`minimum_depth`]
>      * [`u16`:`to_self_delay`]
>      * [`u16`:`max_accepted_htlcs`]
> -    * [`point`:`funding_pubkey`]
> +    * [`u64`:`generation`]
>      * [`point`:`revocation_basepoint`]
>      * [`point`:`payment_basepoint`]
> -    * [`point`:`delayed_payment_basepoint`]
> -    * [`point`:`htlc_basepoint`]
>      * [`point`:`first_per_commitment_point`]
>      * [`accept_tlvs`:`tlvs`]
>
> @@ -967,6 +973,10 @@ additions.
>
>  The accepting node:
>      - MAY respond with a `funding_satoshis` value of zero.
> +    - MUST set `generation` to a number greater than any previous
> +      `generation` it has sent to this receiving node which has reached
> +      `commitment_signed`.
> +    - SHOULD set `generation` to the lowest number which meets this requirement.
>
>  #### Rationale
>
> @@ -985,6 +995,31 @@ Funding composition for channel establishment v2 makes use of the
>  [Interactive Transaction Construction](#interactive-transaction-construction)
>  protocol, with the following additional caveats.
>
> +#### Point Derivation
> +
> +The `funding_pubkey` and basepoints are derived from the two
> +`node_id`s and the higher of the two `generation` values; the
> +`payment_basepoint` is supplied directly.
> +
> +Derivation is done as follows:
> +
> +1. Start with two node ids, `N1` and `N2` (`N1` is the lesser of the
> +   two SEC1-encoded compressed public keys, `N2` the greater).
> +2. Derive a shared secret, `SS`, using ECDH on `N1` and `N2`.
> +3. Define tweaks `T` for each peer, using `SHA256(SS || generation || node_id || name)`, where:
> +   1. `generation` is the `u64` larger of the two `generation` fields from `open_channel2` and `accept_channel2`.
> +   2. `node_id` is the SEC1-encoded compressed public key of the peer.
> +   3. `name` is a non-terminated ASCII string, e.g. `htlc` is the four bytes
> +   `0x68 0x74 0x6C 0x63`
> +4. The `funding_pubkey` is defined as the `node_id` + G*T(`funding`).
> +5. The `delayed_payment_basepoint` is defined as `node_id` + G*T(`delayed_payment`).
> +6. The `htlc_basepoint` is defined as the `node_id` + G*T(`htlc`).
> +
> +If the secret for `payment_basepoint` is derived in a similar manner,
> +it too can be easily recovered from just the `generation`, node key
> +and peer `node_id`.  However, it may also point to an address for a
> +completely separate system (e.g. cold storage), so it is specified
> +explicitly in the protocol.
>
>  #### The `tx_add_input` Message
>
Lloyd Fournier in reply to Rusty Russell permalink original

Hey Rusty,

Thoughts on each point below.

On Fri, 23 Apr 2021 at 14:29, Rusty Russell <rusty at rustcorp.com.au> wrote:

Quoted text (8 lines)
> OK, I'm now leaning *against* this method.
>
> 1. It removes the ability to update a channel without access to the node's
>    secret key.  At the moment the node secret key is only needed for
>    gossip and to DH to set up a new peer connection.  c-lightning does
>    not use this for now (we keep the per-channel keys in the HSM too),
>    but it would be a perfectly acceptable tradeoff not to do this.
>

Don't you also need the node secret key for onion routing? i.e. every time you update your channel to forward a payment. I am not familiar with lightning HSM designs and security goals but to me it doesn't sound like much of a cost to keep the key on the HSM and to include doing channel updates as well seeing as it's already doing so much work. If it is desirable to have different keys for DH and channel updates then a simple solution is to have two static public keys -- one for each task.

Quoted text (1 line)
>From my perspective it is worth making the necessary sacrifices to include

this feature. For me and many people, lost data without backups is the biggest risk to my funds in lightning. Certainly much more threatening than whether certain keys are on a HSM or not. Anecdotally I've heard stories like "I put my lnd on autopilot and then lost my disk died -- all my funds are gone!?" more than once.

Quoted text (4 lines)
2. It doesn't get rid of temporary_channel_id, since we don't know
>    the generation_number until both sides have sent it.  We have a
>    workaround for this already in dual-funding anyway.
>

Why did you decide to send this rather than just look up in your own database what "generation" should be? I think that it's easy to make sure that you and the other node are on the same page about this number without communicating it. If someone is opening a channel with data that appears to be invlaid because they are using the wrong generation then sending an error back indicating what you are up to should be sufficient to recover?

Quoted text (3 lines)
> 3. Because we need a generation counter, it's not quite as easily
>    scannable as you'd hope (the "gap" problem).
>

This doesn't seem to be a big issue. You are trying to recover your funds after all so you can afford to scan over very large gaps i.e. leave the node on for days. I mean my Bitcoin wallet manages to handle this so why wouldn't it work here? I wonder if it is even necessary to bump the generation until a funding tx is confirmed -- I can't think of a good reason why you would want to open two channels to the same node at the same time (why not put all your funds into the same funding).

Quoted text (4 lines)
> I think the "encrypted blob served by peers", even in a very naive way,
> offers another way to do this, though it requires the assumption that at
> least one peer is honest.
>

I see encrypted backups as complementary. With this scheme you can at least find a peer that you've had a channel with. From the encrypted backup you left with them you can then find others and check against them.

LL

Lloyd Fournier in reply to Lloyd Fournier permalink original

On Wed, 28 Apr 2021 at 09:36, Lloyd Fournier <lloyd.fourn at gmail.com> wrote:

Quoted text (5 lines)
>  I wonder if it is even necessary to bump the generation until a funding
> tx is confirmed -- I can't think of a good reason why you would want to
> open two channels to the same node at the same time (why not put all your
> funds into the same funding).
>

Nevermind I've thought of a few -- ignore this point!

LL

Rusty Russell in reply to Lloyd Fournier permalink original

Lloyd Fournier <lloyd.fourn at gmail.com> writes:

Quoted text (17 lines)
> Hey Rusty,
>
> Thoughts on each point below.
>
> On Fri, 23 Apr 2021 at 14:29, Rusty Russell <rusty at rustcorp.com.au> wrote:
>
>> OK, I'm now leaning *against* this method.
>>
>> 1. It removes the ability to update a channel without access to the node's
>>    secret key.  At the moment the node secret key is only needed for
>>    gossip and to DH to set up a new peer connection.  c-lightning does
>>    not use this for now (we keep the per-channel keys in the HSM too),
>>    but it would be a perfectly acceptable tradeoff not to do this.
>>
>
> Don't you also need the node secret key for onion routing? i.e. every time
> you update your channel to forward a payment.

You need to ECDH with the node_id privkey, yes (as you do to establish peer comms). But in the c-lightning model, that's part of central routing, not the subdaemon which deals with a single channel. You can still shutdown a channel without knowing the node's private key.

Quoted text (6 lines)
> I am not familiar with lightning HSM designs and security goals but to me
> it doesn't sound like much of a cost to keep the key on the HSM and to
> include doing channel updates as well seeing as it's already doing so much
> work. If it is desirable to have different keys for DH and channel updates
> then a simple solution is to have two static public keys -- one for each
> task.

The main concern is that access to one channel's keys doesn't give you any access to the other channels' keys. I don't think there's a way around that in any "I can derive another nodes' keys" model.

Quoted text (6 lines)
>>From my perspective it is worth making the necessary sacrifices to include
> this feature. For me and many people, lost data without backups is the
> biggest risk to my funds in lightning. Certainly much more threatening than
> whether certain keys are on a HSM or not. Anecdotally I've heard stories
> like "I put my lnd on autopilot and then lost my disk died -- all my funds
> are gone!?" more than once.

Fair, but more reliable backups solve this better IMHO. (Roasbeef told us that Electrum uses OP_RETURN to tag opens, which also works).

Quoted text (11 lines)
> 2. It doesn't get rid of temporary_channel_id, since we don't know
>>    the generation_number until both sides have sent it.  We have a
>>    workaround for this already in dual-funding anyway.
>>
>
> Why did you decide to send this rather than just look up in your own
> database what "generation" should be? I think that it's easy to make sure
> that you and the other node are on the same page about this number without
> communicating it. If someone is opening a channel with data that appears to
> be invlaid because they are using the wrong generation then sending an
> error back indicating what you are up to should be sufficient to recover?

If you ever lose that information, you can never open a channel again? Or you simply believe them and retry if they offer a higher generation?

Quoted text (8 lines)
>> 3. Because we need a generation counter, it's not quite as easily
>>    scannable as you'd hope (the "gap" problem).
>>
>
> This doesn't seem to be a big issue. You are trying to recover your funds
> after all so you can afford to scan over very large gaps i.e. leave the
> node on for days. I mean my Bitcoin wallet manages to handle this so why
> wouldn't it work here?

Well, bitcoin core famously didn't do this at all (had a key pool) and people lost funds. Deterministic key generation is better, but it's still making gross assumptions, usually undocumented, on how many keys you can hand out before you *have* to use one.

It's sometimes shocking how unpolished Bitcoin infrastructure is. But it's stuff like this that so many exchanges offer fixed deposit addresses :(

Quoted text (4 lines)
> I wonder if it is even necessary to bump the
> generation until a funding tx is confirmed -- I can't think of a good
> reason why you would want to open two channels to the same node at the same
> time (why not put all your funds into the same funding).

Well, I'd agree with you of course, but other implementations do allow it. If you don't allow it, you don't need a temporary_channel_id at all.

But that still only prevents gaps if you scan the TXO set, not the UTXO set. And it doesn't help with unannounced peers or peers which are no longer in the public graph. You want backups :)

Quoted text (7 lines)
>> I think the "encrypted blob served by peers", even in a very naive way,
>> offers another way to do this, though it requires the assumption that at
>> least one peer is honest.
>
> I see encrypted backups as complementary. With this scheme you can at least
> find a peer that you've had a channel with. From the encrypted backup you
> left with them you can then find others and check against them.

I see encrypted backups as a more-likely-to-be-implemented solution though. Because they're useful to send to places other than peers, and they can also contain HTLC information if you want.

And after that, I don't think the remaining problem is sufficient to engineer salvage solutions for.

But we can revisit if I'm wrong!

Cheers, Rusty.

David A. Harding in reply to Lloyd Fournier permalink original

On Mon, Dec 07, 2020 at 11:32:27AM +1100, Lloyd Fournier wrote:

Quoted text (11 lines)
> I've been considering the problem of recovering lightning channels after
> losing channel state in a boating accident. The modern way of doing this
> seems to be "static channel backups" -- these are essentially lists of
> channel ids and the nodes you had the channels with.
>
> The idea is that with this backup you can remember who you had channels
> with, connect to them and ask them to force close the channel (can someone
> link me the concrete protocol messages you send to do this?).
>
> It occurred to me that if the lightning protocol were changed slightly you
> could do this without the channel backup at all.

I was re-reading this OP after seeing some of Rusty's concerns later in the thread and I was wondering why we need to alter funding at all. We're assuming that when Alice recovers from her backup, she knows her node_id and has the private key necessary to sign messages for it, so why can't she sign a message that gets gossiped across the network that says, "if you have a channel with node_id 0xa11ce, please close it now"?

Maybe the message also includes a signed timestamp or block height so only channels with funding transactions confirmed before that time/height get closed, preventing future replays of the message from closing Alice's later channels opened with the same seed/node_id.

-Dave

Lloyd Fournier in reply to David A. Harding permalink original

On Thu, 29 Apr 2021 at 06:15, David A. Harding <dave at dtrt.org> wrote:

Quoted text (3 lines)
> why can't she sign a message that gets gossiped across the network that
> says, "if you have a channel with node_id 0xa11ce, please close it now"?
>

Hi David,

As a user this would be an improvement. There are a few downsides though:

1. It creates a new gossip message that can be spammed. 2. It is not easy to figure out whether it worked or not (maybe the person I had my channel with is offline?). 3. This is incompatible with covert recovery schemes like in [1] whereas what I propose here builds towards making such schemes possible in the future.

(3) is also a problem with just doing encrypted backups -- going around looking for backups means you tell everyone that you are in recovery mode.

[1]: https://www.learnbitcoin.com/archive/lists/lightning-dev/t/2020/covert-channel-recovery-with-oblivious-signatures#m-b2142d15

LL

David A. Harding in reply to Lloyd Fournier permalink original

On Mon, May 03, 2021 at 11:01:48AM +1000, Lloyd Fournier wrote:

Quoted text (1 line)
> 2. It is not easy to figure out whether it worked or not

Good point.

Quoted text (3 lines)
> 3. This is incompatible with covert recovery schemes like in [1] [...]
> (3) is also a problem with just doing encrypted backups -- going around
> looking for backups means you tell everyone that you are in recovery mode.

Eh, I assume nodes using the backup commons would, each time they're restarted, go through the steps of downloading some number of backups even if they haven't lost any data. This tests that the backups are being stored faithfully (essential to any backup process) and provides cover for cases where a node does lose data.

Thus encrypted backups seem fully compatible to me with covert recovery. Sorry, I didn't realize that compatibility was one of your design goals for seed-based peer rediscovery.

-Dave

Lloyd Fournier in reply to David A. Harding permalink original

On Mon, 3 May 2021 at 22:58, David A. Harding <dave at dtrt.org> wrote:

Quoted text (16 lines)
> On Mon, May 03, 2021 at 11:01:48AM +1000, Lloyd Fournier wrote:
> > 2. It is not easy to figure out whether it worked or not
>
> Good point.
>
> > 3. This is incompatible with covert recovery schemes like in [1] [...]
> > (3) is also a problem with just doing encrypted backups -- going around
> > looking for backups means you tell everyone that you are in recovery
> mode.
>
> Eh, I assume nodes using the backup commons would, each time they're
> restarted, go through the steps of downloading some number of backups
> even if they haven't lost any data.  This tests that the backups are
> being stored faithfully (essential to any backup process) and provides
> cover for cases where a node does lose data.
>

Ok this is a fun idea and hadn't thought of it like that before. Here are the thoughts that come to mind:

1. Each time you start up your node you backup you go around to different nodes -- but the obvious question is *which* nodes do you go to? You could try and do something like rendezvous hashing [1] to reduce the set (with some secret data as input so it is not predictable to anyone but yourself) . 2. What do you backup? Full-channel state or just a channel list? Even if you have mostly honest backup nodes you need to make sure you delete old states from your remote backups before revoking them if you do full backups. This slows down sending payments but it might be worth it for users like myself. So perhaps it's still better to avoid full backups here.

[1] https://en.wikipedia.org/wiki/Rendezvous_hashing

LL