mint-token.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use std::sync::Arc;
  2. use std::time::Duration;
  3. use cdk::error::Error;
  4. use cdk::nuts::nut00::ProofsMethods;
  5. use cdk::nuts::CurrencyUnit;
  6. use cdk::wallet::{SendOptions, Wallet};
  7. use cdk::Amount;
  8. use cdk_sqlite::wallet::memory;
  9. use rand::random;
  10. use tracing_subscriber::EnvFilter;
  11. #[tokio::main]
  12. async fn main() -> Result<(), Error> {
  13. let default_filter = "debug";
  14. let sqlx_filter = "sqlx=warn,hyper_util=warn,reqwest=warn,rustls=warn";
  15. let env_filter = EnvFilter::new(format!("{},{}", default_filter, sqlx_filter));
  16. // Parse input
  17. tracing_subscriber::fmt().with_env_filter(env_filter).init();
  18. // Initialize the memory store for the wallet
  19. let localstore = Arc::new(memory::empty().await?);
  20. // Generate a random seed for the wallet
  21. let seed = random::<[u8; 64]>();
  22. // Define the mint URL and currency unit
  23. let mint_url = "https://fake.thesimplekid.dev";
  24. let unit = CurrencyUnit::Sat;
  25. let amount = Amount::from(10);
  26. // Create a new wallet
  27. let wallet = Wallet::new(mint_url, unit, localstore, seed, None)?;
  28. let quote = wallet.mint_quote(amount, None).await?;
  29. let proofs = wallet
  30. .wait_and_mint_quote(
  31. quote,
  32. Default::default(),
  33. Default::default(),
  34. Duration::from_secs(10),
  35. )
  36. .await?;
  37. // Mint the received amount
  38. let receive_amount = proofs.total_amount()?;
  39. println!("Received {} from mint {}", receive_amount, mint_url);
  40. // Send a token with the specified amount
  41. let prepared_send = wallet.prepare_send(amount, SendOptions::default()).await?;
  42. let token = prepared_send.confirm(None).await?;
  43. println!("Token:");
  44. println!("{}", token);
  45. Ok(())
  46. }