Skip to content

hsmtool: recover HTLC outputs after channel state loss - #9730

Open
sangbida wants to merge 14 commits into
ElementsProject:masterfrom
sangbida:hsmtool/recover-htlc
Open

sangbida wants to merge 14 commits into
ElementsProject:masterfrom
sangbida:hsmtool/recover-htlc

Conversation

@sangbida

@sangbida sangbida commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • HTLCs we offered, which we reclaim after the CLTV expiry;
  • HTLCs the peer offered, which we claim with the preimage;
  • anchor and non-anchor channels;
  • identical MPP outputs, which are swept together.
    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_secret so hsmd and the recovery tools all use the same implementation.

Limitations

This is not recovery from hsm_secret alone. The operator still needs the peer ID, channel DBID, peer HTLC basepoint, commitment transaction, HTLC details, and the peer's per-commitment point.

sangbida and others added 14 commits November 11, 2026 15:40
Allow callers to derive one public commitment point without printing the shaseed or any per-commitment secrets.

Co-authored-by: Cursor <cursoragent@cursor.com>
Move the historical channel-seed calculation into common/hsm_secret and reuse it from hsmd, checkchannels and lightning-hsmtool.

Co-authored-by: Cursor <cursoragent@cursor.com>
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 <cursoragent@cursor.com>
Derive the channel basepoints and commitment-specific keys needed to recover HTLC outputs without contacting lightningd or hsmd.

Co-authored-by: Cursor <cursoragent@cursor.com>
Build offered and received HTLC scripts for the peer’s commitment, including anchor channels and CLTV expiry.

Co-authored-by: Cursor <cursoragent@cursor.com>
Match reconstructed P2WSH scripts against commitment outputs and retain every matching index so identical MPP parts can be recovered together.

Co-authored-by: Cursor <cursoragent@cursor.com>
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 <cursoragent@cursor.com>
Build a single transaction spending every matched HTLC output and sign each input with the appropriate timeout or preimage witness.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sum the matched HTLC outputs and calculate the fee and destination amount from the estimated signed transaction weight.

Co-authored-by: Cursor <cursoragent@cursor.com>
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 <cursoragent@cursor.com>
Read a recovery bundle, validate its external inputs, and print signed transactions for each matching HTLC claim.

Co-authored-by: Cursor <cursoragent@cursor.com>
Show the recovered inputs, output amount, fee, transaction ID, and verification and broadcast instructions.

Co-authored-by: Cursor <cursoragent@cursor.com>
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 <cursoragent@cursor.com>
Document the recovery bundle, where each input comes from, the point-only peer workflow, transaction timing, and operator verification steps.

Co-authored-by: Cursor <cursoragent@cursor.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant