|
11 | 11 | from test_framework.descriptors import descsum_create
|
12 | 12 | from test_framework.key import ECKey, H_POINT
|
13 | 13 | from test_framework.messages import (
|
| 14 | + COutPoint, |
| 15 | + CTransaction, |
| 16 | + CTxIn, |
| 17 | + CTxOut, |
14 | 18 | MAX_BIP125_RBF_SEQUENCE,
|
15 | 19 | WITNESS_SCALE_FACTOR,
|
16 | 20 | ser_compact_size,
|
17 | 21 | )
|
| 22 | +from test_framework.psbt import ( |
| 23 | + PSBT, |
| 24 | + PSBTMap, |
| 25 | + PSBT_GLOBAL_UNSIGNED_TX, |
| 26 | + PSBT_IN_RIPEMD160, |
| 27 | + PSBT_IN_SHA256, |
| 28 | + PSBT_IN_HASH160, |
| 29 | + PSBT_IN_HASH256, |
| 30 | +) |
18 | 31 | from test_framework.test_framework import BitcoinTestFramework
|
19 | 32 | from test_framework.util import (
|
20 | 33 | assert_approx,
|
|
23 | 36 | assert_raises_rpc_error,
|
24 | 37 | find_output,
|
25 | 38 | find_vout_for_address,
|
| 39 | + random_bytes, |
26 | 40 | )
|
27 | 41 | from test_framework.wallet_util import bytes_to_wif
|
28 | 42 |
|
@@ -775,5 +789,37 @@ def test_psbt_input_keys(psbt_input, keys):
|
775 | 789 | self.nodes[0].sendrawtransaction(rawtx)
|
776 | 790 | self.generate(self.nodes[0], 1)
|
777 | 791 |
|
| 792 | + self.log.info("Test decoding PSBT with per-input preimage types") |
| 793 | + # note that the decodepsbt RPC doesn't check whether preimages and hashes match |
| 794 | + hash_ripemd160, preimage_ripemd160 = random_bytes(20), random_bytes(50) |
| 795 | + hash_sha256, preimage_sha256 = random_bytes(32), random_bytes(50) |
| 796 | + hash_hash160, preimage_hash160 = random_bytes(20), random_bytes(50) |
| 797 | + hash_hash256, preimage_hash256 = random_bytes(32), random_bytes(50) |
| 798 | + |
| 799 | + tx = CTransaction() |
| 800 | + tx.vin = [CTxIn(outpoint=COutPoint(hash=int('aa' * 32, 16), n=0), scriptSig=b""), |
| 801 | + CTxIn(outpoint=COutPoint(hash=int('bb' * 32, 16), n=0), scriptSig=b""), |
| 802 | + CTxIn(outpoint=COutPoint(hash=int('cc' * 32, 16), n=0), scriptSig=b""), |
| 803 | + CTxIn(outpoint=COutPoint(hash=int('dd' * 32, 16), n=0), scriptSig=b"")] |
| 804 | + tx.vout = [CTxOut(nValue=0, scriptPubKey=b"")] |
| 805 | + psbt = PSBT() |
| 806 | + psbt.g = PSBTMap({PSBT_GLOBAL_UNSIGNED_TX: tx.serialize()}) |
| 807 | + psbt.i = [PSBTMap({bytes([PSBT_IN_RIPEMD160]) + hash_ripemd160: preimage_ripemd160}), |
| 808 | + PSBTMap({bytes([PSBT_IN_SHA256]) + hash_sha256: preimage_sha256}), |
| 809 | + PSBTMap({bytes([PSBT_IN_HASH160]) + hash_hash160: preimage_hash160}), |
| 810 | + PSBTMap({bytes([PSBT_IN_HASH256]) + hash_hash256: preimage_hash256})] |
| 811 | + psbt.o = [PSBTMap()] |
| 812 | + res_inputs = self.nodes[0].decodepsbt(psbt.to_base64())["inputs"] |
| 813 | + assert_equal(len(res_inputs), 4) |
| 814 | + preimage_keys = ["ripemd160_preimages", "sha256_preimages", "hash160_preimages", "hash256_preimages"] |
| 815 | + expected_hashes = [hash_ripemd160, hash_sha256, hash_hash160, hash_hash256] |
| 816 | + expected_preimages = [preimage_ripemd160, preimage_sha256, preimage_hash160, preimage_hash256] |
| 817 | + for res_input, preimage_key, hash, preimage in zip(res_inputs, preimage_keys, expected_hashes, expected_preimages): |
| 818 | + assert preimage_key in res_input |
| 819 | + assert_equal(len(res_input[preimage_key]), 1) |
| 820 | + assert hash.hex() in res_input[preimage_key] |
| 821 | + assert_equal(res_input[preimage_key][hash.hex()], preimage.hex()) |
| 822 | + |
| 823 | + |
778 | 824 | if __name__ == '__main__':
|
779 | 825 | PSBTTest().main()
|
0 commit comments