Skip to content

Commit baa90b9

Browse files
feat: add descType method to WrapDescriptor
Issue: BTC-1451
1 parent 2f2b137 commit baa90b9

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/wasm-miniscript/src/descriptor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ impl WrapDescriptor {
5454
_ => Err(JsError::new("Cannot derive from a definite descriptor")),
5555
}
5656
}
57+
58+
#[wasm_bindgen(js_name = descType)]
59+
pub fn desc_type(&self) -> Result<JsValue, JsError> {
60+
(match &self.0 {
61+
WrapDescriptorEnum::Derivable(desc, _) => desc.desc_type(),
62+
WrapDescriptorEnum::Definite(desc) => desc.desc_type(),
63+
WrapDescriptorEnum::String(desc) => desc.desc_type(),
64+
}).try_to_js_value()
65+
}
5766

5867
#[wasm_bindgen(js_name = scriptPubkey)]
5968
pub fn script_pubkey(&self) -> Result<Vec<u8>, JsError> {

packages/wasm-miniscript/src/try_into_js_value.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use wasm_bindgen::{JsError, JsValue};
22
use js_sys::Array;
33
use miniscript::{AbsLockTime, DefiniteDescriptorKey, Descriptor, DescriptorPublicKey, hash256, Miniscript, MiniscriptKey, RelLockTime, ScriptContext, Terminal, Threshold};
4-
use miniscript::descriptor::{ShInner, SortedMultiVec, TapTree, Tr, WshInner};
4+
use miniscript::descriptor::{DescriptorType, ShInner, SortedMultiVec, TapTree, Tr, WshInner};
55
use miniscript::bitcoin::{PublicKey, XOnlyPublicKey};
66
use miniscript::bitcoin::hashes::{hash160, ripemd160};
77
use std::sync::Arc;
@@ -245,4 +245,11 @@ impl<Pk: MiniscriptKey + TryIntoJsValue> TryIntoJsValue for Descriptor<Pk> {
245245
Descriptor::Tr(v) => js_obj!("Tr" => v),
246246
}
247247
}
248+
}
249+
250+
impl TryIntoJsValue for DescriptorType {
251+
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
252+
let str_from_enum = format!("{:?}", self);
253+
Ok(JsValue::from_str(&str_from_enum))
254+
}
248255
}

packages/wasm-miniscript/test/test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ describe("Descriptor fixtures", function () {
6060
}
6161

6262
assert.ok(Number.isInteger(descriptor.maxWeightToSatisfy()));
63+
64+
switch (descriptor.descType()) {
65+
case "Bare":
66+
case "Pkh":
67+
case "Sh":
68+
case "ShWsh":
69+
case "Wsh":
70+
case "Wpkh":
71+
case "ShWpkh":
72+
break;
73+
default:
74+
throw new Error("unexpected descriptor type " + descriptor.descType());
75+
}
6376
});
6477
});
6578
});

0 commit comments

Comments
 (0)