regtest.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. use std::fmt::Debug;
  2. use std::str::FromStr;
  3. use std::sync::Arc;
  4. use std::time::Duration;
  5. use anyhow::{bail, Result};
  6. use bip39::Mnemonic;
  7. use cdk::amount::{Amount, SplitTarget};
  8. use cdk::cdk_database::WalletMemoryDatabase;
  9. use cdk::nuts::{
  10. CurrencyUnit, MeltQuoteState, MintBolt11Request, MintQuoteState, NotificationPayload,
  11. PreMintSecrets, State,
  12. };
  13. use cdk::wallet::client::{HttpClient, MintConnector};
  14. use cdk::wallet::{Wallet, WalletSubscription};
  15. use cdk_integration_tests::init_regtest::{
  16. get_mint_url, get_mint_ws_url, init_cln_client, init_lnd_client,
  17. };
  18. use futures::{SinkExt, StreamExt};
  19. use lightning_invoice::Bolt11Invoice;
  20. use ln_regtest_rs::ln_client::LightningClient;
  21. use ln_regtest_rs::InvoiceStatus;
  22. use serde_json::json;
  23. use tokio::time::timeout;
  24. use tokio_tungstenite::connect_async;
  25. use tokio_tungstenite::tungstenite::protocol::Message;
  26. async fn get_notification<T: StreamExt<Item = Result<Message, E>> + Unpin, E: Debug>(
  27. reader: &mut T,
  28. timeout_to_wait: Duration,
  29. ) -> (String, NotificationPayload<String>) {
  30. let msg = timeout(timeout_to_wait, reader.next())
  31. .await
  32. .expect("timeout")
  33. .unwrap()
  34. .unwrap();
  35. let mut response: serde_json::Value =
  36. serde_json::from_str(msg.to_text().unwrap()).expect("valid json");
  37. let mut params_raw = response
  38. .as_object_mut()
  39. .expect("object")
  40. .remove("params")
  41. .expect("valid params");
  42. let params_map = params_raw.as_object_mut().expect("params is object");
  43. (
  44. params_map
  45. .remove("subId")
  46. .unwrap()
  47. .as_str()
  48. .unwrap()
  49. .to_string(),
  50. serde_json::from_value(params_map.remove("payload").unwrap()).unwrap(),
  51. )
  52. }
  53. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  54. async fn test_regtest_mint_melt_round_trip() -> Result<()> {
  55. let lnd_client = init_lnd_client().await.unwrap();
  56. let wallet = Wallet::new(
  57. &get_mint_url(),
  58. CurrencyUnit::Sat,
  59. Arc::new(WalletMemoryDatabase::default()),
  60. &Mnemonic::generate(12)?.to_seed_normalized(""),
  61. None,
  62. )?;
  63. let (ws_stream, _) = connect_async(get_mint_ws_url())
  64. .await
  65. .expect("Failed to connect");
  66. let (mut write, mut reader) = ws_stream.split();
  67. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  68. lnd_client.pay_invoice(mint_quote.request).await?;
  69. let mint_amount = wallet
  70. .mint(&mint_quote.id, SplitTarget::default(), None)
  71. .await?;
  72. assert!(mint_amount == 100.into());
  73. let invoice = lnd_client.create_invoice(Some(50)).await?;
  74. let melt = wallet.melt_quote(invoice, None).await?;
  75. write
  76. .send(Message::Text(serde_json::to_string(&json!({
  77. "jsonrpc": "2.0",
  78. "id": 2,
  79. "method": "subscribe",
  80. "params": {
  81. "kind": "bolt11_melt_quote",
  82. "filters": [
  83. melt.id.clone(),
  84. ],
  85. "subId": "test-sub",
  86. }
  87. }))?))
  88. .await?;
  89. assert_eq!(
  90. reader.next().await.unwrap().unwrap().to_text().unwrap(),
  91. r#"{"jsonrpc":"2.0","result":{"status":"OK","subId":"test-sub"},"id":2}"#
  92. );
  93. let melt_response = wallet.melt(&melt.id).await.unwrap();
  94. assert!(melt_response.preimage.is_some());
  95. assert!(melt_response.state == MeltQuoteState::Paid);
  96. let (sub_id, payload) = get_notification(&mut reader, Duration::from_millis(15000)).await;
  97. // first message is the current state
  98. assert_eq!("test-sub", sub_id);
  99. let payload = match payload {
  100. NotificationPayload::MeltQuoteBolt11Response(melt) => melt,
  101. _ => panic!("Wrong payload"),
  102. };
  103. assert_eq!(payload.amount + payload.fee_reserve, 100.into());
  104. assert_eq!(payload.quote.to_string(), melt.id);
  105. assert_eq!(payload.state, MeltQuoteState::Unpaid);
  106. // get current state
  107. let (sub_id, payload) = get_notification(&mut reader, Duration::from_millis(15000)).await;
  108. assert_eq!("test-sub", sub_id);
  109. let payload = match payload {
  110. NotificationPayload::MeltQuoteBolt11Response(melt) => melt,
  111. _ => panic!("Wrong payload"),
  112. };
  113. assert_eq!(payload.amount + payload.fee_reserve, 100.into());
  114. assert_eq!(payload.quote.to_string(), melt.id);
  115. assert_eq!(payload.state, MeltQuoteState::Paid);
  116. Ok(())
  117. }
  118. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  119. async fn test_regtest_mint_melt() -> Result<()> {
  120. let lnd_client = init_lnd_client().await?;
  121. let wallet = Wallet::new(
  122. &get_mint_url(),
  123. CurrencyUnit::Sat,
  124. Arc::new(WalletMemoryDatabase::default()),
  125. &Mnemonic::generate(12)?.to_seed_normalized(""),
  126. None,
  127. )?;
  128. let mint_amount = Amount::from(100);
  129. let mint_quote = wallet.mint_quote(mint_amount, None).await?;
  130. assert_eq!(mint_quote.amount, mint_amount);
  131. lnd_client.pay_invoice(mint_quote.request).await?;
  132. let mint_amount = wallet
  133. .mint(&mint_quote.id, SplitTarget::default(), None)
  134. .await?;
  135. assert!(mint_amount == 100.into());
  136. Ok(())
  137. }
  138. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  139. async fn test_restore() -> Result<()> {
  140. let lnd_client = init_lnd_client().await?;
  141. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  142. let wallet = Wallet::new(
  143. &get_mint_url(),
  144. CurrencyUnit::Sat,
  145. Arc::new(WalletMemoryDatabase::default()),
  146. &seed,
  147. None,
  148. )?;
  149. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  150. lnd_client.pay_invoice(mint_quote.request).await?;
  151. let _mint_amount = wallet
  152. .mint(&mint_quote.id, SplitTarget::default(), None)
  153. .await?;
  154. assert!(wallet.total_balance().await? == 100.into());
  155. let wallet_2 = Wallet::new(
  156. &get_mint_url(),
  157. CurrencyUnit::Sat,
  158. Arc::new(WalletMemoryDatabase::default()),
  159. &seed,
  160. None,
  161. )?;
  162. assert!(wallet_2.total_balance().await? == 0.into());
  163. let restored = wallet_2.restore().await?;
  164. let proofs = wallet_2.get_unspent_proofs().await?;
  165. wallet_2
  166. .swap(None, SplitTarget::default(), proofs, None, false)
  167. .await?;
  168. assert!(restored == 100.into());
  169. assert!(wallet_2.total_balance().await? == 100.into());
  170. let proofs = wallet.get_unspent_proofs().await?;
  171. let states = wallet.check_proofs_spent(proofs).await?;
  172. for state in states {
  173. if state.state != State::Spent {
  174. bail!("All proofs should be spent");
  175. }
  176. }
  177. Ok(())
  178. }
  179. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  180. async fn test_pay_invoice_twice() -> Result<()> {
  181. let lnd_client = init_lnd_client().await?;
  182. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  183. let wallet = Wallet::new(
  184. &get_mint_url(),
  185. CurrencyUnit::Sat,
  186. Arc::new(WalletMemoryDatabase::default()),
  187. &seed,
  188. None,
  189. )?;
  190. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  191. lnd_client.pay_invoice(mint_quote.request).await?;
  192. let mint_amount = wallet
  193. .mint(&mint_quote.id, SplitTarget::default(), None)
  194. .await?;
  195. assert_eq!(mint_amount, 100.into());
  196. let invoice = lnd_client.create_invoice(Some(10)).await?;
  197. let melt_quote = wallet.melt_quote(invoice.clone(), None).await?;
  198. let melt = wallet.melt(&melt_quote.id).await.unwrap();
  199. let melt_two = wallet.melt_quote(invoice, None).await?;
  200. let melt_two = wallet.melt(&melt_two.id).await;
  201. match melt_two {
  202. Err(err) => match err {
  203. cdk::Error::RequestAlreadyPaid => (),
  204. _ => {
  205. bail!("Wrong invoice already paid");
  206. }
  207. },
  208. Ok(_) => {
  209. bail!("Should not have allowed second payment");
  210. }
  211. }
  212. let balance = wallet.total_balance().await?;
  213. assert_eq!(balance, (Amount::from(100) - melt.fee_paid - melt.amount));
  214. Ok(())
  215. }
  216. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  217. async fn test_internal_payment() -> Result<()> {
  218. let lnd_client = init_lnd_client().await?;
  219. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  220. let wallet = Wallet::new(
  221. &get_mint_url(),
  222. CurrencyUnit::Sat,
  223. Arc::new(WalletMemoryDatabase::default()),
  224. &seed,
  225. None,
  226. )?;
  227. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  228. lnd_client.pay_invoice(mint_quote.request).await?;
  229. let _mint_amount = wallet
  230. .mint(&mint_quote.id, SplitTarget::default(), None)
  231. .await?;
  232. assert!(wallet.total_balance().await? == 100.into());
  233. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  234. let wallet_2 = Wallet::new(
  235. &get_mint_url(),
  236. CurrencyUnit::Sat,
  237. Arc::new(WalletMemoryDatabase::default()),
  238. &seed,
  239. None,
  240. )?;
  241. let mint_quote = wallet_2.mint_quote(10.into(), None).await?;
  242. let melt = wallet.melt_quote(mint_quote.request.clone(), None).await?;
  243. assert_eq!(melt.amount, 10.into());
  244. let _melted = wallet.melt(&melt.id).await.unwrap();
  245. let _wallet_2_mint = wallet_2
  246. .mint(&mint_quote.id, SplitTarget::default(), None)
  247. .await
  248. .unwrap();
  249. let cln_client = init_cln_client().await?;
  250. let payment_hash = Bolt11Invoice::from_str(&mint_quote.request)?;
  251. let check_paid = cln_client
  252. .check_incoming_payment_status(&payment_hash.payment_hash().to_string())
  253. .await?;
  254. match check_paid {
  255. InvoiceStatus::Unpaid => (),
  256. _ => {
  257. bail!("Invoice has incorrect status: {:?}", check_paid);
  258. }
  259. }
  260. let wallet_2_balance = wallet_2.total_balance().await?;
  261. assert!(wallet_2_balance == 10.into());
  262. let wallet_1_balance = wallet.total_balance().await?;
  263. assert!(wallet_1_balance == 90.into());
  264. Ok(())
  265. }
  266. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  267. async fn test_cached_mint() -> Result<()> {
  268. let lnd_client = init_lnd_client().await.unwrap();
  269. let wallet = Wallet::new(
  270. &get_mint_url(),
  271. CurrencyUnit::Sat,
  272. Arc::new(WalletMemoryDatabase::default()),
  273. &Mnemonic::generate(12)?.to_seed_normalized(""),
  274. None,
  275. )?;
  276. let mint_amount = Amount::from(100);
  277. let quote = wallet.mint_quote(mint_amount, None).await?;
  278. lnd_client.pay_invoice(quote.request).await?;
  279. let mut subscription = wallet
  280. .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
  281. .id
  282. .clone()]))
  283. .await;
  284. while let Some(msg) = subscription.recv().await {
  285. if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
  286. if response.state == MintQuoteState::Paid {
  287. break;
  288. }
  289. }
  290. }
  291. let active_keyset_id = wallet.get_active_mint_keyset().await?.id;
  292. let http_client = HttpClient::new(get_mint_url().as_str().parse()?);
  293. let premint_secrets =
  294. PreMintSecrets::random(active_keyset_id, 31.into(), &SplitTarget::default()).unwrap();
  295. let request = MintBolt11Request {
  296. quote: quote.id,
  297. outputs: premint_secrets.blinded_messages(),
  298. };
  299. let response = http_client.post_mint(request.clone()).await?;
  300. let response1 = http_client.post_mint(request).await?;
  301. assert!(response == response1);
  302. Ok(())
  303. }