Skip to content

Commit

Permalink
Merge pull request #45 from BitGo/BTC-1451.add-descType.format
Browse files Browse the repository at this point in the history
refactor: format repo
  • Loading branch information
OttoAllmendinger authored Sep 19, 2024
2 parents a8b0ad1 + 742af21 commit 5f5aa92
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 37 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:

- name: Install wasm-pack
run: |
rustup component add rustfmt
cargo install wasm-pack
- name: Build Info
Expand Down
2 changes: 1 addition & 1 deletion packages/wasm-miniscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"build:ts-browser": "tsc --module es2020 --target es2020 --outDir dist/browser",
"build:ts": "tsc && npm run build:ts-browser",
"build": "npm run build:wasm && npm run build:ts",
"check-fmt": "prettier --check ."
"check-fmt": "prettier --check . && cargo fmt -- --check"
},
"devDependencies": {
"@bitgo/utxo-lib": "^10.1.0",
Expand Down
14 changes: 8 additions & 6 deletions packages/wasm-miniscript/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ impl WrapDescriptor {
_ => Err(JsError::new("Cannot derive from a definite descriptor")),
}
}

#[wasm_bindgen(js_name = descType)]
pub fn desc_type(&self) -> Result<JsValue, JsError> {
(match &self.0 {
WrapDescriptorEnum::Derivable(desc, _) => desc.desc_type(),
WrapDescriptorEnum::Definite(desc) => desc.desc_type(),
WrapDescriptorEnum::String(desc) => desc.desc_type(),
}).try_to_js_value()
})
.try_to_js_value()
}

#[wasm_bindgen(js_name = scriptPubkey)]
Expand Down Expand Up @@ -94,13 +95,14 @@ impl WrapDescriptor {
#[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::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"))
weight
.to_wu()
.try_into()
.map_err(|_| JsError::new("Weight exceeds u32"))
}

#[wasm_bindgen(js_name = fromString, skip_typescript)]
Expand Down
8 changes: 3 additions & 5 deletions packages/wasm-miniscript/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@


mod try_into_js_value;
mod miniscript;
mod descriptor;
mod miniscript;
mod psbt;
mod try_into_js_value;

pub use miniscript::WrapMiniscript;
pub use descriptor::WrapDescriptor;
pub use miniscript::WrapMiniscript;
pub use psbt::WrapPsbt;
35 changes: 20 additions & 15 deletions packages/wasm-miniscript/src/psbt.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use miniscript::bitcoin::Psbt;
use crate::descriptor::WrapDescriptorEnum;
use crate::WrapDescriptor;
use miniscript::bitcoin::secp256k1::Secp256k1;
use miniscript::bitcoin::Psbt;
use miniscript::psbt::PsbtExt;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError};
use crate::descriptor::WrapDescriptorEnum;
use crate::WrapDescriptor;
use wasm_bindgen::JsError;

