def hash_to_address(address_hash, testnet=False, script_hash=False, witness_version=0): """ Get address from public key/script hash. In case PUBKEY, P2PKH, P2PKH public key/script hash is SHA256+RIPEMD160, P2WSH script hash is SHA256. :param address_hash: public key hash or script hash in HEX or bytes string format. :param testnet: (optional) flag for testnet network, by default is False. :param script_hash: (optional) flag for script hash (P2SH address), by default is False. :param witness_version: (optional) witness program version, by default is 0, for legacy address format use None. :return: address in base58 or bech32 format. """ if isinstance(address_hash, str): address_hash = bytes_from_hex(address_hash) if not isinstance(address_hash, bytes): raise TypeError("address hash must be HEX encoded string or bytes") if not script_hash: if witness_version is None: if len(address_hash) != 20: raise ValueError("address hash length incorrect") if testnet: prefix = TESTNET_ADDRESS_BYTE_PREFIX else: prefix = MAINNET_ADDRESS_BYTE_PREFIX address_hash = b"%s%s" % (prefix, address_hash) address_hash += double_sha256(address_hash)[:4] return encode_base58(address_hash) else: if len(address_hash) not in (20, 32): raise ValueError("address hash length incorrect") if witness_version is None: if testnet: prefix = TESTNET_SCRIPT_ADDRESS_BYTE_PREFIX else: prefix = MAINNET_SCRIPT_ADDRESS_BYTE_PREFIX address_hash = b"%s%s" % (prefix, address_hash) address_hash += double_sha256(address_hash)[:4] return encode_base58(address_hash) if testnet: prefix = TESTNET_SEGWIT_ADDRESS_BYTE_PREFIX hrp = TESTNET_SEGWIT_ADDRESS_PREFIX else: prefix = MAINNET_SEGWIT_ADDRESS_BYTE_PREFIX hrp = MAINNET_SEGWIT_ADDRESS_PREFIX address_hash = b"%s%s" % (witness_version.to_bytes( 1, "big"), rebase_8_to_5(address_hash)) checksum = bech32_polymod(b"%s%s%s" % (prefix, address_hash, b"\x00" * 6)) checksum = rebase_8_to_5(checksum.to_bytes(5, "big"))[2:] return "%s1%s" % (hrp, rebase_5_to_32(address_hash + checksum).decode())
def create_master_xprivate_key(seed, testnet=False, base58=True, hex=False): """ Create extended private key from seed :param str,bytes key: seed HEX or bytes string. :param boolean base58: (optional) return result as base58 encoded string, by default True. :param boolean hex: (optional) return result as HEX encoded string, by default False. In case True base58 flag value will be ignored. :return: extended private key in base58, HEX or bytes string format. """ if isinstance(seed, str): seed = bytes.fromhex(seed) if not isinstance(seed, bytes): raise TypeError("seed should be bytes or hex encoded string") i = hmac_sha512(b"Bitcoin seed", seed) m, c = i[:32], i[32:] m_int = int.from_bytes(m, byteorder="big") if m_int <= 0 or m_int > ECDSA_SEC256K1_ORDER: return None prefix = TESTNET_XPRIVATE_KEY_PREFIX if testnet else MAINNET_XPRIVATE_KEY_PREFIX key = b''.join( [prefix, b'\x00\x00\x00\x00\x00\x00\x00\x00\x00', c, b'\x00', m]) if base58: key = b"".join([key, double_sha256(key)[:4]]) return encode_base58(key) else: return key if not hex else key.hex()
def decode_block_tx(block): s = get_stream(block) b = dict() b["amount"] = 0 b["size"] = int(len(block)/2) b["strippedSize"] = 80 b["version"] = unpack("<L", s.read(4))[0] b["versionHex"] = pack(">L", b["version"]).hex() b["previousBlockHash"] = rh2s(s.read(32)) b["merkleRoot"] = rh2s(s.read(32)) b["time"] = unpack("<L", s.read(4))[0] b["bits"] = s.read(4) b["target"] = bits_to_target(unpack("<L", b["bits"])[0]) b["targetDifficulty"] = target_to_difficulty(b["target"]) b["target"] = b["target"].to_bytes(32, byteorder="little") b["nonce"] = unpack("<L", s.read(4))[0] s.seek(-80, 1) b["header"] = s.read(80) b["bits"] = rh2s(b["bits"]) b["target"] = rh2s(b["target"]) b["hash"] = double_sha256(b["header"], hex=0) b["hash"] = rh2s(b["hash"]) b["rawTx"] = dict() b["tx"] = list() for i in range(var_int_to_int(read_var_int(s))): b["rawTx"][i] = Transaction(s, format="raw", keep_raw_tx=True) b["tx"].append(rh2s(b["rawTx"][i]["txId"])) b["amount"] += b["rawTx"][i]["amount"] b["strippedSize"] += b["rawTx"][i]["bSize"] b["strippedSize"] += var_int_len(len(b["tx"])) b["weight"] = b["strippedSize"] * 3 + b["size"] return b
def is_wif_valid(wif): """ Check is private key in WIF format string is valid. :param wif: private key in WIF format string. :return: boolean. """ if not isinstance(wif, str): raise TypeError("invalid wif key") if wif[0] not in PRIVATE_KEY_PREFIX_LIST: return False try: h = decode_base58(wif) except: return False checksum = h[-4:] if wif[0] in (MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX, TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX): if len(h) != 37: return False elif len(h) != 38: return False if double_sha256(h[:-4])[:4] != checksum: return False return True
def merkle_root(tx_hash_list, return_hex=True, receive_hex=True): """ Calculate merkle root from transaction hash list :param tx_hash_list: list of transaction hashes in bytes or HEX encoded string. :param return_hex: (optional) If set to True return result in HEX format, by default is True. :param receive_hex: (optional) If set to False no internal check or decode from hex to bytes, by default is True. :return: merkle root in bytes or HEX encoded string corresponding hex flag. """ if receive_hex: tx_hash_list = deque([h if isinstance(h, bytes) else s2rh(h) for h in tx_hash_list]) else: tx_hash_list = deque(tx_hash_list) if len(tx_hash_list) == 1: return rh2s(tx_hash_list[0]) if return_hex else tx_hash_list[0] while True: new_hash_list = deque() append = new_hash_list.append while tx_hash_list: h1 = tx_hash_list.popleft() try: h2 = tx_hash_list.popleft() except: h2 = h1 append(double_sha256(b"".join((h1, h2)))) if len(new_hash_list) > 1: tx_hash_list = new_hash_list else: return new_hash_list[0] if not return_hex else rh2s(new_hash_list[0])
def merkle_tree(tx_hash_list, return_hex=False, receive_hex=False): if receive_hex: tx_hash_deque = deque() tx_hash_deque_append = tx_hash_deque.append for h in tx_hash_list: tx_hash_deque_append(h if isinstance(h, bytes) else s2rh(h)) else: tx_hash_deque = deque(tx_hash_list) c = merkle_tree_depth(len(tx_hash_deque)) m = {c: deque(tx_hash_deque)} while len(tx_hash_deque) > 1: new_deque = deque() new_deque_append = new_deque.append while tx_hash_deque: h1 = tx_hash_deque.popleft() try: h2 = tx_hash_deque.popleft() except: h2 = h1 hs = double_sha256(b"".join((h1, h2))) new_deque_append(hs) tx_hash_deque = new_deque c -= 1 m[c] = deque(tx_hash_deque) if return_hex: for i in m: for k in range(len(m[i])): m[i][k] = rh2s(m[i][k]) return m
def merkle_branches(tx_hash_list, hex=True): """ Calculate merkle branches for coinbase transacton :param tx_hash_list: list of transaction hashes in bytes or HEX encoded string. :param hex: (optional) If set to True return result in HEX format, by default is True. :return: list of merkle branches in bytes or HEX encoded string corresponding hex flag. """ tx_hash_list = [h if isinstance(h, bytes) else s2rh(h) for h in tx_hash_list] branches = [] if len(tx_hash_list) == 1: return [] tx_hash_list.pop(0) while True: branches.append(tx_hash_list.pop(0)) new_hash_list = list() while tx_hash_list: h1 = tx_hash_list.pop(0) try: h2 = tx_hash_list.pop(0) except: h2 = h1 new_hash_list.append(double_sha256(h1 + h2)) if len(new_hash_list) > 1: tx_hash_list = new_hash_list else: if new_hash_list: branches.append(new_hash_list.pop(0)) return branches if not hex else [h.hex() for h in branches]
def calculate_commitment(self, witness_reserved_value): # print("calculate_commitment") wtxid_list = [b"\x00" * 32,] if self.transactions: for tx in self.transactions: wtxid_list.append(s2rh(tx["hash"])) # print("wtxid_list", wtxid_list) # print("wtxid_list", wtxid_list) # print("commitment ", double_sha256(merkle_root_double_sha256(wtxid_list, return_hex=0) + witness_reserved_value)) return double_sha256(merkle_root_double_sha256(wtxid_list, return_hex=0) + witness_reserved_value)
def __init__(self, raw_block=None, format="decoded", version=536870912, testnet=False, keep_raw_tx=False): if format not in ("decoded", "raw"): raise ValueError("tx_format error, raw or decoded allowed") self["format"] = format self["testnet"] = testnet self["header"] = None self["hash"] = None self["version"] = version self["versionHex"] = pack(">L", version).hex() self["previousBlockHash"] = None self["merkleRoot"] = None self["tx"] = dict() self["time"] = None self["bits"] = None self["nonce"] = None self["weight"] = 0 self["size"] = 80 self["strippedSize"] = 80 self["amount"] = 0 self["height"] = None self["difficulty"] = None self["targetDifficulty"] = None self["target"] = None if raw_block is None: return self["size"] = len(raw_block) if isinstance(raw_block, bytes) else int(len(raw_block)/2) s = self.get_stream(raw_block) self["format"] = "raw" self["version"] = unpack("<L", s.read(4))[0] self["versionHex"] = pack(">L", self["version"]).hex() self["previousBlockHash"] = s.read(32) self["merkleRoot"] = s.read(32) self["time"] = unpack("<L", s.read(4))[0] self["bits"] = s.read(4) self["target"] = bits_to_target(unpack("<L", self["bits"])[0]) self["targetDifficulty"] = target_to_difficulty(self["target"]) self["target"] = self["target"].to_bytes(32, byteorder="little") self["nonce"] = unpack("<L", s.read(4))[0] s.seek(-80, 1) self["header"] = s.read(80) self["hash"] = double_sha256(self["header"]) block_target = int.from_bytes(self["hash"], byteorder="little") self["difficulty"] = target_to_difficulty(block_target) tx_count = var_int_to_int(read_var_int(s)) self["tx"] = dict() for i in range(tx_count): self["tx"][i] = Transaction(s, format="raw", keep_raw_tx=keep_raw_tx) self["amount"] += self["tx"][i]["amount"] self["strippedSize"] += self["tx"][i]["bSize"] self["strippedSize"] += var_int_len(tx_count) self["weight"] = self["strippedSize"] * 3 + self["size"] if format == "decoded": self.decode(testnet=testnet)
def wif_to_private_key(h, hex=True): """ Decode WIF private key to bytes string or HEX encoded string :param hex: (optional) if set to True return key in HEX format, by default is True. :return: Private key HEX encoded string or raw bytes string. """ h = decode_base58(h) if double_sha256(h[:-4])[:4] != h[-4:]: raise TypeError("invalid wif key") return h[1:33].hex() if hex else h[1:33]
def merkle_root_from_branches(merkle_branches, coinbase_hash, hex=True): """ Calculate merkle root from merkle branches and coinbase transacton hash :param merkle_branches: list merkle branches in bytes or HEX encoded string. :param coinbase_hash: list coinbase transaction hash in bytes or HEX encoded string. :param hex: (optional) If set to True return result in HEX format, by default is True. :return: merkle root in bytes or HEX encoded string corresponding hex flag. """ merkle_root = coinbase_hash if not isinstance(coinbase_hash, str) else bytes_from_hex(coinbase_hash) for h in merkle_branches: if type(h) == str: h = bytes_from_hex(h) merkle_root = double_sha256(merkle_root + h) return bytes_from_hex(merkle_root) if not hex else merkle_root
def xprivate_to_xpublic_key(xprivate_key, base58=True, hex=False): """ Get extended public key from extended private key using ECDSA secp256k1 :param str,bytes key: extended private key in base58, HEX or bytes string. :param boolean base58: (optional) return result as base58 encoded string, by default True. :param boolean hex: (optional) return result as HEX encoded string, by default False. In case True base58 flag value will be ignored. :return: extended public key in base58, HEX or bytes string format. """ if isinstance(xprivate_key, str): try: if len(xprivate_key) == 156: xprivate_key = bytes.fromhex(xprivate_key) else: xprivate_key = decode_base58_with_checksum(xprivate_key) except: raise ValueError("invalid extended private key") if not isinstance(xprivate_key, bytes): raise TypeError( "extended private key should be base58 string or bytes") if xprivate_key[:4] == TESTNET_XPRIVATE_KEY_PREFIX: prefix = TESTNET_XPUBLIC_KEY_PREFIX elif xprivate_key[:4] == MAINNET_XPRIVATE_KEY_PREFIX: prefix = MAINNET_XPUBLIC_KEY_PREFIX else: raise ValueError("invalid extended private key") key = b"".join([ prefix, xprivate_key[4:45], private_to_public_key(xprivate_key[46:], hex=False) ]) if hex: return key.hex() elif base58: key = b"".join([key, double_sha256(key)[:4]]) return encode_base58(key) else: return key
def merkle_root_from_proof(merkle_proof, tx_id, index, return_hex=True, receive_hex=True): if isinstance(merkle_proof, str): merkle_proof = bytes_from_hex(merkle_proof) if isinstance(merkle_proof, bytes): merkle_proof = [merkle_proof[y - 32:y] for y in range(32, len(merkle_proof) + 32, 32)] if receive_hex: _merkle_proof = deque() _merkle_proof_append = _merkle_proof.append for h in merkle_proof: _merkle_proof_append(s2rh(h) if isinstance(h, str) else h) merkle_proof = _merkle_proof tx_id = s2rh(tx_id) if isinstance(tx_id, str) else tx_id root = tx_id for h in merkle_proof: root = double_sha256(b"".join((h, root) if index % 2 else (root, h))) index = index // 2 if return_hex: return rh2s(root) return root
def private_key_to_wif(h, compressed=True, testnet=False): """ Encode private key in HEX or RAW bytes format to WIF format. :param h: private key 32 byte string or HEX encoded string. :param compressed: (optional) flag of public key compressed format, by default set to True. :param testnet: (optional) flag for testnet network, by default is False. :return: Private key in WIF format. """ # uncompressed: 0x80 + [32-byte secret] + [4 bytes of Hash() of previous 33 bytes], base58 encoded. # compressed: 0x80 + [32-byte secret] + 0x01 + [4 bytes of Hash() previous 34 bytes], base58 encoded. if isinstance(h, str): h = bytes_from_hex(h) if len(h) != 32 and isinstance(h, bytes): raise TypeError("private key must be a 32 bytes or hex encoded string") if testnet: h = TESTNET_PRIVATE_KEY_BYTE_PREFIX + h else: h = MAINNET_PRIVATE_KEY_BYTE_PREFIX + h if compressed: h += b'\x01' h += double_sha256(h)[:4] return encode_base58(h)
def private_to_public_key(private_key, compressed=True, hex=True): """ Get public key from private key using ECDSA secp256k1 :param private_key: private key in WIF, HEX or bytes. :param compressed: (optional) flag of public key compressed format, by default set to True. In case private_key in WIF format, this flag is set in accordance with the key format specified in WIF string. :param hex: (optional) if set to True return key in HEX format, by default is True. :return: 33/65 bytes public key in HEX or bytes string. """ if not isinstance(private_key, bytes): if isinstance(private_key, bytearray): private_key = bytes(private_key) elif isinstance(private_key, str): try: if private_key[0] in (MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX, TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX): compressed = False h = decode_base58(private_key) if double_sha256(h[:-4])[:4] != h[-4:]: raise Exception() private_key = h[1:33] except: try: private_key = bytes_from_hex(private_key) except: raise TypeError("private key HEX or WIF invalid") else: raise TypeError( "private key must be a bytes or WIF or hex encoded string") if len(private_key) != 32: raise TypeError("private key length invalid") pub = __secp256k1_ec_pubkey_create__(private_key, bool(compressed)) if not pub: raise RuntimeError("secp256k1 error") return pub.hex() if hex else pub
def submit_job(self, extra_nonce_1, extra_nonce_2, nonce, time): version = s2rh(self.version) prev_hash = s2rh_step4(self.previous_block_hash) cb = self.coinb1 + extra_nonce_1 + extra_nonce_2 + self.coinb2 time = s2rh(time) bits = s2rh(self.bits) nonce = s2rh(nonce) c = Transaction(cb) cbh = s2rh(c["txId"]) merkle_root = merkle_root_from_branches(self.merkle_branches, cbh) # print("version ", version.hex()) # print("prev_hash ", self.previous_block_hash) # print("cbh ", cbh.hex()) # print("cbh2 ", s2rh(c["txId"])) # print("merkle_root ", merkle_root.hex()) # print("merkle_root ", s2rh(c["txId"])) # print("branches ", self.merkle_branches) header = version + prev_hash + merkle_root + time + bits + nonce block = hexlify(header).decode() block += hexlify(int_to_var_int(len (self.transactions) + 1)).decode() block += cb for t in self.transactions: block += t["data"] return double_sha256(header,1), block
def decode_base58_with_checksum(s): b = decode_base58(s) if double_sha256(b[:-4])[:4] != b[-4:]: raise Exception("invalid checksum") return b[:-4]
def is_address_valid(address, testnet=False): """ Check is address valid. :param address: address in base58 or bech32 format. :param testnet: (optional) flag for testnet network, by default is False. :return: boolean. """ if not address or type(address) != str: return False if address[0] in (MAINNET_ADDRESS_PREFIX, MAINNET_SCRIPT_ADDRESS_PREFIX, TESTNET_ADDRESS_PREFIX, TESTNET_ADDRESS_PREFIX_2, TESTNET_SCRIPT_ADDRESS_PREFIX): if testnet: if address[0] not in (TESTNET_ADDRESS_PREFIX, TESTNET_ADDRESS_PREFIX_2, TESTNET_SCRIPT_ADDRESS_PREFIX): return False else: if address[0] not in (MAINNET_ADDRESS_PREFIX, MAINNET_SCRIPT_ADDRESS_PREFIX): return False h = decode_base58(address) if len(h) != 25: return False checksum = h[-4:] if double_sha256(h[:-4])[:4] != checksum: return False return True elif address[:2].lower() in (TESTNET_SEGWIT_ADDRESS_PREFIX, MAINNET_SEGWIT_ADDRESS_PREFIX): if len(address) not in (42, 62): return False try: prefix, payload = address.split('1') except: return False upp = True if prefix[0].isupper() else False for i in payload[1:]: if upp: if not i.isupper() or i not in base32charset_upcase: return False else: if i.isupper() or i not in base32charset: return False payload = payload.lower() prefix = prefix.lower() if testnet: if prefix != TESTNET_SEGWIT_ADDRESS_PREFIX: return False stripped_prefix = TESTNET_SEGWIT_ADDRESS_BYTE_PREFIX else: if prefix != MAINNET_SEGWIT_ADDRESS_PREFIX: return False stripped_prefix = MAINNET_SEGWIT_ADDRESS_BYTE_PREFIX d = rebase_32_to_5(payload) address_hash = d[:-6] checksum = d[-6:] checksum2 = bech32_polymod( b"%s%s%s" % (stripped_prefix, address_hash, b"\x00" * 6)) checksum2 = rebase_8_to_5(checksum2.to_bytes(5, "big"))[2:] if checksum != checksum2: return False return True
def encode_base58_with_checksum(b): return encode_base58(b"%s%s" % (b, double_sha256(b)[:4]))