Skip to content

Commit

Permalink
Merge pull request #42 from BitGo/BTC-1460.wrap-maxweight
Browse files Browse the repository at this point in the history
feat: wrap maxWeightToSatisfy
  • Loading branch information
OttoAllmendinger authored Sep 5, 2024
2 parents eb0966b + c29ebb4 commit ca40ccc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/wasm-miniscript/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ impl WrapDescriptor {
Ok(self.explicit_script()?.to_asm_string())
}

#[wasm_bindgen(js_name = maxWeightToSatisfy)]
pub fn max_weight_to_satisfy(&self) -> Result<u32, JsError> {
let weight = (match &self.0 {
WrapDescriptorEnum::Derivable(desc, _) => {
desc.max_weight_to_satisfy()
}
WrapDescriptorEnum::Definite(desc) => desc.max_weight_to_satisfy(),
WrapDescriptorEnum::String(desc) => desc.max_weight_to_satisfy(),
})?;
weight.to_wu().try_into().map_err(|_| JsError::new("Weight exceeds u32"))
}

#[wasm_bindgen(js_name = fromString, skip_typescript)]
pub fn from_string(descriptor: &str, pk_type: &str) -> Result<WrapDescriptor, JsError> {
match pk_type {
Expand Down
2 changes: 2 additions & 0 deletions packages/wasm-miniscript/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ describe("Descriptor fixtures", function () {
fixture.script,
);
}

assert.ok(Number.isInteger(descriptor.maxWeightToSatisfy()));
});
});
});

0 comments on commit ca40ccc

Please sign in to comment.