Commit Graph

188 Commits

Author SHA1 Message Date
SomberNight
3379e46249 follow-up prev: add type-hints, etc 2025-10-09 14:57:41 +00:00
SomberNight
f1ad5bc23d transaction: (move-only) move some methods from Tx to PartialTx cls
these were already assuming `isinstance(self, PartialTransaction)`
2025-10-09 14:46:42 +00:00
SomberNight
a4293c483e transaction: SPV-verify TxInput.block_height and .spent_height 2025-09-24 13:46:35 +00:00
SomberNight
6d99717d5b txbatcher: add comment to _to_pay_after 2025-08-21 15:15:08 +00:00
SomberNight
f8926b4957 type-hint some Callables
could not figure out how to type-hint coinchooser.sufficient_funds with typing.Protocol,
at least PyCharm complained on all my attempts
2025-08-18 15:38:25 +00:00
shangchenglumetro
deca7837f1 chore: fix some minor issues in the comments
Signed-off-by: shangchenglumetro <shuang.cui@live.com>
2025-07-04 16:28:47 +08:00
SomberNight
3e4601c61d base64.b64decode: always set validate=True
Notably verifymessage and decrypt(message) were silently ignoring trailing garbage
or inserted non-base64 characters present in signatures/ciphertext.
(both the CLI commands and in the GUI)
I think it is much cleaner and preferable to treat such signatures/ciphertext as invalid.

In fact I find it surprising that base64.b64decode(validate=False) is the default.
Perhaps we should create a helper function for it that set validate=True and use that.
2025-06-03 18:58:05 +00:00
SomberNight
0508625afc transaction: add method verify_sig_for_txin
This new `Transaction.verify_sig_for_txin` function is an instance method of `Transaction` instead of `PartialTransaction`.
It takes a complete txin, a pubkey and a signature, and verifies the signature.

- `get_preimage_script` is renamed to `get_scriptcode_for_sighash` and now effectively has two implementations:
  - the old impl became `PartialTxInput.get_scriptcode_for_sighash`
    - this assumes we are the ones constructing a spending txin and can have knowledge beyond what will be revealed onchain
  - the new impl is in the base class, `TxInput.get_scriptcode_for_sighash`
    - this assumes the txin is already "complete", and mimics a consensus-verifier by extracting the required fields
      from the already complete witness/scriptSig and the scriptpubkey of the funding utxo
- `serialize_preimage` now does not require a PartialTransaction, it also works on the base class Transaction

-----

I intend to use this for debugging only atm: I noticed TxBatcher sometimes creates invalid signatures by seeing
that bitcoind rejects txs with `mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)`.
However the txs in question have multiple txins, with some txins containing multiple signatures, and bitcoind does not tell us
which txin/signature is invalid. Knowing which signature is invalid would be a start, but I can now add some temp debug logging
to `serialize_preimage` to compare the message being signed with the message being verified.

As can be seen from the tests, the signature and the pubkey needs to be manually extracted from the txin to be verified:
we still don't have a script interpreter so we don't have logic to "verify a txin". However this new code adds logic
to verify a signature for a txin/pubkey combo (which is a small part of an interpreter/verifier).
2025-05-18 15:20:19 +00:00
SomberNight
120d8ac62e transaction: (move-only) move serialize_preimage to base class 2025-05-18 14:44:09 +00:00
ThomasV
7bcc7fb0c4 txbatcher: do not require password in memory
- if password is needed, await future and request it from GUI
 - run asyncio task for each TxBatch, so that batches can
   request the password concurrently
2025-05-06 13:04:19 +02:00
Sander van Grieken
64a160027a imports, whitespace, type hints 2025-04-23 16:09:31 +02:00
ThomasV
bcf60fffe3 relative timelocks: also check that nVersion >= 2 2025-04-22 13:28:05 +02:00
ThomasV
69f8176aab Qt: show relative locktime in tx details 2025-04-22 09:30:53 +02:00
SomberNight
6584ae2ef8 transaction: add note serialize_preimage is not caching Sighash.SINGLE
Unlike a full bitcoin node, we rarely (if ever) validate the signatures of arbitrary transactions,
so I don't think these DOS issues can really be used against us.

ref https://rubin.io/bitcoin/2025/03/11/core-vuln-taproot-dos/
ref https://github.com/bitcoin/bitcoin/pull/24105

