proof-selection.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, StreamExt};
  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 mut proof_streams = wallet.proof_stream(
  28. quote,
  29. Default::default(),
  30. Default::default(),
  31. Duration::from_secs(10),
  32. );
  33. while let Some(proofs) = proof_streams.next().await {
  34. // Mint the received amount
  35. let receive_amount = proofs?.total_amount()?;
  36. println!("Minted {}", receive_amount);
  37. }
  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. }