diff --git a/electrum/plugins/joinmarket/jm_base_code.py b/electrum/plugins/joinmarket/jm_base_code.py index d7908a963..08a4ab4e1 100644 --- a/electrum/plugins/joinmarket/jm_base_code.py +++ b/electrum/plugins/joinmarket/jm_base_code.py @@ -8,7 +8,8 @@ from math import ceil, exp from numbers import Integral from typing import Union, List, Tuple, Optional, Dict, Any -from electrum import ecc +import electrum_ecc as ecc + from electrum.bip32 import convert_bip32_intpath_to_strpath from electrum.bitcoin import (script_to_scripthash, address_to_script, construct_script, opcodes) diff --git a/electrum/plugins/joinmarket/jm_util.py b/electrum/plugins/joinmarket/jm_util.py index a7b620617..ed2330ad1 100644 --- a/electrum/plugins/joinmarket/jm_util.py +++ b/electrum/plugins/joinmarket/jm_util.py @@ -5,7 +5,8 @@ import logging import re from enum import IntEnum -from electrum import ecc +import electrum_ecc as ecc + from electrum import constants from electrum.bitcoin import sha256d, address_to_script, is_address, opcodes from electrum.descriptor import (PubkeyProvider, PKHDescriptor, WPKHDescriptor, diff --git a/electrum/plugins/joinmarket/jmbitcoin/secp256k1_main.py b/electrum/plugins/joinmarket/jmbitcoin/secp256k1_main.py index 63e45bf36..ea6b4af74 100644 --- a/electrum/plugins/joinmarket/jmbitcoin/secp256k1_main.py +++ b/electrum/plugins/joinmarket/jmbitcoin/secp256k1_main.py @@ -3,10 +3,9 @@ import base64 from typing import List, Tuple, Union -from electrum import ecc +import electrum_ecc as ecc + from electrum.bitcoin import sha256d, usermessage_magic -from electrum.ecc import (ECPrivkey, ECPubkey, ecdsa_der_sig_from_r_and_s, - ecdsa_sig64_from_der_sig) from electrum.util import to_bytes from ..jmbase import bintohex @@ -25,20 +24,20 @@ def getG(compressed=True): return ecc.GENERATOR.get_public_key_bytes(compressed) -podle_PublicKey_class = ECPubkey -podle_PrivateKey_class = ECPrivkey +podle_PublicKey_class = ecc.ECPubkey +podle_PrivateKey_class = ecc.ECPrivkey -def podle_PublicKey(P: bytes) -> ECPubkey: +def podle_PublicKey(P: bytes) -> ecc.ECPubkey: """Returns a PublicKey object from a binary string """ - return ECPubkey(P) + return ecc.ECPubkey(P) -def podle_PrivateKey(priv: bytes) -> ECPrivkey: +def podle_PrivateKey(priv: bytes) -> ecc.ECPrivkey: """Returns a PrivateKey object from a binary string """ - return ECPrivkey(priv) + return ecc.ECPrivkey(priv) def read_privkey(priv: bytes) -> Tuple[bool, bytes]: @@ -54,14 +53,14 @@ def read_privkey(priv: bytes) -> Tuple[bool, bytes]: return (compressed, priv[:32]) -def privkey_to_pubkey(priv: bytes) -> ECPubkey: +def privkey_to_pubkey(priv: bytes) -> ecc.ECPubkey: '''Take 32/33 byte raw private key as input. If 32 bytes, return as uncompressed raw public key. If 33 bytes and the final byte is 01, return compresse public key. Else throws Exception.''' compressed, priv = read_privkey(priv) - privk = ECPrivkey(priv) - return ECPubkey(privk.get_public_key_bytes()) + privk = ecc.ECPrivkey(priv) + return ecc.ECPubkey(privk.get_public_key_bytes()) def ecdsa_sign(msg: str, priv: bytes) -> str: @@ -89,12 +88,12 @@ def ecdsa_raw_sign(msg: Union[bytes, bytearray], if rawmsg and len(msg) != 32: raise Exception("Invalid hash input to ECDSA raw sign.") compressed, p = read_privkey(priv) - newpriv = ECPrivkey(p) + newpriv = ecc.ECPrivkey(p) if rawmsg: - sig = newpriv.ecdsa_sign(msg, sigencode=ecdsa_der_sig_from_r_and_s) + sig = newpriv.ecdsa_sign(msg, sigencode=ecc.ecdsa_der_sig_from_r_and_s) else: msg = sha256d(usermessage_magic(to_bytes(msg))) - sig = newpriv.ecdsa_sign(msg, sigencode=ecdsa_der_sig_from_r_and_s) + sig = newpriv.ecdsa_sign(msg, sigencode=ecc.ecdsa_der_sig_from_r_and_s) return sig @@ -114,10 +113,10 @@ def ecdsa_raw_verify(msg: bytes, not guaranteed, so return False on any parsing exception. ''' try: - sig = ecdsa_sig64_from_der_sig(sig) + sig = ecc.ecdsa_sig64_from_der_sig(sig) if rawmsg: assert len(msg) == 32 - newpub = ECPubkey(pub) + newpub = ecc.ECPubkey(pub) if rawmsg: retval = newpub.ecdsa_verify(sig, msg) else: @@ -138,22 +137,22 @@ def multiply(s: bytes, pub: bytes, return_serialized: bool = True) -> bytes: ('raw' options passed in) ''' try: - ECPrivkey(int.to_bytes(s, length=32, byteorder="big")) + ecc.ECPrivkey(int.to_bytes(s, length=32, byteorder="big")) except ValueError: raise ValueError("Invalid tweak for libsecp256k1 " "multiply: {}".format(bintohex(s))) - pub_obj = ECPubkey(pub) + pub_obj = ecc.ECPubkey(pub) res = pub_obj * s if not return_serialized: return res return res.get_public_key_bytes() -def add_pubkeys(pubkeys: List[bytes]) -> ECPubkey: +def add_pubkeys(pubkeys: List[bytes]) -> ecc.ECPubkey: '''Input a list of binary compressed pubkeys and return their sum as a binary compressed pubkey.''' - pubkey_list = [ECPubkey(x) for x in pubkeys] + pubkey_list = [ecc.ECPubkey(x) for x in pubkeys] if pubkey_list: r = pubkey_list[0] for p in pubkey_list[1:]: diff --git a/electrum/plugins/joinmarket/jmclient/podle.py b/electrum/plugins/joinmarket/jmclient/podle.py index dbfbeb4c0..21c83daf4 100644 --- a/electrum/plugins/joinmarket/jmclient/podle.py +++ b/electrum/plugins/joinmarket/jmclient/podle.py @@ -7,7 +7,7 @@ import hashlib import struct from pprint import pformat -from electrum.ecc import ECPubkey, ECPrivkey +import electrum_ecc as ecc from ..jmbitcoin import (multiply, add_pubkeys, getG, podle_PublicKey, podle_PrivateKey, N, podle_PublicKey_class) @@ -110,7 +110,7 @@ class PoDLE(object): k = os.urandom(32) k_int = int.from_bytes(k, byteorder='big') J = getNUMS(self.i) - KG = ECPubkey(ECPrivkey(k).get_public_key_bytes()) + KG = ecc.ECPubkey(ecc.ECPrivkey(k).get_public_key_bytes()) KJ = multiply(k_int, J.get_public_key_bytes(), return_serialized=False) self.P2 = getP2(self.priv, J) self.get_commitment() diff --git a/electrum/plugins/joinmarket/tests/jmbitcoin/test_ecc_signing.py b/electrum/plugins/joinmarket/tests/jmbitcoin/test_ecc_signing.py index 584acda63..21a4c22c2 100644 --- a/electrum/plugins/joinmarket/tests/jmbitcoin/test_ecc_signing.py +++ b/electrum/plugins/joinmarket/tests/jmbitcoin/test_ecc_signing.py @@ -9,7 +9,8 @@ import json import pytest import os -from electrum import ecc +import electrum_ecc as ecc + from electrum.plugins.joinmarket import jmbitcoin as btc from electrum.plugins.joinmarket.jmbase import bintohex diff --git a/electrum/plugins/joinmarket/tests/test_jm_base_code.py b/electrum/plugins/joinmarket/tests/test_jm_base_code.py index 2d9d665da..1cc02f77e 100644 --- a/electrum/plugins/joinmarket/tests/test_jm_base_code.py +++ b/electrum/plugins/joinmarket/tests/test_jm_base_code.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- -from electrum.ecc import ECPrivkey, InvalidECPointException +import electrum_ecc as ecc + from electrum.transaction import Transaction from electrum.util import bfh, TxMinedInfo from electrum.plugins.joinmarket.jmbase import utxostr_to_utxo @@ -29,7 +30,7 @@ class JMBaseCodeMixinTestCase(JMTestCase): async def test_mk_freeze_script(self): jmw = self.jmw - privk = ECPrivkey.generate_random_key() + privk = ecc.ECPrivkey.generate_random_key() pubk_bytes = privk.get_public_key_bytes() locktime = 500000000 + 1723833376 # 16 Aug 2024, 18:36:16 UTC script = jmw.mk_freeze_script(pubk_bytes, locktime) @@ -44,7 +45,7 @@ class JMBaseCodeMixinTestCase(JMTestCase): script = jmw.mk_freeze_script(locktime, locktime) bad_pubk_bytes = b'\x02' + b'\x05'*32 - with self.assertRaises(InvalidECPointException): + with self.assertRaises(ecc.InvalidECPointException): script = jmw.mk_freeze_script(bad_pubk_bytes, locktime) async def test_redeem_script_to_p2wsh_script(self): diff --git a/electrum/plugins/joinmarket/tests/test_jm_util.py b/electrum/plugins/joinmarket/tests/test_jm_util.py index 9b85aef75..fcfbe98a2 100644 --- a/electrum/plugins/joinmarket/tests/test_jm_util.py +++ b/electrum/plugins/joinmarket/tests/test_jm_util.py @@ -2,9 +2,9 @@ import os +import electrum_ecc as ecc from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey -from electrum import ecc from electrum.bitcoin import sha256 from electrum.transaction import Transaction from electrum.util import to_bytes, bfh