lightning icon indicating copy to clipboard operation
lightning copied to clipboard

How to restore from hsm_secret file

Open RazerPC opened this issue 7 years ago • 14 comments

Issue and Steps to Reproduce

I had lost access to a node and I only backed up the hsm_secret file because at the time I thought that was the only file I needed to backup

Now I've set up a node again, put my old hsm_secret file but it says my funds are 0

Is there anything I can do?

getinfo output

"id": "ID", "alias": "name", "color": "ff9900", "num_peers": 2, "num_pending_channels": 0, "num_active_channels": 0, "num_inactive_channels": 0, "address": [ { "type": "ipv4", "address": "ip", "port": 9735 } ], "binding": [ { "type": "ipv6", "address": "::", "port": 9735 }, { "type": "ipv4", "address": "0.0.0.0", "port": 9735 } ], "version": "v0.7.0-20-g6f71564", "blockheight": 566235, "network": "bitcoin", "msatoshi_fees_collected": 0, "fees_collected_msat": "0msat" }

RazerPC avatar Mar 08 '19 21:03 RazerPC

Nobody?

RazerPC avatar Mar 13 '19 10:03 RazerPC

Out of my head, for funds not in channels :

Restart c-lightning with --rescan <blockheight> option where blockheight is less then the height of the first deposit to c-lightning's wallet. It should then detect all funds that it can spend from the seed.

Not exactly sure, but maybe you also need to call newaddr once (maybe with p2sh-segwit if the deposit address was that type). Than restart with --rescan <blockheight> as above.

If you have funds in channels and only have the seed, well .. I don't really know, perhaps have a look at #1918, #1156.

ghost avatar Mar 14 '19 16:03 ghost

Unfortunately that didn't work, this is the output of list funds

{ "outputs": [ ], "channels": [ ] }

RazerPC avatar Mar 14 '19 23:03 RazerPC

small correction: to get any funds back in change- or deposit addresses, first call lighnting-cli newaddr a couple of times, as many times as addresses where used for deposits or change or closing tx's in the old node. Then run c-lightning --rescan -<height> (note the minus sign before height) where <height> is the lowest absolute blockheight you expect any of your outputs to be.

ghost avatar Mar 16 '19 14:03 ghost

That worked! :D

Most of my funds seem to be stuck in a channel. Should I wait for the channel to close automatically or what?

From what I can see asking the other party to force close the channel would result in losing my funds. Is this correct?

RazerPC avatar Mar 17 '19 10:03 RazerPC

https://github.com/ElementsProject/lightning/issues/1156#issuecomment-370280072

Coinomatron avatar Mar 20 '19 11:03 Coinomatron

From what I can see asking the other party to force close the channel would result in losing my funds. Is this correct?

TLDR: I am afraid yes. Besides hsm_secret, you also need the (remote) peer_id and dbid of the channel. Then (theoretically) it is possible ... I think!

Well ... not sure if below is correct, but here is my attempt.

When the other party unilaterally closes the channel, it signs and broadcast the last commitment transaction it has. That commit tx has a to_remote output that pays to remotepubkey (from his perspective), which is your localpubkey.

However your localpubkey changes with every commit tx and is derived from basepoint_secret and per_commitment_point.

From BOLT #3

These pubkeys are simply generated by addition from their base points: pubkey = basepoint + SHA256(per_commitment_point || basepoint) * G The localpubkey uses the local node's payment_basepoint

In c-lightning, payment_basepoint is derived from a channel_seed, which in turn is derived from remote node's peer_id (pubkey) and a dbid (some database id of the channel).

https://github.com/ElementsProject/lightning/blob/3e215287cdb25d0c640b4a3f9735666f58573671/hsmd/hsmd.c#L724-L725

Then you also need the per_commitment_point

The per_commitment_point is generated using elliptic-curve multiplication: per_commitment_point = per_commitment_secret * G

https://github.com/ElementsProject/lightning/blob/3e215287cdb25d0c640b4a3f9735666f58573671/hsmd/hsmd.c#L1121-L1125

which needs the commit_num (index), but that can be extracted from the (unilateral) closing tx or one can iterate through it.

ghost avatar Mar 21 '19 07:03 ghost

I believe efforts are underway to create backupss, see #2400

ghost avatar Mar 21 '19 07:03 ghost

I got the same problem - my node suffered a total existence failure, all i have is a copy of .lightning directory from Nov 15th, but most of the funds are in a channel created on Nov 16th and still active.

So i got hsm_secret, i know the public channel ids, and the peer is open to communications.

Is there an actual tested recipe for recovering the funds in this situation if the channel were to be closed from the remote side? Have anyone succeeded at doing this before?

I got some experience and scripts for Bitcoin-related cryptography, and am willing to try complex solutions, but i doubt i'll be able to chew through clightning code to figure it all out.

