electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 53310690a5c58145426047529eaa9af9db0b2741
parent b085d7cc59281bbba9f395cb6d622f799546358b
Author: SomberNight <somber.night@protonmail.com>
Date:   Sat, 26 Jan 2019 15:30:30 +0100

version notifications: sig check would always fail on testnet

Diffstat:
Melectrum/bitcoin.py | 45+++++++++++++++++++++++----------------------
Melectrum/ecc.py | 6++++--
Melectrum/gui/qt/util.py | 4+++-
3 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/electrum/bitcoin.py b/electrum/bitcoin.py @@ -235,28 +235,28 @@ def b58_address_to_hash160(addr: str) -> Tuple[int, bytes]: def hash160_to_p2pkh(h160: bytes, *, net=None) -> str: - if net is None: - net = constants.net + if net is None: net = constants.net return hash160_to_b58_address(h160, net.ADDRTYPE_P2PKH) def hash160_to_p2sh(h160: bytes, *, net=None) -> str: - if net is None: - net = constants.net + if net is None: net = constants.net return hash160_to_b58_address(h160, net.ADDRTYPE_P2SH) -def public_key_to_p2pkh(public_key: bytes) -> str: - return hash160_to_p2pkh(hash_160(public_key)) +def public_key_to_p2pkh(public_key: bytes, *, net=None) -> str: + if net is None: net = constants.net + return hash160_to_p2pkh(hash_160(public_key), net=net) def hash_to_segwit_addr(h: bytes, witver: int, *, net=None) -> str: - if net is None: - net = constants.net + if net is None: net = constants.net return segwit_addr.encode(net.SEGWIT_HRP, witver, h) -def public_key_to_p2wpkh(public_key: bytes) -> str: - return hash_to_segwit_addr(hash_160(public_key), witver=0) +def public_key_to_p2wpkh(public_key: bytes, *, net=None) -> str: + if net is None: net = constants.net + return hash_to_segwit_addr(hash_160(public_key), witver=0, net=net) -def script_to_p2wsh(script: str) -> str: - return hash_to_segwit_addr(sha256(bfh(script)), witver=0) +def script_to_p2wsh(script: str, *, net=None) -> str: + if net is None: net = constants.net + return hash_to_segwit_addr(sha256(bfh(script)), witver=0, net=net) def p2wpkh_nested_script(pubkey: str) -> str: pkh = bh2u(hash_160(bfh(pubkey))) @@ -266,25 +266,27 @@ def p2wsh_nested_script(witness_script: str) -> str: wsh = bh2u(sha256(bfh(witness_script))) return '00' + push_script(wsh) -def pubkey_to_address(txin_type: str, pubkey: str) -> str: +def pubkey_to_address(txin_type: str, pubkey: str, *, net=None) -> str: + if net is None: net = constants.net if txin_type == 'p2pkh': - return public_key_to_p2pkh(bfh(pubkey)) + return public_key_to_p2pkh(bfh(pubkey), net=net) elif txin_type == 'p2wpkh': - return public_key_to_p2wpkh(bfh(pubkey)) + return public_key_to_p2wpkh(bfh(pubkey), net=net) elif txin_type == 'p2wpkh-p2sh': scriptSig = p2wpkh_nested_script(pubkey) - return hash160_to_p2sh(hash_160(bfh(scriptSig))) + return hash160_to_p2sh(hash_160(bfh(scriptSig)), net=net) else: raise NotImplementedError(txin_type) -def redeem_script_to_address(txin_type: str, redeem_script: str) -> str: +def redeem_script_to_address(txin_type: str, redeem_script: str, *, net=None) -> str: + if net is None: net = constants.net if txin_type == 'p2sh': - return hash160_to_p2sh(hash_160(bfh(redeem_script))) + return hash160_to_p2sh(hash_160(bfh(redeem_script)), net=net) elif txin_type == 'p2wsh': - return script_to_p2wsh(redeem_script) + return script_to_p2wsh(redeem_script, net=net) elif txin_type == 'p2wsh-p2sh': scriptSig = p2wsh_nested_script(redeem_script) - return hash160_to_p2sh(hash_160(bfh(scriptSig))) + return hash160_to_p2sh(hash_160(bfh(scriptSig)), net=net) else: raise NotImplementedError(txin_type) @@ -296,8 +298,7 @@ def script_to_address(script: str, *, net=None) -> str: return addr def address_to_script(addr: str, *, net=None) -> str: - if net is None: - net = constants.net + if net is None: net = constants.net if not is_address(addr, net=net): raise BitcoinException(f"invalid bitcoin address: {addr}") witver, witprog = segwit_addr.decode(net.SEGWIT_HRP, addr) diff --git a/electrum/ecc.py b/electrum/ecc.py @@ -37,6 +37,7 @@ from .util import bfh, bh2u, assert_bytes, print_error, to_bytes, InvalidPasswor from .crypto import (sha256d, aes_encrypt_with_iv, aes_decrypt_with_iv, hmac_oneshot) from .ecc_fast import do_monkey_patching_of_python_ecdsa_internals_with_libsecp256k1 from . import msqr +from . import constants do_monkey_patching_of_python_ecdsa_internals_with_libsecp256k1() @@ -309,16 +310,17 @@ def msg_magic(message: bytes) -> bytes: return b"\x18Bitcoin Signed Message:\n" + length + message -def verify_message_with_address(address: str, sig65: bytes, message: bytes): +def verify_message_with_address(address: str, sig65: bytes, message: bytes, *, net=None): from .bitcoin import pubkey_to_address assert_bytes(sig65, message) + if net is None: net = constants.net try: h = sha256d(msg_magic(message)) public_key, compressed = ECPubkey.from_signature65(sig65, h) # check public key using the address pubkey_hex = public_key.get_public_key_hex(compressed) for txin_type in ['p2pkh','p2wpkh','p2wpkh-p2sh']: - addr = pubkey_to_address(txin_type, pubkey_hex) + addr = pubkey_to_address(txin_type, pubkey_hex, net=net) if address == addr: break else: diff --git a/electrum/gui/qt/util.py b/electrum/gui/qt/util.py @@ -16,6 +16,7 @@ from PyQt5.QtWidgets import * from electrum import version from electrum import ecc +from electrum import constants from electrum.i18n import _, languages from electrum.util import FileImportFailed, FileExportFailed, make_aiohttp_session, PrintError from electrum.paymentrequest import PR_UNPAID, PR_PAID, PR_EXPIRED @@ -925,7 +926,8 @@ class UpdateCheckThread(QThread, PrintError): continue sig = base64.b64decode(sig) msg = version_num.encode('utf-8') - if ecc.verify_message_with_address(address=address, sig65=sig, message=msg): + if ecc.verify_message_with_address(address=address, sig65=sig, message=msg, + net=constants.BitcoinMainnet): self.print_error(f"valid sig for version announcement '{version_num}' from address '{address}'") break else: