From 89bd520185e9b45a5de7fb90b05e2586880d55f3 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 24 Oct 2020 05:18:16 +0200 Subject: [PATCH] bitcoin: move construct_witness from transaction.py to bitcoin.py --- electrum/bitcoin.py | 14 +++++++++++++- electrum/lnsweep.py | 4 ++-- electrum/lnutil.py | 4 ++-- electrum/submarine_swaps.py | 5 +++-- electrum/transaction.py | 14 +------------- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/electrum/bitcoin.py b/electrum/bitcoin.py index b615860d2..7732c92dc 100644 --- a/electrum/bitcoin.py +++ b/electrum/bitcoin.py @@ -24,7 +24,7 @@ # SOFTWARE. import hashlib -from typing import List, Tuple, TYPE_CHECKING, Optional, Union +from typing import List, Tuple, TYPE_CHECKING, Optional, Union, Sequence import enum from enum import IntEnum, Enum @@ -299,6 +299,18 @@ def add_number_to_script(i: int) -> bytes: return bfh(push_script(script_num_to_hex(i))) +def construct_witness(items: Sequence[Union[str, int, bytes]]) -> str: + """Constructs a witness from the given stack items.""" + witness = var_int(len(items)) + for item in items: + if type(item) is int: + item = script_num_to_hex(item) + elif isinstance(item, (bytes, bytearray)): + item = bh2u(item) + witness += witness_push(item) + return witness + + def relayfee(network: 'Network' = None) -> int: """Returns feerate in sat/kbyte.""" from .simple_config import FEERATE_DEFAULT_RELAY, FEERATE_MAX_RELAY diff --git a/electrum/lnsweep.py b/electrum/lnsweep.py index a10c64cca..d859aab42 100644 --- a/electrum/lnsweep.py +++ b/electrum/lnsweep.py @@ -6,7 +6,7 @@ from typing import Optional, Dict, List, Tuple, TYPE_CHECKING, NamedTuple, Calla from enum import Enum, auto from .util import bfh, bh2u -from .bitcoin import redeem_script_to_address, dust_threshold +from .bitcoin import redeem_script_to_address, dust_threshold, construct_witness from . import ecc from .lnutil import (make_commitment_output_to_remote_address, make_commitment_output_to_local_witness_script, derive_privkey, derive_pubkey, derive_blinded_pubkey, derive_blinded_privkey, @@ -15,7 +15,7 @@ from .lnutil import (make_commitment_output_to_remote_address, make_commitment_o get_ordered_channel_configs, privkey_to_pubkey, get_per_commitment_secret_from_seed, RevocationStore, extract_ctn_from_tx_and_chan, UnableToDeriveSecret, SENT, RECEIVED, map_htlcs_to_ctx_output_idxs, Direction) -from .transaction import (Transaction, TxOutput, construct_witness, PartialTransaction, PartialTxInput, +from .transaction import (Transaction, TxOutput, PartialTransaction, PartialTxInput, PartialTxOutput, TxOutpoint) from .simple_config import SimpleConfig from .logging import get_logger, Logger diff --git a/electrum/lnutil.py b/electrum/lnutil.py index 24f3d341e..4d1469416 100644 --- a/electrum/lnutil.py +++ b/electrum/lnutil.py @@ -19,7 +19,7 @@ from .transaction import (Transaction, PartialTransaction, PartialTxInput, TxOut PartialTxOutput, opcodes, TxOutput) from .ecc import CURVE_ORDER, sig_string_from_der_sig, ECPubkey, string_to_number from . import ecc, bitcoin, crypto, transaction -from .bitcoin import push_script, redeem_script_to_address, address_to_script +from .bitcoin import push_script, redeem_script_to_address, address_to_script, construct_witness from . import segwit_addr from .i18n import _ from .lnaddr import lndecode @@ -475,7 +475,7 @@ def make_htlc_tx_witness(remotehtlcsig: bytes, localhtlcsig: bytes, assert type(localhtlcsig) is bytes assert type(payment_preimage) is bytes assert type(witness_script) is bytes - return bfh(transaction.construct_witness([0, remotehtlcsig, localhtlcsig, payment_preimage, witness_script])) + return bfh(construct_witness([0, remotehtlcsig, localhtlcsig, payment_preimage, witness_script])) def make_htlc_tx_inputs(htlc_output_txid: str, htlc_output_index: int, amount_msat: int, witness_script: str) -> List[PartialTxInput]: diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py index 33c6e6859..c71ecfa3a 100644 --- a/electrum/submarine_swaps.py +++ b/electrum/submarine_swaps.py @@ -7,8 +7,9 @@ import attr from .crypto import sha256, hash_160 from .ecc import ECPrivkey -from .bitcoin import address_to_script, script_to_p2wsh, redeem_script_to_address, opcodes, p2wsh_nested_script, push_script, is_segwit_address -from .transaction import TxOutpoint, PartialTxInput, PartialTxOutput, PartialTransaction, construct_witness +from .bitcoin import (script_to_p2wsh, opcodes, p2wsh_nested_script, push_script, + is_segwit_address, construct_witness) +from .transaction import PartialTxInput, PartialTxOutput, PartialTransaction from .transaction import script_GetOp, match_script_against_template, OPPushDataGeneric, OPPushDataPubkey from .util import log_exceptions from .lnutil import REDEEM_AFTER_DOUBLE_SPENT_DELAY, ln_dummy_address, LN_MAX_HTLC_VALUE_MSAT diff --git a/electrum/transaction.py b/electrum/transaction.py index 47e38f051..bc48dbfcb 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -48,7 +48,7 @@ from .bitcoin import (TYPE_ADDRESS, TYPE_SCRIPT, hash_160, var_int, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC, COIN, int_to_hex, push_script, b58_address_to_hash160, opcodes, add_number_to_script, base_decode, is_segwit_script_type, - base_encode) + base_encode, construct_witness) from .crypto import sha256d from .logging import get_logger @@ -479,18 +479,6 @@ def parse_input(vds: BCDataStream) -> TxInput: return TxInput(prevout=prevout, script_sig=script_sig, nsequence=nsequence) -def construct_witness(items: Sequence[Union[str, int, bytes]]) -> str: - """Constructs a witness from the given stack items.""" - witness = var_int(len(items)) - for item in items: - if type(item) is int: - item = bitcoin.script_num_to_hex(item) - elif isinstance(item, (bytes, bytearray)): - item = bh2u(item) - witness += bitcoin.witness_push(item) - return witness - - def parse_witness(vds: BCDataStream, txin: TxInput) -> None: n = vds.read_compact_size() witness_elements = list(vds.read_bytes(vds.read_compact_size()) for i in range(n))