cashierd.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use async_std::sync::Arc;
  2. use std::net::SocketAddr;
  3. use std::path::PathBuf;
  4. use blake2b_simd::Params;
  5. use drk::cli::{CashierdCli, CashierdConfig, Config};
  6. use drk::serial::{deserialize, serialize};
  7. use drk::service::CashierService;
  8. use drk::util::join_config_path;
  9. use drk::wallet::{CashierDb, WalletDb};
  10. use drk::{Error, Result};
  11. use serde::{Deserialize, Serialize};
  12. use async_executor::Executor;
  13. use easy_parallel::Parallel;
  14. // TODO: this will be replaced by a vector of assets that can be updated at runtime
  15. #[derive(Deserialize, Serialize, Debug)]
  16. pub struct Asset {
  17. pub name: String,
  18. pub id: Vec<u8>,
  19. }
  20. impl Asset {
  21. pub fn new(name: String) -> Self {
  22. let id = Self::id_hash(&name);
  23. Self { name, id }
  24. }
  25. pub fn id_hash(name: &String) -> Vec<u8> {
  26. let mut hasher = Params::new().hash_length(64).to_state();
  27. hasher.update(name.as_bytes());
  28. let result = hasher.finalize();
  29. let hash = jubjub::Fr::from_bytes_wide(result.as_array());
  30. let id = serialize(&hash);
  31. id
  32. }
  33. }
  34. async fn start(executor: Arc<Executor<'_>>, config: Arc<CashierdConfig>) -> Result<()> {
  35. let ex = executor.clone();
  36. let accept_addr: SocketAddr = config.accept_url.parse()?;
  37. let gateway_addr: SocketAddr = config.gateway_url.parse()?;
  38. let database_path = join_config_path(&PathBuf::from("cashier_client_database.db"))?;
  39. let cashierdb = join_config_path(&PathBuf::from("cashier.db"))?;
  40. let client_wallet = join_config_path(&PathBuf::from("cashier_client_walletdb.db"))?;
  41. let wallet = CashierDb::new(
  42. &cashierdb.clone(),
  43. config.password.clone(),
  44. )?;
  45. let client_wallet = WalletDb::new(
  46. &client_wallet.clone(),
  47. config.client_password.clone(),
  48. )?;
  49. let mint_params_path = join_config_path(&PathBuf::from("cashier_mint.params"))?;
  50. let spend_params_path = join_config_path(&PathBuf::from("cashier_spend.params"))?;
  51. let mut cashier = CashierService::new(
  52. accept_addr,
  53. wallet.clone(),
  54. client_wallet.clone(),
  55. database_path,
  56. (gateway_addr, "127.0.0.1:4444".parse()?),
  57. (mint_params_path, spend_params_path),
  58. )
  59. .await?;
  60. // TODO: make this a vector of accepted assets
  61. let asset = Asset::new("btc".to_string());
  62. // TODO: this should be done by the user
  63. let asset_id = deserialize(&asset.id)?;
  64. // TODO: pass vector of assets into cashier.start()
  65. cashier.start(ex.clone(), asset_id).await?;
  66. Ok(())
  67. }
  68. fn main() -> Result<()> {
  69. let ex = Arc::new(Executor::new());
  70. let (signal, shutdown) = async_channel::unbounded::<()>();
  71. let path = join_config_path(&PathBuf::from("cashierd.toml")).unwrap();
  72. let config: CashierdConfig = Config::<CashierdConfig>::load(path)?;
  73. let config = Arc::new(config);
  74. let options = CashierdCli::load()?;
  75. {
  76. use simplelog::*;
  77. let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
  78. let debug_level = if options.verbose {
  79. LevelFilter::Debug
  80. } else {
  81. LevelFilter::Off
  82. };
  83. let log_path = config.log_path.clone();
  84. CombinedLogger::init(vec![
  85. TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
  86. WriteLogger::new(
  87. LevelFilter::Debug,
  88. Config::default(),
  89. std::fs::File::create(log_path).unwrap(),
  90. ),
  91. ])
  92. .unwrap();
  93. }
  94. let ex2 = ex.clone();
  95. let (_, result) = Parallel::new()
  96. // Run four executor threads.
  97. .each(0..3, |_| smol::future::block_on(ex.run(shutdown.recv())))
  98. // Run the main future on the current thread.
  99. .finish(|| {
  100. smol::future::block_on(async move {
  101. start(ex2, config).await?;
  102. drop(signal);
  103. Ok::<(), Error>(())
  104. })
  105. });
  106. result
  107. }