@@ -37,46 +37,78 @@ def test_createpsbt(
3737 )
3838 unspents = wallet .rpc .listunspent ()
3939 selected_coin = [{"txid" : unspents [0 ]["txid" ], "vout" : unspents [0 ]["vout" ]}]
40- # Spending it all
4140 psbt = wallet .createpsbt (
4241 [random_address ],
43- [20 ],
44- True ,
42+ [19 ],
43+ False , # Important because otherwise there is no change output!
4544 0 ,
4645 1 ,
4746 selected_coins = selected_coin , # Selecting only one UTXO since input ordering seems to also be random in Core.
4847 )
4948 assert len (psbt ["tx" ]["vin" ]) == 1
5049 assert len (psbt ["inputs" ]) == 1
5150
52- # Check the PSBT fields - inputs
53- # The first input seems to be last selected coin from selected_coins
51+ # Input fields
5452 assert (
5553 psbt ["inputs" ][0 ]["bip32_derivs" ][0 ]["pubkey" ]
5654 == "0330955ab511845fb48fc5739da551875ed54fa1f2fdd4cf77f3473ce2cffb4c75"
5755 )
5856 assert psbt ["inputs" ][0 ]["bip32_derivs" ][0 ]["path" ] == "m/84h/1h/0h/0/1"
5957 assert psbt ["inputs" ][0 ]["bip32_derivs" ][0 ]["master_fingerprint" ] == "8c24a510"
6058
61- # Check the PSBT fields - outputs
62- logger .info (f"the whole { psbt } " )
63-
64- logger .info (f"the outputs of the { psbt ['outputs' ]} " )
65- assert (
66- psbt ["outputs" ][0 ]["address" ] == "bcrt1q7mlxxdna2e2ufzgalgp5zhtnndl7qddlxjy5eg"
67- )
68- assert psbt ["outputs" ][0 ]["change" ] == False
69- # assert psbt["outputs"][0]["bip32_derivs"][0]["master_fingerprint"] == "1e9cf8a7"
59+ # Output fields
60+ for output in psbt ["outputs" ]: # The ordering of the outputs is random
61+ if output ["change" ] == False :
62+ assert output ["address" ] == "bcrt1q7mlxxdna2e2ufzgalgp5zhtnndl7qddlxjy5eg"
63+ else :
64+ assert output ["is_mine" ] == True
65+ assert (
66+ output ["bip32_derivs" ][0 ]["pubkey" ]
67+ == "02251fe2ee4bc43729b0903ffadbcf846d9e6acbb3aa593b09d60085645cbe3653"
68+ )
69+ assert output ["bip32_derivs" ][0 ]["path" ] == "m/84h/1h/0h/1/0"
7070
71- # Check the fields of a PSBT created by a taproot wallet
72- # Could be moved to a dedicated test of taproot functionalites in the future
71+ # Taproot fields (could be moved to a dedicated test of taproot functionalites in the future)
7372 taproot_wallet = funded_taproot_wallet
7473 assert taproot_wallet .is_taproot == True
7574 address = taproot_wallet .getnewaddress ()
76- # Taproot test addrs are bcrt1p
7775 assert address .startswith ("bcrt1p" )
7876 assert taproot_wallet .amount_total == 20
79- # TODO: Test psbts with taproot wallet, especially the new taproot fields.
77+ # Let's keep the random address so we have a "mixed" set of outputs: segwit and taproot
78+ psbt = taproot_wallet .createpsbt (
79+ [random_address ],
80+ [3 ],
81+ False ,
82+ 0 ,
83+ 1 ,
84+ )
85+ # Input fields
86+ assert psbt ["inputs" ][0 ]["taproot_bip32_derivs" ][0 ]["path" ] == "m/86h/1h/0h/0/1"
87+ assert (
88+ psbt ["inputs" ][0 ]["taproot_bip32_derivs" ][0 ]["master_fingerprint" ] == "8c24a510"
89+ )
90+ assert psbt ["inputs" ][0 ]["taproot_bip32_derivs" ][0 ]["leaf_hashes" ] == []
91+ complete_pubkey = (
92+ "0274fea50d7f2a69489c2d2a146e317e02f47ad032e81b35fe6059e066670a100e"
93+ )
94+ assert (
95+ psbt ["inputs" ][0 ]["taproot_bip32_derivs" ][0 ]["pubkey" ] == complete_pubkey [2 :]
96+ ) # The pubkey is "xonly", for details: https://embit.rocks/#/api/ec/public_key?id=xonly
97+ # Output fields
98+ for output in psbt ["outputs" ]:
99+ if output ["change" ] == False :
100+ assert output ["address" ] == "bcrt1q7mlxxdna2e2ufzgalgp5zhtnndl7qddlxjy5eg"
101+ else :
102+ assert output ["taproot_bip32_derivs" ][0 ]["path" ] == "m/86h/1h/0h/1/0"
103+ assert (
104+ output ["taproot_bip32_derivs" ][0 ]["pubkey" ]
105+ == "85b747f5ffc1a1ff951790771c86b24725e283afb2d7e5b8392858bc04f5d05c"
106+ )
107+ assert (
108+ output ["taproot_bip32_derivs" ][0 ]["pubkey" ]
109+ == output ["taproot_internal_key" ]
110+ )
111+ assert output ["taproot_bip32_derivs" ][0 ]["leaf_hashes" ] == []
80112
81113
82114@pytest .mark .slow
0 commit comments