1
0
Commit Graph

265 Commits

Author SHA1 Message Date
SomberNight
cba073dfd1 lightning: change derivation of funding_pubkey
Ideally, given an on-chain backup, after the remote force-closes, we should be able to spend our anchor output,
to CPFP the remote commitment tx (assuming the channel used OPTION_ANCHORS).
To spend the anchor output, we need to be able to sign with the local funding_privkey.

Previously we derived the funding_key from the channel_seed (which comes from os.urandom).
Prior to anchors, there was no use case for signing with the funding_key given a channel backup.
Now with anchors, we should make its derivation deterministic somehow, in a way so that it can
be derived given just an on-chain backup.
- one way would be to put some more data into the existing OP_RETURN
  - uses block space
  - the OP_RETURNs can be disabled via "use_recoverable_channels"
  - only the initiator can use OP_RETURNs (so what if channel is in incoming dir?)
- instead, new scheme for our funding_key:
  - we derive the funding_privkey from the lnworker root secret (derived from our bip32 seed)
  - for outgoing channels:
    - lnworker_root_secret + remote_node_id + funding_tx_nlocktime
  - for incoming channels:
    - lnworker_root_secret + remote_node_id + remote_funding_pubkey
  - a check is added to avoid reusing the same key between channels:
      not letting to user open more than one channel with the same peer in a single block
  - only the first 16 bytes of the remote_node_id are used, as the onchain backup OP_RETURNs only contain that
- as the funding_privkey cannot be derived from the channel_seed anymore, it is included in the
imported channel backups, which in turn need a new version defined
  - a wallet db upgrade is used to update already stored imported cbs
  - alternatively we could keep the imported cbs as-is, so no new version, no new funding_privkey field, as it is clearly somewhat redundant given on-chain backups can reconstruct it
    - however adding the field seems easier
      - otherwise the existing code would try to derive the funding_privkey from the channel_seed
      - also note: atm there is no field in the imported backups to distinguish anchor channels vs static-remotekey channels
2025-01-14 17:56:48 +00:00
ThomasV
67470b92b7 lnchannel: fix extract_preimage for MPP.
- enforce MPP in the corresponding regtest.
 - fix get_invoice_status returning inflight if it was settled onchain
2024-12-15 10:46:18 +01:00
ThomasV
3b1dc194e4 Refactor lnsweep:
- txins have an optional make_witness method
 - instead of gen_tx, SweepInfo has a txin and
   an optional txout, for 1st stage HTLCs
 - sweep transactions are created by lnwatcher

The purpose of this change is to allow combining several
inputs in the same sweep transaction.
2024-12-11 17:14:14 +01:00
bitromortac
d535821516 htlctx: deal with possible peer htlctx batching
Due to anchor channel's sighash.SINGLE and sighash.ANYONECANPAY,
several HTLC-transactions can be combined. This means we must watch for
revoked outputs in the HTLC transaction not only at index 0 but at any
index.
2024-11-26 09:28:00 +01:00
bitromortac
9c277802e9 sweep: rename sweep creation functions
naming scheme: tx(s)_our/their_ctx/htlctx_output-description

function names are shortened to whether a single (tx) or several sweep
transactions (txs) are generated
2024-11-25 19:24:51 +01:00
bitromortac
ea584e13fc anchors: switch to zero-fee-htlcs
* sets the weight of htlc transactions to zero, thereby putting a zero
  fee for the htlc transactions
* add inputs to htlc-tx for fee bumping
* switches feature flags
* disable anchor test vectors, which are now partially invalid
2024-11-25 10:56:50 +01:00
ThomasV
78d19c6e2f amchor outputs: fixes after rebase 2024-11-23 11:26:04 +01:00
bitromortac
3a3f5059b4 backups: restore from closing tx, sweep to_remote
* add a method for backups to sweep to_remote
* to_remote sweeping needs the payment_basepoint's private key
  to sign the sweep transaction
* we restore the private key from our funding multisig pubkey
  (pubished with the closing transaction) and a static payment key secret
