Skip to content

Commit 5f5aa92

Browse files
Merge pull request #45 from BitGo/BTC-1451.add-descType.format
refactor: format repo
2 parents a8b0ad1 + 742af21 commit 5f5aa92

File tree

6 files changed

+49
-37
lines changed

6 files changed

+49
-37
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737

3838
- name: Install wasm-pack
3939
run: |
40+
rustup component add rustfmt
4041
cargo install wasm-pack
4142
4243
- name: Build Info

packages/wasm-miniscript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"build:ts-browser": "tsc --module es2020 --target es2020 --outDir dist/browser",
4141
"build:ts": "tsc && npm run build:ts-browser",
4242
"build": "npm run build:wasm && npm run build:ts",
43-
"check-fmt": "prettier --check ."
43+
"check-fmt": "prettier --check . && cargo fmt -- --check"
4444
},
4545
"devDependencies": {
4646
"@bitgo/utxo-lib": "^10.1.0",

packages/wasm-miniscript/src/descriptor.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ impl WrapDescriptor {
5454
_ => Err(JsError::new("Cannot derive from a definite descriptor")),
5555
}
5656
}
57-
57+
5858
#[wasm_bindgen(js_name = descType)]
5959
pub fn desc_type(&self) -> Result<JsValue, JsError> {
6060
(match &self.0 {
6161
WrapDescriptorEnum::Derivable(desc, _) => desc.desc_type(),
6262
WrapDescriptorEnum::Definite(desc) => desc.desc_type(),
6363
WrapDescriptorEnum::String(desc) => desc.desc_type(),
64-
}).try_to_js_value()
64+
})
65+
.try_to_js_value()
6566
}
6667

