env_vars.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. use std::env;
  2. use std::path::PathBuf;
  3. use std::str::FromStr;
  4. use anyhow::{anyhow, bail, Result};
  5. use cdk::nuts::CurrencyUnit;
  6. #[cfg(feature = "management-rpc")]
  7. use crate::config::MintManagementRpc;
  8. use crate::config::{
  9. Cln, Database, DatabaseEngine, FakeWallet, Info, LNbits, Ln, LnBackend, Lnd, MintInfo,
  10. Phoenixd, Settings, Strike,
  11. };
  12. pub const ENV_WORK_DIR: &str = "CDK_MINTD_WORK_DIR";
  13. pub const DATABASE_ENV_VAR: &str = "CDK_MINTD_DATABASE";
  14. pub const ENV_URL: &str = "CDK_MINTD_URL";
  15. pub const ENV_LISTEN_HOST: &str = "CDK_MINTD_LISTEN_HOST";
  16. pub const ENV_LISTEN_PORT: &str = "CDK_MINTD_LISTEN_PORT";
  17. pub const ENV_MNEMONIC: &str = "CDK_MINTD_MNEMONIC";
  18. pub const ENV_SECONDS_QUOTE_VALID: &str = "CDK_MINTD_SECONDS_QUOTE_VALID";
  19. pub const ENV_CACHE_SECONDS: &str = "CDK_MINTD_CACHE_SECONDS";
  20. pub const ENV_EXTEND_CACHE_SECONDS: &str = "CDK_MINTD_EXTEND_CACHE_SECONDS";
  21. pub const ENV_INPUT_FEE_PPK: &str = "CDK_MINTD_INPUT_FEE_PPK";
  22. pub const ENV_ENABLE_SWAGGER: &str = "CDK_MINTD_ENABLE_SWAGGER";
  23. // MintInfo
  24. pub const ENV_MINT_NAME: &str = "CDK_MINTD_MINT_NAME";
  25. pub const ENV_MINT_PUBKEY: &str = "CDK_MINTD_MINT_PUBKEY";
  26. pub const ENV_MINT_DESCRIPTION: &str = "CDK_MINTD_MINT_DESCRIPTION";
  27. pub const ENV_MINT_DESCRIPTION_LONG: &str = "CDK_MINTD_MINT_DESCRIPTION_LONG";
  28. pub const ENV_MINT_ICON_URL: &str = "CDK_MINTD_MINT_ICON_URL";
  29. pub const ENV_MINT_MOTD: &str = "CDK_MINTD_MINT_MOTD";
  30. pub const ENV_MINT_CONTACT_NOSTR: &str = "CDK_MINTD_MINT_CONTACT_NOSTR";
  31. pub const ENV_MINT_CONTACT_EMAIL: &str = "CDK_MINTD_MINT_CONTACT_EMAIL";
  32. // LN
  33. pub const ENV_LN_BACKEND: &str = "CDK_MINTD_LN_BACKEND";
  34. pub const ENV_LN_INVOICE_DESCRIPTION: &str = "CDK_MINTD_LN_INVOICE_DESCRIPTION";
  35. pub const ENV_LN_MIN_MINT: &str = "CDK_MINTD_LN_MIN_MINT";
  36. pub const ENV_LN_MAX_MINT: &str = "CDK_MINTD_LN_MAX_MINT";
  37. pub const ENV_LN_MIN_MELT: &str = "CDK_MINTD_LN_MIN_MELT";
  38. pub const ENV_LN_MAX_MELT: &str = "CDK_MINTD_LN_MAX_MELT";
  39. // CLN
  40. pub const ENV_CLN_RPC_PATH: &str = "CDK_MINTD_CLN_RPC_PATH";
  41. pub const ENV_CLN_BOLT12: &str = "CDK_MINTD_CLN_BOLT12";
  42. pub const ENV_CLN_FEE_PERCENT: &str = "CDK_MINTD_CLN_FEE_PERCENT";
  43. pub const ENV_CLN_RESERVE_FEE_MIN: &str = "CDK_MINTD_CLN_RESERVE_FEE_MIN";
  44. // Strike
  45. pub const ENV_STRIKE_API_KEY: &str = "CDK_MINTD_STRIKE_API_KEY";
  46. pub const ENV_STRIKE_SUPPORTED_UNITS: &str = "CDK_MINTD_STRIKE_SUPPORTED_UNITS";
  47. // LND environment variables
  48. pub const ENV_LND_ADDRESS: &str = "CDK_MINTD_LND_ADDRESS";
  49. pub const ENV_LND_CERT_FILE: &str = "CDK_MINTD_LND_CERT_FILE";
  50. pub const ENV_LND_MACAROON_FILE: &str = "CDK_MINTD_LND_MACAROON_FILE";
  51. pub const ENV_LND_FEE_PERCENT: &str = "CDK_MINTD_LND_FEE_PERCENT";
  52. pub const ENV_LND_RESERVE_FEE_MIN: &str = "CDK_MINTD_LND_RESERVE_FEE_MIN";
  53. // Phoenixd environment variables
  54. pub const ENV_PHOENIXD_API_PASSWORD: &str = "CDK_MINTD_PHOENIXD_API_PASSWORD";
  55. pub const ENV_PHOENIXD_API_URL: &str = "CDK_MINTD_PHOENIXD_API_URL";
  56. pub const ENV_PHOENIXD_BOLT12: &str = "CDK_MINTD_PHOENIXD_BOLT12";
  57. pub const ENV_PHOENIXD_FEE_PERCENT: &str = "CDK_MINTD_PHOENIXD_FEE_PERCENT";
  58. pub const ENV_PHOENIXD_RESERVE_FEE_MIN: &str = "CDK_MINTD_PHOENIXD_RESERVE_FEE_MIN";
  59. // LNBits
  60. pub const ENV_LNBITS_ADMIN_API_KEY: &str = "CDK_MINTD_LNBITS_ADMIN_API_KEY";
  61. pub const ENV_LNBITS_INVOICE_API_KEY: &str = "CDK_MINTD_LNBITS_INVOICE_API_KEY";
  62. pub const ENV_LNBITS_API: &str = "CDK_MINTD_LNBITS_API";
  63. pub const ENV_LNBITS_FEE_PERCENT: &str = "CDK_MINTD_LNBITS_FEE_PERCENT";
  64. pub const ENV_LNBITS_RESERVE_FEE_MIN: &str = "CDK_MINTD_LNBITS_RESERVE_FEE_MIN";
  65. // Fake Wallet
  66. pub const ENV_FAKE_WALLET_SUPPORTED_UNITS: &str = "CDK_MINTD_FAKE_WALLET_SUPPORTED_UNITS";
  67. pub const ENV_FAKE_WALLET_FEE_PERCENT: &str = "CDK_MINTD_FAKE_WALLET_FEE_PERCENT";
  68. pub const ENV_FAKE_WALLET_RESERVE_FEE_MIN: &str = "CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN";
  69. pub const ENV_FAKE_WALLET_MIN_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MIN_DELAY";
  70. pub const ENV_FAKE_WALLET_MAX_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MAX_DELAY";
  71. // Mint RPC Server
  72. #[cfg(feature = "management-rpc")]
  73. pub const ENV_MINT_MANAGEMENT_ENABLED: &str = "CDK_MINTD_MINT_MANAGEMENT_ENABLED";
  74. #[cfg(feature = "management-rpc")]
  75. pub const ENV_MINT_MANAGEMENT_ADDRESS: &str = "CDK_MINTD_MANAGEMENT_ADDRESS";
  76. #[cfg(feature = "management-rpc")]
  77. pub const ENV_MINT_MANAGEMENT_PORT: &str = "CDK_MINTD_MANAGEMENT_PORT";
  78. #[cfg(feature = "management-rpc")]
  79. pub const ENV_MINT_MANAGEMENT_TLS_DIR_PATH: &str = "CDK_MINTD_MANAGEMENT_TLS_DIR_PATH";
  80. impl Settings {
  81. pub fn from_env(&mut self) -> Result<Self> {
  82. if let Ok(database) = env::var(DATABASE_ENV_VAR) {
  83. let engine = DatabaseEngine::from_str(&database).map_err(|err| anyhow!(err))?;
  84. self.database = Database { engine };
  85. }
  86. self.info = self.info.clone().from_env();
  87. self.mint_info = self.mint_info.clone().from_env();
  88. self.ln = self.ln.clone().from_env();
  89. #[cfg(feature = "management-rpc")]
  90. {
  91. self.mint_management_rpc = Some(
  92. self.mint_management_rpc
  93. .clone()
  94. .unwrap_or_default()
  95. .from_env(),
  96. );
  97. }
  98. match self.ln.ln_backend {
  99. LnBackend::Cln => {
  100. self.cln = Some(self.cln.clone().unwrap_or_default().from_env());
  101. }
  102. LnBackend::Strike => {
  103. self.strike = Some(self.strike.clone().unwrap_or_default().from_env());
  104. }
  105. LnBackend::LNbits => {
  106. self.lnbits = Some(self.lnbits.clone().unwrap_or_default().from_env());
  107. }
  108. LnBackend::FakeWallet => {
  109. self.fake_wallet = Some(self.fake_wallet.clone().unwrap_or_default().from_env());
  110. }
  111. LnBackend::Phoenixd => {
  112. self.phoenixd = Some(self.phoenixd.clone().unwrap_or_default().from_env());
  113. }
  114. LnBackend::Lnd => {
  115. self.lnd = Some(self.lnd.clone().unwrap_or_default().from_env());
  116. }
  117. LnBackend::None => bail!("Ln backend must be set"),
  118. }
  119. Ok(self.clone())
  120. }
  121. }
  122. impl Info {
  123. pub fn from_env(mut self) -> Self {
  124. // Required fields
  125. if let Ok(url) = env::var(ENV_URL) {
  126. self.url = url;
  127. }
  128. if let Ok(host) = env::var(ENV_LISTEN_HOST) {
  129. self.listen_host = host;
  130. }
  131. if let Ok(port_str) = env::var(ENV_LISTEN_PORT) {
  132. if let Ok(port) = port_str.parse() {
  133. self.listen_port = port;
  134. }
  135. }
  136. if let Ok(mnemonic) = env::var(ENV_MNEMONIC) {
  137. self.mnemonic = mnemonic;
  138. }
  139. if let Ok(cache_seconds_str) = env::var(ENV_CACHE_SECONDS) {
  140. if let Ok(seconds) = cache_seconds_str.parse() {
  141. self.http_cache.ttl = Some(seconds);
  142. }
  143. }
  144. if let Ok(extend_cache_str) = env::var(ENV_EXTEND_CACHE_SECONDS) {
  145. if let Ok(seconds) = extend_cache_str.parse() {
  146. self.http_cache.tti = Some(seconds);
  147. }
  148. }
  149. if let Ok(fee_str) = env::var(ENV_INPUT_FEE_PPK) {
  150. if let Ok(fee) = fee_str.parse() {
  151. self.input_fee_ppk = Some(fee);
  152. }
  153. }
  154. if let Ok(swagger_str) = env::var(ENV_ENABLE_SWAGGER) {
  155. if let Ok(enable) = swagger_str.parse() {
  156. self.enable_swagger_ui = Some(enable);
  157. }
  158. }
  159. self.http_cache = self.http_cache.from_env();
  160. self
  161. }
  162. }
  163. impl MintInfo {
  164. pub fn from_env(mut self) -> Self {
  165. // Required fields
  166. if let Ok(name) = env::var(ENV_MINT_NAME) {
  167. self.name = name;
  168. }
  169. if let Ok(description) = env::var(ENV_MINT_DESCRIPTION) {
  170. self.description = description;
  171. }
  172. // Optional fields
  173. if let Ok(pubkey_str) = env::var(ENV_MINT_PUBKEY) {
  174. // Assuming PublicKey has a from_str implementation
  175. if let Ok(pubkey) = pubkey_str.parse() {
  176. self.pubkey = Some(pubkey);
  177. }
  178. }
  179. if let Ok(desc_long) = env::var(ENV_MINT_DESCRIPTION_LONG) {
  180. self.description_long = Some(desc_long);
  181. }
  182. if let Ok(icon_url) = env::var(ENV_MINT_ICON_URL) {
  183. self.icon_url = Some(icon_url);
  184. }
  185. if let Ok(motd) = env::var(ENV_MINT_MOTD) {
  186. self.motd = Some(motd);
  187. }
  188. if let Ok(nostr_key) = env::var(ENV_MINT_CONTACT_NOSTR) {
  189. self.contact_nostr_public_key = Some(nostr_key);
  190. }
  191. if let Ok(email) = env::var(ENV_MINT_CONTACT_EMAIL) {
  192. self.contact_email = Some(email);
  193. }
  194. self
  195. }
  196. }
  197. impl Ln {
  198. pub fn from_env(mut self) -> Self {
  199. // LnBackend
  200. if let Ok(backend_str) = env::var(ENV_LN_BACKEND) {
  201. if let Ok(backend) = backend_str.parse() {
  202. self.ln_backend = backend;
  203. }
  204. }
  205. // Optional invoice description
  206. if let Ok(description) = env::var(ENV_LN_INVOICE_DESCRIPTION) {
  207. self.invoice_description = Some(description);
  208. }
  209. // Amount fields
  210. if let Ok(min_mint_str) = env::var(ENV_LN_MIN_MINT) {
  211. if let Ok(amount) = min_mint_str.parse::<u64>() {
  212. self.min_mint = amount.into();
  213. }
  214. }
  215. if let Ok(max_mint_str) = env::var(ENV_LN_MAX_MINT) {
  216. if let Ok(amount) = max_mint_str.parse::<u64>() {
  217. self.max_mint = amount.into();
  218. }
  219. }
  220. if let Ok(min_melt_str) = env::var(ENV_LN_MIN_MELT) {
  221. if let Ok(amount) = min_melt_str.parse::<u64>() {
  222. self.min_melt = amount.into();
  223. }
  224. }
  225. if let Ok(max_melt_str) = env::var(ENV_LN_MAX_MELT) {
  226. if let Ok(amount) = max_melt_str.parse::<u64>() {
  227. self.max_melt = amount.into();
  228. }
  229. }
  230. self
  231. }
  232. }
  233. impl Cln {
  234. pub fn from_env(mut self) -> Self {
  235. // RPC Path
  236. if let Ok(path) = env::var(ENV_CLN_RPC_PATH) {
  237. self.rpc_path = PathBuf::from(path);
  238. }
  239. // BOLT12 flag
  240. if let Ok(bolt12_str) = env::var(ENV_CLN_BOLT12) {
  241. if let Ok(bolt12) = bolt12_str.parse() {
  242. self.bolt12 = bolt12;
  243. }
  244. }
  245. // Fee percent
  246. if let Ok(fee_str) = env::var(ENV_CLN_FEE_PERCENT) {
  247. if let Ok(fee) = fee_str.parse() {
  248. self.fee_percent = fee;
  249. }
  250. }
  251. // Reserve fee minimum
  252. if let Ok(reserve_fee_str) = env::var(ENV_CLN_RESERVE_FEE_MIN) {
  253. if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
  254. self.reserve_fee_min = reserve_fee.into();
  255. }
  256. }
  257. self
  258. }
  259. }
  260. impl Strike {
  261. pub fn from_env(mut self) -> Self {
  262. // API Key
  263. if let Ok(api_key) = env::var(ENV_STRIKE_API_KEY) {
  264. self.api_key = api_key;
  265. }
  266. // Supported Units - expects comma-separated list
  267. if let Ok(units_str) = env::var(ENV_STRIKE_SUPPORTED_UNITS) {
  268. self.supported_units = Some(
  269. units_str
  270. .split(',')
  271. .filter_map(|s| s.trim().parse().ok())
  272. .collect(),
  273. );
  274. }
  275. self
  276. }
  277. }
  278. impl Lnd {
  279. pub fn from_env(mut self) -> Self {
  280. if let Ok(address) = env::var(ENV_LND_ADDRESS) {
  281. self.address = address;
  282. }
  283. if let Ok(cert_path) = env::var(ENV_LND_CERT_FILE) {
  284. self.cert_file = PathBuf::from(cert_path);
  285. }
  286. if let Ok(macaroon_path) = env::var(ENV_LND_MACAROON_FILE) {
  287. self.macaroon_file = PathBuf::from(macaroon_path);
  288. }
  289. if let Ok(fee_str) = env::var(ENV_LND_FEE_PERCENT) {
  290. if let Ok(fee) = fee_str.parse() {
  291. self.fee_percent = fee;
  292. }
  293. }
  294. if let Ok(reserve_fee_str) = env::var(ENV_LND_RESERVE_FEE_MIN) {
  295. if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
  296. self.reserve_fee_min = reserve_fee.into();
  297. }
  298. }
  299. self
  300. }
  301. }
  302. impl Phoenixd {
  303. pub fn from_env(mut self) -> Self {
  304. if let Ok(password) = env::var(ENV_PHOENIXD_API_PASSWORD) {
  305. self.api_password = password;
  306. }
  307. if let Ok(url) = env::var(ENV_PHOENIXD_API_URL) {
  308. self.api_url = url;
  309. }
  310. if let Ok(bolt12_str) = env::var(ENV_PHOENIXD_BOLT12) {
  311. if let Ok(bolt12) = bolt12_str.parse() {
  312. self.bolt12 = bolt12;
  313. }
  314. }
  315. if let Ok(fee_str) = env::var(ENV_PHOENIXD_FEE_PERCENT) {
  316. if let Ok(fee) = fee_str.parse() {
  317. self.fee_percent = fee;
  318. }
  319. }
  320. if let Ok(reserve_fee_str) = env::var(ENV_PHOENIXD_RESERVE_FEE_MIN) {
  321. if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
  322. self.reserve_fee_min = reserve_fee.into();
  323. }
  324. }
  325. self
  326. }
  327. }
  328. impl LNbits {
  329. pub fn from_env(mut self) -> Self {
  330. if let Ok(admin_key) = env::var(ENV_LNBITS_ADMIN_API_KEY) {
  331. self.admin_api_key = admin_key;
  332. }
  333. if let Ok(invoice_key) = env::var(ENV_LNBITS_INVOICE_API_KEY) {
  334. self.invoice_api_key = invoice_key;
  335. }
  336. if let Ok(api) = env::var(ENV_LNBITS_API) {
  337. self.lnbits_api = api;
  338. }
  339. if let Ok(fee_str) = env::var(ENV_LNBITS_FEE_PERCENT) {
  340. if let Ok(fee) = fee_str.parse() {
  341. self.fee_percent = fee;
  342. }
  343. }
  344. if let Ok(reserve_fee_str) = env::var(ENV_LNBITS_RESERVE_FEE_MIN) {
  345. if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
  346. self.reserve_fee_min = reserve_fee.into();
  347. }
  348. }
  349. self
  350. }
  351. }
  352. impl FakeWallet {
  353. pub fn from_env(mut self) -> Self {
  354. // Supported Units - expects comma-separated list
  355. if let Ok(units_str) = env::var(ENV_FAKE_WALLET_SUPPORTED_UNITS) {
  356. if let Ok(units) = units_str
  357. .split(',')
  358. .map(|s| s.trim().parse())
  359. .collect::<Result<Vec<CurrencyUnit>, _>>()
  360. {
  361. self.supported_units = units;
  362. }
  363. }
  364. if let Ok(fee_str) = env::var(ENV_FAKE_WALLET_FEE_PERCENT) {
  365. if let Ok(fee) = fee_str.parse() {
  366. self.fee_percent = fee;
  367. }
  368. }
  369. if let Ok(reserve_fee_str) = env::var(ENV_FAKE_WALLET_RESERVE_FEE_MIN) {
  370. if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
  371. self.reserve_fee_min = reserve_fee.into();
  372. }
  373. }
  374. if let Ok(min_delay_str) = env::var(ENV_FAKE_WALLET_MIN_DELAY) {
  375. if let Ok(min_delay) = min_delay_str.parse() {
  376. self.min_delay_time = min_delay;
  377. }
  378. }
  379. if let Ok(max_delay_str) = env::var(ENV_FAKE_WALLET_MAX_DELAY) {
  380. if let Ok(max_delay) = max_delay_str.parse() {
  381. self.max_delay_time = max_delay;
  382. }
  383. }
  384. self
  385. }
  386. }
  387. #[cfg(feature = "management-rpc")]
  388. impl MintManagementRpc {
  389. pub fn from_env(mut self) -> Self {
  390. if let Ok(enabled) = env::var(ENV_MINT_MANAGEMENT_ENABLED) {
  391. if let Ok(enabled) = enabled.parse() {
  392. self.enabled = enabled;
  393. }
  394. }
  395. if let Ok(address) = env::var(ENV_MINT_MANAGEMENT_ADDRESS) {
  396. self.address = Some(address);
  397. }
  398. if let Ok(port) = env::var(ENV_MINT_MANAGEMENT_PORT) {
  399. if let Ok(port) = port.parse::<u16>() {
  400. self.port = Some(port);
  401. }
  402. }
  403. if let Ok(tls_path) = env::var(ENV_MINT_MANAGEMENT_TLS_DIR_PATH) {
  404. self.tls_dir_path = Some(tls_path.into());
  405. }
  406. self
  407. }
  408. }