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();
  31. mint_builder = mint_builder
  32. .with_localstore(Arc::new(database))
  33. .with_keystore(Arc::new(key_store));
  34. mint_builder = mint_builder
  35. .add_ln_backend(
  36. CurrencyUnit::Sat,
  37. PaymentMethod::Bolt11,
  38. MintMeltLimits::new(1, 300),
  39. Arc::new(fake_wallet),
  40. )
  41. .await?;
  42. mint_builder =
  43. mint_builder.set_clear_auth_settings(openid_discovery, "cashu-client".to_string());
  44. mint_builder = mint_builder.set_blind_auth_settings(50);
  45. let blind_auth_endpoints = vec![
  46. ProtectedEndpoint::new(Method::Post, RoutePath::MintQuoteBolt11),
  47. ProtectedEndpoint::new(Method::Post, RoutePath::MintBolt11),
  48. ProtectedEndpoint::new(Method::Get, RoutePath::MintQuoteBolt11),
  49. ProtectedEndpoint::new(Method::Post, RoutePath::MeltQuoteBolt11),
  50. ProtectedEndpoint::new(Method::Get, RoutePath::MeltQuoteBolt11),
  51. ProtectedEndpoint::new(Method::Post, RoutePath::MeltBolt11),
  52. ProtectedEndpoint::new(Method::Post, RoutePath::Swap),
  53. ProtectedEndpoint::new(Method::Post, RoutePath::Checkstate),
  54. ProtectedEndpoint::new(Method::Post, RoutePath::Restore),
  55. ];
  56. let blind_auth_endpoints =
  57. blind_auth_endpoints
  58. .into_iter()
  59. .fold(HashMap::new(), |mut acc, e| {
  60. acc.insert(e, AuthRequired::Blind);
  61. acc
  62. });
  63. auth_database
  64. .add_protected_endpoints(blind_auth_endpoints)
  65. .await?;
  66. let mut clear_auth_endpoint = HashMap::new();
  67. clear_auth_endpoint.insert(
  68. ProtectedEndpoint::new(Method::Post, RoutePath::MintBlindAuth),
  69. AuthRequired::Clear,
  70. );
  71. auth_database
  72. .add_protected_endpoints(clear_auth_endpoint)
  73. .await?;
  74. mint_builder = mint_builder.with_auth_localstore(Arc::new(auth_database));
  75. let mnemonic = Mnemonic::generate(12)?;
  76. mint_builder = mint_builder
  77. .with_description("fake test mint".to_string())
  78. .with_seed(mnemonic.to_seed_normalized("").to_vec());
  79. let _mint = mint_builder.build().await?;
  80. todo!("Need to start this a cdk mintd keeping as ref for now");
  81. }
  82. pub async fn top_up_blind_auth_proofs(auth_wallet: Arc<AuthWallet>, count: u64) {
  83. let _proofs = auth_wallet
  84. .mint_blind_auth(count.into())
  85. .await
  86. .expect("could not mint blind auth");
  87. }