p2pk.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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;
  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 proofs = wallet
  29. .wait_and_mint_quote(
  30. quote,
  31. Default::default(),
  32. Default::default(),
  33. Duration::from_secs(10),
  34. )
  35. .await?;
  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. let token_amount_to_send = Amount::from(10);
  49. // Send a token with the specified amount and spending conditions
  50. let prepared_send = wallet
  51. .prepare_send(
  52. token_amount_to_send,
  53. SendOptions {
  54. conditions: Some(spending_conditions),
  55. include_fee: true,
  56. ..Default::default()
  57. },
  58. )
  59. .await?;
  60. let swap_fee = prepared_send.swap_fee();
  61. println!("Fee: {}", swap_fee);
  62. let token = prepared_send.confirm(None).await?;
  63. println!("Created token locked to pubkey: {}", secret.public_key());
  64. println!("{}", token);
  65. // Receive the token using the secret key
  66. let amount = wallet
  67. .receive(
  68. &token.to_string(),
  69. ReceiveOptions {
  70. p2pk_signing_keys: vec![secret],
  71. ..Default::default()
  72. },
  73. )
  74. .await?;
  75. assert!(amount == token_amount_to_send);
  76. println!("Redeemed locked token worth: {}", u64::from(amount));
  77. Ok(())
  78. }