Skip to content

Commit a8b0ad1

Browse files
Merge pull request #44 from BitGo/BTC-1451.add-descType
feat: add descType method to WrapDescriptor
2 parents 2f2b137 + ffe0e55 commit a8b0ad1

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
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: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ function removeChecksum(descriptor: string): string {
1717
return parts[0];
1818
}
1919

20+
function getScriptPubKeyLength(descType: string): number {
21+
switch (descType) {
22+
case "Wpkh":
23+
return 22;
24+
case "Sh":
25+
case "ShWsh":
26+
case "ShWpkh":
27+
return 23;
28+
case "Pkh":
29+
return 25;
30+
case "Wsh":
31+
return 34;
32+
case "Bare":
33+
throw new Error("cannot determine scriptPubKey length for Bare descriptor");
34+
default:
35+
throw new Error("unexpected descriptor type " + descType);
36+
}
37+
}
38+
2039
describe("Descriptor fixtures", function () {
2140
fixtures.valid.forEach((fixture, i) => {
2241
it("should parse fixture " + i, function () {
@@ -51,15 +70,33 @@ describe("Descriptor fixtures", function () {
5170
descriptorString = removeChecksum(descriptorString);
5271
}
5372
const descriptor = Descriptor.fromString(descriptorString, "derivable");
54-
assert.strictEqual(
55-
Buffer.from(descriptor.atDerivationIndex(fixture.index ?? 0).scriptPubkey()).toString(
56-
"hex",
57-
),
58-
fixture.script,
73+
const scriptPubKey = Buffer.from(
74+
descriptor.atDerivationIndex(fixture.index ?? 0).scriptPubkey(),
5975
);
76+
assert.strictEqual(scriptPubKey.toString("hex"), fixture.script);
77+
if (descriptor.descType() !== "Bare") {
78+
assert.strictEqual(
79+
scriptPubKey.length,
80+
getScriptPubKeyLength(descriptor.descType()),
81+
`Unexpected scriptPubKey length for descriptor ${descriptor.descType()}: ${scriptPubKey.length}`,
82+
);
83+
}
6084
}
6185

6286
assert.ok(Number.isInteger(descriptor.maxWeightToSatisfy()));
87+
88+
switch (descriptor.descType()) {
89+
case "Bare":
90+
case "Pkh":
91+
case "Sh":
92+
case "ShWsh":
93+
case "Wsh":
94+
case "Wpkh":
95+
case "ShWpkh":
96+
break;
97+
default:
98+
throw new Error("unexpected descriptor type " + descriptor.descType());
99+
}
63100
});
64101
});
65102
});

0 commit comments

Comments
 (0)