1
0

fix sweeping chan after local force-close using cb

scenario:
- user opens a lightning channel and exports an "imported channel backup"
- user closes channel via local-force-close
  - local ctx is published, to_local output has user's funds and they are CSV-locked for days
- user restores wallet file from seed and imports channel backup
- new wallet file should be able to sweep coins from to_local output (after CSV expires)

This was not working previously, as the local_payment_basepoint was not included in the
imported channel backups, and the code was interpreting the lack of this as the channel not
having option_static_remotekey enabled. This resulted in lnutil.extract_ctn_from_tx
using an incorrect funder_payment_basepoint, and lnsweep not recognising the ctx due to
the garbage ctn value.

The imported channel backup serialisation format is slightly changed to include the
previously missing field, and its version number is bumped (0->1). We allow importing
both version 0 and version 1 backups, however v0 backups cannot handle the above
described scenario (they can only be used to request a remote-force-close).

Note that we were/are setting the missing local_payment_basepoint to the pubkey of
one of the wallet change addresses, which is bruteforceable if necessary, but I
think it is not worth the complexity to add this bruteforce logic. Also note
that the bruteforcing could only be done after the local-force-close was broadcast.

Ideally people with existing channels and already exported v0 backups should re-export
v1 backups... Not sure how to handle this.

closes https://github.com/spesmilo/electrum/issues/8516
This commit is contained in:
SomberNight
2023-07-14 14:21:50 +00:00
parent b04ade5d7d
commit 1a46460d11
5 changed files with 103 additions and 30 deletions

View File

@@ -184,6 +184,7 @@ class AbstractChannel(Logger, ABC):
funding_outpoint: Outpoint
node_id: bytes # note that it might not be the full 33 bytes; for OCB it is only the prefix
_state: ChannelState
sweep_address: str
def set_short_channel_id(self, short_id: ShortChannelID) -> None:
self.short_channel_id = short_id
@@ -289,10 +290,10 @@ class AbstractChannel(Logger, ABC):
if self._sweep_info.get(txid) is None:
our_sweep_info = self.create_sweeptxs_for_our_ctx(ctx)
their_sweep_info = self.create_sweeptxs_for_their_ctx(ctx)
if our_sweep_info is not None:
if our_sweep_info:
self._sweep_info[txid] = our_sweep_info
self.logger.info(f'we (local) force closed')
elif their_sweep_info is not None:
elif their_sweep_info:
self._sweep_info[txid] = their_sweep_info
self.logger.info(f'they (remote) force closed.')
else:
@@ -300,6 +301,12 @@ class AbstractChannel(Logger, ABC):
self.logger.info(f'not sure who closed.')
return self._sweep_info[txid]
def maybe_sweep_revoked_htlc(self, ctx: Transaction, htlc_tx: Transaction) -> Optional[SweepInfo]:
return None
def extract_preimage_from_htlc_txin(self, txin: TxInput) -> None:
return
def update_onchain_state(self, *, funding_txid: str, funding_height: TxMinedInfo,
closing_txid: str, closing_height: TxMinedInfo, keep_watching: bool) -> None:
# note: state transitions are irreversible, but
@@ -479,15 +486,21 @@ class ChannelBackup(AbstractChannel):
Logger.__init__(self)
self.config = {}
if self.is_imported:
assert isinstance(cb, ImportedChannelBackupStorage)
self.init_config(cb)
self.unconfirmed_closing_txid = None # not a state, only for GUI
def init_config(self, cb):
def init_config(self, cb: ImportedChannelBackupStorage):
local_payment_pubkey = cb.local_payment_pubkey
if local_payment_pubkey is None:
self.logger.warning(
f"local_payment_pubkey missing from (old-type) channel backup. "
f"You should export and re-import a newer backup.")
self.config[LOCAL] = LocalConfig.from_seed(
channel_seed=cb.channel_seed,
to_self_delay=cb.local_delay,
static_remotekey=local_payment_pubkey,
# dummy values
static_remotekey=None,
dust_limit_sat=None,
max_htlc_value_in_flight_msat=None,
max_accepted_htlcs=None,
@@ -580,8 +593,6 @@ class ChannelBackup(AbstractChannel):
@property
def sweep_address(self) -> str:
# Since channel backups do not save the static_remotekey, payment_basepoint in
# their local config is not static)
return self.lnworker.wallet.get_new_sweep_address_for_channel()
def get_local_pubkey(self) -> bytes: