From 0a093754d70e27abeb3506df452acee5885e10b8 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 1 Mar 2025 18:18:52 +0000 Subject: [PATCH] bitcoin.py: rm bip340_tagged_hash (duplicated from electrum_ecc) --- electrum/bitcoin.py | 7 +------ electrum/lnutil.py | 5 +++-- electrum/transaction.py | 3 ++- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/electrum/bitcoin.py b/electrum/bitcoin.py index 88c29aebe..13af035c7 100644 --- a/electrum/bitcoin.py +++ b/electrum/bitcoin.py @@ -28,6 +28,7 @@ import enum from enum import IntEnum, Enum import electrum_ecc as ecc +from electrum_ecc.util import bip340_tagged_hash from .util import bfh, BitcoinException, assert_bytes, to_bytes, inv_dict, is_hex_str, classproperty from . import segwit_addr @@ -813,12 +814,6 @@ TapTreeLeaf = Tuple[int, bytes] TapTree = Union[TapTreeLeaf, Sequence['TapTree']] -# FIXME just use electrum_ecc.util.bip340_tagged_hash instead -def bip340_tagged_hash(tag: bytes, msg: bytes) -> bytes: - # note: _libsecp256k1.secp256k1_tagged_sha256 benchmarks about 70% slower than this (on my machine) - return sha256(sha256(tag) + sha256(tag) + msg) - - def taproot_tree_helper(script_tree: TapTree): if isinstance(script_tree, tuple): leaf_version, script = script_tree diff --git a/electrum/lnutil.py b/electrum/lnutil.py index edc0e2fee..a6b14fb5b 100644 --- a/electrum/lnutil.py +++ b/electrum/lnutil.py @@ -12,6 +12,7 @@ import sys import electrum_ecc as ecc from electrum_ecc import CURVE_ORDER, ecdsa_sig64_from_der_sig, ECPubkey, string_to_number +from electrum_ecc.util import bip340_tagged_hash import attr from .util import bfh, inv_dict, UserFacingException @@ -641,7 +642,7 @@ def derive_multisig_funding_key_if_we_opened( assert isinstance(nlocktime, int) nlocktime_bytes = int.to_bytes(nlocktime, length=4, byteorder="little", signed=False) node_id_prefix = remote_node_id_or_prefix[0:NODE_ID_PREFIX_LEN] - funding_key = ecc.ECPrivkey(bitcoin.bip340_tagged_hash( + funding_key = ecc.ECPrivkey(bip340_tagged_hash( tag=b"electrum/ln_multisig_funding_key/we_opened", msg=funding_root_secret + node_id_prefix + nlocktime_bytes, )) @@ -665,7 +666,7 @@ def derive_multisig_funding_key_if_they_opened( assert isinstance(remote_funding_pubkey, bytes) assert len(remote_funding_pubkey) == 33 node_id_prefix = remote_node_id_or_prefix[0:NODE_ID_PREFIX_LEN] - funding_key = ecc.ECPrivkey(bitcoin.bip340_tagged_hash( + funding_key = ecc.ECPrivkey(bip340_tagged_hash( tag=b"electrum/ln_multisig_funding_key/they_opened", msg=funding_root_secret + node_id_prefix + remote_funding_pubkey, )) diff --git a/electrum/transaction.py b/electrum/transaction.py index 5712ffec2..4aebe140c 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -41,6 +41,7 @@ import binascii import copy import electrum_ecc as ecc +from electrum_ecc.util import bip340_tagged_hash from . import bitcoin, constants, segwit_addr, bip32 from .bip32 import BIP32Node @@ -2319,7 +2320,7 @@ class PartialTransaction(Transaction): merkle_root = txin.tap_merkle_root or bytes() output_privkey_bytes = taproot_tweak_seckey(privkey_bytes, merkle_root) output_privkey = ecc.ECPrivkey(output_privkey_bytes) - msg_hash = bitcoin.bip340_tagged_hash(b"TapSighash", pre_hash) + msg_hash = bip340_tagged_hash(b"TapSighash", pre_hash) sig = output_privkey.schnorr_sign(msg_hash) sighash = txin.sighash if txin.sighash is not None else Sighash.DEFAULT else: