From c100b4f5f909bc642d9e84b70ccc9e5913e0db46 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Wed, 8 May 2024 22:47:01 +0100 Subject: [PATCH] feat(bindings): swap --- bindings/cdk-js/src/wallet.rs | 44 +++++++++++++++++++++++++++++++++-- crates/cdk/src/wallet.rs | 2 +- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/bindings/cdk-js/src/wallet.rs b/bindings/cdk-js/src/wallet.rs index 4c358702..2385750c 100644 --- a/bindings/cdk-js/src/wallet.rs +++ b/bindings/cdk-js/src/wallet.rs @@ -2,7 +2,7 @@ use std::ops::Deref; use std::str::FromStr; use std::sync::Arc; -use cdk::nuts::SigningKey; +use cdk::nuts::{Proofs, SigningKey}; use cdk::url::UncheckedUrl; use cdk::wallet::Wallet; use cdk::{Amount, HttpClient}; @@ -12,7 +12,7 @@ use wasm_bindgen::prelude::*; use crate::error::{into_err, Result}; use crate::nuts::nut11::JsP2PKSpendingConditions; use crate::nuts::nut14::JsHTLCSpendingConditions; -use crate::nuts::{JsCurrencyUnit, JsMintInfo}; +use crate::nuts::{JsCurrencyUnit, JsMintInfo, JsProof}; use crate::types::melt_quote::JsMeltQuote; use crate::types::{JsAmount, JsMelted, JsMintQuote}; @@ -184,4 +184,44 @@ impl JsWallet { .await .map_err(into_err) } + + #[wasm_bindgen(js_name = swap)] + pub async fn swap( + &mut self, + mint_url: String, + unit: JsCurrencyUnit, + amount: u64, + input_proofs: Vec, + p2pk_condition: Option, + htlc_condition: Option, + ) -> Result { + let conditions = match (p2pk_condition, htlc_condition) { + (Some(_), Some(_)) => { + return Err(JsValue::from_str( + "Cannot define both p2pk and htlc conditions", + )); + } + (None, Some(htlc_condition)) => Some(htlc_condition.deref().clone()), + (Some(p2pk_condition), None) => Some(p2pk_condition.deref().clone()), + (None, None) => None, + }; + + let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?; + + let proofs: Proofs = input_proofs.iter().map(|p| p.deref()).cloned().collect(); + + let post_swap_proofs = self + .inner + .swap( + &mint_url, + &unit.into(), + Some(Amount::from(amount)), + proofs, + conditions, + ) + .await + .map_err(into_err)?; + + Ok(serde_wasm_bindgen::to_value(&post_swap_proofs)?) + } } diff --git a/crates/cdk/src/wallet.rs b/crates/cdk/src/wallet.rs index 706f035c..762e312f 100644 --- a/crates/cdk/src/wallet.rs +++ b/crates/cdk/src/wallet.rs @@ -467,7 +467,7 @@ impl Wallet { } /// Swap - async fn swap( + pub async fn swap( &mut self, mint_url: &UncheckedUrl, unit: &CurrencyUnit,