Skip to content

Commit

Permalink
feat: expose script_pubkey function within WrapDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
rushilbg committed Aug 8, 2024
1 parent 10b5525 commit d44dd5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/wasm-miniscript/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type Descriptor = {
atDerivationIndex(index: number): Descriptor;
encode(): Uint8Array;
toAsmString(): string;
scriptPubkey(): Uint8Array;
};

export function isDescriptor(obj: unknown): obj is Descriptor {
Expand Down
12 changes: 11 additions & 1 deletion packages/wasm-miniscript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ impl WrapDescriptor {
}
}

#[wasm_bindgen(js_name = scriptPubkey)]
pub fn script_pubkey(&self) -> Result<Vec<u8>, JsError> {
match &self.0 {
WrapDescriptorEnum::Definite(desc) => {
Ok(desc.script_pubkey().to_bytes())
}
_ => Err(JsError::new("Cannot derive from a non-definite descriptor")),
}
}

fn explicit_script(&self) -> Result<ScriptBuf, JsError> {
match &self.0 {
WrapDescriptorEnum::Definite(desc) => {
Expand Down Expand Up @@ -250,4 +260,4 @@ pub fn panic_xprv() {
let dd = d.at_derivation_index(0).unwrap();

let _ = dd.explicit_script().unwrap();
}
}
14 changes: 13 additions & 1 deletion packages/wasm-miniscript/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Descriptor fixtures", function () {
}
let expected = fixture.descriptor;
if (i === 56 || i === 57) {
// for reasons I do not really understand, teh `a:and_n` gets converted into `a:and_b` for these
// for reasons I do not really understand, the `a:and_n` gets converted into `a:and_b` for these
expected = expected.replace("and_n", "and_b");
}
assert.strictEqual(descriptorString, expected);
Expand All @@ -45,6 +45,18 @@ describe("Descriptor fixtures", function () {
assert.doesNotThrow(() =>
descriptorFromString(fixture.descriptor, "derivable").atDerivationIndex(0).encode(),
);

let descriptorString = fixture.descriptor;
if (fixture.checksumRequired === false) {
descriptorString = removeChecksum(descriptorString);
}
const descriptor = descriptorFromString(descriptorString, "derivable");
assert.strictEqual(
Buffer.from(descriptor.atDerivationIndex(fixture.index ?? 0).scriptPubkey()).toString(
"hex",
),
fixture.script,
);
}
});
});
Expand Down

0 comments on commit d44dd5b

Please sign in to comment.