fake_wallet.rs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. //! Fake Wallet Integration Tests
  2. //!
  3. //! This file contains tests for the fake wallet backend functionality.
  4. //! The fake wallet simulates Lightning Network behavior for testing purposes,
  5. //! allowing verification of mint behavior in various payment scenarios without
  6. //! requiring a real Lightning node.
  7. //!
  8. //! Test Scenarios:
  9. //! - Pending payment states and proof handling
  10. //! - Payment failure cases and proof state management
  11. //! - Change output verification in melt operations
  12. //! - Witness signature validation
  13. //! - Cross-unit transaction validation
  14. //! - Overflow and balance validation
  15. //! - Duplicate proof detection
  16. use std::sync::Arc;
  17. use std::time::Duration;
  18. use bip39::Mnemonic;
  19. use cashu::Amount;
  20. use cdk::amount::SplitTarget;
  21. use cdk::nuts::nut00::ProofsMethods;
  22. use cdk::nuts::{
  23. CurrencyUnit, MeltQuoteState, MeltRequest, MintRequest, PreMintSecrets, Proofs, SecretKey,
  24. State, SwapRequest,
  25. };
  26. use cdk::wallet::types::TransactionDirection;
  27. use cdk::wallet::{HttpClient, MintConnector, Wallet};
  28. use cdk::StreamExt;
  29. use cdk_fake_wallet::{create_fake_invoice, FakeInvoiceDescription};
  30. use cdk_integration_tests::attempt_to_swap_pending;
  31. use cdk_sqlite::wallet::memory;
  32. const MINT_URL: &str = "http://127.0.0.1:8086";
  33. /// Tests that when both pay and check return pending status, input proofs should remain pending
  34. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  35. async fn test_fake_tokens_pending() {
  36. let wallet = Wallet::new(
  37. MINT_URL,
  38. CurrencyUnit::Sat,
  39. Arc::new(memory::empty().await.unwrap()),
  40. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  41. None,
  42. )
  43. .expect("failed to create new wallet");
  44. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  45. let mut proof_streams = wallet.proof_stream(
  46. mint_quote.clone(),
  47. SplitTarget::default(),
  48. None,
  49. Duration::from_secs(60),
  50. );
  51. let _proofs = proof_streams
  52. .next()
  53. .await
  54. .expect("payment")
  55. .expect("no error");
  56. let fake_description = FakeInvoiceDescription {
  57. pay_invoice_state: MeltQuoteState::Pending,
  58. check_payment_state: MeltQuoteState::Pending,
  59. pay_err: false,
  60. check_err: false,
  61. };
  62. let invoice = create_fake_invoice(1000, serde_json::to_string(&fake_description).unwrap());
  63. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  64. let melt = wallet.melt(&melt_quote.id).await;
  65. assert!(melt.is_err());
  66. attempt_to_swap_pending(&wallet).await.unwrap();
  67. }
  68. /// Tests that if the pay error fails and the check returns unknown or failed,
  69. /// the input proofs should be unset as spending (returned to unspent state)
  70. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  71. async fn test_fake_melt_payment_fail() {
  72. let wallet = Wallet::new(
  73. MINT_URL,
  74. CurrencyUnit::Sat,
  75. Arc::new(memory::empty().await.unwrap()),
  76. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  77. None,
  78. )
  79. .expect("Failed to create new wallet");
  80. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  81. let mut proof_streams = wallet.proof_stream(
  82. mint_quote.clone(),
  83. SplitTarget::default(),
  84. None,
  85. Duration::from_secs(60),
  86. );
  87. let _proofs = proof_streams
  88. .next()
  89. .await
  90. .expect("payment")
  91. .expect("no error");
  92. let fake_description = FakeInvoiceDescription {
  93. pay_invoice_state: MeltQuoteState::Unknown,
  94. check_payment_state: MeltQuoteState::Unknown,
  95. pay_err: true,
  96. check_err: false,
  97. };
  98. let invoice = create_fake_invoice(1000, serde_json::to_string(&fake_description).unwrap());
  99. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  100. // The melt should error at the payment invoice command
  101. let melt = wallet.melt(&melt_quote.id).await;
  102. assert!(melt.is_err());
  103. let fake_description = FakeInvoiceDescription {
  104. pay_invoice_state: MeltQuoteState::Failed,
  105. check_payment_state: MeltQuoteState::Failed,
  106. pay_err: true,
  107. check_err: false,
  108. };
  109. let invoice = create_fake_invoice(1000, serde_json::to_string(&fake_description).unwrap());
  110. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  111. // The melt should error at the payment invoice command
  112. let melt = wallet.melt(&melt_quote.id).await;
  113. assert!(melt.is_err());
  114. // The mint should have unset proofs from pending since payment failed
  115. let all_proof = wallet.get_unspent_proofs().await.unwrap();
  116. let states = wallet.check_proofs_spent(all_proof).await.unwrap();
  117. for state in states {
  118. assert!(state.state == State::Unspent);
  119. }
  120. let wallet_bal = wallet.total_balance().await.unwrap();
  121. assert_eq!(wallet_bal, 100.into());
  122. }
  123. /// Tests that when both the pay_invoice and check_invoice both fail,
  124. /// the proofs should remain in pending state
  125. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  126. async fn test_fake_melt_payment_fail_and_check() {
  127. let wallet = Wallet::new(
  128. MINT_URL,
  129. CurrencyUnit::Sat,
  130. Arc::new(memory::empty().await.unwrap()),
  131. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  132. None,
  133. )
  134. .expect("Failed to create new wallet");
  135. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  136. let mut proof_streams = wallet.proof_stream(
  137. mint_quote.clone(),
  138. SplitTarget::default(),
  139. None,
  140. Duration::from_secs(60),
  141. );
  142. let _proofs = proof_streams
  143. .next()
  144. .await
  145. .expect("payment")
  146. .expect("no error");
  147. let fake_description = FakeInvoiceDescription {
  148. pay_invoice_state: MeltQuoteState::Unknown,
  149. check_payment_state: MeltQuoteState::Unknown,
  150. pay_err: true,
  151. check_err: true,
  152. };
  153. let invoice = create_fake_invoice(7000, serde_json::to_string(&fake_description).unwrap());
  154. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  155. // The melt should error at the payment invoice command
  156. let melt = wallet.melt(&melt_quote.id).await;
  157. assert!(melt.is_err());
  158. let pending = wallet
  159. .localstore
  160. .get_proofs(None, None, Some(vec![State::Pending]), None)
  161. .await
  162. .unwrap();
  163. assert!(!pending.is_empty());
  164. }
  165. /// Tests that when the ln backend returns a failed status but does not error,
  166. /// the mint should do a second check, then remove proofs from pending state
  167. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  168. async fn test_fake_melt_payment_return_fail_status() {
  169. let wallet = Wallet::new(
  170. MINT_URL,
  171. CurrencyUnit::Sat,
  172. Arc::new(memory::empty().await.unwrap()),
  173. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  174. None,
  175. )
  176. .expect("Failed to create new wallet");
  177. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  178. let mut proof_streams = wallet.proof_stream(
  179. mint_quote.clone(),
  180. SplitTarget::default(),
  181. None,
  182. Duration::from_secs(60),
  183. );
  184. let _proofs = proof_streams
  185. .next()
  186. .await
  187. .expect("payment")
  188. .expect("no error");
  189. let fake_description = FakeInvoiceDescription {
  190. pay_invoice_state: MeltQuoteState::Failed,
  191. check_payment_state: MeltQuoteState::Failed,
  192. pay_err: false,
  193. check_err: false,
  194. };
  195. let invoice = create_fake_invoice(7000, serde_json::to_string(&fake_description).unwrap());
  196. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  197. // The melt should error at the payment invoice command
  198. let melt = wallet.melt(&melt_quote.id).await;
  199. assert!(melt.is_err());
  200. let fake_description = FakeInvoiceDescription {
  201. pay_invoice_state: MeltQuoteState::Unknown,
  202. check_payment_state: MeltQuoteState::Unknown,
  203. pay_err: false,
  204. check_err: false,
  205. };
  206. let invoice = create_fake_invoice(7000, serde_json::to_string(&fake_description).unwrap());
  207. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  208. // The melt should error at the payment invoice command
  209. let melt = wallet.melt(&melt_quote.id).await;
  210. assert!(melt.is_err());
  211. let pending = wallet
  212. .localstore
  213. .get_proofs(None, None, Some(vec![State::Pending]), None)
  214. .await
  215. .unwrap();
  216. assert!(pending.is_empty());
  217. }
  218. /// Tests that when the ln backend returns an error with unknown status,
  219. /// the mint should do a second check, then remove proofs from pending state
  220. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  221. async fn test_fake_melt_payment_error_unknown() {
  222. let wallet = Wallet::new(
  223. MINT_URL,
  224. CurrencyUnit::Sat,
  225. Arc::new(memory::empty().await.unwrap()),
  226. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  227. None,
  228. )
  229. .unwrap();
  230. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  231. let mut proof_streams = wallet.proof_stream(
  232. mint_quote.clone(),
  233. SplitTarget::default(),
  234. None,
  235. Duration::from_secs(60),
  236. );
  237. let _proofs = proof_streams
  238. .next()
  239. .await
  240. .expect("payment")
  241. .expect("no error");
  242. let fake_description = FakeInvoiceDescription {
  243. pay_invoice_state: MeltQuoteState::Failed,
  244. check_payment_state: MeltQuoteState::Unknown,
  245. pay_err: true,
  246. check_err: false,
  247. };
  248. let invoice = create_fake_invoice(7000, serde_json::to_string(&fake_description).unwrap());
  249. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  250. // The melt should error at the payment invoice command
  251. let melt = wallet.melt(&melt_quote.id).await;
  252. assert_eq!(melt.unwrap_err().to_string(), "Payment failed");
  253. let fake_description = FakeInvoiceDescription {
  254. pay_invoice_state: MeltQuoteState::Unknown,
  255. check_payment_state: MeltQuoteState::Unknown,
  256. pay_err: true,
  257. check_err: false,
  258. };
  259. let invoice = create_fake_invoice(7000, serde_json::to_string(&fake_description).unwrap());
  260. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  261. // The melt should error at the payment invoice command
  262. let melt = wallet.melt(&melt_quote.id).await;
  263. assert_eq!(melt.unwrap_err().to_string(), "Payment failed");
  264. let pending = wallet
  265. .localstore
  266. .get_proofs(None, None, Some(vec![State::Pending]), None)
  267. .await
  268. .unwrap();
  269. assert!(pending.is_empty());
  270. }
  271. /// Tests that when the ln backend returns an error but the second check returns paid,
  272. /// proofs should remain in pending state
  273. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  274. async fn test_fake_melt_payment_err_paid() {
  275. let wallet = Wallet::new(
  276. MINT_URL,
  277. CurrencyUnit::Sat,
  278. Arc::new(memory::empty().await.unwrap()),
  279. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  280. None,
  281. )
  282. .expect("Failed to create new wallet");
  283. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  284. let mut proof_streams = wallet.proof_stream(
  285. mint_quote.clone(),
  286. SplitTarget::default(),
  287. None,
  288. Duration::from_secs(60),
  289. );
  290. let _proofs = proof_streams
  291. .next()
  292. .await
  293. .expect("payment")
  294. .expect("no error");
  295. let fake_description = FakeInvoiceDescription {
  296. pay_invoice_state: MeltQuoteState::Failed,
  297. check_payment_state: MeltQuoteState::Paid,
  298. pay_err: true,
  299. check_err: false,
  300. };
  301. let invoice = create_fake_invoice(7000, serde_json::to_string(&fake_description).unwrap());
  302. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  303. // The melt should error at the payment invoice command
  304. let melt = wallet.melt(&melt_quote.id).await;
  305. assert!(melt.is_err());
  306. attempt_to_swap_pending(&wallet).await.unwrap();
  307. }
  308. /// Tests that change outputs in a melt quote are correctly handled
  309. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  310. async fn test_fake_melt_change_in_quote() {
  311. let wallet = Wallet::new(
  312. MINT_URL,
  313. CurrencyUnit::Sat,
  314. Arc::new(memory::empty().await.unwrap()),
  315. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  316. None,
  317. )
  318. .expect("Failed to create new wallet");
  319. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  320. let mut proof_streams = wallet.proof_stream(
  321. mint_quote.clone(),
  322. SplitTarget::default(),
  323. None,
  324. Duration::from_secs(60),
  325. );
  326. let _proofs = proof_streams
  327. .next()
  328. .await
  329. .expect("payment")
  330. .expect("no error");
  331. let transaction = wallet
  332. .list_transactions(Some(TransactionDirection::Incoming))
  333. .await
  334. .unwrap()
  335. .pop()
  336. .expect("No transaction found");
  337. assert_eq!(wallet.mint_url, transaction.mint_url);
  338. assert_eq!(TransactionDirection::Incoming, transaction.direction);
  339. assert_eq!(Amount::from(100), transaction.amount);
  340. assert_eq!(Amount::from(0), transaction.fee);
  341. assert_eq!(CurrencyUnit::Sat, transaction.unit);
  342. let fake_description = FakeInvoiceDescription::default();
  343. let invoice = create_fake_invoice(9000, serde_json::to_string(&fake_description).unwrap());
  344. let proofs = wallet.get_unspent_proofs().await.unwrap();
  345. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  346. let keyset = wallet.fetch_active_keyset().await.unwrap();
  347. let premint_secrets =
  348. PreMintSecrets::random(keyset.id, 100.into(), &SplitTarget::default()).unwrap();
  349. let client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  350. let melt_request = MeltRequest::new(
  351. melt_quote.id.clone(),
  352. proofs.clone(),
  353. Some(premint_secrets.blinded_messages()),
  354. );
  355. let melt_response = client.post_melt(melt_request).await.unwrap();
  356. assert!(melt_response.change.is_some());
  357. let check = wallet.melt_quote_status(&melt_quote.id).await.unwrap();
  358. let mut melt_change = melt_response.change.unwrap();
  359. melt_change.sort_by(|a, b| a.amount.cmp(&b.amount));
  360. let mut check = check.change.unwrap();
  361. check.sort_by(|a, b| a.amount.cmp(&b.amount));
  362. assert_eq!(melt_change, check);
  363. }
  364. /// Tests minting tokens with a valid witness signature
  365. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  366. async fn test_fake_mint_with_witness() {
  367. let wallet = Wallet::new(
  368. MINT_URL,
  369. CurrencyUnit::Sat,
  370. Arc::new(memory::empty().await.unwrap()),
  371. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  372. None,
  373. )
  374. .expect("failed to create new wallet");
  375. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  376. let mut proof_streams = wallet.proof_stream(
  377. mint_quote.clone(),
  378. SplitTarget::default(),
  379. None,
  380. Duration::from_secs(60),
  381. );
  382. let proofs = proof_streams
  383. .next()
  384. .await
  385. .expect("payment")
  386. .expect("no error");
  387. let mint_amount = proofs.total_amount().unwrap();
  388. assert!(mint_amount == 100.into());
  389. }
  390. /// Tests that minting without a witness signature fails with the correct error
  391. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  392. async fn test_fake_mint_without_witness() {
  393. let wallet = Wallet::new(
  394. MINT_URL,
  395. CurrencyUnit::Sat,
  396. Arc::new(memory::empty().await.unwrap()),
  397. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  398. None,
  399. )
  400. .expect("failed to create new wallet");
  401. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  402. let mut payment_streams = wallet.payment_stream(&mint_quote, Duration::from_secs(60));
  403. payment_streams
  404. .next()
  405. .await
  406. .expect("payment")
  407. .expect("no error");
  408. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  409. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  410. let premint_secrets =
  411. PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::default()).unwrap();
  412. let request = MintRequest {
  413. quote: mint_quote.id,
  414. outputs: premint_secrets.blinded_messages(),
  415. signature: None,
  416. };
  417. let response = http_client.post_mint(request.clone()).await;
  418. match response {
  419. Err(cdk::error::Error::SignatureMissingOrInvalid) => {} //pass
  420. Err(err) => panic!("Wrong mint response for minting without witness: {}", err),
  421. Ok(_) => panic!("Minting should not have succeed without a witness"),
  422. }
  423. }
  424. /// Tests that minting with an incorrect witness signature fails with the correct error
  425. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  426. async fn test_fake_mint_with_wrong_witness() {
  427. let wallet = Wallet::new(
  428. MINT_URL,
  429. CurrencyUnit::Sat,
  430. Arc::new(memory::empty().await.unwrap()),
  431. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  432. None,
  433. )
  434. .expect("failed to create new wallet");
  435. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  436. let mut payment_streams = wallet.payment_stream(&mint_quote, Duration::from_secs(60));
  437. payment_streams
  438. .next()
  439. .await
  440. .expect("payment")
  441. .expect("no error");
  442. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  443. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  444. let premint_secrets =
  445. PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::default()).unwrap();
  446. let mut request = MintRequest {
  447. quote: mint_quote.id,
  448. outputs: premint_secrets.blinded_messages(),
  449. signature: None,
  450. };
  451. let secret_key = SecretKey::generate();
  452. request
  453. .sign(secret_key)
  454. .expect("failed to sign the mint request");
  455. let response = http_client.post_mint(request.clone()).await;
  456. match response {
  457. Err(cdk::error::Error::SignatureMissingOrInvalid) => {} //pass
  458. Err(err) => panic!("Wrong mint response for minting without witness: {}", err),
  459. Ok(_) => panic!("Minting should not have succeed without a witness"),
  460. }
  461. }
  462. /// Tests that attempting to mint more tokens than allowed by the quote fails
  463. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  464. async fn test_fake_mint_inflated() {
  465. let wallet = Wallet::new(
  466. MINT_URL,
  467. CurrencyUnit::Sat,
  468. Arc::new(memory::empty().await.unwrap()),
  469. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  470. None,
  471. )
  472. .expect("failed to create new wallet");
  473. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  474. let mut payment_streams = wallet.payment_stream(&mint_quote, Duration::from_secs(60));
  475. payment_streams
  476. .next()
  477. .await
  478. .expect("payment")
  479. .expect("no error");
  480. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  481. let pre_mint =
  482. PreMintSecrets::random(active_keyset_id, 500.into(), &SplitTarget::None).unwrap();
  483. let quote_info = wallet
  484. .localstore
  485. .get_mint_quote(&mint_quote.id)
  486. .await
  487. .unwrap()
  488. .expect("there is a quote");
  489. let mut mint_request = MintRequest {
  490. quote: mint_quote.id,
  491. outputs: pre_mint.blinded_messages(),
  492. signature: None,
  493. };
  494. if let Some(secret_key) = quote_info.secret_key {
  495. mint_request
  496. .sign(secret_key)
  497. .expect("failed to sign the mint request");
  498. }
  499. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  500. let response = http_client.post_mint(mint_request.clone()).await;
  501. match response {
  502. Err(err) => match err {
  503. cdk::Error::TransactionUnbalanced(_, _, _) => (),
  504. err => {
  505. panic!("Wrong mint error returned: {}", err);
  506. }
  507. },
  508. Ok(_) => {
  509. panic!("Should not have allowed second payment");
  510. }
  511. }
  512. }
  513. /// Tests that attempting to mint with multiple currency units in the same request fails
  514. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  515. async fn test_fake_mint_multiple_units() {
  516. let wallet = Wallet::new(
  517. MINT_URL,
  518. CurrencyUnit::Sat,
  519. Arc::new(memory::empty().await.unwrap()),
  520. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  521. None,
  522. )
  523. .expect("failed to create new wallet");
  524. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  525. let mut payment_streams = wallet.payment_stream(&mint_quote, Duration::from_secs(60));
  526. payment_streams
  527. .next()
  528. .await
  529. .expect("payment")
  530. .expect("no error");
  531. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  532. let pre_mint = PreMintSecrets::random(active_keyset_id, 50.into(), &SplitTarget::None).unwrap();
  533. let wallet_usd = Wallet::new(
  534. MINT_URL,
  535. CurrencyUnit::Usd,
  536. Arc::new(memory::empty().await.unwrap()),
  537. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  538. None,
  539. )
  540. .expect("failed to create new wallet");
  541. let active_keyset_id = wallet_usd.fetch_active_keyset().await.unwrap().id;
  542. let usd_pre_mint =
  543. PreMintSecrets::random(active_keyset_id, 50.into(), &SplitTarget::None).unwrap();
  544. let quote_info = wallet
  545. .localstore
  546. .get_mint_quote(&mint_quote.id)
  547. .await
  548. .unwrap()
  549. .expect("there is a quote");
  550. let mut sat_outputs = pre_mint.blinded_messages();
  551. let mut usd_outputs = usd_pre_mint.blinded_messages();
  552. sat_outputs.append(&mut usd_outputs);
  553. let mut mint_request = MintRequest {
  554. quote: mint_quote.id,
  555. outputs: sat_outputs,
  556. signature: None,
  557. };
  558. if let Some(secret_key) = quote_info.secret_key {
  559. mint_request
  560. .sign(secret_key)
  561. .expect("failed to sign the mint request");
  562. }
  563. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  564. let response = http_client.post_mint(mint_request.clone()).await;
  565. match response {
  566. Err(err) => match err {
  567. cdk::Error::MultipleUnits => (),
  568. err => {
  569. panic!("Wrong mint error returned: {}", err);
  570. }
  571. },
  572. Ok(_) => {
  573. panic!("Should not have allowed to mint with multiple units");
  574. }
  575. }
  576. }
  577. /// Tests that attempting to swap tokens with multiple currency units fails
  578. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  579. async fn test_fake_mint_multiple_unit_swap() {
  580. let wallet = Wallet::new(
  581. MINT_URL,
  582. CurrencyUnit::Sat,
  583. Arc::new(memory::empty().await.unwrap()),
  584. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  585. None,
  586. )
  587. .expect("failed to create new wallet");
  588. wallet.refresh_keysets().await.unwrap();
  589. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  590. let mut proof_streams = wallet.proof_stream(
  591. mint_quote.clone(),
  592. SplitTarget::default(),
  593. None,
  594. Duration::from_secs(60),
  595. );
  596. let proofs = proof_streams
  597. .next()
  598. .await
  599. .expect("payment")
  600. .expect("no error");
  601. let wallet_usd = Wallet::new(
  602. MINT_URL,
  603. CurrencyUnit::Usd,
  604. Arc::new(memory::empty().await.unwrap()),
  605. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  606. None,
  607. )
  608. .expect("failed to create usd wallet");
  609. wallet_usd.refresh_keysets().await.unwrap();
  610. let mint_quote = wallet_usd.mint_quote(100.into(), None).await.unwrap();
  611. let mut proof_streams = wallet_usd.proof_stream(
  612. mint_quote.clone(),
  613. SplitTarget::default(),
  614. None,
  615. Duration::from_secs(60),
  616. );
  617. let usd_proofs = proof_streams
  618. .next()
  619. .await
  620. .expect("payment")
  621. .expect("no error");
  622. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  623. {
  624. let inputs: Proofs = vec![
  625. proofs.first().expect("There is a proof").clone(),
  626. usd_proofs.first().expect("There is a proof").clone(),
  627. ];
  628. let pre_mint = PreMintSecrets::random(
  629. active_keyset_id,
  630. inputs.total_amount().unwrap(),
  631. &SplitTarget::None,
  632. )
  633. .unwrap();
  634. let swap_request = SwapRequest::new(inputs, pre_mint.blinded_messages());
  635. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  636. let response = http_client.post_swap(swap_request.clone()).await;
  637. match response {
  638. Err(err) => match err {
  639. cdk::Error::MultipleUnits => (),
  640. err => {
  641. panic!("Wrong mint error returned: {}", err);
  642. }
  643. },
  644. Ok(_) => {
  645. panic!("Should not have allowed to mint with multiple units");
  646. }
  647. }
  648. }
  649. {
  650. let usd_active_keyset_id = wallet_usd.fetch_active_keyset().await.unwrap().id;
  651. let inputs: Proofs = proofs.into_iter().take(2).collect();
  652. let total_inputs = inputs.total_amount().unwrap();
  653. let half = total_inputs / 2.into();
  654. let usd_pre_mint =
  655. PreMintSecrets::random(usd_active_keyset_id, half, &SplitTarget::None).unwrap();
  656. let pre_mint =
  657. PreMintSecrets::random(active_keyset_id, total_inputs - half, &SplitTarget::None)
  658. .unwrap();
  659. let mut usd_outputs = usd_pre_mint.blinded_messages();
  660. let mut sat_outputs = pre_mint.blinded_messages();
  661. usd_outputs.append(&mut sat_outputs);
  662. let swap_request = SwapRequest::new(inputs, usd_outputs);
  663. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  664. let response = http_client.post_swap(swap_request.clone()).await;
  665. match response {
  666. Err(err) => match err {
  667. cdk::Error::MultipleUnits => (),
  668. err => {
  669. panic!("Wrong mint error returned: {}", err);
  670. }
  671. },
  672. Ok(_) => {
  673. panic!("Should not have allowed to mint with multiple units");
  674. }
  675. }
  676. }
  677. }
  678. /// Tests that attempting to melt tokens with multiple currency units fails
  679. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  680. async fn test_fake_mint_multiple_unit_melt() {
  681. let wallet = Wallet::new(
  682. MINT_URL,
  683. CurrencyUnit::Sat,
  684. Arc::new(memory::empty().await.unwrap()),
  685. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  686. None,
  687. )
  688. .expect("failed to create new wallet");
  689. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  690. let mut proof_streams = wallet.proof_stream(
  691. mint_quote.clone(),
  692. SplitTarget::default(),
  693. None,
  694. Duration::from_secs(60),
  695. );
  696. let proofs = proof_streams
  697. .next()
  698. .await
  699. .expect("payment")
  700. .expect("no error");
  701. println!("Minted sat");
  702. let wallet_usd = Wallet::new(
  703. MINT_URL,
  704. CurrencyUnit::Usd,
  705. Arc::new(memory::empty().await.unwrap()),
  706. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  707. None,
  708. )
  709. .expect("failed to create new wallet");
  710. let mint_quote = wallet_usd.mint_quote(100.into(), None).await.unwrap();
  711. println!("Minted quote usd");
  712. let mut proof_streams = wallet_usd.proof_stream(
  713. mint_quote.clone(),
  714. SplitTarget::default(),
  715. None,
  716. Duration::from_secs(60),
  717. );
  718. let usd_proofs = proof_streams
  719. .next()
  720. .await
  721. .expect("payment")
  722. .expect("no error");
  723. {
  724. let inputs: Proofs = vec![
  725. proofs.first().expect("There is a proof").clone(),
  726. usd_proofs.first().expect("There is a proof").clone(),
  727. ];
  728. let input_amount: u64 = inputs.total_amount().unwrap().into();
  729. let invoice = create_fake_invoice((input_amount - 1) * 1000, "".to_string());
  730. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  731. let melt_request = MeltRequest::new(melt_quote.id, inputs, None);
  732. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  733. let response = http_client.post_melt(melt_request.clone()).await;
  734. match response {
  735. Err(err) => match err {
  736. cdk::Error::MultipleUnits => (),
  737. err => {
  738. panic!("Wrong mint error returned: {}", err);
  739. }
  740. },
  741. Ok(_) => {
  742. panic!("Should not have allowed to melt with multiple units");
  743. }
  744. }
  745. }
  746. {
  747. let inputs: Proofs = vec![proofs.first().expect("There is a proof").clone()];
  748. let input_amount: u64 = inputs.total_amount().unwrap().into();
  749. let invoice = create_fake_invoice((input_amount - 1) * 1000, "".to_string());
  750. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  751. let usd_active_keyset_id = wallet_usd.fetch_active_keyset().await.unwrap().id;
  752. let usd_pre_mint = PreMintSecrets::random(
  753. usd_active_keyset_id,
  754. inputs.total_amount().unwrap() + 100.into(),
  755. &SplitTarget::None,
  756. )
  757. .unwrap();
  758. let pre_mint =
  759. PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::None).unwrap();
  760. let mut usd_outputs = usd_pre_mint.blinded_messages();
  761. let mut sat_outputs = pre_mint.blinded_messages();
  762. usd_outputs.append(&mut sat_outputs);
  763. let quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  764. let melt_request = MeltRequest::new(quote.id, inputs, Some(usd_outputs));
  765. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  766. let response = http_client.post_melt(melt_request.clone()).await;
  767. match response {
  768. Err(err) => match err {
  769. cdk::Error::MultipleUnits => (),
  770. err => {
  771. panic!("Wrong mint error returned: {}", err);
  772. }
  773. },
  774. Ok(_) => {
  775. panic!("Should not have allowed to melt with multiple units");
  776. }
  777. }
  778. }
  779. }
  780. /// Tests that swapping tokens where input unit doesn't match output unit fails
  781. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  782. async fn test_fake_mint_input_output_mismatch() {
  783. let wallet = Wallet::new(
  784. MINT_URL,
  785. CurrencyUnit::Sat,
  786. Arc::new(memory::empty().await.unwrap()),
  787. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  788. None,
  789. )
  790. .expect("failed to create new wallet");
  791. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  792. let mut proof_streams = wallet.proof_stream(
  793. mint_quote.clone(),
  794. SplitTarget::default(),
  795. None,
  796. Duration::from_secs(60),
  797. );
  798. let proofs = proof_streams
  799. .next()
  800. .await
  801. .expect("payment")
  802. .expect("no error");
  803. let wallet_usd = Wallet::new(
  804. MINT_URL,
  805. CurrencyUnit::Usd,
  806. Arc::new(memory::empty().await.unwrap()),
  807. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  808. None,
  809. )
  810. .expect("failed to create new usd wallet");
  811. let usd_active_keyset_id = wallet_usd.fetch_active_keyset().await.unwrap().id;
  812. let inputs = proofs;
  813. let pre_mint = PreMintSecrets::random(
  814. usd_active_keyset_id,
  815. inputs.total_amount().unwrap(),
  816. &SplitTarget::None,
  817. )
  818. .unwrap();
  819. let swap_request = SwapRequest::new(inputs, pre_mint.blinded_messages());
  820. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  821. let response = http_client.post_swap(swap_request.clone()).await;
  822. match response {
  823. Err(err) => match err {
  824. cdk::Error::UnitMismatch => (),
  825. err => panic!("Wrong error returned: {}", err),
  826. },
  827. Ok(_) => {
  828. panic!("Should not have allowed to mint with multiple units");
  829. }
  830. }
  831. }
  832. /// Tests that swapping tokens where output amount is greater than input amount fails
  833. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  834. async fn test_fake_mint_swap_inflated() {
  835. let wallet = Wallet::new(
  836. MINT_URL,
  837. CurrencyUnit::Sat,
  838. Arc::new(memory::empty().await.unwrap()),
  839. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  840. None,
  841. )
  842. .expect("failed to create new wallet");
  843. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  844. let mut proof_streams = wallet.proof_stream(
  845. mint_quote.clone(),
  846. SplitTarget::default(),
  847. None,
  848. Duration::from_secs(60),
  849. );
  850. let proofs = proof_streams
  851. .next()
  852. .await
  853. .expect("payment")
  854. .expect("no error");
  855. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  856. let pre_mint =
  857. PreMintSecrets::random(active_keyset_id, 101.into(), &SplitTarget::None).unwrap();
  858. let swap_request = SwapRequest::new(proofs, pre_mint.blinded_messages());
  859. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  860. let response = http_client.post_swap(swap_request.clone()).await;
  861. match response {
  862. Err(err) => match err {
  863. cdk::Error::TransactionUnbalanced(_, _, _) => (),
  864. err => {
  865. panic!("Wrong mint error returned: {}", err);
  866. }
  867. },
  868. Ok(_) => {
  869. panic!("Should not have allowed to mint with multiple units");
  870. }
  871. }
  872. }
  873. /// Tests that tokens cannot be spent again after a failed swap attempt
  874. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  875. async fn test_fake_mint_swap_spend_after_fail() {
  876. let wallet = Wallet::new(
  877. MINT_URL,
  878. CurrencyUnit::Sat,
  879. Arc::new(memory::empty().await.unwrap()),
  880. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  881. None,
  882. )
  883. .expect("failed to create new wallet");
  884. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  885. let mut proof_streams = wallet.proof_stream(
  886. mint_quote.clone(),
  887. SplitTarget::default(),
  888. None,
  889. Duration::from_secs(60),
  890. );
  891. let proofs = proof_streams
  892. .next()
  893. .await
  894. .expect("payment")
  895. .expect("no error");
  896. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  897. let pre_mint =
  898. PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::None).unwrap();
  899. let swap_request = SwapRequest::new(proofs.clone(), pre_mint.blinded_messages());
  900. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  901. let response = http_client.post_swap(swap_request.clone()).await;
  902. assert!(response.is_ok());
  903. let pre_mint =
  904. PreMintSecrets::random(active_keyset_id, 101.into(), &SplitTarget::None).unwrap();
  905. let swap_request = SwapRequest::new(proofs.clone(), pre_mint.blinded_messages());
  906. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  907. let response = http_client.post_swap(swap_request.clone()).await;
  908. match response {
  909. Err(err) => match err {
  910. cdk::Error::TransactionUnbalanced(_, _, _) => (),
  911. err => panic!("Wrong mint error returned expected TransactionUnbalanced, got: {err}"),
  912. },
  913. Ok(_) => panic!("Should not have allowed swap with unbalanced"),
  914. }
  915. let pre_mint =
  916. PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::None).unwrap();
  917. let swap_request = SwapRequest::new(proofs, pre_mint.blinded_messages());
  918. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  919. let response = http_client.post_swap(swap_request.clone()).await;
  920. match response {
  921. Err(err) => match err {
  922. cdk::Error::TokenAlreadySpent => (),
  923. err => {
  924. panic!("Wrong mint error returned: {}", err);
  925. }
  926. },
  927. Ok(_) => {
  928. panic!("Should not have allowed to mint with multiple units");
  929. }
  930. }
  931. }
  932. /// Tests that tokens cannot be melted after a failed swap attempt
  933. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  934. async fn test_fake_mint_melt_spend_after_fail() {
  935. let wallet = Wallet::new(
  936. MINT_URL,
  937. CurrencyUnit::Sat,
  938. Arc::new(memory::empty().await.unwrap()),
  939. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  940. None,
  941. )
  942. .expect("failed to create new wallet");
  943. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  944. let mut proof_streams = wallet.proof_stream(
  945. mint_quote.clone(),
  946. SplitTarget::default(),
  947. None,
  948. Duration::from_secs(60),
  949. );
  950. let proofs = proof_streams
  951. .next()
  952. .await
  953. .expect("payment")
  954. .expect("no error");
  955. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  956. let pre_mint =
  957. PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::None).unwrap();
  958. let swap_request = SwapRequest::new(proofs.clone(), pre_mint.blinded_messages());
  959. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  960. let response = http_client.post_swap(swap_request.clone()).await;
  961. assert!(response.is_ok());
  962. let pre_mint =
  963. PreMintSecrets::random(active_keyset_id, 101.into(), &SplitTarget::None).unwrap();
  964. let swap_request = SwapRequest::new(proofs.clone(), pre_mint.blinded_messages());
  965. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  966. let response = http_client.post_swap(swap_request.clone()).await;
  967. match response {
  968. Err(err) => match err {
  969. cdk::Error::TransactionUnbalanced(_, _, _) => (),
  970. err => panic!("Wrong mint error returned expected TransactionUnbalanced, got: {err}"),
  971. },
  972. Ok(_) => panic!("Should not have allowed swap with unbalanced"),
  973. }
  974. let input_amount: u64 = proofs.total_amount().unwrap().into();
  975. let invoice = create_fake_invoice((input_amount - 1) * 1000, "".to_string());
  976. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  977. let melt_request = MeltRequest::new(melt_quote.id, proofs, None);
  978. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  979. let response = http_client.post_melt(melt_request.clone()).await;
  980. match response {
  981. Err(err) => match err {
  982. cdk::Error::TokenAlreadySpent => (),
  983. err => {
  984. panic!("Wrong mint error returned: {}", err);
  985. }
  986. },
  987. Ok(_) => {
  988. panic!("Should not have allowed to melt with multiple units");
  989. }
  990. }
  991. }
  992. /// Tests that attempting to swap with duplicate proofs fails
  993. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  994. async fn test_fake_mint_duplicate_proofs_swap() {
  995. let wallet = Wallet::new(
  996. MINT_URL,
  997. CurrencyUnit::Sat,
  998. Arc::new(memory::empty().await.unwrap()),
  999. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  1000. None,
  1001. )
  1002. .expect("failed to create new wallet");
  1003. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  1004. let mut proof_streams = wallet.proof_stream(
  1005. mint_quote.clone(),
  1006. SplitTarget::default(),
  1007. None,
  1008. Duration::from_secs(60),
  1009. );
  1010. let proofs = proof_streams
  1011. .next()
  1012. .await
  1013. .expect("payment")
  1014. .expect("no error");
  1015. let active_keyset_id = wallet.fetch_active_keyset().await.unwrap().id;
  1016. let inputs = vec![proofs[0].clone(), proofs[0].clone()];
  1017. let pre_mint = PreMintSecrets::random(
  1018. active_keyset_id,
  1019. inputs.total_amount().unwrap(),
  1020. &SplitTarget::None,
  1021. )
  1022. .unwrap();
  1023. let swap_request = SwapRequest::new(inputs.clone(), pre_mint.blinded_messages());
  1024. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  1025. let response = http_client.post_swap(swap_request.clone()).await;
  1026. match response {
  1027. Err(err) => match err {
  1028. cdk::Error::DuplicateInputs => (),
  1029. err => {
  1030. panic!(
  1031. "Wrong mint error returned, expected duplicate inputs: {}",
  1032. err
  1033. );
  1034. }
  1035. },
  1036. Ok(_) => {
  1037. panic!("Should not have allowed duplicate inputs");
  1038. }
  1039. }
  1040. let blinded_message = pre_mint.blinded_messages();
  1041. let inputs = vec![proofs[0].clone()];
  1042. let outputs = vec![blinded_message[0].clone(), blinded_message[0].clone()];
  1043. let swap_request = SwapRequest::new(inputs, outputs);
  1044. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  1045. let response = http_client.post_swap(swap_request.clone()).await;
  1046. match response {
  1047. Err(err) => match err {
  1048. cdk::Error::DuplicateOutputs => (),
  1049. err => {
  1050. panic!(
  1051. "Wrong mint error returned, expected duplicate outputs: {}",
  1052. err
  1053. );
  1054. }
  1055. },
  1056. Ok(_) => {
  1057. panic!("Should not have allow duplicate inputs");
  1058. }
  1059. }
  1060. }
  1061. /// Tests that attempting to melt with duplicate proofs fails
  1062. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  1063. async fn test_fake_mint_duplicate_proofs_melt() {
  1064. let wallet = Wallet::new(
  1065. MINT_URL,
  1066. CurrencyUnit::Sat,
  1067. Arc::new(memory::empty().await.unwrap()),
  1068. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  1069. None,
  1070. )
  1071. .expect("failed to create new wallet");
  1072. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  1073. let mut proof_streams = wallet.proof_stream(
  1074. mint_quote.clone(),
  1075. SplitTarget::default(),
  1076. None,
  1077. Duration::from_secs(60),
  1078. );
  1079. let proofs = proof_streams
  1080. .next()
  1081. .await
  1082. .expect("payment")
  1083. .expect("no error");
  1084. let inputs = vec![proofs[0].clone(), proofs[0].clone()];
  1085. let invoice = create_fake_invoice(7000, "".to_string());
  1086. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  1087. let melt_request = MeltRequest::new(melt_quote.id, inputs, None);
  1088. let http_client = HttpClient::new(MINT_URL.parse().unwrap(), None);
  1089. let response = http_client.post_melt(melt_request.clone()).await;
  1090. match response {
  1091. Err(err) => match err {
  1092. cdk::Error::DuplicateInputs => (),
  1093. err => {
  1094. panic!("Wrong mint error returned: {}", err);
  1095. }
  1096. },
  1097. Ok(_) => {
  1098. panic!("Should not have allow duplicate inputs");
  1099. }
  1100. }
  1101. }