From fdd84dc4f83cb1f804b85f7b348c471040838dae Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 24 Jan 2025 09:31:25 +0000 Subject: [PATCH 1/3] chore: add ci tests to ci --- .github/workflows/ci.yml | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0ae7e940..a018018d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,31 @@ jobs: cargo fmt --check ' - name: typos - run: nix develop -i -L .#nightly --command typos + run: nix develop -i -L .#nightly --command typos + + examples: + name: "Run examples" + runs-on: ubuntu-latest + strategy: + matrix: + build-args: + [ + mint-token, + p2pk, + proof-selection, + wallet + ] + steps: + - name: checkout + uses: actions/checkout@v4 + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@v11 + - name: Nix Cache + uses: DeterminateSystems/magic-nix-cache-action@v6 + - name: Rust Cache + uses: Swatinem/rust-cache@v2 + - name: Run example + run: nix develop -i -L .#stable --command cargo r --example ${{ matrix.build-args }} clippy: name: "Stable build, clippy and test" From 80b5305e96e8ff72090ab945bf99e25a8fe34ad1 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 24 Jan 2025 10:34:20 +0000 Subject: [PATCH 2/3] fix: multi mint check --- crates/cashu/src/nuts/nut00/token.rs | 4 ++++ crates/cdk/src/wallet/receive.rs | 15 +++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/cashu/src/nuts/nut00/token.rs b/crates/cashu/src/nuts/nut00/token.rs index 2e4fc809..59bf6beb 100644 --- a/crates/cashu/src/nuts/nut00/token.rs +++ b/crates/cashu/src/nuts/nut00/token.rs @@ -272,6 +272,10 @@ impl TokenV3 { mint_urls } + + pub fn is_multi_mint(&self) -> bool { + self.token.len() > 1 + } } impl FromStr for TokenV3 { diff --git a/crates/cdk/src/wallet/receive.rs b/crates/cdk/src/wallet/receive.rs index aed875a3..92d06161 100644 --- a/crates/cdk/src/wallet/receive.rs +++ b/crates/cdk/src/wallet/receive.rs @@ -190,20 +190,23 @@ impl Wallet { p2pk_signing_keys: &[SecretKey], preimages: &[String], ) -> Result { - let token_data = Token::from_str(encoded_token)?; + let token = Token::from_str(encoded_token)?; - let unit = token_data.unit().unwrap_or_default(); + let unit = token.unit().unwrap_or_default(); if unit != self.unit { return Err(Error::UnitUnsupported); } - let proofs = token_data.proofs(); - if proofs.len() != 1 { - return Err(Error::MultiMintTokenNotSupported); + let proofs = token.proofs(); + + if let Token::TokenV3(token) = &token { + if token.is_multi_mint() { + return Err(Error::MultiMintTokenNotSupported); + } } - if self.mint_url != token_data.mint_url()? { + if self.mint_url != token.mint_url()? { return Err(Error::IncorrectMint); } From 19156552ffd618815e7c9ab823fac8b9d2df71c0 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 24 Jan 2025 11:17:29 +0000 Subject: [PATCH 3/3] fix: p2pk example to account for fees --- crates/cdk/Cargo.toml | 1 + crates/cdk/examples/p2pk.rs | 14 ++++++++++++-- crates/cdk/src/wallet/proofs.rs | 17 +++++++++++++++-- crates/cdk/src/wallet/send.rs | 12 +++--------- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index 35eff847..1dfad52a 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -100,6 +100,7 @@ required-features = ["wallet"] [dev-dependencies] rand = "0.8.5" bip39 = "2.0" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } criterion = "0.5.1" [[bench]] diff --git a/crates/cdk/examples/p2pk.rs b/crates/cdk/examples/p2pk.rs index 85112d06..ca3639d8 100644 --- a/crates/cdk/examples/p2pk.rs +++ b/crates/cdk/examples/p2pk.rs @@ -8,9 +8,19 @@ use cdk::wallet::types::SendKind; use cdk::wallet::{Wallet, WalletSubscription}; use cdk::Amount; use rand::Rng; +use tracing_subscriber::EnvFilter; #[tokio::main] async fn main() -> Result<(), Error> { + let default_filter = "debug"; + + let sqlx_filter = "sqlx=warn,hyper_util=warn,reqwest=warn,rustls=warn"; + + let env_filter = EnvFilter::new(format!("{},{}", default_filter, sqlx_filter)); + + // Parse input + tracing_subscriber::fmt().with_env_filter(env_filter).init(); + // Initialize the memory store for the wallet let localstore = WalletMemoryDatabase::default(); @@ -20,7 +30,7 @@ async fn main() -> Result<(), Error> { // Define the mint URL and currency unit let mint_url = "https://testnut.cashu.space"; let unit = CurrencyUnit::Sat; - let amount = Amount::from(10); + let amount = Amount::from(50); // Create a new wallet let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None)?; @@ -62,7 +72,7 @@ async fn main() -> Result<(), Error> { // Send a token with the specified amount and spending conditions let token = wallet .send( - amount, + 10.into(), None, Some(spending_conditions), &SplitTarget::default(), diff --git a/crates/cdk/src/wallet/proofs.rs b/crates/cdk/src/wallet/proofs.rs index 2ecc8354..73ece131 100644 --- a/crates/cdk/src/wallet/proofs.rs +++ b/crates/cdk/src/wallet/proofs.rs @@ -161,8 +161,11 @@ impl Wallet { proofs: Proofs, include_fees: bool, ) -> Result { - // TODO: Check all proofs are same unit - + tracing::debug!( + "Selecting proofs to send {} from {}", + amount, + proofs.total_amount()? + ); if proofs.total_amount()? < amount { return Err(Error::InsufficientFunds); } @@ -228,6 +231,11 @@ impl Wallet { amount: Amount, proofs: Proofs, ) -> Result { + tracing::debug!( + "Selecting proofs to swap {} from {}", + amount, + proofs.total_amount()? + ); let active_keyset_id = self.get_active_mint_keyset().await?.id; let (mut active_proofs, mut inactive_proofs): (Proofs, Proofs) = proofs @@ -259,6 +267,11 @@ impl Wallet { } } + tracing::debug!( + "Could not select proofs to swap: total selected: {}", + selected_proofs.total_amount()? + ); + Err(Error::InsufficientFunds) } } diff --git a/crates/cdk/src/wallet/send.rs b/crates/cdk/src/wallet/send.rs index f5146852..1d2b739a 100644 --- a/crates/cdk/src/wallet/send.rs +++ b/crates/cdk/src/wallet/send.rs @@ -52,19 +52,13 @@ impl Wallet { ) .await?; - let (available_proofs, proofs_sum) = available_proofs.into_iter().fold( - (Vec::new(), Amount::ZERO), - |(mut acc1, mut acc2), p| { - acc2 += p.amount; - acc1.push(p); - (acc1, acc2) - }, - ); + let proofs_sum = available_proofs.total_amount()?; + let available_proofs = if proofs_sum < amount { match &conditions { Some(conditions) => { + tracing::debug!("Insufficient prrofs matching conditions attempting swap"); let unspent_proofs = self.get_unspent_proofs().await?; - let proofs_to_swap = self.select_proofs_to_swap(amount, unspent_proofs).await?; let proofs_with_conditions = self