main.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // #![deny(unused)]
  2. use std::str::FromStr;
  3. use std::thread;
  4. use std::time::Duration;
  5. use bitcoin::Amount;
  6. use cashu_crab::cashu_wallet::CashuWallet;
  7. use cashu_crab::client::Client;
  8. use cashu_crab::keyset::Keys;
  9. use cashu_crab::types::{Invoice, MintProofs, Proofs, Token};
  10. const MINTURL: &str = "https://testnut.cashu.space";
  11. const MINTAMOUNT: u64 = 21;
  12. const SENDAMOUNT: u64 = 5;
  13. const MELTINVOICE: &str = "lnbc10n1pj9d299pp5dwz8y2xuk3yrrfuayclwpffgnd3htfzmxvd8xqa4hzcp58kdeq2sdq5g9kxy7fqd9h8vmmfvdjscqzzsxqyz5vqsp5hdjrv0gx59qy7m4a9xk66m0kvja9ljjzh6zp6wn8np8kky5xnqks9qyyssqd09y339sp53l0apkrt8klmrrvl7qmxd2237p8m8cz8xcl725ems8al55hkjl6c2sjx755s7m8rlmhwej9u8glv2ltwzfssddzpwk05cp4re0t3";
  14. #[tokio::main]
  15. async fn main() {
  16. let client = Client::new(MINTURL).unwrap();
  17. // NUT-09
  18. test_get_mint_info(&client).await;
  19. let keys = test_get_mint_keys(&client).await;
  20. let wallet = CashuWallet::new(client.to_owned(), keys);
  21. test_get_mint_keysets(&client).await;
  22. test_request_mint(&wallet).await;
  23. let proofs = test_mint(&wallet).await;
  24. let token = Token::new(
  25. client.mint_url.clone(),
  26. proofs,
  27. Some("Hello World".to_string()),
  28. );
  29. let new_token = test_receive(&wallet, &token.convert_to_string().unwrap()).await;
  30. let _proofs = Token::from_str(&new_token).unwrap().token[0].clone().proofs;
  31. let spendable = test_check_spendable(&client, &new_token).await;
  32. let proofs = test_send(&wallet, spendable).await;
  33. test_melt(&wallet, Invoice::from_str(MELTINVOICE).unwrap(), proofs).await;
  34. test_check_fees(&client).await;
  35. }
  36. async fn test_get_mint_keys(client: &Client) -> Keys {
  37. let mint_keys = client.get_keys().await.unwrap();
  38. // println!("{:?}", mint_keys.0.capacity());
  39. assert!(mint_keys.as_hashmap().capacity() > 1);
  40. mint_keys
  41. }
  42. async fn test_get_mint_keysets(client: &Client) {
  43. let mint_keysets = client.get_keysets().await.unwrap();
  44. assert!(!mint_keysets.keysets.is_empty())
  45. }
  46. async fn test_request_mint(wallet: &CashuWallet) {
  47. let mint = wallet
  48. .request_mint(Amount::from_sat(MINTAMOUNT))
  49. .await
  50. .unwrap();
  51. assert!(mint.pr.check_signature().is_ok())
  52. }
  53. async fn test_mint(wallet: &CashuWallet) -> Proofs {
  54. let mint_req = wallet
  55. .request_mint(Amount::from_sat(MINTAMOUNT))
  56. .await
  57. .unwrap();
  58. println!("Mint Req: {:?}", mint_req.pr.to_string());
  59. // Since before the mint happens the invoice in the mint req has to be paid this wait is here
  60. // probally some way to simulate this in a better way
  61. // but for now pay it quick
  62. thread::sleep(Duration::from_secs(30));
  63. wallet
  64. .mint_token(Amount::from_sat(MINTAMOUNT), &mint_req.hash)
  65. .await
  66. .unwrap()
  67. // println!("Mint: {:?}", mint_res.to_string());
  68. }
  69. async fn test_check_fees(mint: &Client) {
  70. let invoice = Invoice::from_str(MELTINVOICE).unwrap();
  71. let _fee = mint.check_fees(invoice).await.unwrap();
  72. // println!("{fee:?}");
  73. }
  74. async fn test_receive(wallet: &CashuWallet, token: &str) -> String {
  75. let prom = wallet.receive(token).await.unwrap();
  76. println!("{:?}", prom);
  77. let token = MintProofs {
  78. mint: wallet.client.mint_url.clone(),
  79. proofs: prom,
  80. };
  81. let token = Token {
  82. token: vec![token],
  83. memo: Some("Hello world".to_string()),
  84. };
  85. let s = token.convert_to_string();
  86. // println!("{s}");
  87. s.unwrap()
  88. }
  89. async fn test_check_spendable(client: &Client, token: &str) -> Proofs {
  90. let mint_keys = client.get_keys().await.unwrap();
  91. let wallet = CashuWallet::new(client.to_owned(), mint_keys);
  92. let token_data = Token::from_str(token).unwrap();
  93. let spendable = wallet
  94. .check_proofs_spent(token_data.token[0].clone().proofs)
  95. .await
  96. .unwrap();
  97. println!("Spendable: {:?}", spendable);
  98. assert!(!spendable.spendable.is_empty());
  99. spendable.spendable
  100. }
  101. async fn test_send(wallet: &CashuWallet, proofs: Proofs) -> Proofs {
  102. let send = wallet
  103. .send(Amount::from_sat(SENDAMOUNT), proofs)
  104. .await
  105. .unwrap();
  106. println!("{:?}", send);
  107. let keep_token = wallet
  108. .proofs_to_token(send.change_proofs, Some("Keeping these".to_string()))
  109. .unwrap();
  110. let send_token = wallet
  111. .proofs_to_token(send.send_proofs.clone(), Some("Sending these".to_string()))
  112. .unwrap();
  113. println!("Keep Token: {keep_token}");
  114. println!("Send Token: {send_token}");
  115. send.send_proofs
  116. }
  117. async fn test_melt(wallet: &CashuWallet, invoice: Invoice, proofs: Proofs) {
  118. let res = wallet.melt(invoice, proofs).await.unwrap();
  119. println!("{:?}", res);
  120. }
  121. async fn test_get_mint_info(mint: &Client) {
  122. // let mint_info = mint.get_info().await.unwrap();
  123. // println!("{:?}", mint_info);
  124. }