cli_config.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. use serde::de::DeserializeOwned;
  2. use serde::{Deserialize, Serialize};
  3. use std::{
  4. fs,
  5. marker::PhantomData,
  6. net::SocketAddr,
  7. path::{Path, PathBuf},
  8. str,
  9. };
  10. use crate::{Error, Result};
  11. #[derive(Clone, Default)]
  12. pub struct Config<T> {
  13. config: PhantomData<T>,
  14. }
  15. impl<T: Serialize + DeserializeOwned> Config<T> {
  16. pub fn load(path: PathBuf) -> Result<T> {
  17. if Path::new(&path).exists() {
  18. let toml = fs::read(&path)?;
  19. let str_buff = str::from_utf8(&toml)?;
  20. let config: T = toml::from_str(str_buff)?;
  21. Ok(config)
  22. } else {
  23. println!("Could not parse configuration");
  24. println!("Please follow the instructions in the README");
  25. Err(Error::ConfigNotFound)
  26. }
  27. }
  28. }
  29. /// The configuration for drk
  30. #[derive(Clone, Serialize, Deserialize, Debug)]
  31. pub struct DrkConfig {
  32. /// The URL where darkfid RPC is listening on
  33. pub darkfid_rpc_url: String,
  34. }
  35. /// The configuration for darkfid
  36. #[derive(Clone, Serialize, Deserialize, Debug)]
  37. pub struct DarkfidConfig {
  38. /// The address where darkfid should bind its RPC socket
  39. pub rpc_listen_address: SocketAddr,
  40. /// Whether to listen with TLS or plain TCP
  41. pub serve_tls: bool,
  42. /// Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
  43. pub tls_identity_path: String,
  44. /// Password for the TLS identity. (Unused if serve_tls=false)
  45. pub tls_identity_password: String,
  46. /// The RPC endpoint for a selected cashier
  47. pub cashier_rpc_url: String,
  48. /// Path to the client database
  49. pub database_path: String,
  50. /// Path to the wallet database
  51. pub wallet_path: String,
  52. /// The wallet password
  53. pub wallet_password: String,
  54. }
  55. /// The configuration for gatewayd
  56. #[derive(Serialize, Deserialize, Debug)]
  57. pub struct GatewaydConfig {
  58. /// The address where gatewayd should bind its protocol socket
  59. pub protocol_listen_address: SocketAddr,
  60. /// The address where gatewayd should bind its publisher socket
  61. pub publisher_listen_address: SocketAddr,
  62. /// Whether to listen with TLS or plain TCP
  63. pub serve_tls: bool,
  64. /// Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
  65. pub tls_identity_path: String,
  66. /// Password for the TLS identity. (Unused if serve_tls=false)
  67. pub tls_identity_password: String,
  68. /// Path to the database
  69. pub database_path: String,
  70. }
  71. #[derive(Clone, Debug, Serialize, Deserialize)]
  72. pub struct FeatureNetwork {
  73. /// Network name
  74. pub name: String,
  75. /// Blockchain (mainnet/testnet/etc.)
  76. pub blockchain: String,
  77. }
  78. #[derive(Clone, Serialize, Deserialize, Debug)]
  79. pub struct CashierdConfig {
  80. /// The endpoint where cashierd will bind its RPC socket
  81. pub rpc_listen_address: SocketAddr,
  82. /// Whether to listen with TLS or plain TCP
  83. pub serve_tls: bool,
  84. /// Path to DER-formatted PKCS#12 archive. (Unused if serve_tls=false)
  85. pub tls_identity_path: String,
  86. /// Password for the TLS identity. (Unused if serve_tls=false)
  87. pub tls_identity_password: String,
  88. /// The endpoint to a gatewayd protocol API
  89. pub gateway_protocol_url: String,
  90. /// The endpoint to a gatewayd publisher API
  91. pub gateway_publisher_url: String,
  92. /// Path to mint.params
  93. pub mint_params_path: String,
  94. /// Path to spend.params
  95. pub spend_params_path: String,
  96. /// Path to cashierd wallet
  97. pub cashier_wallet_path: String,
  98. /// Password for cashierd wallet
  99. pub cashier_wallet_password: String,
  100. /// Path to client wallet
  101. pub client_wallet_path: String,
  102. /// Password for client wallet
  103. pub client_wallet_password: String,
  104. /// Path to database
  105. pub database_path: String,
  106. /// The configured networks to use
  107. pub networks: Vec<FeatureNetwork>,
  108. }