Skip to content

Commit efa1841

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): add BitGoPsbt.into_psbt method to convert to standard Psbt
The method allows for extracting a standard Psbt from either Bitcoin-like or Zcash PSBTs, which simplifies transaction finalization. Tests verify that extracted transactions match expected hex values. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent d35b1df commit efa1841

File tree

1 file changed

+38
-2
lines changed
  • packages/wasm-utxo/src/bitgo_psbt

1 file changed

+38
-2
lines changed

packages/wasm-utxo/src/bitgo_psbt/mod.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,26 @@ impl BitGoPsbt {
133133
BitGoPsbt::Zcash(zcash_psbt, _network) => Ok(zcash_psbt.serialize()?),
134134
}
135135
}
136+
137+
pub fn into_psbt(self) -> Psbt {
138+
match self {
139+
BitGoPsbt::BitcoinLike(psbt, _network) => psbt,
140+
BitGoPsbt::Zcash(zcash_psbt, _network) => zcash_psbt.into_bitcoin_psbt(),
141+
}
142+
}
136143
}
137144

138145
#[cfg(test)]
139146
mod tests {
140147
use super::*;
148+
use crate::fixed_script_wallet::Chain;
141149
use crate::fixed_script_wallet::{RootWalletKeys, WalletScripts};
142150
use crate::test_utils::fixtures;
143-
use crate::fixed_script_wallet::Chain;
144151
use base64::engine::{general_purpose::STANDARD as BASE64_STANDARD, Engine};
145152
use miniscript::bitcoin::bip32::Xpub;
146153
use miniscript::bitcoin::consensus::Decodable;
147154
use miniscript::bitcoin::Transaction;
155+
148156
use std::str::FromStr;
149157

150158
crate::test_all_networks!(test_deserialize_invalid_bytes, network, {
@@ -531,6 +539,32 @@ mod tests {
531539
}
532540
);
533541

542+
crate::test_psbt_fixtures!(test_extract_transaction, network, {
543+
let fixture = fixtures::load_psbt_fixture_with_format(
544+
network.to_utxolib_name(),
545+
fixtures::SignatureState::Fullsigned,
546+
fixtures::TxFormat::Psbt,
547+
)
548+
.expect("Failed to load fixture");
549+
let psbt = fixture
550+
.to_bitgo_psbt(network)
551+
.expect("Failed to convert to BitGo PSBT");
552+
let fixture_extracted_transaction = fixture
553+
.extracted_transaction
554+
.expect("Failed to extract transaction");
555+
let extracted_transaction = psbt
556+
.into_psbt()
557+
.finalize(&crate::bitcoin::secp256k1::Secp256k1::verification_only())
558+
.expect("Failed to finalize PSBT")
559+
.extract_tx()
560+
.expect("Failed to extract transaction");
561+
let extracted_transaction_hex = hex::encode(serialize(&extracted_transaction));
562+
assert_eq!(
563+
extracted_transaction_hex, fixture_extracted_transaction,
564+
"Extracted transaction should match"
565+
);
566+
});
567+
534568
#[test]
535569
fn test_serialize_bitcoin_psbt() {
536570
// Test that Bitcoin-like PSBTs can be serialized
@@ -539,7 +573,9 @@ mod tests {
539573
fixtures::SignatureState::Unsigned,
540574
)
541575
.unwrap();
542-
let psbt = fixture.to_bitgo_psbt(Network::Bitcoin).expect("Failed to convert to BitGo PSBT");
576+
let psbt = fixture
577+
.to_bitgo_psbt(Network::Bitcoin)
578+
.expect("Failed to convert to BitGo PSBT");
543579

544580
// Serialize should succeed
545581
let serialized = psbt.serialize();

0 commit comments

Comments
 (0)