def hash(bs: bytearray, engine): if engine.op_code == ScriptOp.SHA1: return None elif engine.op_code == ScriptOp.SHA256: return Digest.sha256(bs) elif engine.op_code == ScriptOp.HASH160: return Digest.hash160(bs) elif engine.op_code == ScriptOp.HASH256: return Digest.hash256(bs) return None
def test_hash160(self): msg = b'Nobody inspects the spammish repetition' hash160_digest = b'\x1c\x9b{H\x04\x9a\x8f\x98i\x9b\xca"\xa5\x85l^\xf5q\xcdh' hex_hash160_digest = '1c9b7b48049a8f98699bca22a5856c5ef571cd68' self.assertEqual(hash160_digest, Digest.hash160(msg)) self.assertEqual(hex_hash160_digest, Digest.hash160(msg, is_hex=True))
def __from_byte_script(cls, byte_script: bytes, little_endian: bool = True): if not little_endian: return cls(Digest.hash160(msg=byte_script, is_hex=False)[::-1]) return cls(Digest.hash160(msg=byte_script, is_hex=False))
def to_script_hash(byte_script): return a2b_hex(Digest.hash160(msg=byte_script, is_hex=True))
def to_script_hash(byte_script) -> bytes: return Digest.hash160(msg=byte_script, is_hex=False)
def to_hash160(self, is_compressed=True): return Digest.hash160(self.to_bytes(is_compressed))