From fdb0bc4f7319712346168e97dc455aa4d0da97f0 Mon Sep 17 00:00:00 2001 From: Otto Allmendinger Date: Thu, 19 Sep 2024 14:14:32 +0200 Subject: [PATCH 1/3] feat: apply cargo fmt Issue: BTC-1451 --- packages/wasm-miniscript/src/descriptor.rs | 14 ++++---- packages/wasm-miniscript/src/lib.rs | 8 ++--- packages/wasm-miniscript/src/psbt.rs | 35 +++++++++++-------- .../wasm-miniscript/src/try_into_js_value.rs | 26 ++++++++------ 4 files changed, 47 insertions(+), 36 deletions(-) diff --git a/packages/wasm-miniscript/src/descriptor.rs b/packages/wasm-miniscript/src/descriptor.rs index 860b860..d0e068a 100644 --- a/packages/wasm-miniscript/src/descriptor.rs +++ b/packages/wasm-miniscript/src/descriptor.rs @@ -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 { (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)] @@ -94,13 +95,14 @@ impl WrapDescriptor { #[wasm_bindgen(js_name = maxWeightToSatisfy)] pub fn max_weight_to_satisfy(&self) -> Result { 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)] diff --git a/packages/wasm-miniscript/src/lib.rs b/packages/wasm-miniscript/src/lib.rs index e8fcd80..40047b9 100644 --- a/packages/wasm-miniscript/src/lib.rs +++ b/packages/wasm-miniscript/src/lib.rs @@ -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; diff --git a/packages/wasm-miniscript/src/psbt.rs b/packages/wasm-miniscript/src/psbt.rs index fc5db52..3c9a655 100644 --- a/packages/wasm-miniscript/src/psbt.rs +++ b/packages/wasm-miniscript/src/psbt.rs @@ -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); @@ -14,7 +14,7 @@ impl WrapPsbt { pub fn deserialize(psbt: Vec) -> Result { Ok(WrapPsbt(Psbt::deserialize(&psbt).map_err(JsError::from)?)) } - + pub fn serialize(&self) -> Vec { self.0.serialize() } @@ -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")) } @@ -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))) } } diff --git a/packages/wasm-miniscript/src/try_into_js_value.rs b/packages/wasm-miniscript/src/try_into_js_value.rs index 2178650..28bca7b 100644 --- a/packages/wasm-miniscript/src/try_into_js_value.rs +++ b/packages/wasm-miniscript/src/try_into_js_value.rs @@ -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; @@ -64,7 +67,7 @@ impl TryIntoJsValue for Option { fn try_to_js_value(&self) -> Result { match self { Some(v) => v.try_to_js_value(), - None => Ok(JsValue::NULL) + None => Ok(JsValue::NULL), } } } @@ -128,7 +131,9 @@ impl TryIntoJsValue for Threshold { } } -impl TryIntoJsValue for Miniscript { +impl TryIntoJsValue + for Miniscript +{ fn try_to_js_value(&self) -> Result { self.node.try_to_js_value() } @@ -169,7 +174,9 @@ impl TryIntoJsValue for } } -impl TryIntoJsValue for SortedMultiVec { +impl TryIntoJsValue + for SortedMultiVec +{ fn try_to_js_value(&self) -> Result { js_obj!( "k" => self.k(), @@ -212,7 +219,7 @@ impl TryIntoJsValue for TapTree { fn try_to_js_value(&self) -> Result { 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(), } } } @@ -233,7 +240,6 @@ impl TryIntoJsValue for DefiniteDescriptorKey { } } - impl TryIntoJsValue for Descriptor { fn try_to_js_value(&self) -> Result { match self { @@ -252,4 +258,4 @@ impl TryIntoJsValue for DescriptorType { let str_from_enum = format!("{:?}", self); Ok(JsValue::from_str(&str_from_enum)) } -} \ No newline at end of file +} From 83a984ff6f7f59a0d95dd22253bcd99b9cc659cc Mon Sep 17 00:00:00 2001 From: Otto Allmendinger Date: Thu, 19 Sep 2024 14:16:27 +0200 Subject: [PATCH 2/3] feat: enforce rust formatting in wasm-miniscript Issue: BTC-1451 --- packages/wasm-miniscript/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wasm-miniscript/package.json b/packages/wasm-miniscript/package.json index 405c2ff..112ffcd 100644 --- a/packages/wasm-miniscript/package.json +++ b/packages/wasm-miniscript/package.json @@ -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", From 742af21a6eeb6df4e6960248774e1b737a98ac5b Mon Sep 17 00:00:00 2001 From: Otto Allmendinger Date: Thu, 19 Sep 2024 15:45:11 +0200 Subject: [PATCH 3/3] feat: install cargo-fmt in CI Issue: BTC-1451 --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 78f4336..471b821 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,6 +37,7 @@ jobs: - name: Install wasm-pack run: | + rustup component add rustfmt cargo install wasm-pack - name: Build Info