Skip to content

Commit 5a2890d

Browse files
author
+Sharon
committed
Add model test for decode_script with P2WPKH SegWit output
1 parent d9f5d2f commit 5a2890d

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

integration_test/tests/raw_transactions.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,51 @@ fn raw_transactions__decode_script__modelled() {
214214
}
215215
}
216216

217+
#[test]
218+
fn raw_transactions__decode_script_segwit__modelled() {
219+
use bitcoin::{Address, Network};
220+
use bitcoin::address::NetworkUnchecked;
221+
let node = Node::with_wallet(Wallet::Default, &["-txindex"]);
222+
// Ensure wallet is loaded (ignore if already loaded)
223+
node.client.load_wallet("default").ok();
224+
node.fund_wallet();
225+
// Get the address from the RPC result
226+
let address_unc = node
227+
.client
228+
.get_new_address(None, None)
229+
.expect("getnewaddress")
230+
.address()
231+
.expect("valid address string");
232+
// address_unc is already Address<NetworkUnchecked>
233+
// Convert to Address<Network> for script_pubkey
234+
let address = address_unc
235+
.require_network(Network::Regtest)
236+
.expect("must be regtest");
237+
let script = address.script_pubkey();
238+
let hex = script.to_hex_string();
239+
// Decode and model
240+
let json = node.client.decode_script(&hex).expect("decodescript");
241+
let model: Result<mtype::DecodeScript, DecodeScriptError> = json.into_model();
242+
let decoded = model.expect("DecodeScript into model");
243+
244+
let segwit = decoded
245+
.segwit
246+
.expect("Expected DecodeScriptSegwit present for P2WPKH");
247+
// Assertions
248+
assert!(segwit.asm.contains("OP_"));
249+
assert_eq!(segwit.hex, script);
250+
assert_eq!(segwit.type_, "witness_v0_keyhash");
251+
// Convert `Address<Network>` back to unchecked for comparison
252+
let address_unc_check = address.into_unchecked();
253+
254+
assert!(
255+
segwit.addresses.iter().any(|a| a == &address_unc_check),
256+
"Expected address {:?} in segwit.addresses: {:?}",
257+
address_unc_check,
258+
segwit.addresses
259+
);
260+
}
261+
217262
// Script builder code copied from rust-bitcoin script unit tests.
218263
fn arbitrary_p2pkh_script() -> ScriptBuf {
219264
let pubkey_hash = <[u8; 20]>::from_hex("16e1ae70ff0fa102905d4af297f6912bda6cce19").unwrap();

types/src/v17/raw_transactions/into.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use bitcoin::{
1111
use super::{
1212
CombinePsbt, CombineRawTransaction, ConvertToPsbt, CreatePsbt, CreateRawTransaction,
1313
DecodePsbt, DecodePsbtError, DecodeRawTransaction, DecodeScript, DecodeScriptError,
14-
FinalizePsbt, FinalizePsbtError, FundRawTransaction, FundRawTransactionError,
15-
GetRawTransaction, GetRawTransactionVerbose, GetRawTransactionVerboseError, MempoolAcceptance,
16-
PsbtInput, PsbtInputError, PsbtOutput, PsbtOutputError, SendRawTransaction, SignFail,
17-
SignFailError, SignRawTransaction, SignRawTransactionError, TestMempoolAccept,DecodeScriptSegwit,
18-
DecodeScriptSegwitError,
14+
DecodeScriptSegwit, DecodeScriptSegwitError, FinalizePsbt, FinalizePsbtError,
15+
FundRawTransaction, FundRawTransactionError, GetRawTransaction, GetRawTransactionVerbose,
16+
GetRawTransactionVerboseError, MempoolAcceptance, PsbtInput, PsbtInputError, PsbtOutput,
17+
PsbtOutputError, SendRawTransaction, SignFail, SignFailError, SignRawTransaction,
18+
SignRawTransactionError, TestMempoolAccept,
1919
};
2020
use crate::model;
2121
use crate::psbt::RawTransactionError;
@@ -338,7 +338,7 @@ impl DecodeScriptSegwit {
338338
asm: self.asm,
339339
hex: self.hex,
340340
descriptor: None,
341-
address:None,
341+
address: None,
342342
type_: self.type_,
343343
required_signatures,
344344
addresses,

types/src/v22/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ pub use self::{
256256
control::Logging,
257257
network::{Banned, GetPeerInfo, ListBanned},
258258
raw_transactions::{
259-
DecodeScript, DecodeScriptError,DecodeScriptSegwit, DecodeScriptSegwitError},
259+
DecodeScript, DecodeScriptError, DecodeScriptSegwit, DecodeScriptSegwitError,
260+
},
260261
signer::EnumerateSigners,
261262
wallet::{ListDescriptors, WalletDisplayAddress},
262263
};

types/src/v23/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub use self::{
253253
network::GetPeerInfo,
254254
raw_transactions::{
255255
DecodePsbt, DecodePsbtError, DecodeScript, DecodeScriptError, DecodeScriptSegwit,
256-
GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
256+
GlobalXpub, Proprietary, PsbtInput, PsbtOutput,
257257
},
258258
util::CreateMultisig,
259259
wallet::{GetTransaction, GetTransactionError, RestoreWallet},

types/src/v23/raw_transactions/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,4 @@ impl std::error::Error for DecodeScriptError {
299299
E::P2sh(ref e) => Some(e),
300300
}
301301
}
302-
}
302+
}

0 commit comments

Comments
 (0)