Skip to content

Commit 267a66a

Browse files
Merge pull request #254 from BitGo/BTC-0.remove-debug-err
refactor: improve error formatting in PSBT signing
2 parents c0ada5b + 2bd6287 commit 267a66a

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

packages/wasm-utxo/src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ impl WasmUtxoError {
4242
pub fn new(s: &str) -> WasmUtxoError {
4343
WasmUtxoError::StringError(s.to_string())
4444
}
45+
46+
pub fn from_errors<E: fmt::Display>(errors: impl IntoIterator<Item = E>) -> WasmUtxoError {
47+
let messages: Vec<String> = errors.into_iter().map(|e| e.to_string()).collect();
48+
WasmUtxoError::StringError(format!(
49+
"{} errors: {}",
50+
messages.len(),
51+
messages.join(", ")
52+
))
53+
}
4554
}
4655

4756
impl From<crate::address::AddressError> for WasmUtxoError {

packages/wasm-utxo/src/wasm/psbt.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,7 @@ impl WrapPsbt {
425425
let key = bip32::Xpriv::from_str(&xprv).map_err(|_| WasmUtxoError::new("Invalid xprv"))?;
426426
self.0
427427
.sign(&key, &Secp256k1::new())
428-
.map_err(|(_, errors)| {
429-
WasmUtxoError::new(&format!("{} errors: {:?}", errors.len(), errors))
430-
})
428+
.map_err(|(_, errors)| WasmUtxoError::from_errors(errors.into_values()))
431429
.and_then(|r| r.try_to_js_value())
432430
}
433431

@@ -437,9 +435,7 @@ impl WrapPsbt {
437435
let secp = Secp256k1::new();
438436
self.0
439437
.sign(&SingleKeySigner::from_privkey(privkey, &secp), &secp)
440-
.map_err(|(_r, errors)| {
441-
WasmUtxoError::new(&format!("{} errors: {:?}", errors.len(), errors))
442-
})
438+
.map_err(|(_r, errors)| WasmUtxoError::from_errors(errors.into_values()))
443439
.and_then(|r| r.try_to_js_value())
444440
}
445441

@@ -457,9 +453,7 @@ impl WrapPsbt {
457453
let xpriv = key.to_xpriv()?;
458454
self.0
459455
.sign(&xpriv, &Secp256k1::new())
460-
.map_err(|(_, errors)| {
461-
WasmUtxoError::new(&format!("{} errors: {:?}", errors.len(), errors))
462-
})
456+
.map_err(|(_, errors)| WasmUtxoError::from_errors(errors.into_values()))
463457
.and_then(|r| r.try_to_js_value())
464458
}
465459

@@ -479,9 +473,7 @@ impl WrapPsbt {
479473
let private_key = PrivateKey::new(privkey, miniscript::bitcoin::network::Network::Bitcoin);
480474
self.0
481475
.sign(&SingleKeySigner::from_privkey(private_key, &secp), &secp)
482-
.map_err(|(_r, errors)| {
483-
WasmUtxoError::new(&format!("{} errors: {:?}", errors.len(), errors))
484-
})
476+
.map_err(|(_r, errors)| WasmUtxoError::from_errors(errors.into_values()))
485477
.and_then(|r| r.try_to_js_value())
486478
}
487479

@@ -596,9 +588,7 @@ impl WrapPsbt {
596588
pub fn finalize_mut(&mut self) -> Result<(), WasmUtxoError> {
597589
self.0
598590
.finalize_mut(&Secp256k1::verification_only())
599-
.map_err(|vec_err| {
600-
WasmUtxoError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err))
601-
})
591+
.map_err(WasmUtxoError::from_errors)
602592
}
603593

604594
/// Extract the final transaction from a finalized PSBT

0 commit comments

Comments
 (0)