From 64e667c5551f8cd0b39a54453ced2e4b3976b1cf Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Tue, 21 May 2024 00:32:25 +0100 Subject: [PATCH] feat: targeted amount --- bindings/cdk-js/src/wallet.rs | 4 +++- crates/cdk/src/amount.rs | 13 ++++++++++--- crates/cdk/src/nuts/nut00.rs | 4 +++- crates/cdk/src/wallet.rs | 33 +++++++++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/bindings/cdk-js/src/wallet.rs b/bindings/cdk-js/src/wallet.rs index 3dbdbeb9..18791101 100644 --- a/bindings/cdk-js/src/wallet.rs +++ b/bindings/cdk-js/src/wallet.rs @@ -216,7 +216,7 @@ impl JsWallet { Ok(self .inner - .receive(&encoded_token, signing_keys, preimages) + .receive(&encoded_token, None, signing_keys, preimages) .await .map_err(into_err)? .into()) @@ -251,6 +251,7 @@ impl JsWallet { &unit.into(), memo, Amount::from(amount), + None, conditions, ) .await @@ -288,6 +289,7 @@ impl JsWallet { &mint_url, &unit.into(), Some(Amount::from(amount)), + None, proofs, conditions, ) diff --git a/crates/cdk/src/amount.rs b/crates/cdk/src/amount.rs index 79769ec4..c1e16c3e 100644 --- a/crates/cdk/src/amount.rs +++ b/crates/cdk/src/amount.rs @@ -27,8 +27,11 @@ impl Amount { let mut parts = vec![]; let mut parts_total = Amount::ZERO; - match target { - &SplitTarget::Value(amount) => { + match *target { + SplitTarget::None => { + parts = self.split(); + } + SplitTarget::Value(amount) => { if self.le(&amount) { return self.split(); } @@ -60,8 +63,12 @@ impl Amount { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[derive( + Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize, +)] pub enum SplitTarget { + #[default] + None, Value(Amount), } diff --git a/crates/cdk/src/nuts/nut00.rs b/crates/cdk/src/nuts/nut00.rs index acd602e1..99fa0d60 100644 --- a/crates/cdk/src/nuts/nut00.rs +++ b/crates/cdk/src/nuts/nut00.rs @@ -16,6 +16,7 @@ use url::Url; use super::nut10; use super::nut11::SpendingConditions; +use crate::amount::SplitTarget; use crate::dhke::{blind_message, hash_to_curve}; use crate::nuts::nut01::{PublicKey, SecretKey}; use crate::nuts::nut11::{serde_p2pk_witness, P2PKWitness}; @@ -426,9 +427,10 @@ impl PreMintSecrets { pub fn with_conditions( keyset_id: Id, amount: Amount, + amount_split_target: SplitTarget, conditions: SpendingConditions, ) -> Result { - let amount_split = amount.split(); + let amount_split = amount.split_targeted(&amount_split_target); let mut output = Vec::with_capacity(amount_split.len()); diff --git a/crates/cdk/src/wallet.rs b/crates/cdk/src/wallet.rs index 11f218ba..58d78d5c 100644 --- a/crates/cdk/src/wallet.rs +++ b/crates/cdk/src/wallet.rs @@ -16,6 +16,7 @@ use nostr_sdk::{Filter, Timestamp}; use thiserror::Error; use tracing::instrument; +use crate::amount::SplitTarget; use crate::cdk_database::{self, WalletDatabase}; use crate::client::HttpClient; use crate::dhke::{construct_proofs, hash_to_curve}; @@ -598,6 +599,7 @@ impl Wallet { mint_url: &UncheckedUrl, unit: &CurrencyUnit, amount: Option, + amount_split_target: Option, input_proofs: Proofs, spending_conditions: Option, ) -> Result, Error> { @@ -606,6 +608,7 @@ impl Wallet { mint_url, unit, amount, + amount_split_target, input_proofs.clone(), spending_conditions, ) @@ -703,6 +706,7 @@ impl Wallet { mint_url: &UncheckedUrl, unit: &CurrencyUnit, amount: Option, + amount_split_target: Option, proofs: Proofs, spending_conditions: Option, ) -> Result { @@ -732,7 +736,12 @@ impl Wallet { )?; ( - PreMintSecrets::with_conditions(active_keyset_id, desired_amount, conditions)?, + PreMintSecrets::with_conditions( + active_keyset_id, + desired_amount, + amount_split_target.unwrap_or_default(), + conditions, + )?, change_premint_secrets, ) } @@ -787,6 +796,7 @@ impl Wallet { unit: &CurrencyUnit, memo: Option, amount: Amount, + amount_split_target: Option, conditions: Option, ) -> Result { let input_proofs = self.select_proofs(mint_url.clone(), unit, amount).await?; @@ -801,8 +811,15 @@ impl Wallet { ) { (true, None) => Some(input_proofs), _ => { - self.swap(mint_url, unit, Some(amount), input_proofs, conditions) - .await? + self.swap( + mint_url, + unit, + Some(amount), + amount_split_target, + input_proofs, + conditions, + ) + .await? } }; @@ -1028,6 +1045,7 @@ impl Wallet { pub async fn receive( &self, encoded_token: &str, + amount_split_target: Option, signing_keys: Option>, preimages: Option>, ) -> Result { @@ -1127,7 +1145,14 @@ impl Wallet { } let mut pre_swap = self - .create_swap(&token.mint, &unit, Some(amount), proofs, None) + .create_swap( + &token.mint, + &unit, + Some(amount), + amount_split_target, + proofs, + None, + ) .await?; if sig_flag.eq(&SigFlag::SigAll) {