regtest.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. use std::{str::FromStr, sync::Arc, time::Duration};
  2. use anyhow::{bail, Result};
  3. use bip39::Mnemonic;
  4. use cdk::{
  5. amount::{Amount, SplitTarget},
  6. cdk_database::WalletMemoryDatabase,
  7. nuts::{CurrencyUnit, MeltQuoteState, MintQuoteState, PreMintSecrets, State},
  8. wallet::{client::HttpClient, Wallet},
  9. };
  10. use cdk_integration_tests::init_regtest::{get_mint_url, init_cln_client, init_lnd_client};
  11. use lightning_invoice::Bolt11Invoice;
  12. use ln_regtest_rs::InvoiceStatus;
  13. use tokio::time::sleep;
  14. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  15. async fn test_regtest_mint_melt_round_trip() -> Result<()> {
  16. let lnd_client = init_lnd_client().await.unwrap();
  17. let wallet = Wallet::new(
  18. &get_mint_url(),
  19. CurrencyUnit::Sat,
  20. Arc::new(WalletMemoryDatabase::default()),
  21. &Mnemonic::generate(12)?.to_seed_normalized(""),
  22. None,
  23. )?;
  24. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  25. lnd_client.pay_invoice(mint_quote.request).await?;
  26. let mint_amount = wallet
  27. .mint(&mint_quote.id, SplitTarget::default(), None)
  28. .await?;
  29. assert!(mint_amount == 100.into());
  30. let invoice = lnd_client.create_invoice(50).await?;
  31. let melt = wallet.melt_quote(invoice, None).await?;
  32. let melt = wallet.melt(&melt.id).await.unwrap();
  33. assert!(melt.preimage.is_some());
  34. assert!(melt.state == MeltQuoteState::Paid);
  35. Ok(())
  36. }
  37. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  38. async fn test_regtest_mint_melt() -> Result<()> {
  39. let lnd_client = init_lnd_client().await?;
  40. let wallet = Wallet::new(
  41. &get_mint_url(),
  42. CurrencyUnit::Sat,
  43. Arc::new(WalletMemoryDatabase::default()),
  44. &Mnemonic::generate(12)?.to_seed_normalized(""),
  45. None,
  46. )?;
  47. let mint_amount = Amount::from(100);
  48. let mint_quote = wallet.mint_quote(mint_amount, None).await?;
  49. assert_eq!(mint_quote.amount, mint_amount);
  50. lnd_client.pay_invoice(mint_quote.request).await?;
  51. let mint_amount = wallet
  52. .mint(&mint_quote.id, SplitTarget::default(), None)
  53. .await?;
  54. assert!(mint_amount == 100.into());
  55. Ok(())
  56. }
  57. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  58. async fn test_restore() -> Result<()> {
  59. let lnd_client = init_lnd_client().await?;
  60. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  61. let wallet = Wallet::new(
  62. &get_mint_url(),
  63. CurrencyUnit::Sat,
  64. Arc::new(WalletMemoryDatabase::default()),
  65. &seed,
  66. None,
  67. )?;
  68. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  69. lnd_client.pay_invoice(mint_quote.request).await?;
  70. let _mint_amount = wallet
  71. .mint(&mint_quote.id, SplitTarget::default(), None)
  72. .await?;
  73. assert!(wallet.total_balance().await? == 100.into());
  74. let wallet_2 = Wallet::new(
  75. &get_mint_url(),
  76. CurrencyUnit::Sat,
  77. Arc::new(WalletMemoryDatabase::default()),
  78. &seed,
  79. None,
  80. )?;
  81. assert!(wallet_2.total_balance().await? == 0.into());
  82. let restored = wallet_2.restore().await?;
  83. let proofs = wallet_2.get_proofs().await?;
  84. wallet_2
  85. .swap(None, SplitTarget::default(), proofs, None, false)
  86. .await?;
  87. assert!(restored == 100.into());
  88. assert!(wallet_2.total_balance().await? == 100.into());
  89. let proofs = wallet.get_proofs().await?;
  90. let states = wallet.check_proofs_spent(proofs).await?;
  91. for state in states {
  92. if state.state != State::Spent {
  93. bail!("All proofs should be spent");
  94. }
  95. }
  96. Ok(())
  97. }
  98. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  99. async fn test_pay_invoice_twice() -> Result<()> {
  100. let lnd_client = init_lnd_client().await?;
  101. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  102. let wallet = Wallet::new(
  103. &get_mint_url(),
  104. CurrencyUnit::Sat,
  105. Arc::new(WalletMemoryDatabase::default()),
  106. &seed,
  107. None,
  108. )?;
  109. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  110. lnd_client.pay_invoice(mint_quote.request).await?;
  111. let mint_amount = wallet
  112. .mint(&mint_quote.id, SplitTarget::default(), None)
  113. .await?;
  114. assert_eq!(mint_amount, 100.into());
  115. let invoice = lnd_client.create_invoice(10).await?;
  116. let melt_quote = wallet.melt_quote(invoice.clone(), None).await?;
  117. let melt = wallet.melt(&melt_quote.id).await.unwrap();
  118. let melt_two = wallet.melt_quote(invoice, None).await?;
  119. let melt_two = wallet.melt(&melt_two.id).await;
  120. match melt_two {
  121. Err(err) => match err {
  122. cdk::Error::RequestAlreadyPaid => (),
  123. _ => {
  124. bail!("Wrong invoice already paid");
  125. }
  126. },
  127. Ok(_) => {
  128. bail!("Should not have allowed second payment");
  129. }
  130. }
  131. let balance = wallet.total_balance().await?;
  132. assert_eq!(balance, (Amount::from(100) - melt.fee_paid - melt.amount));
  133. Ok(())
  134. }
  135. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  136. async fn test_internal_payment() -> Result<()> {
  137. let lnd_client = init_lnd_client().await?;
  138. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  139. let wallet = Wallet::new(
  140. &get_mint_url(),
  141. CurrencyUnit::Sat,
  142. Arc::new(WalletMemoryDatabase::default()),
  143. &seed,
  144. None,
  145. )?;
  146. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  147. lnd_client.pay_invoice(mint_quote.request).await?;
  148. let _mint_amount = wallet
  149. .mint(&mint_quote.id, SplitTarget::default(), None)
  150. .await?;
  151. assert!(wallet.total_balance().await? == 100.into());
  152. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  153. let wallet_2 = Wallet::new(
  154. &get_mint_url(),
  155. CurrencyUnit::Sat,
  156. Arc::new(WalletMemoryDatabase::default()),
  157. &seed,
  158. None,
  159. )?;
  160. let mint_quote = wallet_2.mint_quote(10.into(), None).await?;
  161. let melt = wallet.melt_quote(mint_quote.request.clone(), None).await?;
  162. assert_eq!(melt.amount, 10.into());
  163. let _melted = wallet.melt(&melt.id).await.unwrap();
  164. let _wallet_2_mint = wallet_2
  165. .mint(&mint_quote.id, SplitTarget::default(), None)
  166. .await
  167. .unwrap();
  168. let cln_client = init_cln_client().await?;
  169. let payment_hash = Bolt11Invoice::from_str(&mint_quote.request)?;
  170. let check_paid = cln_client
  171. .check_incoming_invoice(payment_hash.payment_hash().to_string())
  172. .await?;
  173. match check_paid {
  174. InvoiceStatus::Unpaid => (),
  175. _ => {
  176. bail!("Invoice has incorrect status: {:?}", check_paid);
  177. }
  178. }
  179. let wallet_2_balance = wallet_2.total_balance().await?;
  180. assert!(wallet_2_balance == 10.into());
  181. let wallet_1_balance = wallet.total_balance().await?;
  182. assert!(wallet_1_balance == 90.into());
  183. Ok(())
  184. }
  185. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  186. async fn test_cached_mint() -> Result<()> {
  187. let lnd_client = init_lnd_client().await.unwrap();
  188. let wallet = Wallet::new(
  189. &get_mint_url(),
  190. CurrencyUnit::Sat,
  191. Arc::new(WalletMemoryDatabase::default()),
  192. &Mnemonic::generate(12)?.to_seed_normalized(""),
  193. None,
  194. )?;
  195. let mint_amount = Amount::from(100);
  196. let quote = wallet.mint_quote(mint_amount, None).await?;
  197. lnd_client.pay_invoice(quote.request).await?;
  198. loop {
  199. let status = wallet.mint_quote_state(&quote.id).await.unwrap();
  200. println!("Quote status: {}", status.state);
  201. if status.state == MintQuoteState::Paid {
  202. break;
  203. }
  204. sleep(Duration::from_secs(5)).await;
  205. }
  206. let active_keyset_id = wallet.get_active_mint_keyset().await?.id;
  207. let http_client = HttpClient::new();
  208. let premint_secrets =
  209. PreMintSecrets::random(active_keyset_id, 31.into(), &SplitTarget::default()).unwrap();
  210. let response = http_client
  211. .post_mint(
  212. get_mint_url().as_str().parse()?,
  213. &quote.id,
  214. premint_secrets.clone(),
  215. )
  216. .await?;
  217. let response1 = http_client
  218. .post_mint(get_mint_url().as_str().parse()?, &quote.id, premint_secrets)
  219. .await?;
  220. assert!(response == response1);
  221. Ok(())
  222. }