diff --git a/bindings/cdk-js/Cargo.toml b/bindings/cdk-js/Cargo.toml index 85ecd313..516a77d1 100644 --- a/bindings/cdk-js/Cargo.toml +++ b/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"] } diff --git a/bindings/cdk-js/src/nuts/mod.rs b/bindings/cdk-js/src/nuts/mod.rs index 7fdc1a3e..0e3e30f2 100644 --- a/bindings/cdk-js/src/nuts/mod.rs +++ b/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; diff --git a/bindings/cdk-js/src/nuts/nut11.rs b/bindings/cdk-js/src/nuts/nut11.rs index e7c7f7d7..a1520070 100644 --- a/bindings/cdk-js/src/nuts/nut11.rs +++ b/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 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 { + 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, + pubkeys: JsValue, + refund_key: JsValue, + num_sigs: Option, + sig_flag: String, + ) -> Result { + let pubkeys: Result, _> = serde_wasm_bindgen::from_value(pubkeys); + let refund_key: Result, _> = 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 { diff --git a/bindings/cdk-js/src/wallet.rs b/bindings/cdk-js/src/wallet.rs index 0f768d80..0695711d 100644 --- a/bindings/cdk-js/src/wallet.rs +++ b/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, amount: u64, + p2pk_condition: Option, ) -> Result { 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) }