diff --git a/Cargo.toml b/Cargo.toml index e45530727..13d438eb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ cdk = { path = "./crates/cdk", default-features = false } cdk-rexie = { path = "./crates/cdk-rexie", default-features = false } tokio = { version = "1.32", default-features = false } thiserror = "1" -tracing = { version = "0.1", default-features = false } +tracing = { version = "0.1", default-features = false, features = ["attributes"] } serde = { version = "1", default-features = false, features = ["derive"] } serde_json = "1" serde-wasm-bindgen = { version = "0.6.5", default-features = false } diff --git a/crates/cdk-redb/src/wallet.rs b/crates/cdk-redb/src/wallet.rs index 07d1f4212..bb0aac542 100644 --- a/crates/cdk-redb/src/wallet.rs +++ b/crates/cdk-redb/src/wallet.rs @@ -10,6 +10,7 @@ use cdk::types::{MeltQuote, MintQuote}; use cdk::url::UncheckedUrl; use redb::{Database, MultimapTableDefinition, ReadableTable, TableDefinition}; use tokio::sync::Mutex; +use tracing::instrument; use super::error::Error; @@ -79,6 +80,7 @@ impl RedbWalletDatabase { impl WalletDatabase for RedbWalletDatabase { type Err = cdk_database::Error; + #[instrument(skip(self))] async fn add_mint( &self, mint_url: UncheckedUrl, @@ -104,6 +106,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn get_mint(&self, mint_url: UncheckedUrl) -> Result, Self::Err> { let db = self.db.lock().await; let read_txn = db.begin_read().map_err(Into::::into)?; @@ -119,6 +122,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(None) } + #[instrument(skip(self))] async fn get_mints(&self) -> Result>, Self::Err> { let db = self.db.lock().await; let read_txn = db.begin_read().map_err(Error::from)?; @@ -138,6 +142,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(mints) } + #[instrument(skip(self))] async fn add_mint_keysets( &self, mint_url: UncheckedUrl, @@ -168,6 +173,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn get_mint_keysets( &self, mint_url: UncheckedUrl, @@ -188,6 +194,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(keysets) } + #[instrument(skip_all)] async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Self::Err> { let db = self.db.lock().await; let write_txn = db.begin_write().map_err(Error::from)?; @@ -209,6 +216,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip_all)] async fn get_mint_quote(&self, quote_id: &str) -> Result, Self::Err> { let db = self.db.lock().await; let read_txn = db.begin_read().map_err(Into::::into)?; @@ -223,6 +231,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(None) } + #[instrument(skip_all)] async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Self::Err> { let db = self.db.lock().await; let write_txn = db.begin_write().map_err(Error::from)?; @@ -239,6 +248,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip_all)] async fn add_melt_quote(&self, quote: MeltQuote) -> Result<(), Self::Err> { let db = self.db.lock().await; let write_txn = db.begin_write().map_err(Error::from)?; @@ -260,6 +270,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip_all)] async fn get_melt_quote(&self, quote_id: &str) -> Result, Self::Err> { let db = self.db.lock().await; let read_txn = db.begin_read().map_err(Error::from)?; @@ -274,6 +285,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(None) } + #[instrument(skip_all)] async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Self::Err> { let db = self.db.lock().await; let write_txn = db.begin_write().map_err(Error::from)?; @@ -290,6 +302,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn add_keys(&self, keys: Keys) -> Result<(), Self::Err> { let db = self.db.lock().await; let write_txn = db.begin_write().map_err(Error::from)?; @@ -309,6 +322,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn get_keys(&self, id: &Id) -> Result, Self::Err> { let db = self.db.lock().await; let read_txn = db.begin_read().map_err(Error::from)?; @@ -321,6 +335,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(None) } + #[instrument(skip(self))] async fn remove_keys(&self, id: &Id) -> Result<(), Self::Err> { let db = self.db.lock().await; let write_txn = db.begin_write().map_err(Error::from)?; @@ -336,6 +351,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self, proofs))] async fn add_proofs(&self, mint_url: UncheckedUrl, proofs: Proofs) -> Result<(), Self::Err> { let db = self.db.lock().await; @@ -360,6 +376,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn get_proofs(&self, mint_url: UncheckedUrl) -> Result, Self::Err> { let db = self.db.lock().await; let read_txn = db.begin_read().map_err(Error::from)?; @@ -377,6 +394,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(proofs) } + #[instrument(skip(self, proofs))] async fn remove_proofs( &self, mint_url: UncheckedUrl, @@ -405,6 +423,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self, proofs))] async fn add_pending_proofs( &self, mint_url: UncheckedUrl, @@ -433,6 +452,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn get_pending_proofs( &self, mint_url: UncheckedUrl, @@ -453,6 +473,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(proofs) } + #[instrument(skip(self, proofs))] async fn remove_pending_proofs( &self, mint_url: UncheckedUrl, @@ -481,6 +502,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn increment_keyset_counter(&self, keyset_id: &Id, count: u64) -> Result<(), Self::Err> { let db = self.db.lock().await; @@ -512,6 +534,7 @@ impl WalletDatabase for RedbWalletDatabase { Ok(()) } + #[instrument(skip(self))] async fn get_keyset_counter(&self, keyset_id: &Id) -> Result, Self::Err> { let db = self.db.lock().await; let read_txn = db.begin_read().map_err(Error::from)?; diff --git a/crates/cdk/Cargo.toml b/crates/cdk/Cargo.toml index c3d068abe..fd31c5e9e 100644 --- a/crates/cdk/Cargo.toml +++ b/crates/cdk/Cargo.toml @@ -21,25 +21,39 @@ nut13 = ["dep:bip39"] async-trait = "0.1" base64 = "0.22" # bitcoin uses v0.13 (optional dep) bip39 = { version = "2.0", optional = true } -bitcoin = { version = "0.30", features = ["serde", "rand", "rand-std"] } # lightning-invoice uses v0.30 +bitcoin = { version = "0.30", features = [ + "serde", + "rand", + "rand-std", +] } # lightning-invoice uses v0.30 http = "1.0" lightning-invoice = { version = "0.30", features = ["serde"] } once_cell = "1.19" -reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls", "socks"], optional = true } -serde = { version = "1.0", default-features = false, features = ["derive"]} +reqwest = { version = "0.12", default-features = false, features = [ + "json", + "rustls-tls", + "socks", +], optional = true } +serde = { version = "1.0", default-features = false, features = ["derive"] } serde_json = "1.0" serde_with = "3.4" -tracing = { version = "0.1", default-features = false } +tracing = { version = "0.1", default-features = false, features = [ + "attributes", + "log", +] } thiserror = "1.0" url = "2.3" uuid = { version = "1.6", features = ["v4"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -tokio = { workspace = true, features = ["rt-multi-thread", "time", "macros", "sync"] } +tokio = { workspace = true, features = [ + "rt-multi-thread", + "time", + "macros", + "sync", +] } [target.'cfg(target_arch = "wasm32")'.dependencies] tokio = { workspace = true, features = ["rt", "macros", "sync", "time"] } getrandom = { version = "0.2", features = ["js"] } -instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] } - - +instant = { version = "0.1", features = ["wasm-bindgen", "inaccurate"] } diff --git a/crates/cdk/src/client.rs b/crates/cdk/src/client.rs index d793aa318..5d585bbd6 100644 --- a/crates/cdk/src/client.rs +++ b/crates/cdk/src/client.rs @@ -3,6 +3,7 @@ use reqwest::Client; use serde_json::Value; use thiserror::Error; +use tracing::instrument; use url::Url; use crate::error::ErrorResponse; @@ -74,6 +75,7 @@ impl HttpClient { } /// Get Active Mint Keys [NUT-01] + #[instrument(skip(self))] pub async fn get_mint_keys(&self, mint_url: Url) -> Result, Error> { let url = join_url(mint_url, &["v1", "keys"])?; let keys = self.inner.get(url).send().await?.json::().await?; @@ -83,6 +85,7 @@ impl HttpClient { } /// Get Keyset Keys [NUT-01] + #[instrument(skip(self))] pub async fn get_mint_keyset(&self, mint_url: Url, keyset_id: Id) -> Result { let url = join_url(mint_url, &["v1", "keys", &keyset_id.to_string()])?; let keys = self @@ -99,6 +102,7 @@ impl HttpClient { } /// Get Keysets [NUT-02] + #[instrument(skip(self))] pub async fn get_mint_keysets(&self, mint_url: Url) -> Result { let url = join_url(mint_url, &["v1", "keysets"])?; let res = self.inner.get(url).send().await?.json::().await?; @@ -113,6 +117,7 @@ impl HttpClient { } /// Mint Quote [NUT-04] + #[instrument(skip(self))] pub async fn post_mint_quote( &self, mint_url: Url, @@ -137,6 +142,7 @@ impl HttpClient { } /// Mint Tokens [NUT-04] + #[instrument(skip(self, quote, premint_secrets))] pub async fn post_mint( &self, mint_url: Url, @@ -169,6 +175,7 @@ impl HttpClient { } /// Melt Quote [NUT-05] + #[instrument(skip(self))] pub async fn post_melt_quote( &self, mint_url: Url, @@ -194,6 +201,7 @@ impl HttpClient { /// Melt [NUT-05] /// [Nut-08] Lightning fee return if outputs defined + #[instrument(skip(self, quote, inputs, outputs))] pub async fn post_melt( &self, mint_url: Url, @@ -222,6 +230,7 @@ impl HttpClient { } /// Split Token [NUT-06] + #[instrument(skip(self, swap_request))] pub async fn post_swap( &self, mint_url: Url, @@ -241,6 +250,7 @@ impl HttpClient { } /// Get Mint Info [NUT-06] + #[instrument(skip(self))] pub async fn get_mint_info(&self, mint_url: Url) -> Result { let url = join_url(mint_url, &["v1", "info"])?; @@ -255,6 +265,7 @@ impl HttpClient { } /// Spendable check [NUT-07] + #[instrument(skip(self))] pub async fn post_check_state( &self, mint_url: Url, @@ -281,6 +292,7 @@ impl HttpClient { } } + #[instrument(skip(self, request))] pub async fn post_restore( &self, mint_url: Url, diff --git a/crates/cdk/src/wallet.rs b/crates/cdk/src/wallet.rs index ab7c6e984..20cebee70 100644 --- a/crates/cdk/src/wallet.rs +++ b/crates/cdk/src/wallet.rs @@ -9,6 +9,7 @@ use bip39::Mnemonic; use bitcoin::hashes::sha256::Hash as Sha256Hash; use bitcoin::hashes::Hash; use thiserror::Error; +use tracing::instrument; use crate::cdk_database::wallet_memory::WalletMemoryDatabase; use crate::cdk_database::{self, WalletDatabase}; @@ -125,6 +126,7 @@ impl Wallet { } /// Total Balance of wallet + #[instrument(skip(self))] pub async fn total_balance(&self) -> Result { let mints = self.localstore.get_mints().await?; let mut balance = Amount::ZERO; @@ -140,6 +142,7 @@ impl Wallet { Ok(balance) } + #[instrument(skip(self))] pub async fn mint_balances(&self) -> Result, Error> { let mints = self.localstore.get_mints().await?; @@ -158,10 +161,12 @@ impl Wallet { Ok(balances) } + #[instrument(skip(self))] pub async fn get_proofs(&self, mint_url: UncheckedUrl) -> Result, Error> { Ok(self.localstore.get_proofs(mint_url).await?) } + #[instrument(skip(self))] pub async fn add_mint(&self, mint_url: UncheckedUrl) -> Result, Error> { let mint_info = match self .client @@ -182,6 +187,7 @@ impl Wallet { Ok(mint_info) } + #[instrument(skip(self))] pub async fn get_keyset_keys( &self, mint_url: &UncheckedUrl, @@ -203,6 +209,7 @@ impl Wallet { Ok(keys) } + #[instrument(skip(self))] pub async fn get_mint_keysets( &self, mint_url: &UncheckedUrl, @@ -217,6 +224,7 @@ impl Wallet { } /// Get active mint keyset + #[instrument(skip(self))] pub async fn get_active_mint_keys( &self, mint_url: &UncheckedUrl, @@ -237,6 +245,7 @@ impl Wallet { } /// Refresh Mint keys + #[instrument(skip(self))] pub async fn refresh_mint_keys(&self, mint_url: &UncheckedUrl) -> Result<(), Error> { let current_mint_keysets_info = self .client @@ -275,6 +284,7 @@ impl Wallet { } /// Check if a proof is spent + #[instrument(skip(self, proofs))] pub async fn check_proofs_spent( &self, mint_url: UncheckedUrl, @@ -296,6 +306,7 @@ impl Wallet { } /// Mint Quote + #[instrument(skip(self))] pub async fn mint_quote( &mut self, mint_url: UncheckedUrl, @@ -321,6 +332,7 @@ impl Wallet { Ok(quote) } + #[instrument(skip(self))] async fn active_mint_keyset( &mut self, mint_url: &UncheckedUrl, @@ -351,6 +363,7 @@ impl Wallet { Err(Error::NoActiveKeyset) } + #[instrument(skip(self))] async fn active_keys( &mut self, mint_url: &UncheckedUrl, @@ -376,6 +389,7 @@ impl Wallet { } /// Mint + #[instrument(skip(self, quote_id))] pub async fn mint(&mut self, mint_url: UncheckedUrl, quote_id: &str) -> Result { // Check that mint is in store of mints if self.localstore.get_mint(mint_url.clone()).await?.is_none() { @@ -483,6 +497,7 @@ impl Wallet { } /// Swap + #[instrument(skip(self, input_proofs))] pub async fn swap( &mut self, mint_url: &UncheckedUrl, @@ -590,6 +605,7 @@ impl Wallet { } /// Create Swap Payload + #[instrument(skip(self, proofs))] async fn create_swap( &mut self, mint_url: &UncheckedUrl, @@ -720,6 +736,7 @@ impl Wallet { } /// Send + #[instrument(skip(self))] pub async fn send( &mut self, mint_url: &UncheckedUrl, @@ -754,6 +771,7 @@ impl Wallet { } /// Melt Quote + #[instrument(skip(self))] pub async fn melt_quote( &mut self, mint_url: UncheckedUrl, @@ -785,6 +803,7 @@ impl Wallet { } // Select proofs + #[instrument(skip(self))] pub async fn select_proofs( &self, mint_url: UncheckedUrl, @@ -845,6 +864,7 @@ impl Wallet { } /// Melt + #[instrument(skip(self, quote_id))] pub async fn melt(&mut self, mint_url: &UncheckedUrl, quote_id: &str) -> Result { let quote_info = self.localstore.get_melt_quote(quote_id).await?; @@ -961,6 +981,7 @@ impl Wallet { } /// Receive + #[instrument(skip_all)] pub async fn receive( &mut self, encoded_token: &str, @@ -977,6 +998,16 @@ impl Wallet { continue; } + // Add mint if it does not exist in the store + if self + .localstore + .get_mint(token.mint.clone()) + .await? + .is_none() + { + self.add_mint(token.mint.clone()).await?; + } + let active_keyset_id = self.active_mint_keyset(&token.mint, &unit).await?; let keys = self.get_keyset_keys(&token.mint, active_keyset_id).await?; @@ -1095,6 +1126,7 @@ impl Wallet { Ok(()) } + #[instrument(skip(self, proofs))] pub fn proofs_to_token( &self, mint_url: UncheckedUrl, @@ -1106,6 +1138,7 @@ impl Wallet { } #[cfg(feature = "nut13")] + #[instrument(skip(self))] pub async fn restore(&mut self, mint_url: UncheckedUrl) -> Result { // Check that mint is in store of mints if self.localstore.get_mint(mint_url.clone()).await?.is_none() { @@ -1209,6 +1242,7 @@ impl Wallet { /// Verify all proofs in token have meet the required spend /// Can be used to allow a wallet to accept payments offline while reducing /// the risk of claiming back to the limits let by the spending_conditions + #[instrument(skip(self, token))] pub fn verify_token_p2pk( &self, token: &Token, @@ -1326,6 +1360,7 @@ impl Wallet { } /// Verify all proofs in token have a valid DLEQ proof + #[instrument(skip(self, token))] pub async fn verify_token_dleq(&self, token: &Token) -> Result<(), Error> { let mut keys_cache: HashMap = HashMap::new();