More precisely, looking at @SimonVrouwe 's answer, i want to double check:

  1. dbid sounds like the index of the channel in the channels table. Given a backup of a state prior to creation of the channel, i should just add one to the last entry in the table, right?

  2. hsm_secret key derivatives are described here: https://github.com/hkjn/lnhw/tree/master/doc/hsmd , are they still valid?

  3. Once i derive the funding_pubkey, is there a way to test it in case i'll have to guess dbid? Since the value is hashed along with an unknown remote_funding_pubkey, only way i can see is asking for the channel to be closed, and then looking at the script in the closing transaction?

  4. Then i'll have to brute force through commit_num, generating private keys as payment_basepoint_secret + SHA256(per_commitment_point || payment_basepoint), and looking for one that maps to the public key of my output of the closing transaction, correct?

  5. Finally, is asking the channel to be closed a safe action given the situation? Is there something i missed that i'll need to know from the peer's knowledge about the channel, or is it no longer relevant past their ability to produce a closing transaction?

artlav avatar Dec 17 '19 23:12 artlav

Given a backup of a state prior to creation of the channel, i should just add one to the last entry in the table, right?

Assuming you did not create other channels then close them, or other nodes create channels and close them, before creating the channel in question. However, you could indeed grind it starting there, and the chances are pretty good that you will only grind a few dbid, possibly just one.

hsm_secret key derivatives are described here: https://github.com/hkjn/lnhw/tree/master/doc/hsmd , are they still valid?

Looks right, but I have not seen the hsmd code in a good while. In particular I remember noting before that we have an endianness regarding dbid, we just use whatever endianness the processor has, so you need to know your processor endianness. I am uncertain if we have fixed that since.

Once i derive the funding_pubkey, is there a way to test it in case i'll have to guess dbid? Since the value is hashed along with an unknown remote_funding_pubkey, only way i can see is asking for the channel to be closed, and then looking at the script in the closing transaction?

I believe that is correct (modulo the fact that I have not seen hsmd code in a good bit).

Then i'll have to brute force through commit_num, generating private keys as payment_basepoint_secret + SHA256(per_commitment_point || payment_basepoint), and looking for one that maps to the public key of my output of the closing transaction, correct?

I think commit_num is encoded somewhere in the commitment transaction. https://github.com/lightningnetwork/lightning-rfc/blob/master/03-transactions.md#commitment-transaction Some grinding is still needed since only the upper 24 bits is stored in obscured form.

Or if you are trusting the remote peer anyway maybe just ask them the commitment number....

Finally, is asking the channel to be closed a safe action given the situation? Is there something i missed that i'll need to know from the peer's knowledge about the channel, or is it no longer relevant past their ability to produce a closing transaction?

Definitely not, if you do not trust the peer. If you think you can trust the peer, you could risk it, and most humans will probably be honest and give the latest commitment tx. Most commodity software will not store previous commitment txes anyway, though of course the code is open source and anyone can "improve" the commodity software to store the best (for them) previous commitment transaction. But I think your chances are still good anyway, LN is not under active attack yet.

ZmnSCPxj avatar Dec 18 '19 02:12 ZmnSCPxj

Ok, so i spent quite some time banging my head against a testnet reproduction of the situation, only to discover that a unilateral close paid out to the payment_basepoint.

Apparently there is a feature called option_static_remotekey, which makes salvaging things like that quite easy.

Which raises a question: Is there a way for me to check if a channel was created with it or not? The feature has been around for a year in clightning, but i can't figure out if LND supports it or not (the mainnet peer is likely LND).

artlav avatar Dec 22 '19 04:12 artlav

Well, dead end.

From the point of view of my node a regular unilateral closure transaction pays out to a public key which is owned by

my_payment_basepoint_secret + SHA256(remote_per_commitment_point || my_payment_basepoint)

Where remote_per_commitment_point is per_commitment_point derived from shaseed of the other node.

I don't see how i can find this out without asking the peer directly. So, best i can tell it's impossible to recover in this situation.

Have i missed something?

artlav avatar Dec 22 '19 20:12 artlav

Followup: The funds were successfully recovered once the peer managed to perform the necessary LND shenanigans needed to extract the per_commitment_point from their database.

To sum it up:

Either you get lucky having option_static_remotekey on and the closing UTXO will be in name of your payment_basepoint, derived as described above.

Or you don't get lucky, and the closing UTXO will be in name of payment_basepoint_secret + SHA256(remote_per_commitment_point || payment_basepoint), to derive which you would have to ask the peer to provide their per_commitment_point.

The latter is extractable from lightningd.sqlite3 on clightning with sqlite command line tool, and from channel.db on LND using the tool in the issue linked above.

artlav avatar Jan 06 '20 23:01 artlav

Hey that's good news @artlav, thanks for following up :+1:

cdecker avatar Jan 07 '20 20:01 cdecker