init_pure_tests.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. use std::collections::{HashMap, HashSet};
  2. use std::fmt::{Debug, Formatter};
  3. use std::str::FromStr;
  4. use std::sync::Arc;
  5. use async_trait::async_trait;
  6. use bip39::Mnemonic;
  7. use cdk::amount::SplitTarget;
  8. use cdk::cdk_database::MintDatabase;
  9. use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits};
  10. use cdk::nuts::nut00::ProofsMethods;
  11. use cdk::nuts::{
  12. CheckStateRequest, CheckStateResponse, CurrencyUnit, Id, KeySet, KeysetResponse,
  13. MeltBolt11Request, MeltQuoteBolt11Request, MeltQuoteBolt11Response, MintBolt11Request,
  14. MintBolt11Response, MintInfo, MintQuoteBolt11Request, MintQuoteBolt11Response, PaymentMethod,
  15. RestoreRequest, RestoreResponse, SwapRequest, SwapResponse,
  16. };
  17. use cdk::types::QuoteTTL;
  18. use cdk::util::unix_time;
  19. use cdk::wallet::client::MintConnector;
  20. use cdk::wallet::Wallet;
  21. use cdk::{Amount, Error, Mint};
  22. use cdk_fake_wallet::FakeWallet;
  23. use tokio::sync::Notify;
  24. use tracing_subscriber::EnvFilter;
  25. use uuid::Uuid;
  26. use crate::wait_for_mint_to_be_paid;
  27. pub struct DirectMintConnection {
  28. pub mint: Arc<Mint>,
  29. }
  30. impl DirectMintConnection {
  31. pub fn new(mint: Arc<Mint>) -> Self {
  32. Self { mint }
  33. }
  34. }
  35. impl Debug for DirectMintConnection {
  36. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  37. write!(f, "DirectMintConnection",)
  38. }
  39. }
  40. /// Implements the generic [MintConnector] (i.e. use the interface that expects to communicate
  41. /// to a generic mint, where we don't know that quote ID's are [Uuid]s) for [DirectMintConnection],
  42. /// where we know we're dealing with a mint that uses [Uuid]s for quotes.
  43. /// Convert the requests and responses between the [String] and [Uuid] variants as necessary.
  44. #[async_trait]
  45. impl MintConnector for DirectMintConnection {
  46. async fn get_mint_keys(&self) -> Result<Vec<KeySet>, Error> {
  47. self.mint.pubkeys().await.map(|pks| pks.keysets)
  48. }
  49. async fn get_mint_keyset(&self, keyset_id: Id) -> Result<KeySet, Error> {
  50. self.mint
  51. .keyset(&keyset_id)
  52. .await
  53. .and_then(|res| res.ok_or(Error::UnknownKeySet))
  54. }
  55. async fn get_mint_keysets(&self) -> Result<KeysetResponse, Error> {
  56. self.mint.keysets().await
  57. }
  58. async fn post_mint_quote(
  59. &self,
  60. request: MintQuoteBolt11Request,
  61. ) -> Result<MintQuoteBolt11Response<String>, Error> {
  62. self.mint
  63. .get_mint_bolt11_quote(request)
  64. .await
  65. .map(Into::into)
  66. }
  67. async fn get_mint_quote_status(
  68. &self,
  69. quote_id: &str,
  70. ) -> Result<MintQuoteBolt11Response<String>, Error> {
  71. let quote_id_uuid = Uuid::from_str(quote_id).unwrap();
  72. self.mint
  73. .check_mint_quote(&quote_id_uuid)
  74. .await
  75. .map(Into::into)
  76. }
  77. async fn post_mint(
  78. &self,
  79. request: MintBolt11Request<String>,
  80. ) -> Result<MintBolt11Response, Error> {
  81. let request_uuid = request.try_into().unwrap();
  82. self.mint.process_mint_request(request_uuid).await
  83. }
  84. async fn post_melt_quote(
  85. &self,
  86. request: MeltQuoteBolt11Request,
  87. ) -> Result<MeltQuoteBolt11Response<String>, Error> {
  88. self.mint
  89. .get_melt_bolt11_quote(&request)
  90. .await
  91. .map(Into::into)
  92. }
  93. async fn get_melt_quote_status(
  94. &self,
  95. quote_id: &str,
  96. ) -> Result<MeltQuoteBolt11Response<String>, Error> {
  97. let quote_id_uuid = Uuid::from_str(quote_id).unwrap();
  98. self.mint
  99. .check_melt_quote(&quote_id_uuid)
  100. .await
  101. .map(Into::into)
  102. }
  103. async fn post_melt(
  104. &self,
  105. request: MeltBolt11Request<String>,
  106. ) -> Result<MeltQuoteBolt11Response<String>, Error> {
  107. let request_uuid = request.try_into().unwrap();
  108. self.mint.melt_bolt11(&request_uuid).await.map(Into::into)
  109. }
  110. async fn post_swap(&self, swap_request: SwapRequest) -> Result<SwapResponse, Error> {
  111. self.mint.process_swap_request(swap_request).await
  112. }
  113. async fn get_mint_info(&self) -> Result<MintInfo, Error> {
  114. Ok(self.mint.mint_info().await?.clone().time(unix_time()))
  115. }
  116. async fn post_check_state(
  117. &self,
  118. request: CheckStateRequest,
  119. ) -> Result<CheckStateResponse, Error> {
  120. self.mint.check_state(&request).await
  121. }
  122. async fn post_restore(&self, request: RestoreRequest) -> Result<RestoreResponse, Error> {
  123. self.mint.restore(request).await
  124. }
  125. }
  126. pub async fn create_and_start_test_mint() -> anyhow::Result<Arc<Mint>> {
  127. let default_filter = "debug";
  128. let sqlx_filter = "sqlx=warn";
  129. let hyper_filter = "hyper=warn";
  130. let env_filter = EnvFilter::new(format!(
  131. "{},{},{}",
  132. default_filter, sqlx_filter, hyper_filter
  133. ));
  134. tracing_subscriber::fmt().with_env_filter(env_filter).init();
  135. let mut mint_builder = MintBuilder::new();
  136. let database = cdk_sqlite::mint::memory::empty()
  137. .await
  138. .expect("valid db instance");
  139. let localstore = Arc::new(database);
  140. mint_builder = mint_builder.with_localstore(localstore.clone());
  141. let fee_reserve = FeeReserve {
  142. min_fee_reserve: 1.into(),
  143. percent_fee_reserve: 1.0,
  144. };
  145. let ln_fake_backend = Arc::new(FakeWallet::new(
  146. fee_reserve.clone(),
  147. HashMap::default(),
  148. HashSet::default(),
  149. 0,
  150. ));
  151. mint_builder = mint_builder.add_ln_backend(
  152. CurrencyUnit::Sat,
  153. PaymentMethod::Bolt11,
  154. MintMeltLimits::new(1, 1_000),
  155. ln_fake_backend,
  156. );
  157. let mnemonic = Mnemonic::generate(12)?;
  158. mint_builder = mint_builder
  159. .with_name("pure test mint".to_string())
  160. .with_description("pure test mint".to_string())
  161. .with_seed(mnemonic.to_seed_normalized("").to_vec());
  162. localstore
  163. .set_mint_info(mint_builder.mint_info.clone())
  164. .await?;
  165. let quote_ttl = QuoteTTL::new(10000, 10000);
  166. localstore.set_quote_ttl(quote_ttl).await?;
  167. let mint = mint_builder.build().await?;
  168. let mint_arc = Arc::new(mint);
  169. let mint_arc_clone = Arc::clone(&mint_arc);
  170. let shutdown = Arc::new(Notify::new());
  171. tokio::spawn({
  172. let shutdown = Arc::clone(&shutdown);
  173. async move { mint_arc_clone.wait_for_paid_invoices(shutdown).await }
  174. });
  175. Ok(mint_arc)
  176. }
  177. pub async fn create_test_wallet_for_mint(mint: Arc<Mint>) -> anyhow::Result<Arc<Wallet>> {
  178. let connector = DirectMintConnection::new(mint);
  179. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  180. let mint_url = "http://aa".to_string();
  181. let unit = CurrencyUnit::Sat;
  182. let localstore = cdk_sqlite::wallet::memory::empty()
  183. .await
  184. .expect("valid db instance");
  185. let mut wallet = Wallet::new(&mint_url, unit, Arc::new(localstore), &seed, None)?;
  186. wallet.set_client(connector);
  187. Ok(Arc::new(wallet))
  188. }
  189. /// Creates a mint quote for the given amount and checks its state in a loop. Returns when
  190. /// amount is minted.
  191. pub async fn fund_wallet(wallet: Arc<Wallet>, amount: u64) -> anyhow::Result<Amount> {
  192. let desired_amount = Amount::from(amount);
  193. let quote = wallet.mint_quote(desired_amount, None).await?;
  194. wait_for_mint_to_be_paid(&wallet, &quote.id, 60).await?;
  195. Ok(wallet
  196. .mint(&quote.id, SplitTarget::default(), None)
  197. .await?
  198. .total_amount()?)
  199. }