Skip to content

Commit 35f0d3c

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(wasm-utxo): rename error type from WasmMiniscriptError to WasmUtxoError
This change renames the main error type to better align with the package name. The previous name was a remnant from when this was primarily a miniscript wrapper, but now it's a more general UTXO handling library. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent bbc3098 commit 35f0d3c

File tree

10 files changed

+172
-202
lines changed

10 files changed

+172
-202
lines changed

packages/wasm-utxo/src/descriptor.rs

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::error::WasmMiniscriptError;
1+
use crate::error::WasmUtxoError;
22
use crate::try_into_js_value::TryIntoJsValue;
33
use miniscript::bitcoin::secp256k1::{Secp256k1, Signing};
44
use miniscript::bitcoin::ScriptBuf;
@@ -19,7 +19,7 @@ pub struct WrapDescriptor(pub(crate) WrapDescriptorEnum);
1919

2020
#[wasm_bindgen]
2121
impl WrapDescriptor {
22-
pub fn node(&self) -> Result<JsValue, WasmMiniscriptError> {
22+
pub fn node(&self) -> Result<JsValue, WasmUtxoError> {
2323
Ok(match &self.0 {
2424
WrapDescriptorEnum::Derivable(desc, _) => desc.try_to_js_value()?,
2525
WrapDescriptorEnum::Definite(desc) => desc.try_to_js_value()?,
@@ -43,20 +43,20 @@ impl WrapDescriptor {
4343
}
4444

4545
#[wasm_bindgen(js_name = atDerivationIndex)]
46-
pub fn at_derivation_index(&self, index: u32) -> Result<WrapDescriptor, WasmMiniscriptError> {
46+
pub fn at_derivation_index(&self, index: u32) -> Result<WrapDescriptor, WasmUtxoError> {
4747
match &self.0 {
4848
WrapDescriptorEnum::Derivable(desc, _keys) => {
4949
let d = desc.at_derivation_index(index)?;
5050
Ok(WrapDescriptor(WrapDescriptorEnum::Definite(d)))
5151
}
52-
_ => Err(WasmMiniscriptError::new(
52+
_ => Err(WasmUtxoError::new(
5353
"Cannot derive from a definite descriptor",
5454
)),
5555
}
5656
}
5757

5858
#[wasm_bindgen(js_name = descType)]
59-
pub fn desc_type(&self) -> Result<JsValue, WasmMiniscriptError> {
59+
pub fn desc_type(&self) -> Result<JsValue, WasmUtxoError> {
6060
(match &self.0 {
6161
WrapDescriptorEnum::Derivable(desc, _) => desc.desc_type(),
6262
WrapDescriptorEnum::Definite(desc) => desc.desc_type(),
@@ -66,38 +66,36 @@ impl WrapDescriptor {
6666
}
6767

6868
#[wasm_bindgen(js_name = scriptPubkey)]
69-
pub fn script_pubkey(&self) -> Result<Vec<u8>, WasmMiniscriptError> {
69+
pub fn script_pubkey(&self) -> Result<Vec<u8>, WasmUtxoError> {
7070
match &self.0 {
7171
WrapDescriptorEnum::Definite(desc) => Ok(desc.script_pubkey().to_bytes()),
72-
_ => Err(WasmMiniscriptError::new(
73-
"Cannot encode a derivable descriptor",
74-
)),
72+
_ => Err(WasmUtxoError::new("Cannot encode a derivable descriptor")),
7573
}
7674
}
7775

78-
fn explicit_script(&self) -> Result<ScriptBuf, WasmMiniscriptError> {
76+
fn explicit_script(&self) -> Result<ScriptBuf, WasmUtxoError> {
7977
match &self.0 {
8078
WrapDescriptorEnum::Definite(desc) => Ok(desc.explicit_script()?),
81-
WrapDescriptorEnum::Derivable(_, _) => Err(WasmMiniscriptError::new(
82-
"Cannot encode a derivable descriptor",
83-
)),
84-
WrapDescriptorEnum::String(_) => Err(WasmMiniscriptError::new(
85-
"Cannot encode a string descriptor",
86-
)),
79+
WrapDescriptorEnum::Derivable(_, _) => {
80+
Err(WasmUtxoError::new("Cannot encode a derivable descriptor"))
81+
}
82+
WrapDescriptorEnum::String(_) => {
83+
Err(WasmUtxoError::new("Cannot encode a string descriptor"))
84+
}
8785
}
8886
}
8987

90-
pub fn encode(&self) -> Result<Vec<u8>, WasmMiniscriptError> {
88+
pub fn encode(&self) -> Result<Vec<u8>, WasmUtxoError> {
9189
Ok(self.explicit_script()?.to_bytes())
9290
}
9391

9492
#[wasm_bindgen(js_name = toAsmString)]
95-
pub fn to_asm_string(&self) -> Result<String, WasmMiniscriptError> {
93+
pub fn to_asm_string(&self) -> Result<String, WasmUtxoError> {
9694
Ok(self.explicit_script()?.to_asm_string())
9795
}
9896

9997
#[wasm_bindgen(js_name = maxWeightToSatisfy)]
100-
pub fn max_weight_to_satisfy(&self) -> Result<u32, WasmMiniscriptError> {
98+
pub fn max_weight_to_satisfy(&self) -> Result<u32, WasmUtxoError> {
10199
let weight = (match &self.0 {
102100
WrapDescriptorEnum::Derivable(desc, _) => desc.max_weight_to_satisfy(),
103101
WrapDescriptorEnum::Definite(desc) => desc.max_weight_to_satisfy(),
@@ -106,18 +104,18 @@ impl WrapDescriptor {
106104
weight
107105
.to_wu()
108106
.try_into()
109-
.map_err(|_| WasmMiniscriptError::new("Weight exceeds u32"))
107+
.map_err(|_| WasmUtxoError::new("Weight exceeds u32"))
110108
}
111109

112110
fn from_string_derivable<C: Signing>(
113111
secp: &Secp256k1<C>,
114112
descriptor: &str,
115-
) -> Result<WrapDescriptor, WasmMiniscriptError> {
113+
) -> Result<WrapDescriptor, WasmUtxoError> {
116114
let (desc, keys) = Descriptor::parse_descriptor(secp, descriptor)?;
117115
Ok(WrapDescriptor(WrapDescriptorEnum::Derivable(desc, keys)))
118116
}
119117

120-
fn from_string_definite(descriptor: &str) -> Result<WrapDescriptor, WasmMiniscriptError> {
118+
fn from_string_definite(descriptor: &str) -> Result<WrapDescriptor, WasmUtxoError> {
121119
let desc = Descriptor::<DefiniteDescriptorKey>::from_str(descriptor)?;
122120
Ok(WrapDescriptor(WrapDescriptorEnum::Definite(desc)))
123121
}
@@ -135,7 +133,7 @@ impl WrapDescriptor {
135133
/// - "string": For descriptors with string placeholders
136134
///
137135
/// # Returns
138-
/// * `Result<WrapDescriptor, WasmMiniscriptError>` - The parsed descriptor or an error
136+
/// * `Result<WrapDescriptor, WasmUtxoError>` - The parsed descriptor or an error
139137
///
140138
/// # Example
141139
/// ```
@@ -145,18 +143,15 @@ impl WrapDescriptor {
145143
/// );
146144
/// ```
147145
#[wasm_bindgen(js_name = fromString, skip_typescript)]
148-
pub fn from_string(
149-
descriptor: &str,
150-
pk_type: &str,
151-
) -> Result<WrapDescriptor, WasmMiniscriptError> {
146+
pub fn from_string(descriptor: &str, pk_type: &str) -> Result<WrapDescriptor, WasmUtxoError> {
152147
match pk_type {
153148
"derivable" => WrapDescriptor::from_string_derivable(&Secp256k1::new(), descriptor),
154149
"definite" => WrapDescriptor::from_string_definite(descriptor),
155150
"string" => {
156151
let desc = Descriptor::<String>::from_str(descriptor)?;
157152
Ok(WrapDescriptor(WrapDescriptorEnum::String(desc)))
158153
}
159-
_ => Err(WasmMiniscriptError::new("Invalid descriptor type")),
154+
_ => Err(WasmUtxoError::new("Invalid descriptor type")),
160155
}
161156
}
162157

@@ -168,7 +163,7 @@ impl WrapDescriptor {
168163
/// * `descriptor` - A string containing the descriptor to parse
169164
///
170165
/// # Returns
171-
/// * `Result<WrapDescriptor, WasmMiniscriptError>` - The parsed descriptor or an error
166+
/// * `Result<WrapDescriptor, WasmUtxoError>` - The parsed descriptor or an error
172167
///
173168
/// # Example
174169
/// ```
@@ -183,12 +178,10 @@ impl WrapDescriptor {
183178
/// );
184179
/// ```
185180
#[wasm_bindgen(js_name = fromStringDetectType, skip_typescript)]
186-
pub fn from_string_detect_type(
187-
descriptor: &str,
188-
) -> Result<WrapDescriptor, WasmMiniscriptError> {
181+
pub fn from_string_detect_type(descriptor: &str) -> Result<WrapDescriptor, WasmUtxoError> {
189182
let secp = Secp256k1::new();
190183
let (descriptor, _key_map) = Descriptor::parse_descriptor(&secp, descriptor)
191-
.map_err(|_| WasmMiniscriptError::new("Invalid descriptor"))?;
184+
.map_err(|_| WasmUtxoError::new("Invalid descriptor"))?;
192185
if descriptor.has_wildcard() {
193186
WrapDescriptor::from_string_derivable(&secp, &descriptor.to_string())
194187
} else {
@@ -208,7 +201,7 @@ impl fmt::Display for WrapDescriptor {
208201
}
209202

210203
impl FromStr for WrapDescriptor {
211-
type Err = WasmMiniscriptError;
204+
type Err = WasmUtxoError;
212205
fn from_str(s: &str) -> Result<Self, Self::Err> {
213206
WrapDescriptor::from_string_detect_type(s)
214207
}

packages/wasm-utxo/src/error.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
use core::fmt;
22

33
#[derive(Debug, Clone)]
4-
pub enum WasmMiniscriptError {
4+
pub enum WasmUtxoError {
55
StringError(String),
66
}
77

8-
impl std::error::Error for WasmMiniscriptError {}
9-
impl fmt::Display for WasmMiniscriptError {
8+
impl std::error::Error for WasmUtxoError {}
9+
impl fmt::Display for WasmUtxoError {
1010
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1111
match self {
12-
WasmMiniscriptError::StringError(s) => write!(f, "{}", s),
12+
WasmUtxoError::StringError(s) => write!(f, "{}", s),
1313
}
1414
}
1515
}
1616

17-
impl From<&str> for WasmMiniscriptError {
17+
impl From<&str> for WasmUtxoError {
1818
fn from(s: &str) -> Self {
19-
WasmMiniscriptError::StringError(s.to_string())
19+
WasmUtxoError::StringError(s.to_string())
2020
}
2121
}
2222

23-
impl From<String> for WasmMiniscriptError {
23+
impl From<String> for WasmUtxoError {
2424
fn from(s: String) -> Self {
25-
WasmMiniscriptError::StringError(s)
25+
WasmUtxoError::StringError(s)
2626
}
2727
}
2828

29-
impl From<miniscript::Error> for WasmMiniscriptError {
29+
impl From<miniscript::Error> for WasmUtxoError {
3030
fn from(err: miniscript::Error) -> Self {
31-
WasmMiniscriptError::StringError(err.to_string())
31+
WasmUtxoError::StringError(err.to_string())
3232
}
3333
}
3434

35-
impl From<miniscript::descriptor::ConversionError> for WasmMiniscriptError {
35+
impl From<miniscript::descriptor::ConversionError> for WasmUtxoError {
3636
fn from(err: miniscript::descriptor::ConversionError) -> Self {
37-
WasmMiniscriptError::StringError(err.to_string())
37+
WasmUtxoError::StringError(err.to_string())
3838
}
3939
}
4040

41-
impl WasmMiniscriptError {
42-
pub fn new(s: &str) -> WasmMiniscriptError {
43-
WasmMiniscriptError::StringError(s.to_string())
41+
impl WasmUtxoError {
42+
pub fn new(s: &str) -> WasmUtxoError {
43+
WasmUtxoError::StringError(s.to_string())
4444
}
4545
}
4646

47-
impl From<crate::address::AddressError> for WasmMiniscriptError {
47+
impl From<crate::address::AddressError> for WasmUtxoError {
4848
fn from(err: crate::address::AddressError) -> Self {
49-
WasmMiniscriptError::StringError(err.to_string())
49+
WasmUtxoError::StringError(err.to_string())
5050
}
5151
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::str::FromStr;
22

33
use crate::bitcoin::bip32::Xpub;
4-
use crate::error::WasmMiniscriptError;
4+
use crate::error::WasmUtxoError;
55
use crate::try_from_js_value::{get_buffer_field, get_field, get_nested_field};
66
use wasm_bindgen::JsValue;
77

8-
fn try_xpub_from_bip32_properties(bip32_key: &JsValue) -> Result<Xpub, WasmMiniscriptError> {
8+
fn try_xpub_from_bip32_properties(bip32_key: &JsValue) -> Result<Xpub, WasmUtxoError> {
99
// Extract properties using helper functions
1010
let version: u32 = get_nested_field(bip32_key, "network.bip32.public")?;
1111
let depth: u8 = get_field(bip32_key, "depth")?;
@@ -24,33 +24,32 @@ fn try_xpub_from_bip32_properties(bip32_key: &JsValue) -> Result<Xpub, WasmMinis
2424
data.extend_from_slice(&public_key_bytes); // 33 bytes: public key
2525

2626
// Use the Xpub::decode method which properly handles network detection and constructs the Xpub
27-
Xpub::decode(&data)
28-
.map_err(|e| WasmMiniscriptError::new(&format!("Failed to decode xpub: {}", e)))
27+
Xpub::decode(&data).map_err(|e| WasmUtxoError::new(&format!("Failed to decode xpub: {}", e)))
2928
}
3029

31-
fn xpub_from_base58_method(bip32_key: &JsValue) -> Result<Xpub, WasmMiniscriptError> {
30+
fn xpub_from_base58_method(bip32_key: &JsValue) -> Result<Xpub, WasmUtxoError> {
3231
// Fallback: Call toBase58() method on BIP32Interface
3332
let to_base58 = js_sys::Reflect::get(bip32_key, &JsValue::from_str("toBase58"))
34-
.map_err(|_| WasmMiniscriptError::new("Failed to get 'toBase58' method"))?;
33+
.map_err(|_| WasmUtxoError::new("Failed to get 'toBase58' method"))?;
3534

3635
if !to_base58.is_function() {
37-
return Err(WasmMiniscriptError::new("'toBase58' is not a function"));
36+
return Err(WasmUtxoError::new("'toBase58' is not a function"));
3837
}
3938

4039
let to_base58_fn = js_sys::Function::from(to_base58);
4140
let xpub_str = to_base58_fn
4241
.call0(bip32_key)
43-
.map_err(|_| WasmMiniscriptError::new("Failed to call 'toBase58'"))?;
42+
.map_err(|_| WasmUtxoError::new("Failed to call 'toBase58'"))?;
4443

4544
let xpub_string = xpub_str
4645
.as_string()
47-
.ok_or_else(|| WasmMiniscriptError::new("'toBase58' did not return a string"))?;
46+
.ok_or_else(|| WasmUtxoError::new("'toBase58' did not return a string"))?;
4847

4948
Xpub::from_str(&xpub_string)
50-
.map_err(|e| WasmMiniscriptError::new(&format!("Failed to parse xpub: {}", e)))
49+
.map_err(|e| WasmUtxoError::new(&format!("Failed to parse xpub: {}", e)))
5150
}
5251

53-
pub fn xpub_from_bip32interface(bip32_key: &JsValue) -> Result<Xpub, WasmMiniscriptError> {
52+
pub fn xpub_from_bip32interface(bip32_key: &JsValue) -> Result<Xpub, WasmUtxoError> {
5453
// Try to construct from properties first, fall back to toBase58() if that fails
5554
try_xpub_from_bip32_properties(bip32_key).or_else(|_| xpub_from_base58_method(bip32_key))
5655
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use wallet_scripts::*;
1313
use wasm_bindgen::prelude::*;
1414

1515
use crate::address::networks::AddressFormat;
16-
use crate::error::WasmMiniscriptError;
16+
use crate::error::WasmUtxoError;
1717
use crate::try_from_js_value::TryFromJsValue;
1818
use crate::utxolib_compat::UtxolibNetwork;
1919

@@ -28,10 +28,10 @@ impl FixedScriptWalletNamespace {
2828
chain: u32,
2929
index: u32,
3030
network: JsValue,
31-
) -> Result<Vec<u8>, WasmMiniscriptError> {
31+
) -> Result<Vec<u8>, WasmUtxoError> {
3232
let network = UtxolibNetwork::try_from_js_value(&network)?;
3333
let chain = Chain::try_from(chain)
34-
.map_err(|e| WasmMiniscriptError::new(&format!("Invalid chain: {}", e)))?;
34+
.map_err(|e| WasmUtxoError::new(&format!("Invalid chain: {}", e)))?;
3535

3636
let wallet_keys = RootWalletKeys::from_jsvalue(&keys)?;
3737
let scripts = WalletScripts::from_wallet_keys(
@@ -50,11 +50,11 @@ impl FixedScriptWalletNamespace {
5050
index: u32,
5151
network: JsValue,
5252
address_format: Option<String>,
53-
) -> Result<String, WasmMiniscriptError> {
53+
) -> Result<String, WasmUtxoError> {
5454
let network = UtxolibNetwork::try_from_js_value(&network)?;
5555
let wallet_keys = RootWalletKeys::from_jsvalue(&keys)?;
5656
let chain = Chain::try_from(chain)
57-
.map_err(|e| WasmMiniscriptError::new(&format!("Invalid chain: {}", e)))?;
57+
.map_err(|e| WasmUtxoError::new(&format!("Invalid chain: {}", e)))?;
5858
let scripts = WalletScripts::from_wallet_keys(
5959
&wallet_keys,
6060
chain,
@@ -63,13 +63,13 @@ impl FixedScriptWalletNamespace {
6363
)?;
6464
let script = scripts.output_script();
6565
let address_format = AddressFormat::from_optional_str(address_format.as_deref())
66-
.map_err(|e| WasmMiniscriptError::new(&format!("Invalid address format: {}", e)))?;
66+
.map_err(|e| WasmUtxoError::new(&format!("Invalid address format: {}", e)))?;
6767
let address = crate::address::utxolib_compat::from_output_script_with_network(
6868
&script,
6969
&network,
7070
address_format,
7171
)
72-
.map_err(|e| WasmMiniscriptError::new(&format!("Failed to generate address: {}", e)))?;
72+
.map_err(|e| WasmUtxoError::new(&format!("Failed to generate address: {}", e)))?;
7373
Ok(address.to_string())
7474
}
7575
}

0 commit comments

Comments
 (0)