def test_output_scripts_serialization() -> None: input_1 = TxIn( OutPoint( "75ddabb27b8845f5247975c8a5ba7c6f336c4570708ebe230caf6db5217ae858", 0), b"", 0xFFFFFFFF, ) output_1 = TxOut( 149990000, bytes.fromhex("a914256b3a9ae8145e5094329537dd4d7a25dbc9452087")) output_2 = TxOut( 100000000, bytes.fromhex("001400aea9a2e5f0f876a588df5546e8742d1d87008f")) tx = Tx(2, 0, [input_1], [output_1, output_2]) psbt = Psbt.from_tx(tx) # p2sh-p2wsh psbt.outputs[0].redeem_script = bytes.fromhex( "003BD89EE628E6EB745F99DF1E4AEF64A0DAA814850DAA509F30C0F472E0563C7A") psbt.outputs[0].witness_script = bytes.fromhex( "522103dcea327ff7b2b4449413d9dc24cef0cc9e7864bad9d6291f3f1a04b639422c312103a2c5199b333adfaed4fea7ab65485b9e23a6cb317ddae7e8c1f2bd673414bdd352ae" ) assert Psbt.b64decode(psbt.b64encode()) == psbt
def test_native_p2wsh_2() -> None: tx_bytes = "0100000002e9b542c5176808107ff1df906f46bb1f2583b16112b95ee5380665ba7fcfc0010000000000ffffffff80e68831516392fcd100d186b3c2c7b95c80b53c77e77c35ba03a66b429a2a1b0000000000ffffffff0280969800000000001976a914de4b231626ef508c9a74a8517e6783c0546d6b2888ac80969800000000001976a9146648a8cd4531e1ec47f35916de8e259237294d1e88ac00000000" tx = Tx.parse(tx_bytes) tx.vin[0].script_witness = Witness([ "0063ab68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac" ]) tx.vin[1].script_witness = Witness([ "5163ab68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac" ]) previous_txout_1 = TxOut( 16777215, "0020ba468eea561b26301e4cf69fa34bde4ad60c81e70f059f045ca9a79931004a4d") previous_txout_2 = TxOut( 16777215, "0020d9bbfbe56af7c4b7f960a70d7ea107156913d9e5a26b0a71429df5e097ca6537") hash_ = sig_hash.from_tx( [previous_txout_1, previous_txout_2], tx, 0, sig_hash.ANYONECANPAY | sig_hash.SINGLE, ) assert hash_ == bytes.fromhex( "e9071e75e25b8a1e298a72f0d2e9f4f95a0f5cdf86a533cda597eb402ed13b3a") script_ = sig_hash.witness_v0_script(tx.vin[1].script_witness.stack[-1])[1] hash_ = sig_hash.segwit_v0( script_, tx, 1, sig_hash.ANYONECANPAY | sig_hash.SINGLE, previous_txout_2.value, ) assert hash_ == bytes.fromhex( "cd72f1f1a433ee9df816857fad88d8ebd97e09a75cd481583eb841c330275e54")
def test_tx_out() -> None: tx_out = TxOut(0, b"") assert tx_out.value == 0 assert tx_out.script_pub_key.script == b"" assert tx_out.script_pub_key.type == "unknown" assert tx_out.script_pub_key.network == "mainnet" assert tx_out.script_pub_key.addresses == [""] assert tx_out.nValue == tx_out.value assert tx_out.scriptPubKey == tx_out.script_pub_key.script assert tx_out == TxOut.parse(tx_out.serialize()) assert tx_out == TxOut.from_dict(tx_out.to_dict()) value = 3259343370 script = "0020ed8e9600561000f722bd26e850be7d80f24d174fabeff98baef967325e2b5a86" tx_out = TxOut(value, script) assert tx_out.value == value assert tx_out.script_pub_key.script.hex() == script assert tx_out.script_pub_key.type == "p2wsh" assert tx_out.script_pub_key.network == "mainnet" addr = "bc1qak8fvqzkzqq0wg4aym59p0nasrey696040hlnzawl9nnyh3tt2rqzgmhmv" assert tx_out.script_pub_key.addresses == [addr] assert tx_out.nValue == tx_out.value assert tx_out.scriptPubKey == tx_out.script_pub_key.script assert tx_out == TxOut.parse(tx_out.serialize()) assert tx_out == TxOut.from_dict(tx_out.to_dict()) assert tx_out == TxOut.from_address( tx_out.value, tx_out.script_pub_key.addresses[0] )
def test_creation() -> None: psbt_str = "cHNidP8BAJoCAAAAAljoeiG1ba8MI76OcHBFbDNvfLqlyHV5JPVFiHuyq911AAAAAAD/////g40EJ9DsZQpoqka7CwmK6kQiwHGyyng1Kgd5WdB86h0BAAAAAP////8CcKrwCAAAAAAWABTYXCtx0AYLCcmIauuBXlCZHdoSTQDh9QUAAAAAFgAUAK6pouXw+HaliN9VRuh0LR2HAI8AAAAAAAAAAAA=" psbt = Psbt.b64decode(psbt_str) assert psbt.b64encode() == psbt_str output_1 = TxOut( 149990000, ScriptPubKey("0014d85c2b71d0060b09c9886aeb815e50991dda124d")) output_2 = TxOut( 100000000, ScriptPubKey("001400aea9a2e5f0f876a588df5546e8742d1d87008f")) input_1 = TxIn( OutPoint( "75ddabb27b8845f5247975c8a5ba7c6f336c4570708ebe230caf6db5217ae858", 0), b"", 0xFFFFFFFF, ) input_2 = TxIn( OutPoint( "1dea7cd05979072a3578cab271c02244ea8a090bbb46aa680a65ecd027048d83", 1), b"", 0xFFFFFFFF, ) transaction = Tx(2, 0, [input_1, input_2], [output_1, output_2]) psbt_from_tx_ = Psbt.from_tx(transaction) assert psbt_from_tx_ == psbt
def test_legacy_p2pkh() -> None: pub_key = "04280c8f66bf2ccaeb3f60a19ad4a06365f8bd6178aab0e709df2173df8f553366549aec336aae8742a84702b6c7c3052d89f5d76d535ec3716e72187956351613" signature = "3045022100ea43c4800d1a860ec89b5273898a146cfb01d34ff4c364d24a110c480d0e3f7502201c82735577f932f1ca8e1c54bf653e0f8e74e408fe83666bc85cac4472ec950801" script_sig = serialize([signature, pub_key]) out_point = OutPoint( "d8343a35ba951684f2969eafe833d9e6fe436557b9707ae76802875952e860fc", 1) tx_in = TxIn(out_point, script_sig, 0xFFFFFFFF) tx_out1 = TxOut( 2017682, bytes.fromhex("76a91413bd20236d0da56492c325dce289b4da35b4b5bd88ac")) tx_out2 = TxOut( 1049154982, bytes.fromhex("76a914da169b45781ca210f8c11617ba66bd843da76b1688ac")) tx = Tx(1, 0, [tx_in], [tx_out1, tx_out2]) script_pub_key = serialize([ "OP_DUP", "OP_HASH160", "82ac30f58baf99ec9d14e6181eee076f4e27f69c", "OP_EQUALVERIFY", "OP_CHECKSIG", ]) utxo = TxOut(1051173696, script_pub_key) hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.ALL) assert dsa.verify_(hash_, pub_key, bytes.fromhex(signature)[:-1])
def test_native_p2wpkh() -> None: tx_bytes = "0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f0000000000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000" tx = Tx.parse(tx_bytes) utxo_0 = TxOut( 625000000, "2103c9f4836b9a4f77fc0d81f7bcb01b7f1b35916864b9476c241ce9fc198bd25432ac", ) utxo_1 = TxOut(600000000, "00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1") hash_ = sig_hash.from_tx([utxo_0, utxo_1], tx, 1, sig_hash.ALL) assert hash_ == bytes.fromhex( "c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670")
def test_p2pk() -> None: pub_key = "0479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8" signature = "304402200A5C6163F07B8D3B013C4D1D6DBA25E780B39658D79BA37AF7057A3B7F15FFA102201FD9B4EAA9943F734928B99A83592C2E7BF342EA2680F6A2BB705167966B742001" script_pub_key = serialize([pub_key, "OP_CHECKSIG"]) script_sig = serialize([signature]) founding_tx_script = serialize(["OP_0", "OP_0"]) tx_in = TxIn(OutPoint(b"\x00" * 32, 0xFFFFFFFF), founding_tx_script, 0xFFFFFFFF) funding_tx = Tx(1, 0, [tx_in], [TxOut(0, script_pub_key)]) tx_in = TxIn(OutPoint(funding_tx.id, 0), script_sig, 0xFFFFFFFF) receiving_tx = Tx(1, 0, [tx_in], [TxOut(0, b"")]) hash_ = sig_hash.from_tx(funding_tx.vout, receiving_tx, 0, sig_hash.ALL) assert dsa.verify_(hash_, pub_key, bytes.fromhex(signature)[:-1])
def test_empty_stack() -> None: utxo = TxOut( 100000000, serialize([ "OP_1", "cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" ]), ) tx_in = TxIn(OutPoint(), "", 1, Witness([])) tx = Tx(vin=[tx_in], vout=[TxOut(100000000, "")]) err_msg = "Empty stack" with pytest.raises(BTClibValueError, match=err_msg): sig_hash.from_tx([utxo], tx, 0, 0)
def test_wrapped_p2tr() -> None: script = [ "OP_1", "cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf", ] utxo = TxOut( 100000000, serialize(["OP_HASH160", hash160(serialize(script)), "OP_EQUAL"])) tx_in = TxIn(OutPoint(), serialize(script), 1, Witness(["0A" * 32])) tx = Tx(vin=[tx_in], vout=[TxOut(100000000, "")]) err_msg = "Taproot scripts cannot be wrapped in p2sh" with pytest.raises(BTClibValueError, match=err_msg): sig_hash.from_tx([utxo], tx, 0, 0)
def test_wrapped_p2wsh() -> None: tx_bytes = "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000" tx = Tx.parse(tx_bytes) stack = [ "56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae" ] tx.vin[0].script_witness = Witness(stack) utxo = TxOut( 987654321, "0020a16b5755f7f6f96dbd65f5f0d6ab9418b89af4b1f14a1bb8a09062c35f0dcb54", ) hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.ALL) assert hash_ == bytes.fromhex( "185c0be5263dce5b4bb50a047973c1b6272bfbd0103a89444597dc40b248ee7c") hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.NONE) assert hash_ == bytes.fromhex( "e9733bc60ea13c95c6527066bb975a2ff29a925e80aa14c213f686cbae5d2f36") hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.SINGLE) assert hash_ == bytes.fromhex( "1e1f1c303dc025bd664acb72e583e933fae4cff9148bf78c157d1e8f78530aea") hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.ANYONECANPAY | sig_hash.ALL) assert hash_ == bytes.fromhex( "2a67f03e63a6a422125878b40b82da593be8d4efaafe88ee528af6e5a9955c6e") hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.ANYONECANPAY | sig_hash.NONE) assert hash_ == bytes.fromhex( "781ba15f3779d5542ce8ecb5c18716733a5ee42a6f51488ec96154934e2c890a") hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.ANYONECANPAY | sig_hash.SINGLE) assert hash_ == bytes.fromhex( "511e8e52ed574121fc1b654970395502128263f62662e076dc6baf05c2e6a99b")
def test_p2pk_anyonecanpay() -> None: pub_key = "048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf" signature = "304402204710a85181663b32d25c70ec2bbd14adff5ddfff6cb50d09e155ef5f541fc86c0220056b0cc949be9386ecc5f6c2ac0493269031dbb185781db90171b54ac127790281" script_pub_key = serialize([pub_key, "OP_CHECKSIG"]) script_sig = serialize([signature]) founding_tx_script = serialize(["OP_0", "OP_0"]) tx_in = TxIn(OutPoint(b"\x00" * 32, 0xFFFFFFFF), founding_tx_script, 0xFFFFFFFF) funding_tx = Tx(1, 0, [tx_in], [TxOut(0, script_pub_key)]) tx_in = TxIn(OutPoint(funding_tx.id, 0), script_sig, 0xFFFFFFFF) receiving_tx = Tx(1, 0, [tx_in], [TxOut(0, b"")]) hash_ = sig_hash.from_tx(funding_tx.vout, receiving_tx, 0, sig_hash.ANYONECANPAY | sig_hash.ALL) assert dsa.verify_(hash_, pub_key, bytes.fromhex(signature)[:-1])
def test_psbt() -> None: prev_out = OutPoint( "9dcfdb5836ecfe146bdaa896605ba21222f83cd014dd47adde14fab2aba7de9b", 1) script_sig = b"" sequence = 0xFFFFFFFF tx_in = TxIn(prev_out, script_sig, sequence) tx_out1 = TxOut(2500000, "a914f987c321394968be164053d352fc49763b2be55c87") tx_out2 = TxOut( 6381891, "0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d") version = 1 lock_time = 0 tx = Tx(version, lock_time, [tx_in], [tx_out1, tx_out2]) psbt = Psbt.from_tx(tx) assert psbt == Psbt.parse(psbt.serialize()) assert psbt == Psbt.from_dict(psbt.to_dict())
def test_sig_hashsingle_bug() -> None: pub_key = "02D5C25ADB51B61339D2B05315791E21BBE80EA470A49DB0135720983C905AACE0" signature = "3045022100C9CDD08798A28AF9D1BAF44A6C77BCC7E279F47DC487C8C899911BC48FEAFFCC0220503C5C50AE3998A733263C5C0F7061B483E2B56C4C41B456E7D2F5A78A74C07703" script_pub_key = serialize([ "OP_DUP", "OP_HASH160", "5b6462475454710f3c22f5fdf0b40704c92f25c3", "OP_EQUALVERIFY", "OP_CHECKSIGVERIFY", "OP_1", ]) utxo = TxOut(0, script_pub_key) tx_bytes = "01000000020002000000000000000000000000000000000000000000000000000000000000000000000151ffffffff0001000000000000000000000000000000000000000000000000000000000000000000006b483045022100c9cdd08798a28af9d1baf44a6c77bcc7e279f47dc487c8c899911bc48feaffcc0220503c5c50ae3998a733263c5c0f7061b483e2b56c4c41b456e7d2f5a78a74c077032102d5c25adb51b61339d2b05315791e21bbe80ea470a49db0135720983c905aace0ffffffff010000000000000000015100000000" tx = Tx.parse(tx_bytes) hash_ = sig_hash.from_tx([TxOut(0, ""), utxo], tx, 1, sig_hash.SINGLE) assert dsa.verify_(hash_, pub_key, bytes.fromhex(signature)[:-1])
def test_wrapped_p2wpkh() -> None: tx_bytes = "0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a54770100000000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000" tx = Tx.parse(tx_bytes) tx.vin[0].script_sig = bytes.fromhex( "001479091972186c449eb1ded22b78e40d009bdf0089") utxo = TxOut(1000000000, "a9144733f37cf4db86fbc2efed2500b4f4e49f31202387") hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.ALL) assert hash_ == bytes.fromhex( "64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6")
def test_p2pkh() -> None: pub_key = "038282263212C609D9EA2A6E3E172DE238D8C39CABD5AC1CA10646E23FD5F51508" signature = "304402206E05A6FE23C59196FFE176C9DDC31E73A9885638F9D1328D47C0C703863B8876022076FEB53811AA5B04E0E79F938EB19906CC5E67548BC555A8E8B8B0FC603D840C01" script_pub_key = serialize([ "OP_DUP", "OP_HASH160", "1018853670F9F3B0582C5B9EE8CE93764AC32B93", "OP_EQUALVERIFY", "OP_CHECKSIG", ]) script_sig = serialize([signature, pub_key]) founding_tx_script = serialize(["OP_0", "OP_0"]) tx_in = TxIn(OutPoint(b"\x00" * 32, 0xFFFFFFFF), founding_tx_script, 0xFFFFFFFF) funding_tx = Tx(1, 0, [tx_in], [TxOut(0, script_pub_key)]) tx_in = TxIn(OutPoint(funding_tx.id, 0), script_sig, 0xFFFFFFFF) receiving_tx = Tx(1, 0, [tx_in], [TxOut(0, b"")]) hash_ = sig_hash.from_tx(funding_tx.vout, receiving_tx, 0, sig_hash.ALL) assert dsa.verify_(hash_, pub_key, bytes.fromhex(signature)[:-1])
def test_native_p2wsh() -> None: tx_bytes = "0100000002fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b9216b36e0000000000ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac6c79da47c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a30741f8145e5acadf23f751864167f32e0963f788ac00000000" tx = Tx.parse(tx_bytes) tx.vin[1].script_witness = Witness([ "21026dccc749adc2a9d0d89497ac511f760f45c47dc5ed9cf352a58ac706453880aeadab210255a9626aebf5e29c0e6538428ba0d1dcf6ca98ffdf086aa8ced5e0d0215ea465ac" ]) utxo_0 = TxOut( 156250000, "21036d5c20fa14fb2f635474c1dc4ef5909d4568e5569b79fc94d3448486e14685f8ac", ) utxo_1 = TxOut( 4900000000, "00205d1b56b63d714eebe542309525f484b7e9d6f686b3781b6f61ef925d66d6f6a0", ) hash_ = sig_hash.from_tx([utxo_0, utxo_1], tx, 1, sig_hash.SINGLE) assert hash_ == bytes.fromhex( "82dde6e4f1e94d02c2b7ad03d2115d691f48d064e9d52f58194a6637e4194391") script_ = sig_hash.witness_v0_script(tx.vin[1].script_witness.stack[-1])[1] hash_ = sig_hash.segwit_v0(script_, tx, 1, sig_hash.SINGLE, utxo_1.value) assert hash_ == bytes.fromhex( "fef7bd749cce710c5c052bd796df1af0d935e59cea63736268bcbe2d2134fc47")
def test_first_transaction() -> None: tx_bytes = "0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000" tx = Tx.parse(tx_bytes) utxo = TxOut( value=5000000000, script_pub_key=bytes.fromhex( "410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac" ), ) hash_ = sig_hash.from_tx([utxo], tx, 0, sig_hash.ALL) assert hash_ == bytes.fromhex( "7a05c6145f10101e9d6325494245adf1297d80f8f38d4d576d57cdba220bcb19") pub_key = "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3" signature = "304402204E45E16932B8AF514961A1D3A1A25FDF3F4F7732E9D624C6C61548AB5FB8CD410220181522EC8ECA07DE4860A4ACDD12909D831CC56CBBAC4622082221A8768D1D0901" assert dsa.verify_(hash_, pub_key, bytes.fromhex(signature)[:-1])
def test_bip_test_vector(): fname = "taproot_test_vector.json" filename = path.join(path.dirname(__file__), "_data", fname) with open(filename, "r", encoding="ascii") as file_: data = json.load(file_)["keyPathSpending"][0] unsigned_tx = Tx.parse(data["given"]["rawUnsignedTx"]) utxos = [] for utxo in data["given"]["utxosSpent"]: utxos.append(TxOut(utxo["amountSats"], utxo["scriptPubKey"])) for vin in unsigned_tx.vin: vin.script_witness.stack.append(["00"]) for test in data["inputSpending"]: index = test["given"]["txinIndex"] hash_type = test["given"]["hashType"] signature_hash = sig_hash.from_tx(utxos, unsigned_tx, index, hash_type) assert signature_hash.hex() == test["intermediary"]["sigHash"]
def test_tx() -> None: # default constructor tx = Tx() assert not tx.is_segwit() assert not any(bool(w) for w in tx.vwitness) assert not any(bool(tx_in.script_witness) for tx_in in tx.vin) assert not tx.is_coinbase() assert tx.version == 1 assert tx.lock_time == 0 assert not tx.vin assert not tx.vout assert tx.nVersion == tx.version assert tx.nLockTime == tx.lock_time tx_id = "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43" assert tx.id.hex() == tx_id assert tx.hash == tx.id assert tx.size == 10 assert tx.vsize == tx.size assert tx.weight == tx.size * 4 tx_2 = Tx.from_dict(tx.to_dict()) assert tx_2.is_segwit() == tx.is_segwit() assert tx_2 == tx tx_2 = Tx.parse(tx.serialize(include_witness=True)) assert tx_2.is_segwit() == tx.is_segwit() assert tx_2 == tx tx_2 = Tx.parse(tx.serialize(include_witness=False)) assert not tx_2.is_segwit() assert tx_2 == tx # non-default constructor, no segwit prev_out = OutPoint( "9dcfdb5836ecfe146bdaa896605ba21222f83cd014dd47adde14fab2aba7de9b", 1) script_sig = b"" sequence = 0xFFFFFFFF tx_in = TxIn(prev_out, script_sig, sequence) tx_out1 = TxOut(2500000, "a914f987c321394968be164053d352fc49763b2be55c87") tx_out2 = TxOut( 6381891, "0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d") version = 1 lock_time = 0 tx = Tx(version, lock_time, [tx_in], [tx_out1, tx_out2]) assert not tx.is_segwit() assert not any(bool(w) for w in tx.vwitness) assert not any(bool(tx_in.script_witness) for tx_in in tx.vin) assert not tx.is_coinbase() assert tx.version == 1 assert tx.lock_time == 0 assert len(tx.vin) == 1 assert len(tx.vout) == 2 assert tx.nVersion == tx.version assert tx.nLockTime == tx.lock_time tx_id = "4e52f7848dab7dd89ef7ba477939574198a170bfcb2fb34355c69f5e0169f63c" assert tx.id.hex() == tx_id assert tx.hash == tx.id assert tx.size == 126 assert tx.vsize == tx.size assert tx.weight == tx.size * 4 tx_2 = Tx.from_dict(tx.to_dict()) assert tx_2.is_segwit() == tx.is_segwit() assert tx_2 == tx tx_2 = Tx.parse(tx.serialize(include_witness=True)) assert tx_2.is_segwit() == tx.is_segwit() assert tx_2 == tx tx_2 = Tx.parse(tx.serialize(include_witness=False)) assert not tx_2.is_segwit() assert tx_2 == tx # non-default constructor, with segwit version = 1 lock_time = 0 tx = Tx(version, lock_time, [tx_in], [tx_out1, tx_out2]) stack = [ "", "30440220421fbbedf2ee096d6289b99973509809d5e09589040d5e0d453133dd11b2f78a02205686dbdb57e0c44e49421e9400dd4e931f1655332e8d078260c9295ba959e05d01", "30440220398f141917e4525d3e9e0d1c6482cb19ca3188dc5516a3a5ac29a0f4017212d902204ea405fae3a58b1fc30c5ad8ac70a76ab4f4d876e8af706a6a7b4cd6fa100f4401", "52210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae", ] tx.vin[0].script_witness = Witness(stack) assert tx.is_segwit() assert any(bool(w) for w in tx.vwitness) assert any(bool(tx_in.script_witness) for tx_in in tx.vin) assert not tx.is_coinbase() assert tx.version == 1 assert tx.lock_time == 0 assert len(tx.vin) == 1 assert len(tx.vout) == 2 assert tx.nVersion == tx.version assert tx.nLockTime == tx.lock_time tx_id = "4e52f7848dab7dd89ef7ba477939574198a170bfcb2fb34355c69f5e0169f63c" assert tx.id.hex() == tx_id hash_ = "d39eb3e3954be4bdc0b3be2d980124b1e1e11fb414b886b52939b07d95a58a8f" assert tx.hash.hex() == hash_ assert tx.size == 380 assert tx.vsize == 190 assert tx.weight == 758 tx_2 = Tx.from_dict(tx.to_dict()) assert tx_2.is_segwit() == tx.is_segwit() assert tx_2 == tx tx_2 = Tx.parse(tx.serialize(include_witness=True)) assert tx_2.is_segwit() == tx.is_segwit() assert tx_2 == tx tx_2 = Tx.parse(tx.serialize(include_witness=False)) assert not tx_2.is_segwit() assert tx_2 != tx
def test_invalid_tx_out() -> None: with pytest.raises(BTClibValueError, match="invalid satoshi amount: "): script = "0020ed8e9600561000f722bd26e850be7d80f24d174fabeff98baef967325e2b5a86" TxOut(-1, script)