Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/wasm-utxo/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ impl WasmUtxoError {
pub fn new(s: &str) -> WasmUtxoError {
WasmUtxoError::StringError(s.to_string())
}

pub fn from_errors<E: fmt::Display>(errors: impl IntoIterator<Item = E>) -> WasmUtxoError {
let messages: Vec<String> = errors.into_iter().map(|e| e.to_string()).collect();
WasmUtxoError::StringError(format!(
"{} errors: {}",
messages.len(),
messages.join(", ")
))
}
}

impl From<crate::address::AddressError> for WasmUtxoError {
Expand Down
20 changes: 5 additions & 15 deletions packages/wasm-utxo/src/wasm/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,7 @@ impl WrapPsbt {
let key = bip32::Xpriv::from_str(&xprv).map_err(|_| WasmUtxoError::new("Invalid xprv"))?;
self.0
.sign(&key, &Secp256k1::new())
.map_err(|(_, errors)| {
WasmUtxoError::new(&format!("{} errors: {:?}", errors.len(), errors))
})
.map_err(|(_, errors)| WasmUtxoError::from_errors(errors.into_values()))
.and_then(|r| r.try_to_js_value())
}

Expand All @@ -437,9 +435,7 @@ impl WrapPsbt {
let secp = Secp256k1::new();
self.0
.sign(&SingleKeySigner::from_privkey(privkey, &secp), &secp)
.map_err(|(_r, errors)| {
WasmUtxoError::new(&format!("{} errors: {:?}", errors.len(), errors))
})
.map_err(|(_r, errors)| WasmUtxoError::from_errors(errors.into_values()))
.and_then(|r| r.try_to_js_value())
}

Expand All @@ -457,9 +453,7 @@ impl WrapPsbt {
let xpriv = key.to_xpriv()?;
self.0
.sign(&xpriv, &Secp256k1::new())
.map_err(|(_, errors)| {
WasmUtxoError::new(&format!("{} errors: {:?}", errors.len(), errors))
})
.map_err(|(_, errors)| WasmUtxoError::from_errors(errors.into_values()))
.and_then(|r| r.try_to_js_value())
}

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

Expand Down Expand Up @@ -596,9 +588,7 @@ impl WrapPsbt {
pub fn finalize_mut(&mut self) -> Result<(), WasmUtxoError> {
self.0
.finalize_mut(&Secp256k1::verification_only())
.map_err(|vec_err| {
WasmUtxoError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err))
})
.map_err(WasmUtxoError::from_errors)
}

/// Extract the final transaction from a finalized PSBT
Expand Down
Loading