#[wasm_bindgen]
pub struct WrapPsbt(Psbt);
Expand All @@ -14,7 +14,7 @@ impl WrapPsbt {
pub fn deserialize(psbt: Vec<u8>) -> Result<WrapPsbt, JsError> {
Ok(WrapPsbt(Psbt::deserialize(&psbt).map_err(JsError::from)?))
}

pub fn serialize(&self) -> Vec<u8> {
self.0.serialize()
}
Expand All @@ -24,14 +24,19 @@ impl WrapPsbt {
}

#[wasm_bindgen(js_name = updateInputWithDescriptor)]
pub fn update_input_with_descriptor(&mut self, input_index: usize, descriptor: WrapDescriptor) -> Result<(), JsError> {
pub fn update_input_with_descriptor(
&mut self,
input_index: usize,
descriptor: WrapDescriptor,
) -> Result<(), JsError> {
match descriptor.0 {
WrapDescriptorEnum::Definite(d) => {
self.0.update_input_with_descriptor(input_index, &d).map_err(JsError::from)
}
WrapDescriptorEnum::Derivable(_, _) => {
Err(JsError::new("Cannot update input with a derivable descriptor"))
}
WrapDescriptorEnum::Definite(d) => self
.0
.update_input_with_descriptor(input_index, &d)
.map_err(JsError::from),
WrapDescriptorEnum::Derivable(_, _) => Err(JsError::new(
"Cannot update input with a derivable descriptor",
)),
WrapDescriptorEnum::String(_) => {
Err(JsError::new("Cannot update input with a string descriptor"))
}
Expand All @@ -40,8 +45,8 @@ impl WrapPsbt {

#[wasm_bindgen(js_name = finalize)]
pub fn finalize_mut(&mut self) -> Result<(), JsError> {
self.0.finalize_mut(&Secp256k1::verification_only()).map_err(|vec_err| {
JsError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err))
})
self.0
.finalize_mut(&Secp256k1::verification_only())
.map_err(|vec_err| JsError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err)))
}
}
26 changes: 16 additions & 10 deletions packages/wasm-miniscript/src/try_into_js_value.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use wasm_bindgen::{JsError, JsValue};
use js_sys::Array;
use miniscript::{AbsLockTime, DefiniteDescriptorKey, Descriptor, DescriptorPublicKey, hash256, Miniscript, MiniscriptKey, RelLockTime, ScriptContext, Terminal, Threshold};
use miniscript::descriptor::{DescriptorType, ShInner, SortedMultiVec, TapTree, Tr, WshInner};
use miniscript::bitcoin::{PublicKey, XOnlyPublicKey};
use miniscript::bitcoin::hashes::{hash160, ripemd160};
use miniscript::bitcoin::{PublicKey, XOnlyPublicKey};
use miniscript::descriptor::{DescriptorType, ShInner, SortedMultiVec, TapTree, Tr, WshInner};
use miniscript::{
hash256, AbsLockTime, DefiniteDescriptorKey, Descriptor, DescriptorPublicKey, Miniscript,
MiniscriptKey, RelLockTime, ScriptContext, Terminal, Threshold,
};
use std::sync::Arc;
use wasm_bindgen::{JsError, JsValue};

pub(crate) trait TryIntoJsValue {
fn try_to_js_value(&self) -> Result<JsValue, JsError>;
Expand Down Expand Up @@ -64,7 +67,7 @@ impl<T: TryIntoJsValue> TryIntoJsValue for Option<T> {
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
match self {
Some(v) => v.try_to_js_value(),
None => Ok(JsValue::NULL)
None => Ok(JsValue::NULL),
}
}
}
Expand Down Expand Up @@ -128,7 +131,9 @@ impl<T: TryIntoJsValue, const MAX: usize> TryIntoJsValue for Threshold<T, MAX> {
}
}

impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue for Miniscript<Pk, Ctx> {
impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue
for Miniscript<Pk, Ctx>
{
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
self.node.try_to_js_value()
}
Expand Down Expand Up @@ -169,7 +174,9 @@ impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue for
}
}

impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue for SortedMultiVec<Pk, Ctx> {
impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue
for SortedMultiVec<Pk, Ctx>
{
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
js_obj!(
"k" => self.k(),
Expand Down Expand Up @@ -212,7 +219,7 @@ impl<Pk: MiniscriptKey + TryIntoJsValue> TryIntoJsValue for TapTree<Pk> {
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
match self {
TapTree::Tree { left, right, .. } => js_obj!("Tree" => js_arr!(left, right)),
TapTree::Leaf(ms) => ms.try_to_js_value()
TapTree::Leaf(ms) => ms.try_to_js_value(),
}
}
}
Expand All @@ -233,7 +240,6 @@ impl TryIntoJsValue for DefiniteDescriptorKey {
}
}


impl<Pk: MiniscriptKey + TryIntoJsValue> TryIntoJsValue for Descriptor<Pk> {
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
match self {
Expand All @@ -252,4 +258,4 @@ impl TryIntoJsValue for DescriptorType {
let str_from_enum = format!("{:?}", self);
Ok(JsValue::from_str(&str_from_enum))
}
}
}

0 comments on commit 5f5aa92

Please sign in to comment.