def test_CLT() -> None: network = "mainnet" fed_pubkeys = [b"\x00" * 33, b"\x11" * 33, b"\x22" * 33] rec_pubkeys = [b"\x77" * 33, b"\x88" * 33, b"\x99" * 33] # fmt: off redeem_script = script.encode([ "OP_IF", 2, *fed_pubkeys, 3, "OP_CHECKMULTISIG", # noqa E131 "OP_ELSE", 500, "OP_CHECKLOCKTIMEVERIFY", "OP_DROP", # noqa E131 2, *rec_pubkeys, 3, "OP_CHECKMULTISIG", # noqa E131 "OP_ENDIF", ]) # fmt: on payload = sha256(redeem_script) scriptPubKey = ( "00207b5310339c6001f75614daa5083839fa54d46165f6c56025cc54d397a85a5708") assert scriptPubKey == p2wsh(redeem_script).hex() assert scriptPubKey == scriptPubKey_from_payload("p2wsh", payload).hex() address = b"bc1q0df3qvuuvqqlw4s5m2jsswpelf2dgct97mzkqfwv2nfe02z62uyq7n4zjj" assert address == address_from_scriptPubKey(scriptPubKey, network) assert address == b32address_from_witness(0, payload, network)
def test_selfconsistency(self): # OP_RETURN data = "time-stamped data".encode().hex() # opcodes = ['OP_RETURN', data.hex()] opcodes = nulldata_scriptPubKey(data) scriptPubKey = encode(opcodes) opcodes2 = decode(scriptPubKey) self.assertEqual(opcodes, opcodes2) # p2pk pubkey = "04 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf f7d8a473e7e2e6d317b87bafe8bde97e3cf8f065dec022b51d11fcdd0d348ac4" # opcodes = [pubkey, 'OP_CHECKSIG'] opcodes = p2pk_scriptPubKey(pubkey) scriptPubKey = encode(opcodes) opcodes2 = decode(scriptPubKey) self.assertEqual(opcodes, opcodes2) # multi-sig pubkey2 = "04 61cbdcc5409fb4b4d42b51d33381354d80e550078cb532a34bfa2fcfdeb7d765 19aecc62770f5b0e4ef8551946d8a540911abe3e7854a26f39f58b25c15342af" # opcodes = [1, pubkey, pubKey2, 2, 'OP_CHECKMULTISIG'] opcodes = p2ms_scriptPubKey(1, (pubkey, pubkey2)) scriptPubKey = encode(opcodes) opcodes2 = decode(scriptPubKey) self.assertEqual(opcodes, opcodes2) # p2pkh pubkey_hash = hash160(pubkey).hex() # opcodes = ['OP_DUP', 'OP_HASH160', pubkey_hash.hex(), 'OP_EQUALVERIFY', 'OP_CHECKSIG'] opcodes = p2pkh_scriptPubKey(pubkey_hash) scriptPubKey = encode(opcodes) opcodes2 = decode(scriptPubKey) self.assertEqual(opcodes, opcodes2) # p2sh (p2pkh-p2sh) redeem_script_hash = hash160(scriptPubKey).hex() # opcodes = ['OP_HASH160', redeem_script_hash.hex(), 'OP_EQUAL'] opcodes = p2sh_scriptPubKey(redeem_script_hash) scriptPubKey = encode(opcodes) opcodes2 = decode(scriptPubKey) self.assertEqual(opcodes, opcodes2) # p2wpkh # opcodes = [0, pubkey_hash.hex()] opcodes = p2wpkh_scriptPubKey(pubkey_hash) scriptPubKey = encode(opcodes) self.assertEqual(scriptPubKey.hex(), "0014"+pubkey_hash) opcodes2 = decode(scriptPubKey) self.assertEqual(opcodes, opcodes2) # p2wsh witness_script = [pubkey, 'OP_CHECKSIG'] witness_script_bytes = encode(witness_script) witness_script_hash = sha256(witness_script_bytes) # opcodes = [0, witness_script_hash.hex()] opcodes = p2wsh_scriptPubKey(witness_script_hash.hex()) scriptPubKey = encode(opcodes) self.assertEqual(scriptPubKey.hex(), "0020"+witness_script_hash.hex()) opcodes2 = decode(scriptPubKey) self.assertEqual(opcodes, opcodes2)
def assert_signable(self) -> None: self.assert_valid() for i, tx_in in enumerate(self.tx.vin): non_witness_utxo = self.inputs[i].non_witness_utxo witness_utxo = self.inputs[i].witness_utxo redeem_script = self.inputs[i].redeem_script if witness_utxo: script_pub_key = witness_utxo.script_pub_key script_type, payload = type_and_payload(script_pub_key.script) if script_type == "p2sh": script_type, _ = type_and_payload(redeem_script) if script_type not in ("p2wpkh", "p2wsh"): raise BTClibValueError( "script type not it ('p2wpkh', 'p2wsh')") elif non_witness_utxo: script_pub_key = non_witness_utxo.vout[ tx_in.prev_out.vout].script_pub_key _, payload = type_and_payload(script_pub_key.script) else: err_msg = "missing script_pub_key" raise BTClibValueError(err_msg) if redeem_script and payload != hash160(redeem_script): raise BTClibValueError("invalid redeem script hash160") if self.inputs[i].witness_script: if redeem_script: _, payload = type_and_payload(redeem_script) if payload != sha256(self.inputs[i].witness_script): raise BTClibValueError("invalid witness script sha256")
def test_p2wsh() -> None: # self-consistency pub_key = "02 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" redeem_script = ScriptPubKey.p2pkh(pub_key).script payload = sha256(redeem_script) script_pub_key = serialize(["OP_0", payload]) assert_p2wsh(script_pub_key) assert script_pub_key == ScriptPubKey.p2wsh(redeem_script).script assert ("p2wsh", payload) == type_and_payload(script_pub_key) # bech32 address network = "mainnet" addr = b32.p2wsh(redeem_script, network) assert addr == address(script_pub_key, network) assert addr == b32.address_from_witness(0, payload, network) # back from the address to the script_pub_key assert script_pub_key == ScriptPubKey.from_address(addr).script assert network == ScriptPubKey.from_address(addr).network # p2sh-wrapped base58 address addr = b58.p2wsh_p2sh(redeem_script, network) assert addr == b58.address_from_v0_witness(payload, network) err_msg = "invalid witness version: " with pytest.raises(BTClibValueError, match=err_msg): assert_p2wsh(b"\x33" + script_pub_key[1:]) err_msg = "invalid redeem script hash length marker: " with pytest.raises(BTClibValueError, match=err_msg): assert_p2wsh(script_pub_key[:1] + b"\x00" + script_pub_key[2:])
def test_p2wsh() -> None: # self-consistency pubkey = "02 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" pubkey_hash = hash160(pubkey) redeem_script = scriptPubKey_from_payload("p2pkh", pubkey_hash) payload = sha256(redeem_script) scriptPubKey = script.encode([0, payload]) assert scriptPubKey == p2wsh(script.decode(redeem_script)) # to the scriptPubKey in two steps (through payload) script_type = "p2wsh" assert scriptPubKey == scriptPubKey_from_payload(script_type, payload) # back from the scriptPubKey to the payload assert (script_type, payload, 0) == payload_from_scriptPubKey(scriptPubKey) # bech32 address network = "mainnet" address = bech32address.p2wsh(redeem_script, network) assert address == address_from_scriptPubKey(scriptPubKey, network) wit_ver = 0 assert address == b32address_from_witness(wit_ver, payload, network) # back from the address to the scriptPubKey assert (scriptPubKey, network) == scriptPubKey_from_address(address) # p2sh-wrapped base58 address address = base58address.p2wsh_p2sh(redeem_script, network) assert address == b58address_from_witness(payload, network)
def test_non_standard_script_in_p2wsh() -> None: network = "mainnet" fed_pub_keys: List[Command] = ["00" * 33, "11" * 33, "22" * 33] rec_pub_keys: List[Command] = ["77" * 33, "88" * 33, "99" * 33] # fmt: off redeem_script_cmds: List[Command] = [ "OP_IF", "OP_2", *fed_pub_keys, "OP_3", "OP_CHECKMULTISIG", # noqa E131 "OP_ELSE", 500, "OP_CHECKLOCKTIMEVERIFY", "OP_DROP", # noqa E131 "OP_2", *rec_pub_keys, "OP_3", "OP_CHECKMULTISIG", # noqa E131 "OP_ENDIF", ] # fmt: on redeem_script = serialize(redeem_script_cmds) assert redeem_script_cmds == parse(redeem_script) payload = sha256(redeem_script) script_pub_key = ( "00207b5310339c6001f75614daa5083839fa54d46165f6c56025cc54d397a85a5708") assert script_pub_key == ScriptPubKey.p2wsh(redeem_script).script.hex() addr = "bc1q0df3qvuuvqqlw4s5m2jsswpelf2dgct97mzkqfwv2nfe02z62uyq7n4zjj" assert addr == address(script_pub_key, network) assert addr == b32.address_from_witness(0, payload, network)
def test_witness(self): pub = "03 a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" b58addr = p2wpkh_p2sh(pub) _, h160, network, is_script_hash = h160_from_b58address(b58addr) self.assertEqual(network, 'mainnet') self.assertEqual(is_script_hash, True) #?!?!? self.assertEqual(len(h160), 20) b58addr = _b58segwitaddress(hash160(to_pubkey_bytes(pub, True, ec)), network) _, h160_2, network, is_script_hash = h160_from_b58address(b58addr) self.assertEqual(network, 'mainnet') self.assertEqual(is_script_hash, True) #?!?!? self.assertEqual(len(h160), 20) self.assertEqual(h160.hex(), h160_2.hex()) wscript = "a8a58c2d034b28bf90c8803f5a53f769a4" b58addr = p2wsh_p2sh(wscript) _, h160, network, is_script_hash = h160_from_b58address(b58addr) self.assertEqual(network, 'mainnet') self.assertEqual(is_script_hash, True) #?!?!? self.assertEqual(len(h160), 20) b58addr = _b58segwitaddress(sha256(wscript), network) _, h160_2, network, is_script_hash = h160_from_b58address(b58addr) self.assertEqual(network, 'mainnet') self.assertEqual(is_script_hash, True) #?!?!? self.assertEqual(len(h160), 20) self.assertEqual(h160.hex(), h160_2.hex()) # Invalid witness program length (19) self.assertRaises(ValueError, _b58segwitaddress, h160[:-1], network)
def test_CLT(self): network = 'mainnet' vault_pubkeys = [b'\x00' * 33, b'\x11' * 33, b'\x22' * 33] recovery_pubkeys = [b'\x77' * 33, b'\x88' * 33, b'\x99' * 33] redeem_script = encode([ 'OP_IF', 2, *vault_pubkeys, 3, 'OP_CHECKMULTISIG', 'OP_ELSE', 500, 'OP_CHECKLOCKTIMEVERIFY', 'OP_DROP', 2, *recovery_pubkeys, 3, 'OP_CHECKMULTISIG', 'OP_ENDIF' ]) payload = sha256(redeem_script) script = "00207b5310339c6001f75614daa5083839fa54d46165f6c56025cc54d397a85a5708" scriptPubKey = p2wsh(redeem_script) self.assertEqual(scriptPubKey.hex(), script) scriptPubKey = scriptPubKey_from_payload('p2wsh', payload) self.assertEqual(scriptPubKey.hex(), script) address = b"bc1q0df3qvuuvqqlw4s5m2jsswpelf2dgct97mzkqfwv2nfe02z62uyq7n4zjj" address2 = address_from_scriptPubKey(scriptPubKey, network) self.assertEqual(address, address2) address2 = bech32address.p2wsh(redeem_script, network) self.assertEqual(address, address2) address2 = b32address_from_witness(0, payload, network) self.assertEqual(address, address2)
def test_address_scriptPubKey(self): pubkey = "03 a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" pubkey_hash = hash160(pubkey).hex() opcodes = [0, pubkey_hash] address_from_scriptPubKey(opcodes) opcodes2, _ = scriptPubKey_from_address(address_from_scriptPubKey(opcodes)) self.assertEqual(opcodes, opcodes2) opcodes = ['OP_DUP', 'OP_HASH160', pubkey_hash, 'OP_EQUALVERIFY', 'OP_CHECKSIG'] opcodes2, _ = scriptPubKey_from_address(address_from_scriptPubKey(opcodes)) self.assertEqual(opcodes, opcodes2) script_hash = hash160(encode(opcodes)).hex() opcodes = ['OP_HASH160',script_hash, 'OP_EQUAL'] opcodes2, _ = scriptPubKey_from_address(address_from_scriptPubKey(opcodes)) self.assertEqual(opcodes, opcodes2) script_hash = sha256(encode(opcodes)).hex() opcodes = [0, script_hash] opcodes2, _ = scriptPubKey_from_address(address_from_scriptPubKey(opcodes)) self.assertEqual(opcodes, opcodes2) # Unknown script opcodes = [16, pubkey_hash] self.assertRaises(ValueError, address_from_scriptPubKey, opcodes) #address_from_scriptPubKey(opcodes) # Unhandled witness version (16) wp = hash160(pubkey)[2:] addr = b32address_from_witness(16, wp) self.assertRaises(ValueError, scriptPubKey_from_address, addr)
def p2wsh( cls: Type[_ScriptPubKey], redeem_script: Octets, network: str = "mainnet", check_validity: bool = True, ) -> _ScriptPubKey: "Return the p2wsh ScriptPubKey of the provided redeem script." script_h256 = sha256(redeem_script) script = serialize(["OP_0", script_h256]) return cls(script, network, check_validity)
def test_p2wsh() -> None: # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki pub = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" script_pub_key: List[Command] = [pub, "OP_CHECKSIG"] witness_script_bytes = serialize(script_pub_key) addr = "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7" assert addr == b32.p2wsh(witness_script_bytes, "testnet") _, wit_prg, _ = b32.witness_from_address(addr) assert wit_prg == sha256(witness_script_bytes) addr = "bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3" assert addr == b32.p2wsh(witness_script_bytes) _, wit_prg, _ = b32.witness_from_address(addr) assert wit_prg == sha256(witness_script_bytes) err_msg = "invalid size: " with pytest.raises(BTClibValueError, match=err_msg): b32.address_from_witness(0, witness_script_bytes)
def test_p2wsh_address(self): # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki pub = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" witness_script = [pub, 'OP_CHECKSIG'] witness_script_bytes = encode(witness_script) addr = b'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7' self.assertEqual(addr, p2wsh_address(witness_script_bytes, 'testnet')) _, wp, _, _ = witness_from_b32address(addr) self.assertEqual(bytes(wp), sha256(witness_script_bytes)) addr = b'bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3' self.assertEqual(addr, p2wsh_address(witness_script_bytes)) _, wp, _, _ = witness_from_b32address(addr) self.assertEqual(bytes(wp), sha256(witness_script_bytes)) self.assertEqual(witness_from_b32address(addr)[1], sha256(witness_script_bytes)) # witness program length (35) is not 32 self.assertRaises(ValueError, b32address_from_witness, 0, witness_script_bytes[1:])
def test_p2wsh() -> None: # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki pub = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" script_pubkey: List[ScriptToken] = [pub, "OP_CHECKSIG"] witness_script_bytes = script.serialize(script_pubkey) addr = b"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7" assert addr == p2wsh(witness_script_bytes, "testnet") _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) addr = b"bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3" assert addr == p2wsh(witness_script_bytes) _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) assert witness_from_b32address(addr)[1] == sha256(witness_script_bytes) err_msg = "invalid witness program length for witness v0: " with pytest.raises(BTClibValueError, match=err_msg): b32address_from_witness(0, witness_script_bytes)
def test_p2wsh(): # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki pub = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D" "959F2815B16F81798" witness_script = [pub, "OP_CHECKSIG"] witness_script_bytes = encode(witness_script) addr = b"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7" assert addr == p2wsh(witness_script_bytes, "testnet") _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) addr = b"bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3" assert addr == p2wsh(witness_script_bytes) _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) assert witness_from_b32address(addr)[1] == sha256(witness_script_bytes) errMsg = r"witness program length \(35\) is not 20 or 32" with pytest.raises(ValueError, match=errMsg): b32address_from_witness(0, witness_script_bytes)
def test_p2wsh(self): script_type = "p2wsh" # self-consistency pubkey = "02" "cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" pubkey_hash = hash160(pubkey) redeem_script = scriptPubKey_from_payload("p2pkh", pubkey_hash) payload = sha256(redeem_script) script = encode([0, payload]) # straight to the scriptPubKey scriptPubKey = p2wsh(decode(redeem_script)) self.assertEqual(scriptPubKey.hex(), script.hex()) # to the scriptPubKey in two steps (through payload) scriptPubKey = scriptPubKey_from_payload(script_type, payload) self.assertEqual(scriptPubKey.hex(), script.hex()) # back from the scriptPubKey to the payload script_type2, payload2, m2 = payload_from_scriptPubKey(scriptPubKey) self.assertEqual(script_type, script_type2) self.assertEqual(0, m2) self.assertEqual(payload.hex(), payload2.hex()) script_type2, payload2, m2 = payload_from_scriptPubKey(script) self.assertEqual(script_type, script_type2) self.assertEqual(0, m2) self.assertEqual(payload.hex(), payload2.hex()) # data -> payload is not invertible (hash functions) # bech32 address network = "mainnet" address = bech32address.p2wsh(redeem_script, network) address2 = address_from_scriptPubKey(scriptPubKey, network) self.assertEqual(address, address2) address2 = b32address_from_witness(0, payload, network) self.assertEqual(address, address2) scriptPubKey2, network2 = scriptPubKey_from_address(address) self.assertEqual(scriptPubKey2, scriptPubKey) self.assertEqual(network2, network) # p2sh-wrapped base58 address address = base58address.p2wsh_p2sh(redeem_script, network) address2 = b58address_from_witness(payload, network) self.assertEqual(address, address2)
def test_p2w_p2sh() -> None: pub_key = "03 a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" witness_program, network = hash160_from_key(pub_key) b58addr = b58.p2wpkh_p2sh(pub_key, network) assert b58addr == b58.address_from_v0_witness(witness_program, network) script_pub_key = serialize([ "OP_DUP", "OP_HASH160", witness_program, "OP_EQUALVERIFY", "OP_CHECKSIG" ]) witness_program = sha256(script_pub_key) b58addr = b58.p2wsh_p2sh(script_pub_key, network) assert b58addr == b58.address_from_v0_witness(witness_program, network) err_msg = "invalid size: " with pytest.raises(BTClibValueError, match=err_msg): b58.address_from_v0_witness(witness_program[:-1], network)
def test_CLT(self): vault_pubkeys = [b'\x00'*33, b'\x11'*33, b'\x22'*33] recovery_pubkeys = [b'\x77'*33, b'\x88'*33, b'\x99'*33] opcodes = [ 'OP_IF', 2, *vault_pubkeys, 3, 'OP_CHECKMULTISIG', 'OP_ELSE', 500, 'OP_CHECKLOCKTIMEVERIFY', 'OP_DROP', 2, *recovery_pubkeys, 3, 'OP_CHECKMULTISIG', 'OP_ENDIF' ] witness_program = encode(opcodes) witness_hash = sha256(witness_program) script_pubkey = p2wsh_scriptPubKey(witness_hash) self.assertEqual(encode(script_pubkey).hex(), "00207b5310339c6001f75614daa5083839fa54d46165f6c56025cc54d397a85a5708") address = b32address_from_witness(0, witness_hash) self.assertEqual(address, b"bc1q0df3qvuuvqqlw4s5m2jsswpelf2dgct97mzkqfwv2nfe02z62uyq7n4zjj")
def test_CLT(): network = "mainnet" vault_pubkeys = [b"\x00" * 33, b"\x11" * 33, b"\x22" * 33] recovery_pubkeys = [b"\x77" * 33, b"\x88" * 33, b"\x99" * 33] redeem_script = encode( [ "OP_IF", 2, *vault_pubkeys, 3, "OP_CHECKMULTISIG", "OP_ELSE", 500, "OP_CHECKLOCKTIMEVERIFY", "OP_DROP", 2, *recovery_pubkeys, 3, "OP_CHECKMULTISIG", "OP_ENDIF", ] ) payload = sha256(redeem_script) script = "00207b5310339c6001f75614daa5083839fa54d46165f6c56025cc54d397a85a5708" scriptPubKey = p2wsh(redeem_script) assert scriptPubKey.hex() == script scriptPubKey = scriptPubKey_from_payload("p2wsh", payload) assert scriptPubKey.hex() == script address = ( "bc1q0df3qvuuvqqlw4s5m2jsswpelf2dgct97mzkqfwv2nfe02z62uyq7n4zjj" ).encode() address2 = address_from_scriptPubKey(scriptPubKey, network) assert address == address2 assert address == address2 address2 = b32address_from_witness(0, payload, network) assert address == address2
def p2wsh_p2sh(redeem_script: Octets, network: str = "mainnet") -> str: "Return the p2wsh-p2sh base58 address corresponding to a reedem script." witness_program = sha256(redeem_script) return address_from_v0_witness(witness_program, network)
def p2wsh(script_pub_key: Octets, network: str = "mainnet") -> str: "Return the p2wsh bech32 address corresponding to a script_pub_key." h256 = sha256(script_pub_key) return address_from_witness(0, h256, network)
def test_standards(self): # OP_RETURN data = "time-stamped data".encode().hex() # script = ['OP_RETURN', data.hex()] script = nulldata_scriptPubKey(data) script_bytes = encode(script) script2 = decode(script_bytes) self.assertEqual(script, script2) # p2pk pubkey = "03a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" #script = [pubkey, 'OP_CHECKSIG'] script = p2pk_scriptPubKey(pubkey) script_bytes = encode(script) script2 = decode(script_bytes) self.assertEqual(script, script2) # multi-sig pubKey2 = "02530c548d402670b13ad8887ff99c294e67fc18097d236d57880c69261b42def7" # script = [1, pubkey, pubKey2, 2, 'OP_CHECKMULTISIGVERIFY'] script = multisig_scriptPubKey(1, (pubkey, pubKey2)) script_bytes = encode(script) script2 = decode(script_bytes) self.assertEqual(script, script2) # p2pkh pubkey_hash = hash160(pubkey).hex() # script = ['OP_DUP', 'OP_HASH160', pubkey_hash.hex(), 'OP_EQUALVERIFY', 'OP_CHECKSIG'] script = p2pkh_scriptPubKey(pubkey_hash) script_bytes = encode(script) script2 = decode(script_bytes) self.assertEqual(script, script2) # p2sh (p2pkh-p2sh) redeem_script_hash = hash160(script_bytes).hex() # script = ['OP_HASH160', redeem_script_hash.hex(), 'OP_EQUAL'] script = p2sh_scriptPubKey(redeem_script_hash) script_bytes = encode(script) script2 = decode(script_bytes) self.assertEqual(script, script2) # p2wpkh # script = [0, pubkey_hash.hex()] script = p2wpkh_scriptPubKey(pubkey_hash) script_bytes = encode(script) self.assertEqual(script_bytes.hex(), "0014" + pubkey_hash) script2 = decode(script_bytes) self.assertEqual(script, script2) # p2wsh witness_script = [pubkey, 'OP_CHECKSIG'] witness_script_bytes = encode(witness_script) witness_script_hash = sha256(witness_script_bytes) # script = [0, witness_script_hash.hex()] script = p2wsh_scriptPubKey(witness_script_hash.hex()) script_bytes = encode(script) self.assertEqual(script_bytes.hex(), "0020" + witness_script_hash.hex()) script2 = decode(script_bytes) self.assertEqual(script, script2)