ソースを参照

feat: targeted amount

thesimplekid 9 ヶ月 前
コミット
64e667c555

+ 3 - 1
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,
             )

+ 10 - 3
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),
 }
 

+ 3 - 1
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<Self, Error> {
-        let amount_split = amount.split();
+        let amount_split = amount.split_targeted(&amount_split_target);
 
         let mut output = Vec::with_capacity(amount_split.len());
 

+ 29 - 4
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>,
+        amount_split_target: Option<SplitTarget>,
         input_proofs: Proofs,
         spending_conditions: Option<SpendingConditions>,
     ) -> Result<Option<Proofs>, 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>,
+        amount_split_target: Option<SplitTarget>,
         proofs: Proofs,
         spending_conditions: Option<SpendingConditions>,
     ) -> Result<PreSwap, Error> {
@@ -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<String>,
         amount: Amount,
+        amount_split_target: Option<SplitTarget>,
         conditions: Option<SpendingConditions>,
     ) -> Result<String, Error> {
         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<SplitTarget>,
         signing_keys: Option<Vec<SecretKey>>,
         preimages: Option<Vec<String>>,
     ) -> Result<Amount, Error> {
@@ -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) {