btw what is not explained in either source link is that the lack of caching is much
more serious for taproot as bip-342 lifted the 10 kbyte max size for scriptPubKeys.
2025-04-09 15:28:13 +00:00
ThomasV
b339b1e7e3 ln_utxo_reserve
When we send max, decrease sent amount in order to keep some
reserve utxo, in order to be able to sweep lightning channels.
2025-04-03 14:39:05 +02:00
ThomasV
bdb7a82220 batch payment manager:
The class TxBatcher handles the creation, broadcast and replacement
of replaceable transactions. Callers (LNWatcher, SwapManager) use
methods add_payment_output and add_sweep_info. Transactions
created by TxBatcher may combine sweeps and outgoing payments.

Transactions created by TxBatcher will have their fee bumped
automatically (this was only the case for sweeps before).

TxBatcher manages several TxBatches. TxBatches are created
dynamically when needed.

The GUI does not touch txbatcher transactions:
  - wallet.get_candidates_for_batching excludes txbatcher
    transactions
  - RBF dialogs do not work with txbatcher transactions

wallet:
  - instead of reading config variables, make_unsigned_transaction
    takes new parameters: base_tx, send_change_to_lighting

tests:
  - unit tests in test_txbatcher.py (replaces test_sswaps.py)
  - force all regtests to use MPP, so that we sweep transactions
    with several HTLCs. This forces the payment manager to aggregate
    first-stage HTLC tx inputs. second-stage are not batched for now.
2025-03-13 10:17:10 +01:00
ThomasV
0e40be5fb5 swaps: replace request_swap_for_tx with request_swap_for_amount,
as this uses less side effects

(change backported from batch_payment_manager)
2025-03-03 14:02:29 +01:00
SomberNight
0a093754d7 bitcoin.py: rm bip340_tagged_hash (duplicated from electrum_ecc) 2025-03-01 18:18:52 +00:00
Sander van Grieken
19cd408f98 organize import, whitespace 2025-01-23 12:58:28 +01:00
SomberNight
043be2439e follow-up prev: add testcase and minor formatting
(minor reshuffling of check so that it matches following line
 and is more clear it is a bounds check)
2025-01-10 11:59:15 +00:00
thecockatiel
ddb67d9bca fix: transaction.py: add extra check to script_GetOp 2025-01-10 11:59:11 +00:00
ThomasV
62af1ee887 fixes for txin.make_witness:
- add witness_sizehint
  - fix make_unsigned_transaction
  - do not remove witness_script in tx.finalize()
2024-12-13 14:44:35 +01:00
ThomasV
7b72655fba tx serialization: breakup tx witness into elements 2024-12-08 09:29:59 +01:00
ThomasV
62331aeb56 coin_chooser: make BIP69_sort optional 2024-12-04 11:24:06 +01:00
ThomasV
3721f04ac8 replace electrum/ecc with electrum_ecc package 2024-10-10 15:46:00 +00:00
SomberNight
f2c96ca587 transaction.sign: (trivial) nicer log line 2024-09-26 15:09:19 +00:00
ThomasV
2f3d89f415 prepare for separation of ecc module:
- move encrypt/sign functions elsewhere
- remove local dependencies in ecc.py, ecc_fast.py (except logging)
2024-06-17 13:05:57 +02:00
SomberNight
3a305881cc transaction.py: impl taproot key-spends
Add support for key-path-spending taproot utxos into transaction.py.

- no wallet support yet
- add some psbt, and minimal descriptor support
- preliminary work towards script-path spends
2024-06-07 10:58:26 +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
0925f15280 wallet/keystore: add apis for "add_slip_19_ownership_proofs_to_tx"
- implement it specifically for the "singlesig trezor" case
- aimed to be generic enough that support for more complex scripts
  and other keystores could be added later
2024-02-21 11:56:13 +00:00
SomberNight
87214c004e trezor: don't try to sign non-ismine inputs
progress towards coinjoin

related https://github.com/spesmilo/electrum/issues/8868
2024-02-05 07:21:28 +00:00
SomberNight
ef9ba0985e wallet: (trivial) add asserts re args in bump_fee and friends
for clearer exceptions in case of bugs

