init_auth_mint.rs 3.4 KB

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