44
55#![ allow( non_snake_case) ] // Test names intentionally use double underscore.
66#![ allow( unused_imports) ] // Because of feature gated tests.
7-
7+ use bitcoin :: address :: NetworkUnchecked ;
88use bitcoin:: consensus:: encode;
9+ use bitcoin:: hashes:: { hash160, sha256, Hash } ;
910use bitcoin:: hex:: FromHex as _;
11+ use bitcoin:: key:: { Secp256k1 , XOnlyPublicKey } ;
1012use bitcoin:: opcodes:: all:: * ;
13+ use bitcoin:: script:: Builder ;
1114use bitcoin:: {
12- absolute, consensus, hex, psbt, script, transaction, Amount , ScriptBuf , Transaction , TxOut ,
15+ absolute, consensus, hex, psbt, script, secp256k1, transaction, Address , Amount , Network ,
16+ PublicKey , ScriptBuf , Transaction , TxOut , WPubkeyHash , WScriptHash ,
1317} ;
1418use integration_test:: { Node , NodeExt as _, Wallet } ;
1519use node:: vtype:: * ;
1620use node:: { mtype, Input , Output } ; // All the version specific types.
21+ use rand:: Rng ;
1722
1823#[ test]
1924#[ cfg( not( feature = "v17" ) ) ] // analyzepsbt was added in v0.18.
@@ -201,18 +206,56 @@ fn raw_transactions__decode_script__modelled() {
201206 let node = Node :: with_wallet ( Wallet :: Default , & [ "-txindex" ] ) ;
202207 node. fund_wallet ( ) ;
203208
204- let p2pkh = arbitrary_p2pkh_script ( ) ;
205- let multi = arbitrary_multisig_script ( ) ;
206-
207- for script in & [ p2pkh, multi] {
209+ let test_cases: Vec < ( & str , ScriptBuf , Option < & str > ) > = vec ! [
210+ ( "p2pkh" , arbitrary_p2pkh_script( ) , Some ( "pubkeyhash" ) ) ,
211+ ( "multisig" , arbitrary_multisig_script( ) , Some ( "multisig" ) ) ,
212+ ( "p2sh" , arbitrary_p2sh_script( ) , Some ( "scripthash" ) ) ,
213+ ( "bare" , arbitrary_bare_script( ) , Some ( "nonstandard" ) ) ,
214+ ( "p2wpkh" , arbitrary_p2wpkh_script( ) , Some ( "witness_v0_keyhash" ) ) ,
215+ ( "p2wsh" , arbitrary_p2wsh_script( ) , Some ( "witness_v0_scripthash" ) ) ,
216+ ( "p2tr" , arbitrary_p2tr_script( ) , Some ( "witness_v1_taproot" ) ) ,
217+ ] ;
218+
219+ for ( label, script, expected_type) in test_cases {
208220 let hex = script. to_hex_string ( ) ;
209221
210222 let json: DecodeScript = node. client . decode_script ( & hex) . expect ( "decodescript" ) ;
211223 let model: Result < mtype:: DecodeScript , DecodeScriptError > = json. into_model ( ) ;
212- model. unwrap ( ) ;
224+ let decoded = model. expect ( "DecodeScript into model" ) ;
225+
226+ println ! ( "Decoded script ({label}): {:?}" , decoded) ;
227+
228+ if let Some ( expected) = expected_type {
229+ assert_eq ! ( decoded. type_, expected, "Unexpected script type for {label}" ) ;
230+ } else {
231+ println ! ( "Skipping type check for {}" , label) ;
232+ }
233+
234+ // Address should be present for standard scripts
235+ if expected_type != Some ( "nonstandard" ) {
236+ let has_any_address = !decoded. addresses . is_empty ( ) || decoded. address . is_some ( ) ;
237+ assert ! ( has_any_address, "Expected at least one address for {label}" ) ;
238+ }
213239 }
214240}
241+ fn arbitrary_p2sh_script ( ) -> ScriptBuf {
242+ let redeem_script = arbitrary_multisig_script ( ) ; // or arbitrary_p2pkh_script()
243+ let redeem_script_hash = hash160:: Hash :: hash ( redeem_script. as_bytes ( ) ) ;
215244
245+ script:: Builder :: new ( )
246+ . push_opcode ( bitcoin:: opcodes:: all:: OP_HASH160 )
247+ . push_slice ( redeem_script_hash. as_byte_array ( ) ) // [u8; 20]
248+ . push_opcode ( bitcoin:: opcodes:: all:: OP_EQUAL )
249+ . into_script ( )
250+ }
251+ fn arbitrary_bare_script ( ) -> ScriptBuf {
252+ script:: Builder :: new ( ) . push_opcode ( OP_RETURN ) . push_slice ( b"hello" ) . into_script ( )
253+ }
254+ fn arbitrary_pubkey ( ) -> PublicKey {
255+ let secp = Secp256k1 :: new ( ) ;
256+ let secret_key = secp256k1:: SecretKey :: from_slice ( & [ 1u8 ; 32 ] ) . unwrap ( ) ;
257+ PublicKey :: new ( secp256k1:: PublicKey :: from_secret_key ( & secp, & secret_key) )
258+ }
216259// Script builder code copied from rust-bitcoin script unit tests.
217260fn arbitrary_p2pkh_script ( ) -> ScriptBuf {
218261 let pubkey_hash = <[ u8 ; 20 ] >:: from_hex ( "16e1ae70ff0fa102905d4af297f6912bda6cce19" ) . unwrap ( ) ;
@@ -225,7 +268,6 @@ fn arbitrary_p2pkh_script() -> ScriptBuf {
225268 . push_opcode ( OP_CHECKSIG )
226269 . into_script ( )
227270}
228-
229271fn arbitrary_multisig_script ( ) -> ScriptBuf {
230272 let pk1 =
231273 <[ u8 ; 33 ] >:: from_hex ( "022afc20bf379bc96a2f4e9e63ffceb8652b2b6a097f63fbee6ecec2a49a48010e" )
@@ -244,6 +286,89 @@ fn arbitrary_multisig_script() -> ScriptBuf {
244286 . push_opcode ( OP_CHECKMULTISIG )
245287 . into_script ( )
246288}
289+ fn arbitrary_p2wpkh_script ( ) -> ScriptBuf {
290+ let pubkey = arbitrary_pubkey ( ) ;
291+ let pubkey_hash = hash160:: Hash :: hash ( & pubkey. to_bytes ( ) ) ;
292+
293+ // P2WPKH: 0 <20-byte pubkey hash>
294+ Builder :: new ( ) . push_int ( 0 ) . push_slice ( pubkey_hash. as_byte_array ( ) ) . into_script ( )
295+ }
296+
297+ fn arbitrary_p2wsh_script ( ) -> ScriptBuf {
298+ let redeem_script = arbitrary_multisig_script ( ) ; // any witness script
299+ let script_hash = sha256:: Hash :: hash ( redeem_script. as_bytes ( ) ) ;
300+
301+ // P2WSH: 0 <32-byte script hash>
302+ Builder :: new ( ) . push_int ( 0 ) . push_slice ( script_hash. as_byte_array ( ) ) . into_script ( )
303+ }
304+
305+ fn arbitrary_p2tr_script ( ) -> ScriptBuf {
306+ let secp = Secp256k1 :: new ( ) ;
307+ let sk = secp256k1:: SecretKey :: from_slice ( & [ 2u8 ; 32 ] ) . unwrap ( ) ;
308+ let internal_key = secp256k1:: PublicKey :: from_secret_key ( & secp, & sk) ;
309+ let x_only = XOnlyPublicKey :: from ( internal_key) ;
310+
311+ // Taproot output script: OP_1 <x-only pubkey>
312+ Builder :: new ( ) . push_int ( 1 ) . push_slice ( & x_only. serialize ( ) ) . into_script ( )
313+ }
314+
315+ #[ test]
316+ fn raw_transactions__decode_script_segwit__modelled ( ) {
317+ let node = Node :: with_wallet ( Wallet :: Default , & [ "-txindex" ] ) ;
318+ node. client . load_wallet ( "default" ) . ok ( ) ; // Ensure wallet is loaded
319+ node. fund_wallet ( ) ;
320+
321+ // Get a new address and script
322+ let address_unc = node
323+ . client
324+ . get_new_address ( None , None )
325+ . expect ( "getnewaddress" )
326+ . address ( )
327+ . expect ( "valid address string" ) ;
328+
329+ let address = address_unc. require_network ( Network :: Regtest ) . expect ( "must be regtest" ) ;
330+
331+ assert ! ( address. is_segwit( ) , "Expected SegWit address but got {:?}" , address) ;
332+
333+ let script = address. script_pubkey ( ) ;
334+ let hex = script. to_hex_string ( ) ;
335+
336+ // Decode script
337+ let json = node. client . decode_script ( & hex) . expect ( "decodescript" ) ;
338+ let model: Result < mtype:: DecodeScript , DecodeScriptError > = json. into_model ( ) ;
339+ let decoded = model. expect ( "DecodeScript into model" ) ;
340+
341+ let segwit = decoded. segwit . as_ref ( ) . expect ( "Expected segwit field to be present" ) ;
342+
343+ assert_eq ! ( segwit. hex, script, "Segwit hex does not match script" ) ;
344+
345+ // Extract the type field
346+ let script_type =
347+ decoded. segwit . as_ref ( ) . map ( |s| s. type_ . as_str ( ) ) . unwrap_or_else ( || decoded. type_ . as_str ( ) ) ;
348+
349+ assert_eq ! ( script_type, "witness_v0_keyhash" , "Expected script type to be witness_v0_keyhash" ) ;
350+
351+ // Compare hex from segwit
352+ let decoded_hex = decoded
353+ . segwit
354+ . as_ref ( )
355+ . map ( |s| & s. hex )
356+ . unwrap_or_else ( || panic ! ( "Expected segwit hex to be present" ) ) ;
357+
358+ assert_eq ! ( * decoded_hex, script, "Script hex does not match" ) ;
359+
360+ // Compare addresses from segwit or fallback
361+ let address_unc_check = address. into_unchecked ( ) ;
362+ let segwit_addresses =
363+ decoded. segwit . as_ref ( ) . map ( |s| & s. addresses ) . unwrap_or ( & decoded. addresses ) ;
364+
365+ assert ! (
366+ segwit_addresses. iter( ) . any( |a| a == & address_unc_check) ,
367+ "Expected address {:?} in segwit.addresses or top-level addresses: {:?}" ,
368+ address_unc_check,
369+ segwit_addresses
370+ ) ;
371+ }
247372
248373#[ test]
249374fn raw_transactions__finalize_psbt__modelled ( ) {
0 commit comments