p2pk.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. use std::sync::Arc;
  2. use cdk::amount::SplitTarget;
  3. use cdk::error::Error;
  4. use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload, SecretKey, SpendingConditions};
  5. use cdk::wallet::{ReceiveOptions, SendOptions, Wallet, WalletSubscription};
  6. use cdk::Amount;
  7. use cdk_sqlite::wallet::memory;
  8. use rand::random;
  9. use tracing_subscriber::EnvFilter;
  10. #[tokio::main]
  11. async fn main() -> Result<(), Error> {
  12. let default_filter = "debug";
  13. let sqlx_filter = "sqlx=warn,hyper_util=warn,reqwest=warn,rustls=warn";
  14. let env_filter = EnvFilter::new(format!("{},{}", default_filter, sqlx_filter));
  15. // Parse input
  16. tracing_subscriber::fmt().with_env_filter(env_filter).init();
  17. // Initialize the memory store for the wallet
  18. let localstore = Arc::new(memory::empty().await?);
  19. // Generate a random seed for the wallet
  20. let seed = random::<[u8; 32]>();
  21. // Define the mint URL and currency unit
  22. let mint_url = "https://fake.thesimplekid.dev";
  23. let unit = CurrencyUnit::Sat;
  24. let amount = Amount::from(100);
  25. // Create a new wallet
  26. let wallet = Wallet::new(mint_url, unit, localstore, &seed, None).unwrap();
  27. // Request a mint quote from the wallet
  28. let quote = wallet.mint_quote(amount, None).await?;
  29. println!("Minting nuts ...");
  30. // Subscribe to updates on the mint quote state
  31. let mut subscription = wallet
  32. .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
  33. .id
  34. .clone()]))
  35. .await;
  36. // Wait for the mint quote to be paid
  37. while let Some(msg) = subscription.recv().await {
  38. if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
  39. if response.state == MintQuoteState::Paid {
  40. break;
  41. }
  42. }
  43. }
  44. // Mint the received amount
  45. let received_proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
  46. println!(
  47. "Minted nuts: {:?}",
  48. received_proofs
  49. .into_iter()
  50. .map(|p| p.amount)
  51. .collect::<Vec<_>>()
  52. );
  53. // Generate a secret key for spending conditions
  54. let secret = SecretKey::generate();
  55. // Create spending conditions using the generated public key
  56. let spending_conditions = SpendingConditions::new_p2pk(secret.public_key(), None);
  57. // Get the total balance of the wallet
  58. let bal = wallet.total_balance().await?;
  59. println!("Total balance: {}", bal);
  60. // Send a token with the specified amount and spending conditions
  61. let prepared_send = wallet
  62. .prepare_send(
  63. 10.into(),
  64. SendOptions {
  65. conditions: Some(spending_conditions),
  66. include_fee: true,
  67. ..Default::default()
  68. },
  69. )
  70. .await?;
  71. println!("Fee: {}", prepared_send.fee());
  72. let token = wallet.send(prepared_send, None).await?;
  73. println!("Created token locked to pubkey: {}", secret.public_key());
  74. println!("{}", token);
  75. // Receive the token using the secret key
  76. let amount = wallet
  77. .receive(
  78. &token.to_string(),
  79. ReceiveOptions {
  80. p2pk_signing_keys: vec![secret],
  81. ..Default::default()
  82. },
  83. )
  84. .await?;
  85. println!("Redeemed locked token worth: {}", u64::from(amount));
  86. Ok(())
  87. }