init_auth_mint.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. use std::collections::{HashMap, HashSet};
  2. use std::sync::Arc;
  3. use anyhow::Result;
  4. use bip39::Mnemonic;
  5. use cashu::nut00::KnownMethod;
  6. use cashu::{AuthRequired, Method, ProtectedEndpoint, RoutePath};
  7. use cdk::cdk_database::{self, MintAuthDatabase, MintDatabase, MintKeysDatabase};
  8. use cdk::mint::{MintBuilder, MintMeltLimits};
  9. use cdk::nuts::{CurrencyUnit, PaymentMethod};
  10. use cdk::types::FeeReserve;
  11. use cdk::wallet::AuthWallet;
  12. use cdk_fake_wallet::FakeWallet;
  13. pub async fn start_fake_mint_with_auth<D, A, K>(
  14. _addr: &str,
  15. _port: u16,
  16. openid_discovery: String,
  17. database: D,
  18. auth_database: A,
  19. key_store: K,
  20. ) -> Result<()>
  21. where
  22. D: MintDatabase<cdk_database::Error> + Send + Sync + 'static,
  23. A: MintAuthDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
  24. K: MintKeysDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
  25. {
  26. let fee_reserve = FeeReserve {
  27. min_fee_reserve: 1.into(),
  28. percent_fee_reserve: 1.0,
  29. };
  30. let fake_wallet = FakeWallet::new(
  31. fee_reserve,
  32. HashMap::default(),
  33. HashSet::default(),
  34. 2,
  35. CurrencyUnit::Sat,
  36. );
  37. let mut mint_builder = MintBuilder::new(Arc::new(database));
  38. mint_builder
  39. .add_payment_processor(
  40. CurrencyUnit::Sat,
  41. PaymentMethod::Known(KnownMethod::Bolt11),
  42. MintMeltLimits::new(1, 300),
  43. Arc::new(fake_wallet),
  44. )
  45. .await?;
  46. let auth_database = Arc::new(auth_database);
  47. mint_builder = mint_builder.with_auth(
  48. auth_database.clone(),
  49. openid_discovery,
  50. "cashu-client".to_string(),
  51. vec![],
  52. );
  53. let blind_auth_endpoints = vec![
  54. ProtectedEndpoint::new(
  55. Method::Post,
  56. RoutePath::MintQuote(PaymentMethod::Known(KnownMethod::Bolt11).to_string()),
  57. ),
  58. ProtectedEndpoint::new(
  59. Method::Post,
  60. RoutePath::Mint(PaymentMethod::Known(KnownMethod::Bolt11).to_string()),
  61. ),
  62. ProtectedEndpoint::new(
  63. Method::Get,
  64. RoutePath::MintQuote(PaymentMethod::Known(KnownMethod::Bolt11).to_string()),
  65. ),
  66. ProtectedEndpoint::new(
  67. Method::Post,
  68. RoutePath::MeltQuote(PaymentMethod::Known(KnownMethod::Bolt11).to_string()),
  69. ),
  70. ProtectedEndpoint::new(
  71. Method::Get,
  72. RoutePath::MeltQuote(PaymentMethod::Known(KnownMethod::Bolt11).to_string()),
  73. ),
  74. ProtectedEndpoint::new(
  75. Method::Post,
  76. RoutePath::Melt(PaymentMethod::Known(KnownMethod::Bolt11).to_string()),
  77. ),
  78. ProtectedEndpoint::new(Method::Post, RoutePath::Swap),
  79. ProtectedEndpoint::new(Method::Post, RoutePath::Checkstate),
  80. ProtectedEndpoint::new(Method::Post, RoutePath::Restore),
  81. ];
  82. let blind_auth_endpoints =
  83. blind_auth_endpoints
  84. .into_iter()
  85. .fold(HashMap::new(), |mut acc, e| {
  86. acc.insert(e, AuthRequired::Blind);
  87. acc
  88. });
  89. mint_builder = mint_builder.with_blind_auth(50, blind_auth_endpoints.keys().cloned().collect());
  90. let mut tx = auth_database.begin_transaction().await?;
  91. tx.add_protected_endpoints(blind_auth_endpoints).await?;
  92. let mut clear_auth_endpoint = HashMap::new();
  93. clear_auth_endpoint.insert(
  94. ProtectedEndpoint::new(Method::Post, RoutePath::MintBlindAuth),
  95. AuthRequired::Clear,
  96. );
  97. tx.add_protected_endpoints(clear_auth_endpoint).await?;
  98. tx.commit().await?;
  99. let mnemonic = Mnemonic::generate(12)?;
  100. mint_builder = mint_builder.with_description("fake test mint".to_string());
  101. let _mint = mint_builder
  102. .build_with_seed(Arc::new(key_store), &mnemonic.to_seed_normalized(""))
  103. .await?;
  104. todo!("Need to start this a cdk mintd keeping as ref for now");
  105. }
  106. pub async fn top_up_blind_auth_proofs(auth_wallet: Arc<AuthWallet>, count: u64) {
  107. let _proofs = auth_wallet
  108. .mint_blind_auth(count.into())
  109. .await
  110. .expect("could not mint blind auth");
  111. }