瀏覽代碼

refactor(bindings): js conditions

thesimplekid 10 月之前
父節點
當前提交
377f9bc0e4
共有 4 個文件被更改,包括 64 次插入3 次删除
  1. 1 0
      bindings/cdk-js/Cargo.toml
  2. 1 1
      bindings/cdk-js/src/nuts/mod.rs
  3. 53 1
      bindings/cdk-js/src/nuts/nut11.rs
  4. 9 1
      bindings/cdk-js/src/wallet.rs

+ 1 - 0
bindings/cdk-js/Cargo.toml

@@ -21,3 +21,4 @@ serde_json.workspace = true
 serde.workspace = true
 wasm-bindgen = { version = "0.2.92", features = ["serde-serialize"] }
 wasm-bindgen-futures = "0.4.41"
+web-sys =  { version = "0.3.68", features = ["console"] }

+ 1 - 1
bindings/cdk-js/src/nuts/mod.rs

@@ -19,6 +19,6 @@ pub use nut03::{JsSwapRequest, JsSwapResponse};
 pub use nut06::{JsMintInfo, JsMintVersion};
 pub use nut07::*;
 pub use nut09::{JsRestoreRequest, JsRestoreResponse};
-pub use nut11::JsP2PKWitness;
+pub use nut11::*;
 pub use nut12::{JsBlindSignatureDleq, JsProofDleq};
 pub use nut14::JsHTLCWitness;

+ 53 - 1
bindings/cdk-js/src/nuts/nut11.rs

@@ -1,8 +1,11 @@
 use std::ops::Deref;
+use std::str::FromStr;
 
-use cdk::nuts::{Conditions, P2PKWitness};
+use cdk::nuts::{Conditions, P2PKWitness, SigFlag, SpendingConditions, VerifyingKey};
 use wasm_bindgen::prelude::*;
 
+use crate::error::{into_err, Result};
+
 #[wasm_bindgen(js_name = P2PKWitness)]
 pub struct JsP2PKWitness {
     inner: P2PKWitness,
@@ -21,11 +24,60 @@ impl From<P2PKWitness> for JsP2PKWitness {
     }
 }
 
+#[wasm_bindgen(js_name = P2PKSpendingConditions)]
+pub struct JsP2PKSpendingConditions {
+    inner: SpendingConditions,
+}
+
+impl Deref for JsP2PKSpendingConditions {
+    type Target = SpendingConditions;
+    fn deref(&self) -> &Self::Target {
+        &self.inner
+    }
+}
+
+#[wasm_bindgen(js_class = P2PKSpendingConditions)]
+impl JsP2PKSpendingConditions {
+    #[wasm_bindgen(constructor)]
+    pub fn new(pubkey: String, conditions: JsConditions) -> Result<JsP2PKSpendingConditions> {
+        let pubkey = VerifyingKey::from_str(&pubkey).map_err(into_err)?;
+        Ok(Self {
+            inner: SpendingConditions::new_p2pk(pubkey, conditions.deref().clone()),
+        })
+    }
+}
+
 #[wasm_bindgen(js_name = Conditions)]
 pub struct JsConditions {
     inner: Conditions,
 }
 
+#[wasm_bindgen(js_class = Conditions)]
+impl JsConditions {
+    #[wasm_bindgen(constructor)]
+    pub fn new(
+        locktime: Option<u64>,
+        pubkeys: JsValue,
+        refund_key: JsValue,
+        num_sigs: Option<u64>,
+        sig_flag: String,
+    ) -> Result<JsConditions> {
+        let pubkeys: Result<Vec<VerifyingKey>, _> = serde_wasm_bindgen::from_value(pubkeys);
+        let refund_key: Result<Vec<VerifyingKey>, _> = serde_wasm_bindgen::from_value(refund_key);
+
+        Ok(Self {
+            inner: Conditions::new(
+                locktime,
+                pubkeys.ok(),
+                refund_key.ok(),
+                num_sigs,
+                Some(SigFlag::from_str(&sig_flag).unwrap_or_default()),
+            )
+            .map_err(into_err)?,
+        })
+    }
+}
+
 impl Deref for JsConditions {
     type Target = Conditions;
     fn deref(&self) -> &Self::Target {

+ 9 - 1
bindings/cdk-js/src/wallet.rs

@@ -10,6 +10,7 @@ use cdk_rexie::RexieWalletDatabase;
 use wasm_bindgen::prelude::*;
 
 use crate::error::{into_err, Result};
+use crate::nuts::nut11::JsP2PKSpendingConditions;
 use crate::nuts::{JsCurrencyUnit, JsMintInfo};
 use crate::types::melt_quote::JsMeltQuote;
 use crate::types::{JsAmount, JsMelted, JsMintQuote};
@@ -155,11 +156,18 @@ impl JsWallet {
         unit: JsCurrencyUnit,
         memo: Option<String>,
         amount: u64,
+        p2pk_condition: Option<JsP2PKSpendingConditions>,
     ) -> Result<String> {
         let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?;
 
         self.inner
-            .send(&mint_url, &unit.into(), memo, Amount::from(amount), None)
+            .send(
+                &mint_url,
+                &unit.into(),
+                memo,
+                Amount::from(amount),
+                p2pk_condition.map(|c| c.deref().clone()),
+            )
             .await
             .map_err(into_err)
     }