related https://github.com/spesmilo/electrum/issues/8571
2024-01-17 20:37:31 +00:00
SomberNight
248e50eed0 transaction: rename tx.is_final to tx.is_rbf_enabled, and invert it 2024-01-17 02:08:15 +00:00
Sander van Grieken
7b96a83350 wallet: add sighash check to class Abstract_Wallet
qml: use backend sighash check and add user confirmation path
qt: use backend sighash check and add user confirmation path
qml: include get_warning_for_risk_of_burning_coins_as_fees test in txdetails
qt: add warning icon to sighash warning
add sighash and fee checks to wallet.sign_transaction, making all warnings fatal unless ignore_warnings is set to True
tests: test sign_transaction on both code paths with ignore_warnings True and False,
raise specific exceptions TransactionPotentiallyDangerousException and TransactionDangerousException
2023-11-30 12:53:46 +01:00
SomberNight
227e257444 (follow-up) wallet: add option to merge duplicate tx outputs 2023-10-24 14:41:39 +00:00
sha-265
ae8a546bb9 Merge multiple outputs to same address 2023-10-23 16:12:30 +00:00
SomberNight
4c63d8729b add sanity checks we don't sign tx including dummy addr
Somewhat a follow-up to 649ce979ab.

This adds some safety belts so we don't accidentally sign a tx that
contains a dummy address.
Specifically we check that tx does not contain output for dummy addr:
- in wallet.sign_transaction
- in network.broadcast_transaction

The second one is perhaps redundant, but I think it does not hurt.
2023-09-16 04:36:08 +00:00
ThomasV
649ce979ab send tx change to lightning 2023-09-09 14:14:43 +02:00
SomberNight
f29cd810e3 commands: "bumpfee" to accept either a raw tx or a txid as its 1st arg
closes https://github.com/spesmilo/electrum/issues/8603
2023-09-07 13:18:23 +00:00
ThomasV
295734fc53 storage: encapsulate type conversions of stored objects using
decorators (instead of overloading JsonDB._convert_dict and
 _convert_value)
 - stored_in for elements of a StoreDict
 - stored_as for singletons
 - extra register methods are defined for key conversions

This commit was adapted from the jsonpatch branch
2023-06-18 13:08:57 +02:00
SomberNight
eef9680743 trezor plugin: support external pre-signed inputs
closes https://github.com/spesmilo/electrum/issues/8324
2023-06-14 17:03:47 +00:00
SomberNight
209c9f75e0 transaction: fix add_info_from_wallet_and_network
fixes https://github.com/spesmilo/electrum/issues/8406
2023-05-07 21:50:23 +00:00
SomberNight
910832c153 transaction: calc and cache TxInput/TxOutput.address on-demand
In one wallet, before this, make_unsigned_transaction() took 120 sec,
now it takes ~8 sec.

hot path:
```
make_unsigned_transaction (electrum/wallet.py:1696)
add_input_info (electrum/wallet.py:2261)
utxo (electrum/transaction.py:289)
tx_from_any (electrum/transaction.py:1232)
deserialize (electrum/transaction.py:805)
<listcomp> (electrum/transaction.py:805)
parse_output (electrum/transaction.py:706)
__init__ (electrum/transaction.py:127)
scriptpubkey (electrum/transaction.py:173)
get_address_from_output_script (electrum/transaction.py:672)
```
2023-04-26 16:54:13 +00:00
SomberNight
ee52154542 transaction: TxInput: store addr/spk/value_sats, instead of re-calc
We were re-calculating txin.address from the prevtx-utxo on every call,
e.g. in electrum/gui/qt/utxo_list.py#L103 (for every utxo in the wallet).

For a wallet with ~1300 utxos, UTXOList.update() took ~3.2 sec before this,
now it's ~0.8 sec.
2023-04-26 13:08:27 +00:00
SomberNight
312f2641e7 don't use bare except
use "except Exception", or if really needed explicitly "except BaseException"
2023-04-24 12:58:01 +00:00
SomberNight
f0e89b3ef6 addr_sync: migrate usages of get_txpos to get_tx_height
the return value of get_txpos is fine-tuned for sorting... other uses are highly questionable.
2023-04-04 17:49:46 +00:00
SomberNight
0a3e286f1d qt tx dialog: show_qr to warn if QR code is missing data
When exporting a tx as qr code, the prev txs are omitted to save space.
This causes problems with offline signers: software electrum signers will
just warn and then proceed, but hw devices will typically error.
2023-03-30 14:32:31 +00:00
SomberNight
d83863cc52 qt tx dialog: add checkbox "Download input data"
If checked, we download prev (parent) txs from the network, asynchronously.
This allows calculating the fee and showing "input addresses".

We could also SPV-verify the tx, to fill in missing tx_mined_status
(block height, blockhash, timestamp, short ids), but this is not done currently.
Note that there is no clean way to do this with electrum protocol 1.4:
`blockchain.transaction.get_merkle(tx_hash, height)` requires knowledge of the block height.

Loosely based on 6112fe0e51
2023-03-12 00:24:31 +00:00