Skip to content

Commit

Permalink
Add wallet tracing and fix receive bug
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcaseria committed May 13, 2024
1 parent 2671c92 commit 7b52d5d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
23 changes: 23 additions & 0 deletions crates/cdk-redb/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -104,6 +106,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip(self))]
async fn get_mint(&self, mint_url: UncheckedUrl) -> Result<Option<MintInfo>, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Into::<Error>::into)?;
Expand All @@ -119,6 +122,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(None)
}

#[instrument(skip(self))]
async fn get_mints(&self) -> Result<HashMap<UncheckedUrl, Option<MintInfo>>, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Error::from)?;
Expand All @@ -138,6 +142,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(mints)
}

#[instrument(skip(self))]
async fn add_mint_keysets(
&self,
mint_url: UncheckedUrl,
Expand Down Expand Up @@ -168,6 +173,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip(self))]
async fn get_mint_keysets(
&self,
mint_url: UncheckedUrl,
Expand All @@ -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)?;
Expand All @@ -209,6 +216,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip_all)]
async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<MintQuote>, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Into::<Error>::into)?;
Expand All @@ -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)?;
Expand All @@ -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)?;
Expand All @@ -260,6 +270,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip_all)]
async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<MeltQuote>, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Error::from)?;
Expand All @@ -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)?;
Expand All @@ -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)?;
Expand All @@ -309,6 +322,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip(self))]
async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Error::from)?;
Expand All @@ -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)?;
Expand All @@ -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;

Expand All @@ -360,6 +376,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip(self))]
async fn get_proofs(&self, mint_url: UncheckedUrl) -> Result<Option<Proofs>, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Error::from)?;
Expand All @@ -377,6 +394,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(proofs)
}

#[instrument(skip(self, proofs))]
async fn remove_proofs(
&self,
mint_url: UncheckedUrl,
Expand Down Expand Up @@ -405,6 +423,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip(self, proofs))]
async fn add_pending_proofs(
&self,
mint_url: UncheckedUrl,
Expand Down Expand Up @@ -433,6 +452,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip(self))]
async fn get_pending_proofs(
&self,
mint_url: UncheckedUrl,
Expand All @@ -453,6 +473,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(proofs)
}

#[instrument(skip(self, proofs))]
async fn remove_pending_proofs(
&self,
mint_url: UncheckedUrl,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -512,6 +534,7 @@ impl WalletDatabase for RedbWalletDatabase {
Ok(())
}

#[instrument(skip(self))]
async fn get_keyset_counter(&self, keyset_id: &Id) -> Result<Option<u64>, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Error::from)?;
Expand Down
30 changes: 22 additions & 8 deletions crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
12 changes: 12 additions & 0 deletions crates/cdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use reqwest::Client;
use serde_json::Value;
use thiserror::Error;
use tracing::instrument;
use url::Url;

use crate::error::ErrorResponse;
Expand Down Expand Up @@ -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<Vec<KeySet>, Error> {
let url = join_url(mint_url, &["v1", "keys"])?;
let keys = self.inner.get(url).send().await?.json::<Value>().await?;
Expand All @@ -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<KeySet, Error> {
let url = join_url(mint_url, &["v1", "keys", &keyset_id.to_string()])?;
let keys = self
Expand All @@ -99,6 +102,7 @@ impl HttpClient {
}

/// Get Keysets [NUT-02]
#[instrument(skip(self))]
pub async fn get_mint_keysets(&self, mint_url: Url) -> Result<KeysetResponse, Error> {
let url = join_url(mint_url, &["v1", "keysets"])?;
let res = self.inner.get(url).send().await?.json::<Value>().await?;
Expand All @@ -113,6 +117,7 @@ impl HttpClient {
}

/// Mint Quote [NUT-04]
#[instrument(skip(self))]
pub async fn post_mint_quote(
&self,
mint_url: Url,
Expand All @@ -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,
Expand Down Expand Up @@ -169,6 +175,7 @@ impl HttpClient {
}

/// Melt Quote [NUT-05]
#[instrument(skip(self))]
pub async fn post_melt_quote(
&self,
mint_url: Url,
Expand All @@ -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,
Expand Down Expand Up @@ -222,6 +230,7 @@ impl HttpClient {
}

/// Split Token [NUT-06]
#[instrument(skip(self, swap_request))]
pub async fn post_swap(
&self,
mint_url: Url,
Expand All @@ -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<MintInfo, Error> {
let url = join_url(mint_url, &["v1", "info"])?;

Expand All @@ -255,6 +265,7 @@ impl HttpClient {
}

/// Spendable check [NUT-07]
#[instrument(skip(self))]
pub async fn post_check_state(
&self,
mint_url: Url,
Expand All @@ -281,6 +292,7 @@ impl HttpClient {
}
}

#[instrument(skip(self, request))]
pub async fn post_restore(
&self,
mint_url: Url,
Expand Down
Loading

0 comments on commit 7b52d5d

Please sign in to comment.