p2pk.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use std::sync::Arc;
  2. use std::time::Duration;
  3. use cdk::error::Error;
  4. use cdk::nuts::{CurrencyUnit, SecretKey, SpendingConditions};
  5. use cdk::wallet::{ReceiveOptions, SendOptions, Wallet};
  6. use cdk::{Amount, StreamExt};
  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; 64]>();
  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. let quote = wallet.mint_quote(amount, None).await?;
  28. let mut proof_streams = wallet.proof_stream(
  29. quote,
  30. Default::default(),
  31. Default::default(),
  32. Duration::from_secs(10),
  33. );
  34. while let Some(proofs) = proof_streams.next().await {
  35. let proofs = proofs?;
  36. // Mint the received amount
  37. println!(
  38. "Minted nuts: {:?}",
  39. proofs.into_iter().map(|p| p.amount).collect::<Vec<_>>()
  40. );
  41. // Generate a secret key for spending conditions
  42. let secret = SecretKey::generate();
  43. // Create spending conditions using the generated public key
  44. let spending_conditions = SpendingConditions::new_p2pk(secret.public_key(), None);
  45. // Get the total balance of the wallet
  46. let bal = wallet.total_balance().await?;
  47. println!("Total balance: {}", bal);
  48. // Send a token with the specified amount and spending conditions
  49. let prepared_send = wallet
  50. .prepare_send(
  51. 10.into(),
  52. SendOptions {
  53. conditions: Some(spending_conditions),
  54. include_fee: true,
  55. ..Default::default()
  56. },
  57. )
  58. .await?;
  59. println!("Fee: {}", prepared_send.fee());
  60. let token = prepared_send.confirm(None).await?;
  61. println!("Created token locked to pubkey: {}", secret.public_key());
  62. println!("{}", token);
  63. // Receive the token using the secret key
  64. let amount = wallet
  65. .receive(
  66. &token.to_string(),
  67. ReceiveOptions {
  68. p2pk_signing_keys: vec![secret],
  69. ..Default::default()
  70. },
  71. )
  72. .await?;
  73. println!("Redeemed locked token worth: {}", u64::from(amount));
  74. }
  75. Ok(())
  76. }