Skip to content

Commit 73800e5

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): add to_bitgo_psbt helper method for PsbtFixture
Add a convenience method to convert a PsbtFixture directly to a BitGoPsbt, removing the need for duplicate base64 decoding and deserialization logic. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent 52ad8dc commit 73800e5

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,13 @@ mod tests {
140140
use super::*;
141141
use crate::fixed_script_wallet::{RootWalletKeys, WalletScripts};
142142
use crate::test_utils::fixtures;
143-
use crate::{error::WasmUtxoError, fixed_script_wallet::Chain};
143+
use crate::fixed_script_wallet::Chain;
144144
use base64::engine::{general_purpose::STANDARD as BASE64_STANDARD, Engine};
145145
use miniscript::bitcoin::bip32::Xpub;
146146
use miniscript::bitcoin::consensus::Decodable;
147147
use miniscript::bitcoin::Transaction;
148148
use std::str::FromStr;
149149

150-
fn psbt_from_base64_with_network(
151-
s: &str,
152-
network: Network,
153-
) -> Result<BitGoPsbt, WasmUtxoError> {
154-
let psbt = BASE64_STANDARD
155-
.decode(s.as_bytes())
156-
.map_err(|_| WasmUtxoError::new("Invalid base64"))?;
157-
BitGoPsbt::deserialize(&psbt, network)
158-
.map_err(|e| WasmUtxoError::new(&format!("Invalid PSBT: {}", e)))
159-
}
160-
161150
crate::test_all_networks!(test_deserialize_invalid_bytes, network, {
162151
// Invalid PSBT bytes should fail with either consensus, PSBT, or network error
163152
let result = BitGoPsbt::deserialize(&[0x00], network);
@@ -181,7 +170,7 @@ mod tests {
181170
format,
182171
)
183172
.unwrap();
184-
match psbt_from_base64_with_network(&fixture.psbt_base64, network) {
173+
match fixture.to_bitgo_psbt(network) {
185174
Ok(_) => {}
186175
Err(e) => panic!("Failed on network: {:?} with error: {:?}", network, e),
187176
}
@@ -550,11 +539,7 @@ mod tests {
550539
fixtures::SignatureState::Unsigned,
551540
)
552541
.unwrap();
553-
let original_bytes = BASE64_STANDARD
554-
.decode(&fixture.psbt_base64)
555-
.expect("Failed to decode base64");
556-
let psbt = BitGoPsbt::deserialize(&original_bytes, Network::Bitcoin)
557-
.expect("Failed to deserialize PSBT");
542+
let psbt = fixture.to_bitgo_psbt(Network::Bitcoin).expect("Failed to convert to BitGo PSBT");
558543

559544
// Serialize should succeed
560545
let serialized = psbt.serialize();

packages/wasm-utxo/src/fixed_script_wallet/test_utils/fixtures.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,21 @@ pub struct PsbtFixture {
256256
pub psbt_outputs: Vec<PsbtOutputFixture>,
257257
}
258258

259-
// Output script fixture types
259+
impl PsbtFixture {
260+
pub fn to_bitgo_psbt(
261+
&self,
262+
network: Network,
263+
) -> Result<crate::bitgo_psbt::BitGoPsbt, Box<dyn std::error::Error>> {
264+
use base64::engine::{general_purpose::STANDARD as BASE64_STANDARD, Engine};
265+
let psbt = crate::bitgo_psbt::BitGoPsbt::deserialize(
266+
&BASE64_STANDARD.decode(&self.psbt_base64)?,
267+
network,
268+
)?;
269+
Ok(psbt)
270+
}
271+
}
260272

273+
// Output script fixture types
261274
#[derive(Debug, Clone, Deserialize, Serialize)]
262275
#[serde(rename_all = "camelCase")]
263276
pub struct ControlBlockEntry {

0 commit comments

Comments
 (0)