Skip to content

Commit 71a751f

Browse files
committed
test: add test for decoding PSBT with per-input preimage types
1 parent faf4337 commit 71a751f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

test/functional/rpc_psbt.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,23 @@
1111
from test_framework.descriptors import descsum_create
1212
from test_framework.key import ECKey, H_POINT
1313
from test_framework.messages import (
14+
COutPoint,
15+
CTransaction,
16+
CTxIn,
17+
CTxOut,
1418
MAX_BIP125_RBF_SEQUENCE,
1519
WITNESS_SCALE_FACTOR,
1620
ser_compact_size,
1721
)
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+
)
1831
from test_framework.test_framework import BitcoinTestFramework
1932
from test_framework.util import (
2033
assert_approx,
@@ -23,6 +36,7 @@
2336
assert_raises_rpc_error,
2437
find_output,
2538
find_vout_for_address,
39+
random_bytes,
2640
)
2741
from test_framework.wallet_util import bytes_to_wif
2842

@@ -775,5 +789,37 @@ def test_psbt_input_keys(psbt_input, keys):
775789
self.nodes[0].sendrawtransaction(rawtx)
776790
self.generate(self.nodes[0], 1)
777791

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+
778824
if __name__ == '__main__':
779825
PSBTTest().main()

0 commit comments

Comments
 (0)