2024-11-21 12:18:53 +01:00
ThomasV
4d26fb552b fixes after rebase 2024-11-20 15:02:05 +01:00
bitromortac
7aa3dc1e40 lnutil+lnchannel: add anchors, adapt to_remote
* to_remote has now an additional csv lock of 1
* anchor outputs are added if to_local/remote outputs are present
* funder balance is reduced to accomodate anchors
2024-11-20 11:54:55 +01:00
bitromortac
7907b9c05d lnchannel+lnutil: change htlc output, send new sig
* changes the htlc outputs' witness script to have a csv lock of 1
* send signatures for remote ctx with ANYONECANPAY|SINGLE
* refactor htlc weight (useful for zero-fee-htlc)
2024-11-20 11:03:48 +01:00
bitromortac
c8bf515953 lnutil: update ctx fee calculation for anchors 2024-11-20 10:53:00 +01:00
bitromortac
22f5ff0d0e add static payment key
* in order to be able to sweep to_remote in an onchain backup scenario
  we need to retain the private key for the payment_basepoint
* to facilitate the above, we open a channel derived from a static
  secret (tied to the wallet seed), the static_payment_key combined with
the funding pubkey (multisig_key), which we can restore from the channel
closing transaction
2024-11-20 10:08:26 +01:00
bitromortac
3951e07c53 prepare a channel to have anchors
* add anchor ln features
* peer.use_anchors is added
* channel.has_anchors is added
2024-11-20 09:49:37 +01:00
ThomasV
58fee0d1cc Merge pull request #9265 from SomberNight/202410_ln_address_reuse_2
lnworker: reserve wallet addresses also for chan backups
2024-11-13 10:59:03 +01:00
ThomasV
c58c4d7451 Make lntransport not require lnutil.
This will be useful if we decide to ship lntransport as a separate
package. It is also a conceptual cleanup.

Notes:
 - lntransport still requires crypto.py
 - parsing node id from a bolt11 invoice is not supported.
