proof-selection.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //! Wallet example with memory store
  2. use std::collections::HashMap;
  3. use std::sync::Arc;
  4. use std::time::Duration;
  5. use cdk::nuts::nut00::ProofsMethods;
  6. use cdk::nuts::CurrencyUnit;
  7. use cdk::wallet::Wallet;
  8. use cdk::Amount;
  9. use cdk_common::nut02::KeySetInfosMethods;
  10. use cdk_sqlite::wallet::memory;
  11. use rand::random;
  12. #[tokio::main]
  13. async fn main() -> Result<(), Box<dyn std::error::Error>> {
  14. // Generate a random seed for the wallet
  15. let seed = random::<[u8; 64]>();
  16. // Mint URL and currency unit
  17. let mint_url = "https://fake.thesimplekid.dev";
  18. let unit = CurrencyUnit::Sat;
  19. // Initialize the memory store
  20. let localstore = Arc::new(memory::empty().await?);
  21. // Create a new wallet
  22. let wallet = Wallet::new(mint_url, unit, localstore, seed, None)?;
  23. // Amount to mint
  24. for amount in [64] {
  25. let amount = Amount::from(amount);
  26. let quote = wallet.mint_quote(amount, None).await?;
  27. let proofs = wallet
  28. .wait_and_mint_quote(
  29. quote,
  30. Default::default(),
  31. Default::default(),
  32. Duration::from_secs(10),
  33. )
  34. .await?;
  35. // Mint the received amount
  36. let receive_amount = proofs.total_amount()?;
  37. println!("Minted {}", receive_amount);
  38. }
  39. // Get unspent proofs
  40. let proofs = wallet.get_unspent_proofs().await?;
  41. // Select proofs to send
  42. let amount = Amount::from(64);
  43. let active_keyset_ids = wallet
  44. .refresh_keysets()
  45. .await?
  46. .active()
  47. .map(|keyset| keyset.id)
  48. .collect();
  49. let selected =
  50. Wallet::select_proofs(amount, proofs, &active_keyset_ids, &HashMap::new(), false)?;
  51. for (i, proof) in selected.iter().enumerate() {
  52. println!("{}: {}", i, proof.amount);
  53. }
  54. Ok(())
  55. }