diff --git a/electrum/electrumabc/avalanche/primitives.py b/electrum/electrumabc/avalanche/primitives.py --- a/electrum/electrumabc/avalanche/primitives.py +++ b/electrum/electrumabc/avalanche/primitives.py @@ -115,5 +115,4 @@ return schnorr.sign(self.keydata, message_hash) def get_pubkey(self): - pubkey = public_key_from_private_key(self.keydata, self.compressed) - return PublicKey(bytes.fromhex(pubkey)) + return PublicKey(public_key_from_private_key(self.keydata, self.compressed)) diff --git a/electrum/electrumabc/bitcoin.py b/electrum/electrumabc/bitcoin.py --- a/electrum/electrumabc/bitcoin.py +++ b/electrum/electrumabc/bitcoin.py @@ -698,7 +698,7 @@ return ECKey(pk) -def GetPubKey(pubkey, compressed=False): +def GetPubKey(pubkey, compressed=False) -> bytes: return i2o_ECPublicKey(pubkey, compressed) @@ -708,10 +708,9 @@ return deserialize_privkey(sec, net=net)[2] -def public_key_from_private_key(pk, compressed): +def public_key_from_private_key(pk: bytes, compressed) -> bytes: pkey = regenerate_key(pk) - public_key = GetPubKey(pkey.pubkey, compressed) - return bh2u(public_key) + return GetPubKey(pkey.pubkey, compressed) def address_from_private_key(sec, *, net=None): @@ -719,7 +718,7 @@ net = networks.net txin_type, privkey, compressed = deserialize_privkey(sec, net=net) public_key = public_key_from_private_key(privkey, compressed) - return pubkey_to_address(txin_type, bytes.fromhex(public_key), net=net) + return pubkey_to_address(txin_type, public_key, net=net) def is_private_key(key, *, net=None): @@ -1731,7 +1730,7 @@ "Only p2pkh WIF keys may be encrypted using BIP38 at this time." ) public_key = public_key_from_private_key(key_bytes, compressed) - addr_str = pubkey_to_address(_type, bytes.fromhex(public_key), net=net) + addr_str = pubkey_to_address(_type, public_key, net=net) addr_hash = Hash(addr_str)[0:4] # ensure unicode bytes are normalized to NFC standard as specified by bip38 passphrase = cls._normalizeNFC(passphrase) diff --git a/electrum/electrumabc/commands.py b/electrum/electrumabc/commands.py --- a/electrum/electrumabc/commands.py +++ b/electrum/electrumabc/commands.py @@ -428,7 +428,7 @@ sec = txin.get("privkey") if sec: txin_type, privkey, compressed = bitcoin.deserialize_privkey(sec) - pubkey = bitcoin.public_key_from_private_key(privkey, compressed) + pubkey = bitcoin.public_key_from_private_key(privkey, compressed).hex() keypairs[pubkey] = privkey, compressed txin["type"] = txin_type.name txin["x_pubkeys"] = [pubkey] @@ -458,7 +458,7 @@ ) if privkey: txin_type, privkey2, compressed = bitcoin.deserialize_privkey(privkey) - pubkey = bitcoin.public_key_from_private_key(privkey2, compressed) + pubkey = bitcoin.public_key_from_private_key(privkey2, compressed).hex() tx.sign({pubkey: (privkey2, compressed)}) else: self.wallet.sign_transaction(tx, password) diff --git a/electrum/electrumabc/tests/test_bitcoin.py b/electrum/electrumabc/tests/test_bitcoin.py --- a/electrum/electrumabc/tests/test_bitcoin.py +++ b/electrum/electrumabc/tests/test_bitcoin.py @@ -439,7 +439,7 @@ for priv_details in self.priv_pub_addr: txin_type, privkey, compressed = deserialize_privkey(priv_details["priv"]) result = public_key_from_private_key(privkey, compressed) - self.assertEqual(priv_details["pub"], result) + self.assertEqual(priv_details["pub"], result.hex()) self.assertEqual(priv_details["txin_type"], txin_type.name) self.assertEqual(priv_details["compressed"], compressed) diff --git a/electrum/electrumabc/transaction.py b/electrum/electrumabc/transaction.py --- a/electrum/electrumabc/transaction.py +++ b/electrum/electrumabc/transaction.py @@ -70,7 +70,7 @@ serialize_sequence, ) from .uint256 import UInt256 -from .util import bfh, bh2u, profiler, to_bytes +from .util import bh2u, profiler, to_bytes DUST_THRESHOLD: int = 546 """ @@ -1537,8 +1537,7 @@ return sig @staticmethod - def _schnorr_sign(pubkey, sec, pre_hash): - pubkey = bytes.fromhex(pubkey) + def _schnorr_sign(pubkey: bytes, sec: bytes, pre_hash: bytes) -> bytes: sig = schnorr.sign(sec, pre_hash) assert schnorr.verify(pubkey, sig, pre_hash) # verify what we just signed return sig @@ -1582,7 +1581,7 @@ else: sig = self._ecdsa_sign(sec, pre_hash) reason = [] - if not self.verify_signature(bfh(pubkey), sig, pre_hash, reason=reason): + if not self.verify_signature(pubkey, sig, pre_hash, reason=reason): print_error( f"Signature verification failed for input#{i} sig#{j}, reason:" f" {str(reason)}" @@ -1590,7 +1589,7 @@ return None txin = self._inputs[i] txin.update_signature(sig + bytes((nHashType & 0xFF,)), j) - txin.update_pubkey(bytes.fromhex(pubkey), j) # needed for fd keys + txin.update_pubkey(pubkey, j) # needed for fd keys return txin def is_final(self): diff --git a/electrum/electrumabc/wallet.py b/electrum/electrumabc/wallet.py --- a/electrum/electrumabc/wallet.py +++ b/electrum/electrumabc/wallet.py @@ -176,7 +176,7 @@ inputs.append(TxInput.from_coin_dict(item)) def find_utxos_for_privkey(txin_type: bitcoin.ScriptType, privkey, compressed): - pubkey = bitcoin.public_key_from_private_key(privkey, compressed) + pubkey = bitcoin.public_key_from_private_key(privkey, compressed).hex() append_utxos_to_inputs(inputs, pubkey, txin_type) keypairs[pubkey] = privkey, compressed diff --git a/electrum/electrumabc_plugins/fusion/fusion.py b/electrum/electrumabc_plugins/fusion/fusion.py --- a/electrum/electrumabc_plugins/fusion/fusion.py +++ b/electrum/electrumabc_plugins/fusion/fusion.py @@ -382,9 +382,8 @@ for xpubkey in xpubkeys_set: derivation = wallet.keystore.get_pubkey_derivation(bytes.fromhex(xpubkey)) privkey = wallet.keystore.get_private_key(derivation, password) - pubkeyhex = public_key_from_private_key(*privkey) - pubkey = bytes.fromhex(pubkeyhex) - keypairs[pubkeyhex] = privkey + pubkey = public_key_from_private_key(*privkey) + keypairs[pubkey.hex()] = privkey pubkeys[xpubkey] = pubkey coindict = {