p2pk.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use std::str::FromStr;
  2. use std::sync::Arc;
  3. use std::time::Duration;
  4. use cdk::amount::SplitTarget;
  5. use cdk::cdk_database::WalletMemoryDatabase;
  6. use cdk::error::Error;
  7. use cdk::nuts::{Conditions, CurrencyUnit, SecretKey, SpendingConditions};
  8. use cdk::wallet::Wallet;
  9. use cdk::{Amount, UncheckedUrl};
  10. use rand::Rng;
  11. use tokio::time::sleep;
  12. #[tokio::main]
  13. async fn main() -> Result<(), Error> {
  14. let localstore = WalletMemoryDatabase::default();
  15. let seed = rand::thread_rng().gen::<[u8; 32]>();
  16. let mint_url = UncheckedUrl::from_str("https://testnut.cashu.space").unwrap();
  17. let unit = CurrencyUnit::Sat;
  18. let amount = Amount::from(10);
  19. let mut wallet = Wallet::new(Arc::new(localstore), &seed, vec![]);
  20. let quote = wallet
  21. .mint_quote(mint_url.clone(), amount, unit.clone())
  22. .await
  23. .unwrap();
  24. println!("Minting nuts ...");
  25. loop {
  26. let status = wallet
  27. .mint_quote_status(mint_url.clone(), &quote.id)
  28. .await
  29. .unwrap();
  30. println!("Quote status: {}", status.paid);
  31. if status.paid {
  32. break;
  33. }
  34. sleep(Duration::from_secs(5)).await;
  35. }
  36. let _receive_amount = wallet
  37. .mint(mint_url.clone(), &quote.id, SplitTarget::default(), None)
  38. .await
  39. .unwrap();
  40. let secret = SecretKey::generate();
  41. let spending_conditions =
  42. SpendingConditions::new_p2pk(secret.public_key(), Conditions::default());
  43. let token = wallet
  44. .send(
  45. &mint_url,
  46. unit,
  47. None,
  48. amount,
  49. &SplitTarget::None,
  50. Some(spending_conditions),
  51. )
  52. .await
  53. .unwrap();
  54. println!("Created token locked to pubkey: {}", secret.public_key());
  55. println!("{}", token);
  56. wallet.add_p2pk_signing_key(secret).await;
  57. let amount = wallet
  58. .receive(&token, &SplitTarget::default(), None)
  59. .await
  60. .unwrap();
  61. println!("Redeamed locked token worth: {}", u64::from(amount));
  62. Ok(())
  63. }