init_auth_mint.rs 3.3 KB

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