def get_root_of_merkle_tree(branches, branch_positions, working_branch): for i, branch in enumerate(branches): other_branch = unhexlify(branch)[::-1] other_branch_on_left = bool((branch_positions >> i) & 1) if other_branch_on_left: combined = other_branch + working_branch else: combined = working_branch + other_branch working_branch = double_sha256(combined) return hexlify(working_branch[::-1])
def hash_header(header: bytes) -> bytes: if header is None: return b'0' * 64 return hexlify(double_sha256(header)[::-1])
def sign(self, data): """ Produce a signature for piece of data by double hashing it and signing the hash. """ key = self.signing_key digest = double_sha256(data) return key.sign_digest_deterministic(digest, hashlib.sha256, ecdsa.util.sigencode_der)
def verify_proof(proof, rootHash, name): previous_computed_hash = None reverse_computed_name = '' verified_value = False for i, node in enumerate(proof['nodes'][::-1]): found_child_in_chain = False to_hash = b'' previous_child_character = None for child in node['children']: if child['character'] < 0 or child['character'] > 255: raise InvalidProofError( "child character not int between 0 and 255") if previous_child_character: if previous_child_character >= child['character']: raise InvalidProofError("children not in increasing order") previous_child_character = child['character'] to_hash += six.int2byte(child['character']) if 'nodeHash' in child: if len(child['nodeHash']) != 64: raise InvalidProofError("invalid child nodeHash") to_hash += binascii.unhexlify(child['nodeHash'])[::-1] else: if previous_computed_hash is None: raise InvalidProofError("previous computed hash is None") if found_child_in_chain is True: raise InvalidProofError( "already found the next child in the chain") found_child_in_chain = True reverse_computed_name += chr(child['character']) to_hash += previous_computed_hash if not found_child_in_chain: if i != 0: raise InvalidProofError("did not find the alleged child") if i == 0 and 'txhash' in proof and 'nOut' in proof and 'last takeover height' in proof: if len(proof['txhash']) != 64: raise InvalidProofError("txhash was invalid: {}".format( proof['txhash'])) if not isinstance(proof['nOut'], int): raise InvalidProofError("nOut was invalid: {}".format( proof['nOut'])) if not isinstance(proof['last takeover height'], int): raise InvalidProofError( 'last takeover height was invalid: {}'.format( proof['last takeover height'])) to_hash += get_hash_for_outpoint( binascii.unhexlify(proof['txhash'])[::-1], proof['nOut'], proof['last takeover height']) verified_value = True elif 'valueHash' in node: if len(node['valueHash']) != 64: raise InvalidProofError("valueHash was invalid") to_hash += binascii.unhexlify(node['valueHash'])[::-1] previous_computed_hash = double_sha256(to_hash) if previous_computed_hash != binascii.unhexlify(rootHash)[::-1]: raise InvalidProofError("computed hash does not match roothash") if 'txhash' in proof and 'nOut' in proof: if not verified_value: raise InvalidProofError("mismatch between proof claim and outcome") if 'txhash' in proof and 'nOut' in proof: if name != reverse_computed_name[::-1]: raise InvalidProofError("name did not match proof") if not name.startswith(reverse_computed_name[::-1]): raise InvalidProofError("name fragment does not match proof") return True
def get_hash_for_outpoint(txhash, nout, height_of_last_takeover): return double_sha256( double_sha256(txhash) + double_sha256(str(nout).encode()) + double_sha256(struct.pack('>Q', height_of_last_takeover)))
def encrypt(self, password): assert not self.encrypted, "Key is already encrypted." secret = double_sha256(password) self.seed = aes_encrypt(secret, self.seed) self.private_key = aes_encrypt(secret, self.private_key.extended_key_string()) self.encrypted = True
def decrypt(self, password): assert self.encrypted, "Key is not encrypted." secret = double_sha256(password) self.seed = aes_decrypt(secret, self.seed) self.private_key = from_extended_key_string(self.ledger, aes_decrypt(secret, self.private_key)) self.encrypted = False
def hash160_to_address(cls, h160): raw_address = cls.pubkey_address_prefix + h160 return Base58.encode( bytearray(raw_address + double_sha256(raw_address)[0:4]))
def header_hash_to_pow_hash(header_hash: bytes): header_hash_bytes = unhexlify(header_hash)[::-1] h = sha512(header_hash_bytes) pow_hash = double_sha256( ripemd160(h[:len(h) // 2]) + ripemd160(h[len(h) // 2:])) return hexlify(pow_hash[::-1])
def _hash_header(self, header): if header is None: return b'0' * 64 return hash_encode(double_sha256(unhexlify(self._serialize(header))))