6768
#[wasm_bindgen(js_name = scriptPubkey)]
@@ -94,13 +95,14 @@ impl WrapDescriptor {
9495
#[wasm_bindgen(js_name = maxWeightToSatisfy)]
9596
pub fn max_weight_to_satisfy(&self) -> Result<u32, JsError> {
9697
let weight = (match &self.0 {
97-
WrapDescriptorEnum::Derivable(desc, _) => {
98-
desc.max_weight_to_satisfy()
99-
}
98+
WrapDescriptorEnum::Derivable(desc, _) => desc.max_weight_to_satisfy(),
10099
WrapDescriptorEnum::Definite(desc) => desc.max_weight_to_satisfy(),
101100
WrapDescriptorEnum::String(desc) => desc.max_weight_to_satisfy(),
102101
})?;
103-
weight.to_wu().try_into().map_err(|_| JsError::new("Weight exceeds u32"))
102+
weight
103+
.to_wu()
104+
.try_into()
105+
.map_err(|_| JsError::new("Weight exceeds u32"))
104106
}
105107

106108
#[wasm_bindgen(js_name = fromString, skip_typescript)]
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
2-
3-
mod try_into_js_value;
4-
mod miniscript;
51
mod descriptor;
2+
mod miniscript;
63
mod psbt;
4+
mod try_into_js_value;
75

8-
pub use miniscript::WrapMiniscript;
96
pub use descriptor::WrapDescriptor;
7+
pub use miniscript::WrapMiniscript;
108
pub use psbt::WrapPsbt;
Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use miniscript::bitcoin::Psbt;
1+
use crate::descriptor::WrapDescriptorEnum;
2+
use crate::WrapDescriptor;
23
use miniscript::bitcoin::secp256k1::Secp256k1;
4+
use miniscript::bitcoin::Psbt;
35
use miniscript::psbt::PsbtExt;
46
use wasm_bindgen::prelude::wasm_bindgen;
5-
use wasm_bindgen::{JsError};
6-
use crate::descriptor::WrapDescriptorEnum;
7-
use crate::WrapDescriptor;
7+
use wasm_bindgen::JsError;
88

99
#[wasm_bindgen]
1010
pub struct WrapPsbt(Psbt);
@@ -14,7 +14,7 @@ impl WrapPsbt {
1414
pub fn deserialize(psbt: Vec<u8>) -> Result<WrapPsbt, JsError> {
1515
Ok(WrapPsbt(Psbt::deserialize(&psbt).map_err(JsError::from)?))
1616
}
17-
17+
1818
pub fn serialize(&self) -> Vec<u8> {
1919
self.0.serialize()
2020
}
@@ -24,14 +24,19 @@ impl WrapPsbt {
2424
}
2525

2626
#[wasm_bindgen(js_name = updateInputWithDescriptor)]
27-
pub fn update_input_with_descriptor(&mut self, input_index: usize, descriptor: WrapDescriptor) -> Result<(), JsError> {
27+
pub fn update_input_with_descriptor(
28+
&mut self,
29+
input_index: usize,
30+
descriptor: WrapDescriptor,
31+
) -> Result<(), JsError> {
2832
match descriptor.0 {
29-
WrapDescriptorEnum::Definite(d) => {
30-
self.0.update_input_with_descriptor(input_index, &d).map_err(JsError::from)
31-
}
32-
WrapDescriptorEnum::Derivable(_, _) => {
33-
Err(JsError::new("Cannot update input with a derivable descriptor"))
34-
}
33+
WrapDescriptorEnum::Definite(d) => self
34+
.0
35+
.update_input_with_descriptor(input_index, &d)
36+
.map_err(JsError::from),
37+
WrapDescriptorEnum::Derivable(_, _) => Err(JsError::new(
38+
"Cannot update input with a derivable descriptor",
39+
)),
3540
WrapDescriptorEnum::String(_) => {
3641
Err(JsError::new("Cannot update input with a string descriptor"))
3742
}
@@ -40,8 +45,8 @@ impl WrapPsbt {
4045

4146
#[wasm_bindgen(js_name = finalize)]
4247
pub fn finalize_mut(&mut self) -> Result<(), JsError> {
43-
self.0.finalize_mut(&Secp256k1::verification_only()).map_err(|vec_err| {
44-
JsError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err))
45-
})
48+
self.0
49+
.finalize_mut(&Secp256k1::verification_only())
50+
.map_err(|vec_err| JsError::new(&format!("{} errors: {:?}", vec_err.len(), vec_err)))
4651
}
4752
}

packages/wasm-miniscript/src/try_into_js_value.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
use wasm_bindgen::{JsError, JsValue};
21
use js_sys::Array;
3-
use miniscript::{AbsLockTime, DefiniteDescriptorKey, Descriptor, DescriptorPublicKey, hash256, Miniscript, MiniscriptKey, RelLockTime, ScriptContext, Terminal, Threshold};
4-
use miniscript::descriptor::{DescriptorType, ShInner, SortedMultiVec, TapTree, Tr, WshInner};
5-
use miniscript::bitcoin::{PublicKey, XOnlyPublicKey};
62
use miniscript::bitcoin::hashes::{hash160, ripemd160};
3+
use miniscript::bitcoin::{PublicKey, XOnlyPublicKey};
4+
use miniscript::descriptor::{DescriptorType, ShInner, SortedMultiVec, TapTree, Tr, WshInner};
5+
use miniscript::{
6+
hash256, AbsLockTime, DefiniteDescriptorKey, Descriptor, DescriptorPublicKey, Miniscript,
7+
MiniscriptKey, RelLockTime, ScriptContext, Terminal, Threshold,
8+
};
79
use std::sync::Arc;
10+
use wasm_bindgen::{JsError, JsValue};
811

912
pub(crate) trait TryIntoJsValue {
1013
fn try_to_js_value(&self) -> Result<JsValue, JsError>;
@@ -64,7 +67,7 @@ impl<T: TryIntoJsValue> TryIntoJsValue for Option<T> {
6467
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
6568
match self {
6669
Some(v) => v.try_to_js_value(),
67-
None => Ok(JsValue::NULL)
70+
None => Ok(JsValue::NULL),
6871
}
6972
}
7073
}
@@ -128,7 +131,9 @@ impl<T: TryIntoJsValue, const MAX: usize> TryIntoJsValue for Threshold<T, MAX> {
128131
}
129132
}
130133

131-
impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue for Miniscript<Pk, Ctx> {
134+
impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue
135+
for Miniscript<Pk, Ctx>
136+
{
132137
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
133138
self.node.try_to_js_value()
134139
}
@@ -169,7 +174,9 @@ impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue for
169174
}
170175
}
171176

172-
impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue for SortedMultiVec<Pk, Ctx> {
177+
impl<Pk: MiniscriptKey + TryIntoJsValue, Ctx: ScriptContext> TryIntoJsValue
178+
for SortedMultiVec<Pk, Ctx>
179+
{
173180
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
174181
js_obj!(
175182
"k" => self.k(),
@@ -212,7 +219,7 @@ impl<Pk: MiniscriptKey + TryIntoJsValue> TryIntoJsValue for TapTree<Pk> {
212219
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
213220
match self {
214221
TapTree::Tree { left, right, .. } => js_obj!("Tree" => js_arr!(left, right)),
215-
TapTree::Leaf(ms) => ms.try_to_js_value()
222+
TapTree::Leaf(ms) => ms.try_to_js_value(),
216223
}
217224
}
218225
}
@@ -233,7 +240,6 @@ impl TryIntoJsValue for DefiniteDescriptorKey {
233240
}
234241
}
235242

236-
237243
impl<Pk: MiniscriptKey + TryIntoJsValue> TryIntoJsValue for Descriptor<Pk> {
238244
fn try_to_js_value(&self) -> Result<JsValue, JsError> {
239245
match self {
@@ -252,4 +258,4 @@ impl TryIntoJsValue for DescriptorType {
252258
let str_from_enum = format!("{:?}", self);
253259
Ok(JsValue::from_str(&str_from_enum))
254260
}
255-
}
261+
}

0 commit comments

Comments
 (0)