2024-10-22 09:26:36 +02:00
SomberNight
b9a81cd03e lnworker: reserve wallet addresses also for chan backups
We were already reserving wallet addresses for full channels.
Now we also do the same for imported channel backups.
(but not for onchain, as we don't have enough info for that)

Without this, if the same seed is used on multiple devices (with each
device having its own set of LN channels), the wallet instances will
reuse keys (specifically the payment_basepoint, which for
static_remotekey chans is used as the to_remote output).
Now with this change, at least if the wallet instances have imported
channel backups of each other, this reuse is avoided.
2024-10-20 13:58:10 +00:00
SomberNight
4e47e07550 lnchannel: (trivial) add/fix type-hints 2024-10-20 13:55:33 +00:00
SomberNight
dd140df17d lnchannel: convert sweep_address property to get_sweep_address() method
no functional changes
2024-10-20 13:12:52 +00:00
ThomasV
3721f04ac8 replace electrum/ecc with electrum_ecc package 2024-10-10 15:46:00 +00:00
SomberNight
13d9677e53 transaction: tx.sign API change: rm hex usage 2024-04-29 17:10:30 +00:00
SomberNight
2f1095510c bitcoin.py/transaction.py: API changes: rm most hex usage
Instead of some functions operating with hex strings,
and others using bytes, this consolidates most things to use bytes.

This mainly focuses on bitcoin.py and transaction.py,
and then adapts the API usages in other files.

Notably,
- scripts,
- pubkeys,
- signatures
should be bytes in almost all places now.
2024-04-29 17:10:26 +00:00
SomberNight
bd9d0ccc33 ecc: refactor/clean-up sign/verify APIs 2024-04-11 15:25:45 +00:00
SomberNight
30c9f5b6b1 walletdb: chan dict: small clean-up (incl db upgrade)
- "fail_htlc_reasons" was removed in 9b1c40e396
- "unfulfilled_htlcs": rm 2 dead items from the 4-tuple,
   and convert False value of forwarding_key
2024-03-01 16:28:46 +00:00
SomberNight
d7a9e2d022 qml QEChannelDetails: add some type hints 2024-02-03 03:35:54 +00:00
SomberNight
f808ec911a lnpeer.reest_chan: allow state transition WE_ARE_TOXIC->WE_ARE_TOXIC
if the remote does not know we are behind, they will try to send us chan_reest

```
 38.52 | W | P/lnpeer.Peer.[LNWallet, 034cd7a09f-ed50cf95] | channel_reestablish (533x1x1): remote is ahead of us! They should force-close. Remote PCP: 03857eabd943eec820d56b94d2f162763294565627cc42c4a0db0e7c6b77da46be
 38.52 | E | P/lnpeer.Peer.[LNWallet, 034cd7a09f-ed50cf95] | Exception in main_loop: Exception('Transition not allowed: WE_ARE_TOXIC -> WE_ARE_TOXIC')
Traceback (most recent call last):
  File "/home/user/wspace/electrum/electrum/util.py", line 1148, in wrapper
    return await func(*args, **kwargs)
  File "/home/user/wspace/electrum/electrum/lnpeer.py", line 426, in wrapper_func
    return await func(self, *args, **kwargs)
  File "/home/user/wspace/electrum/electrum/lnpeer.py", line 440, in main_loop
    async with self.taskgroup as group:
  File "/home/user/.local/lib/python3.10/site-packages/aiorpcx/curio.py", line 297, in __aexit__
    await self.join()
  File "/home/user/wspace/electrum/electrum/util.py", line 1319, in join
    task.result()
  File "/home/user/wspace/electrum/electrum/lnpeer.py", line 1231, in on_channel_reestablish
    chan.set_state(ChannelState.WE_ARE_TOXIC)
  File "/home/user/wspace/electrum/electrum/lnchannel.py", line 215, in set_state
    raise Exception(f"Transition not allowed: {old_state.name} -> {state.name}")
Exception: Transition not allowed: WE_ARE_TOXIC -> WE_ARE_TOXIC
```
2024-01-15 21:21:29 +00:00
SomberNight
df58dd1f25 lnchannel.get_close_opts: allow REQUEST_REMOTE_FCLOSE if WE_ARE_TOXIC
related https://github.com/spesmilo/electrum/issues/8770
2024-01-15 20:13:38 +00:00
xiaolou86
50e5b0efd1 electrum: fix typos 2023-12-04 14:15:39 +08:00
ThomasV
9b1c40e396 Refactor payment forwarding:
- all forwarding types use the same flow
 - forwarding callback returns a htlc_key or None
 - forwarding info is persisted in lnworker:
   - ongoing_forwardings
   - downstream to upstream htlc_key
   - htlc_key -> error_bytes
2023-11-18 16:03:18 +01:00
ThomasV
a338459d45 just-in-time channels:
- a node scid alias is derived from the node ID
 - the channel opening fee is sent in a TLV field of open_channel
 - the server requires htlc settlement before broadcasting
   (server does not trust client)
2023-11-13 10:47:18 +01:00
ThomasV
816e617aaf option_zeroconf
- accept zeroconf channels only from a single node
 - fw_info uses get_scid_or_local_alias
2023-11-13 10:47:18 +01:00
ThomasV
98f9f295cf fix #8683; do not force close channel if we just sent update_fulfill_htlc 2023-11-12 17:10:50 +01:00
ThomasV
120faa480e If trampoline is enabled, do not add non-trampoline nodes to invoices
Rationale: The sender should not assume that they share the same list of
hardcoded trampolines as the receiver.
2023-10-29 16:21:25 +01:00
SomberNight
22a8348303 renames: use consistent naming of cltv delta vs cltv abs
to avoid confusing relative vs absolute cltvs
(see b0401a6386)
2023-10-19 16:40:05 +00:00
ThomasV
026a64de94 channel_announcements:
- construct_channel_announcement: return also whether
   node ids are in reverse order
 - maybe_send_channel_announcement:
   return early if signatures have not been received
2023-10-17 12:15:35 +02:00
ThomasV
98a4d7b60d public channels:
- send node and channel announcements.
 - store channel_flags in constraints
 - store signatures in local and remote configs
2023-10-16 13:54:16 +02:00
ThomasV
ac177577a6 lnpeer: do not set channel OPEN before channel_ready has been both sent and received.
fixes #8641
2023-10-13 16:56:25 +02:00
ThomasV
e2fb928e4f delete htlc session keys once they are no longer needed.
fixes #8630

Note: maybe we should plan a WalletDB upgrade to cleanup
existing wallets.
2023-10-08 15:52:41 +02:00
SomberNight
2746a9d93f lnchannel: replace assert htlc.payment_hash==sha256(preimage) w/ exc
asserts should not be used for security checks
2023-09-12 13:30:48 +00:00
SomberNight
1a46460d11 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
2023-07-14 14:29:21 +00:00
Sander van Grieken
a572b9bf87 lnchannel: add noop get_local_scid_alias for ChannelBackup 2023-07-10 14:22:38 +02:00
SomberNight
ca93af2b8a ln: some clean-up for option_scid_alias
- qt chan details dlg: show both local and remote aliases
- lnchannel: more descriptive names, add clarification in doctstrings,
  and also save the "local_scid_alias" in the wallet file (to remember if
  we sent it)
- lnpeer:
  - resend channel_ready msg after reestablish, to upgrade old existing channels
    to having local_scid_alias
  - forwarding bugfix, to follow BOLT-04:
    > - if it returns a `channel_update`:
    >   - MUST set `short_channel_id` to the `short_channel_id` used by the incoming onion.
2023-06-23 19:51:57 +00:00
SomberNight
62ab6d9702 (trivial) reduce log spam during ln-channel-open 2023-03-09 15:18:09 +00:00
SomberNight
d11237d6a1 lnworker: start watching already redeemed chans if txs are missing
This fixes a bug where if one runs `wallet.clear_history()` they would see exceptions later:
```
Traceback (most recent call last):
  File "/home/user/wspace/electrum/electrum/gui/qt/main_window.py", line 866, in timer_actions
	self.update_wallet()
  File "/home/user/wspace/electrum/electrum/gui/qt/main_window.py", line 1021, in update_wallet
	self.update_tabs()
  File "/home/user/wspace/electrum/electrum/gui/qt/main_window.py", line 1033, in update_tabs
	self.utxo_list.update()
  File "/home/user/wspace/electrum/electrum/gui/qt/utxo_list.py", line 103, in update
	self.refresh_row(name, idx)
  File "/home/user/wspace/electrum/electrum/gui/qt/utxo_list.py", line 124, in refresh_row
	parents = self.wallet.get_tx_parents(txid)
  File "/home/user/wspace/electrum/electrum/wallet.py", line 885, in get_tx_parents
	result.update(self.get_tx_parents(_txid))
  File "/home/user/wspace/electrum/electrum/wallet.py", line 881, in get_tx_parents
	for i, txin in enumerate(tx.inputs()):
AttributeError: 'NoneType' object has no attribute 'inputs'
```
This is related to the privacy analysis, which assumes that for each tx item in the history list
we should have the raw tx in the db. This is no longer true after wallet.clear_history(), if
the wallet has certain LN channels. E.g. an already redeemed channel that was local-force-closed,
as that closing tx is not related to the wallet directly.

In commit 3541ecb576, we decided not to watch already redeemed channels.
This is potentially good for e.g. privacy, as the server would otherwise see us subscribe to that chan.
However it means that after running wallet.clear_history() txs related to the channel but not to the
wallet won't be re-downloaded.

Instead, now if there are missing txs for a redeemed channel, we start watching it, hence the
synchronizer will re-downloaded the txs.
2023-03-01 16:20:42 +00:00
SomberNight
f6dc72899a lnsweep: use chan.logger instead of module _logger
to log the chan id for free
2023-03-01 15:27:13 +00:00
SomberNight
373db76ac9 util: kill bh2u
no longer useful, and the name is so confusing...
2023-02-17 11:43:11 +00:00
SomberNight
1ce37c8bb1 transaction: rm hardcoded sighash magic numbers 2023-02-17 11:40:12 +00:00
SomberNight
faea1e6e1a lnchannel: add more debug logging for ctx/htlc sigs
related: https://github.com/spesmilo/electrum/issues/8191
2023-02-13 01:23:47 +00:00
ThomasV
23adb53572 fix crash when trying to display channel backup details 2023-01-25 15:44:37 +01:00
ThomasV
b9393b0603 Support scid alias:
- save remote alias for use in invoices
 - derive local alias from wallet xpub
 - send channel_type without the option_scid_alias bit
   (apparently LND does not like it)
2023-01-13 15:47:30 +01:00