Skip to content

Commit

Permalink
Fix CDK docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcaseria committed Feb 5, 2025
1 parent 7f67d51 commit c5569ad
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 47 deletions.
6 changes: 4 additions & 2 deletions crates/cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ See <https://github.com/cashubtc/cdk/blob/main/README.md>
```rust
//! Wallet example with memory store

use std:str::FromStr;
use std::sync::Arc;
use std::time::Duration;

use cdk::amount::SplitTarget;
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::mint_url::MintUrl;
use cdk::nuts::{CurrencyUnit, MintQuoteState};
use cdk::wallet::Wallet;
use cdk::Amount;
Expand All @@ -38,13 +40,13 @@ use tokio::time::sleep;
async fn main() {
let seed = rand::thread_rng().gen::<[u8; 32]>();

let mint_url = "https://testnut.cashu.space";
let mint_url = MintUrl.from_str("https://testnut.cashu.space").unwrap();
let unit = CurrencyUnit::Sat;
let amount = Amount::from(10);

let localstore = WalletMemoryDatabase::default();

let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed);
let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit).unwrap();

let quote = wallet.mint_quote(amount).await.unwrap();

Expand Down
8 changes: 4 additions & 4 deletions crates/cdk/src/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ The CDK [`Wallet`] is a high level Cashu wallet. The [`Wallet`] is for a single

### Create [`Wallet`]
```rust
use std::str::FromStr;
use std::sync::Arc;
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::mint_url::MintUrl;
use cdk::nuts::CurrencyUnit;
use cdk::wallet::Wallet;
use rand::Rng;

let seed = rand::thread_rng().gen::<[u8; 32]>();
let mint_url = "https://testnut.cashu.space";
let mint_url = MintUrl::from_str("https://testnut.cashu.space").unwrap();
let unit = CurrencyUnit::Sat;

