def test_native_p2wsh(): transaction = tx.Tx.deserialize( "0100000002fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b9216b36e0000000000ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac6c79da47c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a30741f8145e5acadf23f751864167f32e0963f788ac00000000" ) transaction.vin[1].txinwitness = [ "21026dccc749adc2a9d0d89497ac511f760f45c47dc5ed9cf352a58ac706453880aeadab210255a9626aebf5e29c0e6538428ba0d1dcf6ca98ffdf086aa8ced5e0d0215ea465ac" ] previous_txout = tx.TxOut( nValue=4900000000, scriptPubKey=script.decode( "00205d1b56b63d714eebe542309525f484b7e9d6f686b3781b6f61ef925d66d6f6a0" ), ) sighash = get_sighash(transaction, previous_txout, 1, 0x03) assert ( sighash.hex() == "82dde6e4f1e94d02c2b7ad03d2115d691f48d064e9d52f58194a6637e4194391" ) script_code = _get_witness_v0_scriptCodes( script.decode(transaction.vin[1].txinwitness[-1]) )[1] sighash = segwit_v0_sighash( script_code, transaction, 1, 0x03, previous_txout.nValue ) assert ( sighash.hex() == "fef7bd749cce710c5c052bd796df1af0d935e59cea63736268bcbe2d2134fc47" )
def test_nulldata() -> None: scripts: List[List[Token]] = [["OP_RETURN", "11" * 79], ["OP_RETURN", "00" * 79]] for scriptPubKey in scripts: assert scriptPubKey == script.decode(script.encode(scriptPubKey)) assert scriptPubKey == script.decode(script.encode(scriptPubKey).hex()) assert scriptPubKey == script.deserialize( script.serialize(scriptPubKey)) assert scriptPubKey == script.deserialize( script.serialize(scriptPubKey).hex())
def test_simple(): script_list = [ [2, 3, "OP_ADD", 5, "OP_EQUAL"], ["1ADD", "OP_1ADD", "1ADE", "OP_EQUAL"], [hex(26)[2:].upper(), -1, "OP_ADD", hex(26)[2:].upper(), "OP_EQUAL"], [ hex(0xFFFFFFFF)[2:].upper(), -1, "OP_ADD", hex(0xFFFFFFFF)[2:].upper(), "OP_EQUAL", ], ["1F" * 250, "OP_DROP"], ["1F" * 520, "OP_DROP"], ] for script in script_list: script_bytes = encode(script) script2 = decode(script_bytes) assert script == script2 script_bytes2 = encode(script2) assert script_bytes == script_bytes2 script_serialized = serialize(script) script3 = deserialize(script_serialized) assert script == script3 script4 = deserialize(script_serialized.hex()) assert script == script4
def test_p2sh(self): script_type = "p2sh" # self-consistency pubkey = "02" "cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" pubkey_hash = hash160(pubkey) redeem_script = scriptPubKey_from_payload("p2pkh", pubkey_hash) payload = hash160(redeem_script) script = encode(["OP_HASH160", payload, "OP_EQUAL"]) # straight to the scriptPubKey scriptPubKey = p2sh(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) # address network = "mainnet" address = base58address.p2sh(decode(redeem_script), network) address2 = address_from_scriptPubKey(scriptPubKey, network) self.assertEqual(address, address2) prefix = NETWORKS[network]["p2sh"] address2 = b58address_from_h160(prefix, payload, network) self.assertEqual(address, address2) scriptPubKey2, network2 = scriptPubKey_from_address(address) self.assertEqual(scriptPubKey2, scriptPubKey) self.assertEqual(network2, network) # documented test case: https://learnmeabitcoin.com/guide/p2sh payload = "748284390f9e263a4b766a75d0633c50426eb875" script = "a914748284390f9e263a4b766a75d0633c50426eb87587" scriptPubKey = scriptPubKey_from_payload(script_type, payload) self.assertEqual(scriptPubKey.hex(), script) network = "mainnet" address = b"3CK4fEwbMP7heJarmU4eqA3sMbVJyEnU3V" address2 = address_from_scriptPubKey(scriptPubKey, network) self.assertEqual(address, address2) scriptPubKey2, network2 = scriptPubKey_from_address(address) self.assertEqual(scriptPubKey2, scriptPubKey) self.assertEqual(network2, network) # invalid size: 21 bytes instead of 20 self.assertRaises(ValueError, scriptPubKey_from_payload, "00" * 21, "p2sh")
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_nulldata(self): script = ["OP_RETURN", "11" * 79] bscript = encode(script) self.assertEqual(script, decode(bscript)) script2 = ["OP_RETURN", b"\x11" * 79] bscript = encode(script2) self.assertEqual(script, decode(bscript)) script = ["OP_RETURN", "00" * 79] bscript = encode(script) self.assertEqual(script, decode(bscript)) script2 = ["OP_RETURN", b"\x11" * 79] bscript = encode(script) self.assertEqual(script, decode(bscript))
def test_nulldata(self): script = ['OP_RETURN', '11' * 79] bscript = encode(script) self.assertEqual(script, decode(bscript)) script2 = ['OP_RETURN', b'\x11' * 79] bscript = encode(script2) self.assertEqual(script, decode(bscript)) script = ['OP_RETURN', '00' * 79] bscript = encode(script) self.assertEqual(script, decode(bscript)) script2 = ['OP_RETURN', b'\x11' * 79] bscript = encode(script) self.assertEqual(script, decode(bscript))
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 test_simple(self): script_list = [2, 3, 'OP_ADD', 5, 'OP_EQUAL'] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_serialized = serialize(script_list) script_list2 = parse(script_serialized) self.assertEqual(script_list, script_list2) script_list = [26, -1, 'OP_ADD', 25, 'OP_EQUAL'] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_serialized = serialize(script_list) script_list2 = parse(script_serialized) self.assertEqual(script_list, script_list2) script_list = [0xffffffff, -1, 'OP_ADD', 0xfffffffe, 'OP_EQUAL'] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_serialized = serialize(script_list) script_list2 = parse(script_serialized) self.assertEqual(script_list, script_list2) script_list = ["1f" * 250, 'OP_DROP'] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_serialized = serialize(script_list) script_list2 = parse(script_serialized) self.assertEqual(script_list, script_list2) script_list = ["1f" * 520, 'OP_DROP'] script_bytes = encode(script_list) script_list2 = decode(script_bytes.hex()) self.assertEqual(script_list, script_list2) script_serialized = serialize(script_list) script_list2 = parse(script_serialized.hex()) self.assertEqual(script_list, script_list2)
def test_nulldata(): scripts = [ ["OP_RETURN", "11" * 79], ["OP_RETURN", "00" * 79], ] for script in scripts: bscript = encode(script) assert script == decode(bscript)
def test_wrapped_p2wpkh(): transaction = tx.Tx.deserialize( "0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a54770100000000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000" ) transaction.vin[0].scriptSig = script.decode( "001479091972186c449eb1ded22b78e40d009bdf0089" ) previous_txout = tx_out.TxOut( nValue=1000000000, scriptPubKey=script.decode("a9144733f37cf4db86fbc2efed2500b4f4e49f31202387"), ) sighash = get_sighash(transaction, previous_txout, 0, 0x01) assert ( sighash.hex() == "64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6" )
def test_native_p2wsh_2(): transaction = tx.Tx.deserialize( "0100000002e9b542c5176808107ff1df906f46bb1f2583b16112b95ee5380665ba7fcfc0010000000000ffffffff80e68831516392fcd100d186b3c2c7b95c80b53c77e77c35ba03a66b429a2a1b0000000000ffffffff0280969800000000001976a914de4b231626ef508c9a74a8517e6783c0546d6b2888ac80969800000000001976a9146648a8cd4531e1ec47f35916de8e259237294d1e88ac00000000" ) transaction.vin[0].txinwitness = [ "0063ab68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac" ] transaction.vin[1].txinwitness = [ "5163ab68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac" ] previous_txout_1 = tx_out.TxOut( nValue=16777215, scriptPubKey=script.decode( "0020ba468eea561b26301e4cf69fa34bde4ad60c81e70f059f045ca9a79931004a4d" ), ) sighash = get_sighash(transaction, previous_txout_1, 0, 0x83) assert ( sighash.hex() == "e9071e75e25b8a1e298a72f0d2e9f4f95a0f5cdf86a533cda597eb402ed13b3a" ) previous_txout_2 = tx.TxOut( nValue=16777215, scriptPubKey=script.decode( "0020d9bbfbe56af7c4b7f960a70d7ea107156913d9e5a26b0a71429df5e097ca6537" ), ) script_code = _get_witness_v0_scriptCodes( script.decode(transaction.vin[1].txinwitness[-1]) )[1] sighash = segwit_v0_sighash( script_code, transaction, 1, 0x83, previous_txout_2.nValue ) assert ( sighash.hex() == "cd72f1f1a433ee9df816857fad88d8ebd97e09a75cd481583eb841c330275e54" )
def test_simple() -> None: script_list: List[List[Token]] = [ [2, 3, "OP_ADD", 5, "OP_EQUAL"], ["1ADD", "OP_1ADD", "1ADE", "OP_EQUAL"], [hex(26)[2:].upper(), -1, "OP_ADD", hex(26)[2:].upper(), "OP_EQUAL"], [ hex(0xFFFFFFFF)[2:].upper(), -1, "OP_ADD", hex(0xFFFFFFFF)[2:].upper(), "OP_EQUAL", ], ["1F" * 250, "OP_DROP"], ["1F" * 520, "OP_DROP"], ] for scriptPubKey in script_list: assert scriptPubKey == script.decode(script.encode(scriptPubKey)) assert scriptPubKey == script.decode(script.encode(scriptPubKey).hex()) assert scriptPubKey == script.deserialize( script.serialize(scriptPubKey)) assert scriptPubKey == script.deserialize( script.serialize(scriptPubKey).hex())
def test_nulldata2() -> None: script_type = "nulldata" # max length case byte = b"\x00" for length in (0, 1, 16, 17, 74, 75, 76, 77, 78, 79, 80): payload = byte * length scriptPubKey = script.encode(["OP_RETURN", payload]) assert scriptPubKey == scriptPubKey_from_payload(script_type, payload) # back from the scriptPubKey to the payload assert (script_type, payload, 0) == payload_from_scriptPubKey(scriptPubKey) assert (script_type, payload, 0) == payload_from_scriptPubKey(script.decode(scriptPubKey))
def test_native_p2wpkh(): transaction = tx.Tx.deserialize( "0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f0000000000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000" ) previous_txout = tx_out.TxOut( nValue=600000000, scriptPubKey=script.decode("00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1"), ) sighash = get_sighash(transaction, previous_txout, 1, 0x01) assert ( sighash.hex() == "c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670" )
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_wrapped_p2wsh(): transaction = tx.Tx.deserialize( "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000" ) transaction.vin[0].txinwitness = [ "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae" ] previous_txout = tx_out.TxOut( nValue=987654321, scriptPubKey=script.decode( "0020a16b5755f7f6f96dbd65f5f0d6ab9418b89af4b1f14a1bb8a09062c35f0dcb54" ), ) assert ( get_sighash(transaction, previous_txout, 0, 0x01).hex() == "185c0be5263dce5b4bb50a047973c1b6272bfbd0103a89444597dc40b248ee7c" ) assert ( get_sighash(transaction, previous_txout, 0, 0x02).hex() == "e9733bc60ea13c95c6527066bb975a2ff29a925e80aa14c213f686cbae5d2f36" ) assert ( get_sighash(transaction, previous_txout, 0, 0x03).hex() == "1e1f1c303dc025bd664acb72e583e933fae4cff9148bf78c157d1e8f78530aea" ) assert ( get_sighash(transaction, previous_txout, 0, 0x81).hex() == "2a67f03e63a6a422125878b40b82da593be8d4efaafe88ee528af6e5a9955c6e" ) assert ( get_sighash(transaction, previous_txout, 0, 0x82).hex() == "781ba15f3779d5542ce8ecb5c18716733a5ee42a6f51488ec96154934e2c890a" ) assert ( get_sighash(transaction, previous_txout, 0, 0x83).hex() == "511e8e52ed574121fc1b654970395502128263f62662e076dc6baf05c2e6a99b" )
def test_p2sh() -> None: # self-consistency pubkey = "02 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" pubkey_hash = hash160(pubkey) redeem_script = scriptPubKey_from_payload("p2pkh", pubkey_hash) payload = hash160(redeem_script) scriptPubKey = script.encode(["OP_HASH160", payload, "OP_EQUAL"]) assert scriptPubKey == p2sh(redeem_script) # to the scriptPubKey in two steps (through payload) script_type = "p2sh" assert scriptPubKey == scriptPubKey_from_payload(script_type, payload) # back from the scriptPubKey to the payload assert (script_type, payload, 0) == payload_from_scriptPubKey(scriptPubKey) # base58 address network = "mainnet" address = base58address.p2sh(script.decode(redeem_script), network) assert address == address_from_scriptPubKey(scriptPubKey, network) prefix = NETWORKS[network]["p2sh"] assert address == b58address_from_h160(prefix, payload, network) # back from the address to the scriptPubKey assert (scriptPubKey, network) == scriptPubKey_from_address(address) # documented test case: https://learnmeabitcoin.com/guide/p2sh payload = "748284390f9e263a4b766a75d0633c50426eb875" scriptPubKey = "a914" + payload + "87" assert scriptPubKey == scriptPubKey_from_payload(script_type, payload).hex() address = b"3CK4fEwbMP7heJarmU4eqA3sMbVJyEnU3V" assert address == address_from_scriptPubKey(scriptPubKey, network) assert (bytes.fromhex(scriptPubKey), network) == scriptPubKey_from_address(address) # invalid size: 21 bytes instead of 20 err_msg = "invalid size: " with pytest.raises(ValueError, match=err_msg): scriptPubKey_from_payload(script_type, "00" * 21)
def test_nulldata2(self): script_type = "nulldata" # max length case byte = b"\x00" for length in (0, 1, 16, 17, 74, 75, 80): payload = byte * length script = encode(["OP_RETURN", 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(decode(script)) self.assertEqual(script_type, script_type2) self.assertEqual(0, m2) self.assertEqual(payload.hex(), payload2.hex())
def test_exceptions() -> None: scriptPubKey: List[Token] = [2, 3, "OP_ADD", 5, "OP_VERIF"] err_msg = "invalid string token: OP_VERIF" with pytest.raises(ValueError, match=err_msg): script.encode(scriptPubKey) err_msg = "Unmanaged <class 'function'> token type" with pytest.raises(ValueError, match=err_msg): script.encode([2, 3, "OP_ADD", 5, script.encode]) # type: ignore scriptPubKey = ["1f" * 521, "OP_DROP"] err_msg = "Too many bytes for OP_PUSHDATA: " with pytest.raises(ValueError, match=err_msg): script.encode(scriptPubKey) # A scriptPubKey with OP_PUSHDATA4 can be decoded script_bytes = "4e09020000" + "00" * 521 + "75" # ['00'*521, 'OP_DROP'] scriptPubKey = script.decode(script_bytes) # but it cannot be encoded err_msg = "Too many bytes for OP_PUSHDATA: " with pytest.raises(ValueError, match=err_msg): script.encode(scriptPubKey)
def test_exceptions(self): # Script: invalid OP_VERIF opcode script = [2, 3, 'OP_ADD', 5, 'OP_VERIF'] self.assertRaises(ValueError, encode, script) # encode(script) # Script: unmanaged <class 'function'> token type script = [2, 3, 'OP_ADD', 5, encode] self.assertRaises(ValueError, encode, script) # encode(script) # Script: Cannot push 521 bytes on the stack script = ["1f" * 521, 'OP_DROP'] self.assertRaises(ValueError, encode, script) # encode(script) # A script with OP_PUSHDATA4 can be decoded script_bytes = b'NN\t\x02\x00\x00' + 521 * b'\xff' # OP_PUSHDATA4 + 521 bytes script = decode(script_bytes) # but cannot be encoded # Script: Cannot push 522 bytes on the stack self.assertRaises(ValueError, encode, script)
def test_exceptions(self): # Script: invalid OP_VERIF opcode script = [2, 3, "OP_ADD", 5, "OP_VERIF"] self.assertRaises(ValueError, encode, script) # encode(script) # Script: unmanaged <class 'function'> token type script = [2, 3, "OP_ADD", 5, encode] self.assertRaises(ValueError, encode, script) # encode(script) # Script: Cannot push 521 bytes on the stack script = ["1f" * 521, "OP_DROP"] self.assertRaises(ValueError, encode, script) # encode(script) # A script with OP_PUSHDATA4 can be decoded script_bytes = "4e09020000" + "00" * 521 + "75" # ['00'*521, 'OP_DROP'] script = decode(script_bytes) # but it cannot be encoded # Cannot push 521 bytes on the stack self.assertRaises(ValueError, encode, script)
def test_exceptions(): script = [2, 3, "OP_ADD", 5, "OP_VERIF"] err_msg = "Invalid string token: OP_VERIF" with pytest.raises(ValueError, match=err_msg): encode(script) script = [2, 3, "OP_ADD", 5, encode] err_msg = "Unmanaged <class 'function'> token type" with pytest.raises(ValueError, match=err_msg): encode(script) script = ["1f" * 521, "OP_DROP"] err_msg = "Cannot push 521 bytes on the stack" with pytest.raises(ValueError, match=err_msg): encode(script) # A script with OP_PUSHDATA4 can be decoded script_bytes = "4e09020000" + "00" * 521 + "75" # ['00'*521, 'OP_DROP'] script = decode(script_bytes) # but it cannot be encoded err_msg = "Cannot push 521 bytes on the stack" with pytest.raises(ValueError, match=err_msg): encode(script)
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)
def test_simple(self): script_list = [2, 3, "OP_ADD", 5, "OP_EQUAL"] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = ["1ADD", "OP_1ADD", "1ADE", "OP_EQUAL"] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = [ hex(26)[2:].upper(), -1, "OP_ADD", hex(26)[2:].upper(), "OP_EQUAL", ] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = [ hex(0xFFFFFFFF)[2:].upper(), -1, "OP_ADD", hex(0xFFFFFFFF)[2:].upper(), "OP_EQUAL", ] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = ["1F" * 250, "OP_DROP"] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = ["1F" * 520, "OP_DROP"] script_bytes = encode(script_list) script_list2 = decode(script_bytes.hex()) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized.hex()) self.assertEqual(script_list, script_list2)
def test_encoding(): script_bytes = b"jKBIP141 \\o/ Hello SegWit :-) keep it strong! LLAP Bitcoin twitter.com/khs9ne" assert script.encode(script.decode(script_bytes)) == script_bytes
def test_simple(self): script_list = [2, 3, 'OP_ADD', 5, 'OP_EQUAL'] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = ['1ADD', 'OP_1ADD', '1ADE', 'OP_EQUAL'] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = [ hex(26)[2:].upper(), -1, 'OP_ADD', hex(26)[2:].upper(), 'OP_EQUAL' ] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = [ hex(0xffffffff)[2:].upper(), -1, 'OP_ADD', hex(0xffffffff)[2:].upper(), 'OP_EQUAL' ] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = ["1F" * 250, 'OP_DROP'] script_bytes = encode(script_list) script_list2 = decode(script_bytes) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized) self.assertEqual(script_list, script_list2) script_list = ["1F" * 520, 'OP_DROP'] script_bytes = encode(script_list) script_list2 = decode(script_bytes.hex()) self.assertEqual(script_list, script_list2) script_bytes2 = encode(script_list2) self.assertEqual(script_bytes, script_bytes2) script_serialized = serialize(script_list) script_list2 = deserialize(script_serialized.hex()) self.assertEqual(script_list, script_list2)