Repository navigation
Conversation
| } | ||
|
|
||
| /* Unlike atoll, refuses anything which is not entirely a number. */ | ||
| static bool parse_u64(const char *arg, u64 *val) |
There was a problem hiding this comment.
parse_u64 accepts leading whitespace as i see, so myabe we could add !cisdigit(arg[0]) (from ccan/str)
|
|
||
| @unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "sqlite3-specific DB rollback") | ||
| @pytest.mark.parametrize("anchors", [False, True]) | ||
| def test_dataloss_protection_hsmtool_recoverhtlc(node_factory, bitcoind, tmp_path, anchors): |
There was a problem hiding this comment.
maybe we should add cases like a trimmed (dust) HTLC, an encrypted hsm_secret, bad --point-only arguments and small test of parse_bundle?
| #include <common/amount.h> | ||
|
|
||
| struct node_id; | ||
| struct ripemd160; |
| size_t num_recovered = 0; | ||
|
|
||
| /* Transaction and signature helpers require libwally's tal allocator. */ | ||
| common_setup(argv0); |
There was a problem hiding this comment.
common_setup() replaces the secp256k1 context created in main(), so maybe call common_setup(argv[0]) once at the top of main() for every command. The other commands would also get a real tmpctx. Or call it in the recoverhtlc branch of main() and remove the argv0 parameter
| bundle->peer_id, bundle->channel_dbid, | ||
| &bundle->commitment_point, &bundle->remote_htlc_basepoint, | ||
| hsm_secret_path, keys)) | ||
| errx(ERROR_USAGE, "Could not derive HTLC recovery keys"); |
There was a problem hiding this comment.
maybe we can move ERROR_USAGE define to tools/lightning-hsmtool.h and use ERROR_KEYDERIV there
| `bitcoin-cli getrawtransaction`. | ||
| * *feerate\_perkw* is optional, in satoshi per 1000 weight: 250 is | ||
| 1 sat/vB. The default is 2500. | ||
| * Each of *htlcs* with *direction* `out` is an HTLC which we offered: it |
There was a problem hiding this comment.
should we add there a short note on where to get payment_hash and cltv_expiry - the peer s listhtlcs/listpeerchannels, logs, or decoding the commitment tx outputs with the peer?
nGoline
left a comment
There was a problem hiding this comment.
This is a useful tool for the #9200 situation. I went through the derivation and scripts against BOLT #3 and it looks right to me.
Two things needed before this can go green:
- There is no
Changelog-Added:line in any commit or the PR body, so "Pre-build checks" fails at "Check changelog" and none of the Linux jobs have run yet. Please add one to the recoverhtlc commit. - Inline on
test_connection.
Two smaller points, not blocking inline.
| assert (closetxid, "confirmed") in set([(o['txid'], o['status']) for o in l2.rpc.listfunds()['outputs']]) | ||
|
|
||
|
|
||
| @unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "sqlite3-specific DB rollback") |
There was a problem hiding this comment.
The full-integration matrix has a liquid-regtest job, and recoverhtlc refuses Elements networks, so this test will fail there. Please add @unittest.skipIf(TEST_NETWORK != 'regtest', "hsmtool recoverhtlc does not support Elements").
| return "its value does not cover the fee"; | ||
| if (amount_sat_less(amounts.output, chainparams->dust_limit)) | ||
| return "its value after the fee is below the dust limit"; | ||
| if (amount_sat_greater(amounts.fee, amount_sat_div(amounts.input, 4))) |
There was a problem hiding this comment.
Not blocking: a fee above a quarter of the HTLC only warns, and the signed tx is still printed. A mistyped feerate_perkw (an extra zero, or sat/vB vs sat/kw) gives a valid tx that hands most of the HTLC to miners. I would either refuse above a cap unless explicitly overridden, or drop "a wrong value cannot produce a wrong spend" from the man page, since it is not true for feerate_perkw or the destination.
| #define PASSPHRASE_HASH_LEN 32 | ||
| #define HSM_SECRET_PLAIN_SIZE 32 | ||
|
|
||
| void derive_channel_seed(struct secret *channel_seed, |
There was a problem hiding this comment.
Not blocking: The move dropped the /*~ comments from hsmd explaining "peer seed"/"per-peer seed" and the native-endian dbid. Since this is now the only copy, it would be good to keep them there.
d287dad to
659ccdf
Compare
Allow callers to derive one public commitment point without printing the shaseed or any per-commitment secrets. Co-authored-by: Cursor <[email protected]>
Move the historical channel-seed calculation into common/hsm_secret and reuse it from hsmd, checkchannels and lightning-hsmtool. Co-authored-by: Cursor <[email protected]>
The recovery tool is long, so it is best kept in its own file. Expose load_hsm_secret in a header so it can be shared with the recovery tool. Co-authored-by: Cursor <[email protected]>
Derive the channel basepoints and commitment-specific keys needed to recover HTLC outputs without contacting lightningd or hsmd. Co-authored-by: Cursor <[email protected]>
Build offered and received HTLC scripts for the peer’s commitment, including anchor channels and CLTV expiry. Co-authored-by: Cursor <[email protected]>
Match reconstructed P2WSH scripts against commitment outputs and retain every matching index so identical MPP parts can be recovered together. Co-authored-by: Cursor <[email protected]>
Build both anchor variants of the requested HTLC script and use the commitment transaction to determine which one contains the outputs. Co-authored-by: Cursor <[email protected]>
Build a single transaction spending every matched HTLC output and sign each input with the appropriate timeout or preimage witness. Co-authored-by: Cursor <[email protected]>
Sum the matched HTLC outputs and calculate the fee and destination amount from the estimated signed transaction weight. Co-authored-by: Cursor <[email protected]>
Read versioned recovery data from JSON so the command line only needs operational inputs such as the destination and fee rate. Co-authored-by: Cursor <[email protected]>
Read a recovery bundle, validate its external inputs, and print signed transactions for each matching HTLC claim. Changelog-Added: tools: lightning-hsmtool can recover HTLC outputs after channel state loss. Co-authored-by: Cursor <[email protected]>
Show the recovered inputs, output amount, fee, transaction ID, and verification and broadcast instructions. Co-authored-by: Cursor <[email protected]>
Reproduce stale channel state with pending HTLCs and prove recoverhtlc signs broadcastable anchor and non-anchor spends. Cover identical MPP outputs, same-hash opposite directions, strict JSON validation, and skipped unmatched claims. Co-authored-by: Cursor <[email protected]>
Document the recovery bundle, where each input comes from, the point-only peer workflow, transaction timing, and operator verification steps. Co-authored-by: Cursor <[email protected]>
659ccdf to
d36e8de
Compare
The issue
This came out of #9200.
When we have lost channel state, the peer may close with a commitment transaction we have never seen. We can recover our main output, but we cannot recognise the HTLC outputs because we no longer have the peer's per-commitment point or the HTLC information.
This adds:
lightning-hsmtool recoverhtlc <bundle.json> <hsm_secret>
The bundle contains the commitment transaction, channel information and the HTLCs we want to recover. The tool rebuilds the HTLC scripts and checks that they actually match outputs in the commitment transaction before signing anything.
It supports:
If one HTLC was trimmed or does not match, we skip it and continue with the others.
This prints one public commitment point without exposing the shaseed or any commitment secrets, so a cooperative peer can safely give us what we need.
This is not general recovery from only an
hsm_secret. We still need the channel information, HTLC details, commitment transaction and help from the peer. It is intended for cases like #9200, where that information can still be recovered.The channel-seed derivation is moved into
common/hsm_secretso hsmd and the recovery tools all use the same implementation.Limitations
This is not recovery from
hsm_secretalone. The operator still needs the peer ID, channel DBID, peer HTLC basepoint, commitment transaction, HTLC details, and the peer's per-commitment point.