let localstore = WalletMemoryDatabase::default();
let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None);
let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit).unwrap();
```
21 changes: 9 additions & 12 deletions crates/cdk/src/wallet/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ impl Wallet {
/// Melt Quote
/// # Synopsis
/// ```rust
/// use std::str::FromStr;
/// use std::sync::Arc;
///
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::mint_url::MintUrl;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let mint_url = MintUrl::from_str("https://testnut.cashu.space")?;
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = WalletMemoryDatabase::default();
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
/// let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit)?;
/// let bolt11 = "lnbc100n1pnvpufspp5djn8hrq49r8cghwye9kqw752qjncwyfnrprhprpqk43mwcy4yfsqdq5g9kxy7fqd9h8vmmfvdjscqzzsxqyz5vqsp5uhpjt36rj75pl7jq2sshaukzfkt7uulj456s4mh7uy7l6vx7lvxs9qxpqysgqedwz08acmqwtk8g4vkwm2w78suwt2qyzz6jkkwcgrjm3r3hs6fskyhvud4fan3keru7emjm8ygqpcrwtlmhfjfmer3afs5hhwamgr4cqtactdq".to_string();
/// let quote = wallet.melt_quote(bolt11, None).await?;
///
Expand Down Expand Up @@ -134,10 +134,7 @@ impl Wallet {

let active_keyset_id = self.get_active_mint_keyset().await?.id;

let count = self
.proof_db
.get_keyset_counter(&active_keyset_id)
.await?;
let count = self.proof_db.get_keyset_counter(&active_keyset_id).await?;

let count = count.map_or(0, |c| c + 1);

Expand Down Expand Up @@ -247,21 +244,21 @@ impl Wallet {
/// Melt
/// # Synopsis
/// ```rust, no_run
/// use std::str::FromStr;
/// use std::sync::Arc;
///
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::mint_url::MintUrl;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let mint_url = MintUrl::from_str("https://testnut.cashu.space")?;
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = WalletMemoryDatabase::default();
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
/// let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit)?;
/// let bolt11 = "lnbc100n1pnvpufspp5djn8hrq49r8cghwye9kqw752qjncwyfnrprhprpqk43mwcy4yfsqdq5g9kxy7fqd9h8vmmfvdjscqzzsxqyz5vqsp5uhpjt36rj75pl7jq2sshaukzfkt7uulj456s4mh7uy7l6vx7lvxs9qxpqysgqedwz08acmqwtk8g4vkwm2w78suwt2qyzz6jkkwcgrjm3r3hs6fskyhvud4fan3keru7emjm8ygqpcrwtlmhfjfmer3afs5hhwamgr4cqtactdq".to_string();
/// let quote = wallet.melt_quote(bolt11, None).await?;
/// let quote_id = quote.id;
Expand Down
21 changes: 9 additions & 12 deletions crates/cdk/src/wallet/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ impl Wallet {
/// Mint Quote
/// # Synopsis
/// ```rust
/// use std::str::FromStr;
/// use std::sync::Arc;
///
/// use cdk::amount::Amount;
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::mint_url::MintUrl;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let mint_url = MintUrl::from_str("https://testnut.cashu.space")?;
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = WalletMemoryDatabase::default();
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None)?;
/// let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit)?;
/// let amount = Amount::from(100);
///
/// let quote = wallet.mint_quote(amount, None).await?;
Expand Down Expand Up @@ -140,11 +140,12 @@ impl Wallet {
/// Mint
/// # Synopsis
/// ```rust
/// use std::str::FromStr;
/// use std::sync::Arc;
///
/// use anyhow::Result;
/// use cdk::amount::{Amount, SplitTarget};
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::mint_url::MintUrl;
/// use cdk::nuts::nut00::ProofsMethods;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
Expand All @@ -153,11 +154,10 @@ impl Wallet {
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let mint_url = MintUrl::from_str("https://testnut.cashu.space")?;
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = WalletMemoryDatabase::default();
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
/// let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit)?;
/// let amount = Amount::from(100);
///
/// let quote = wallet.mint_quote(amount, None).await?;
Expand Down Expand Up @@ -200,10 +200,7 @@ impl Wallet {

let active_keyset_id = self.get_active_mint_keyset().await?.id;

let count = self
.proof_db
.get_keyset_counter(&active_keyset_id)
.await?;
let count = self.proof_db.get_keyset_counter(&active_keyset_id).await?;

let count = count.map_or(0, |c| c + 1);

Expand Down
18 changes: 9 additions & 9 deletions crates/cdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,27 @@ impl WalletBuilder {

impl Wallet {
/// Create new [`Wallet`] using [`WalletBuilder`]
pub fn builder(seed: Vec<u8>) -> WalletBuilder {
WalletBuilder::new(seed)
}

/// Create new [`Wallet`]
/// # Synopsis
/// ```rust
/// use std::str::FromStr;
/// use std::sync::Arc;
///
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::mint_url::MintUrl;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let mint_url = MintUrl::from_str("https://testnut.cashu.space").unwrap();
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = WalletMemoryDatabase::default();
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None);
/// let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit).unwrap();
/// ```
pub fn builder(seed: Vec<u8>) -> WalletBuilder {
WalletBuilder::new(seed)
}

/// Create new [`Wallet`]
pub fn new(
mint_url: &str,
unit: CurrencyUnit,
Expand Down
16 changes: 8 additions & 8 deletions crates/cdk/src/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,22 @@ impl Wallet {
/// Receive
/// # Synopsis
/// ```rust, no_run
/// use std::str::FromStr;
/// use std::sync::Arc;
///
/// use cdk::amount::SplitTarget;
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::mint_url::MintUrl;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use rand::Rng;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let mint_url = MintUrl::from_str("https://testnut.cashu.space")?;
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = WalletMemoryDatabase::default();
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
/// let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit)?;
/// let token = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJhbW91bnQiOjEsInNlY3JldCI6ImI0ZjVlNDAxMDJhMzhiYjg3NDNiOTkwMzU5MTU1MGYyZGEzZTQxNWEzMzU0OTUyN2M2MmM5ZDc5MGVmYjM3MDUiLCJDIjoiMDIzYmU1M2U4YzYwNTMwZWVhOWIzOTQzZmRhMWEyY2U3MWM3YjNmMGNmMGRjNmQ4NDZmYTc2NWFhZjc3OWZhODFkIiwiaWQiOiIwMDlhMWYyOTMyNTNlNDFlIn1dLCJtaW50IjoiaHR0cHM6Ly90ZXN0bnV0LmNhc2h1LnNwYWNlIn1dLCJ1bml0Ijoic2F0In0=";
/// let amount_receive = wallet.receive(token, SplitTarget::default(), &[], &[]).await?;
/// Ok(())
Expand Down Expand Up @@ -220,10 +220,11 @@ impl Wallet {
/// Receive
/// # Synopsis
/// ```rust, no_run
/// use std::str::FromStr;
/// use std::sync::Arc;
///
/// use cdk::amount::SplitTarget;
/// use cdk::cdk_database::WalletMemoryDatabase;
/// use cdk::mint_url::MintUrl;
/// use cdk::nuts::CurrencyUnit;
/// use cdk::wallet::Wallet;
/// use cdk::util::hex;
Expand All @@ -232,11 +233,10 @@ impl Wallet {
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
/// let seed = rand::thread_rng().gen::<[u8; 32]>();
/// let mint_url = "https://testnut.cashu.space";
/// let mint_url = MintUrl::from_str("https://testnut.cashu.space").unwrap();
/// let unit = CurrencyUnit::Sat;
///
/// let localstore = WalletMemoryDatabase::default();
/// let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
/// let wallet = Wallet::builder(seed.to_vec()).build(mint_url, unit)?;
/// let token_raw = hex::decode("6372617742a4617481a261694800ad268c4d1f5826617081a3616101617378403961366462623834376264323332626137366462306466313937323136623239643362386363313435353363643237383237666331636339343266656462346561635821038618543ffb6b8695df4ad4babcde92a34a96bdcd97dcee0d7ccf98d4721267926164695468616e6b20796f75616d75687474703a2f2f6c6f63616c686f73743a33333338617563736174").unwrap();
/// let amount_receive = wallet.receive_raw(&token_raw, SplitTarget::default(), &[], &[]).await?;
/// Ok(())
Expand Down

0 comments on commit c5569ad

Please sign in to comment.