123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- use std::env;
- use std::path::PathBuf;
- use std::str::FromStr;
- use anyhow::{anyhow, bail, Result};
- use cdk::nuts::CurrencyUnit;
- #[cfg(feature = "management-rpc")]
- use crate::config::MintManagementRpc;
- use crate::config::{
- Cln, Database, DatabaseEngine, FakeWallet, Info, LNbits, Ln, LnBackend, Lnd, MintInfo,
- Phoenixd, Settings, Strike,
- };
- pub const ENV_WORK_DIR: &str = "CDK_MINTD_WORK_DIR";
- pub const DATABASE_ENV_VAR: &str = "CDK_MINTD_DATABASE";
- pub const ENV_URL: &str = "CDK_MINTD_URL";
- pub const ENV_LISTEN_HOST: &str = "CDK_MINTD_LISTEN_HOST";
- pub const ENV_LISTEN_PORT: &str = "CDK_MINTD_LISTEN_PORT";
- pub const ENV_MNEMONIC: &str = "CDK_MINTD_MNEMONIC";
- pub const ENV_SECONDS_QUOTE_VALID: &str = "CDK_MINTD_SECONDS_QUOTE_VALID";
- pub const ENV_CACHE_SECONDS: &str = "CDK_MINTD_CACHE_SECONDS";
- pub const ENV_EXTEND_CACHE_SECONDS: &str = "CDK_MINTD_EXTEND_CACHE_SECONDS";
- pub const ENV_INPUT_FEE_PPK: &str = "CDK_MINTD_INPUT_FEE_PPK";
- pub const ENV_ENABLE_SWAGGER: &str = "CDK_MINTD_ENABLE_SWAGGER";
- // MintInfo
- pub const ENV_MINT_NAME: &str = "CDK_MINTD_MINT_NAME";
- pub const ENV_MINT_PUBKEY: &str = "CDK_MINTD_MINT_PUBKEY";
- pub const ENV_MINT_DESCRIPTION: &str = "CDK_MINTD_MINT_DESCRIPTION";
- pub const ENV_MINT_DESCRIPTION_LONG: &str = "CDK_MINTD_MINT_DESCRIPTION_LONG";
- pub const ENV_MINT_ICON_URL: &str = "CDK_MINTD_MINT_ICON_URL";
- pub const ENV_MINT_MOTD: &str = "CDK_MINTD_MINT_MOTD";
- pub const ENV_MINT_CONTACT_NOSTR: &str = "CDK_MINTD_MINT_CONTACT_NOSTR";
- pub const ENV_MINT_CONTACT_EMAIL: &str = "CDK_MINTD_MINT_CONTACT_EMAIL";
- // LN
- pub const ENV_LN_BACKEND: &str = "CDK_MINTD_LN_BACKEND";
- pub const ENV_LN_INVOICE_DESCRIPTION: &str = "CDK_MINTD_LN_INVOICE_DESCRIPTION";
- pub const ENV_LN_MIN_MINT: &str = "CDK_MINTD_LN_MIN_MINT";
- pub const ENV_LN_MAX_MINT: &str = "CDK_MINTD_LN_MAX_MINT";
- pub const ENV_LN_MIN_MELT: &str = "CDK_MINTD_LN_MIN_MELT";
- pub const ENV_LN_MAX_MELT: &str = "CDK_MINTD_LN_MAX_MELT";
- // CLN
- pub const ENV_CLN_RPC_PATH: &str = "CDK_MINTD_CLN_RPC_PATH";
- pub const ENV_CLN_BOLT12: &str = "CDK_MINTD_CLN_BOLT12";
- pub const ENV_CLN_FEE_PERCENT: &str = "CDK_MINTD_CLN_FEE_PERCENT";
- pub const ENV_CLN_RESERVE_FEE_MIN: &str = "CDK_MINTD_CLN_RESERVE_FEE_MIN";
- // Strike
- pub const ENV_STRIKE_API_KEY: &str = "CDK_MINTD_STRIKE_API_KEY";
- pub const ENV_STRIKE_SUPPORTED_UNITS: &str = "CDK_MINTD_STRIKE_SUPPORTED_UNITS";
- // LND environment variables
- pub const ENV_LND_ADDRESS: &str = "CDK_MINTD_LND_ADDRESS";
- pub const ENV_LND_CERT_FILE: &str = "CDK_MINTD_LND_CERT_FILE";
- pub const ENV_LND_MACAROON_FILE: &str = "CDK_MINTD_LND_MACAROON_FILE";
- pub const ENV_LND_FEE_PERCENT: &str = "CDK_MINTD_LND_FEE_PERCENT";
- pub const ENV_LND_RESERVE_FEE_MIN: &str = "CDK_MINTD_LND_RESERVE_FEE_MIN";
- // Phoenixd environment variables
- pub const ENV_PHOENIXD_API_PASSWORD: &str = "CDK_MINTD_PHOENIXD_API_PASSWORD";
- pub const ENV_PHOENIXD_API_URL: &str = "CDK_MINTD_PHOENIXD_API_URL";
- pub const ENV_PHOENIXD_BOLT12: &str = "CDK_MINTD_PHOENIXD_BOLT12";
- pub const ENV_PHOENIXD_FEE_PERCENT: &str = "CDK_MINTD_PHOENIXD_FEE_PERCENT";
- pub const ENV_PHOENIXD_RESERVE_FEE_MIN: &str = "CDK_MINTD_PHOENIXD_RESERVE_FEE_MIN";
- // LNBits
- pub const ENV_LNBITS_ADMIN_API_KEY: &str = "CDK_MINTD_LNBITS_ADMIN_API_KEY";
- pub const ENV_LNBITS_INVOICE_API_KEY: &str = "CDK_MINTD_LNBITS_INVOICE_API_KEY";
- pub const ENV_LNBITS_API: &str = "CDK_MINTD_LNBITS_API";
- pub const ENV_LNBITS_FEE_PERCENT: &str = "CDK_MINTD_LNBITS_FEE_PERCENT";
- pub const ENV_LNBITS_RESERVE_FEE_MIN: &str = "CDK_MINTD_LNBITS_RESERVE_FEE_MIN";
- // Fake Wallet
- pub const ENV_FAKE_WALLET_SUPPORTED_UNITS: &str = "CDK_MINTD_FAKE_WALLET_SUPPORTED_UNITS";
- pub const ENV_FAKE_WALLET_FEE_PERCENT: &str = "CDK_MINTD_FAKE_WALLET_FEE_PERCENT";
- pub const ENV_FAKE_WALLET_RESERVE_FEE_MIN: &str = "CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN";
- pub const ENV_FAKE_WALLET_MIN_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MIN_DELAY";
- pub const ENV_FAKE_WALLET_MAX_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MAX_DELAY";
- // Mint RPC Server
- #[cfg(feature = "management-rpc")]
- pub const ENV_MINT_MANAGEMENT_ENABLED: &str = "CDK_MINTD_MINT_MANAGEMENT_ENABLED";
- #[cfg(feature = "management-rpc")]
- pub const ENV_MINT_MANAGEMENT_ADDRESS: &str = "CDK_MINTD_MANAGEMENT_ADDRESS";
- #[cfg(feature = "management-rpc")]
- pub const ENV_MINT_MANAGEMENT_PORT: &str = "CDK_MINTD_MANAGEMENT_PORT";
- #[cfg(feature = "management-rpc")]
- pub const ENV_MINT_MANAGEMENT_TLS_DIR_PATH: &str = "CDK_MINTD_MANAGEMENT_TLS_DIR_PATH";
- impl Settings {
- pub fn from_env(&mut self) -> Result<Self> {
- if let Ok(database) = env::var(DATABASE_ENV_VAR) {
- let engine = DatabaseEngine::from_str(&database).map_err(|err| anyhow!(err))?;
- self.database = Database { engine };
- }
- self.info = self.info.clone().from_env();
- self.mint_info = self.mint_info.clone().from_env();
- self.ln = self.ln.clone().from_env();
- #[cfg(feature = "management-rpc")]
- {
- self.mint_management_rpc = Some(
- self.mint_management_rpc
- .clone()
- .unwrap_or_default()
- .from_env(),
- );
- }
- match self.ln.ln_backend {
- LnBackend::Cln => {
- self.cln = Some(self.cln.clone().unwrap_or_default().from_env());
- }
- LnBackend::Strike => {
- self.strike = Some(self.strike.clone().unwrap_or_default().from_env());
- }
- LnBackend::LNbits => {
- self.lnbits = Some(self.lnbits.clone().unwrap_or_default().from_env());
- }
- LnBackend::FakeWallet => {
- self.fake_wallet = Some(self.fake_wallet.clone().unwrap_or_default().from_env());
- }
- LnBackend::Phoenixd => {
- self.phoenixd = Some(self.phoenixd.clone().unwrap_or_default().from_env());
- }
- LnBackend::Lnd => {
- self.lnd = Some(self.lnd.clone().unwrap_or_default().from_env());
- }
- LnBackend::None => bail!("Ln backend must be set"),
- }
- Ok(self.clone())
- }
- }
- impl Info {
- pub fn from_env(mut self) -> Self {
- // Required fields
- if let Ok(url) = env::var(ENV_URL) {
- self.url = url;
- }
- if let Ok(host) = env::var(ENV_LISTEN_HOST) {
- self.listen_host = host;
- }
- if let Ok(port_str) = env::var(ENV_LISTEN_PORT) {
- if let Ok(port) = port_str.parse() {
- self.listen_port = port;
- }
- }
- if let Ok(mnemonic) = env::var(ENV_MNEMONIC) {
- self.mnemonic = mnemonic;
- }
- if let Ok(cache_seconds_str) = env::var(ENV_CACHE_SECONDS) {
- if let Ok(seconds) = cache_seconds_str.parse() {
- self.http_cache.ttl = Some(seconds);
- }
- }
- if let Ok(extend_cache_str) = env::var(ENV_EXTEND_CACHE_SECONDS) {
- if let Ok(seconds) = extend_cache_str.parse() {
- self.http_cache.tti = Some(seconds);
- }
- }
- if let Ok(fee_str) = env::var(ENV_INPUT_FEE_PPK) {
- if let Ok(fee) = fee_str.parse() {
- self.input_fee_ppk = Some(fee);
- }
- }
- if let Ok(swagger_str) = env::var(ENV_ENABLE_SWAGGER) {
- if let Ok(enable) = swagger_str.parse() {
- self.enable_swagger_ui = Some(enable);
- }
- }
- self.http_cache = self.http_cache.from_env();
- self
- }
- }
- impl MintInfo {
- pub fn from_env(mut self) -> Self {
- // Required fields
- if let Ok(name) = env::var(ENV_MINT_NAME) {
- self.name = name;
- }
- if let Ok(description) = env::var(ENV_MINT_DESCRIPTION) {
- self.description = description;
- }
- // Optional fields
- if let Ok(pubkey_str) = env::var(ENV_MINT_PUBKEY) {
- // Assuming PublicKey has a from_str implementation
- if let Ok(pubkey) = pubkey_str.parse() {
- self.pubkey = Some(pubkey);
- }
- }
- if let Ok(desc_long) = env::var(ENV_MINT_DESCRIPTION_LONG) {
- self.description_long = Some(desc_long);
- }
- if let Ok(icon_url) = env::var(ENV_MINT_ICON_URL) {
- self.icon_url = Some(icon_url);
- }
- if let Ok(motd) = env::var(ENV_MINT_MOTD) {
- self.motd = Some(motd);
- }
- if let Ok(nostr_key) = env::var(ENV_MINT_CONTACT_NOSTR) {
- self.contact_nostr_public_key = Some(nostr_key);
- }
- if let Ok(email) = env::var(ENV_MINT_CONTACT_EMAIL) {
- self.contact_email = Some(email);
- }
- self
- }
- }
- impl Ln {
- pub fn from_env(mut self) -> Self {
- // LnBackend
- if let Ok(backend_str) = env::var(ENV_LN_BACKEND) {
- if let Ok(backend) = backend_str.parse() {
- self.ln_backend = backend;
- }
- }
- // Optional invoice description
- if let Ok(description) = env::var(ENV_LN_INVOICE_DESCRIPTION) {
- self.invoice_description = Some(description);
- }
- // Amount fields
- if let Ok(min_mint_str) = env::var(ENV_LN_MIN_MINT) {
- if let Ok(amount) = min_mint_str.parse::<u64>() {
- self.min_mint = amount.into();
- }
- }
- if let Ok(max_mint_str) = env::var(ENV_LN_MAX_MINT) {
- if let Ok(amount) = max_mint_str.parse::<u64>() {
- self.max_mint = amount.into();
- }
- }
- if let Ok(min_melt_str) = env::var(ENV_LN_MIN_MELT) {
- if let Ok(amount) = min_melt_str.parse::<u64>() {
- self.min_melt = amount.into();
- }
- }
- if let Ok(max_melt_str) = env::var(ENV_LN_MAX_MELT) {
- if let Ok(amount) = max_melt_str.parse::<u64>() {
- self.max_melt = amount.into();
- }
- }
- self
- }
- }
- impl Cln {
- pub fn from_env(mut self) -> Self {
- // RPC Path
- if let Ok(path) = env::var(ENV_CLN_RPC_PATH) {
- self.rpc_path = PathBuf::from(path);
- }
- // BOLT12 flag
- if let Ok(bolt12_str) = env::var(ENV_CLN_BOLT12) {
- if let Ok(bolt12) = bolt12_str.parse() {
- self.bolt12 = bolt12;
- }
- }
- // Fee percent
- if let Ok(fee_str) = env::var(ENV_CLN_FEE_PERCENT) {
- if let Ok(fee) = fee_str.parse() {
- self.fee_percent = fee;
- }
- }
- // Reserve fee minimum
- if let Ok(reserve_fee_str) = env::var(ENV_CLN_RESERVE_FEE_MIN) {
- if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
- self.reserve_fee_min = reserve_fee.into();
- }
- }
- self
- }
- }
- impl Strike {
- pub fn from_env(mut self) -> Self {
- // API Key
- if let Ok(api_key) = env::var(ENV_STRIKE_API_KEY) {
- self.api_key = api_key;
- }
- // Supported Units - expects comma-separated list
- if let Ok(units_str) = env::var(ENV_STRIKE_SUPPORTED_UNITS) {
- self.supported_units = Some(
- units_str
- .split(',')
- .filter_map(|s| s.trim().parse().ok())
- .collect(),
- );
- }
- self
- }
- }
- impl Lnd {
- pub fn from_env(mut self) -> Self {
- if let Ok(address) = env::var(ENV_LND_ADDRESS) {
- self.address = address;
- }
- if let Ok(cert_path) = env::var(ENV_LND_CERT_FILE) {
- self.cert_file = PathBuf::from(cert_path);
- }
- if let Ok(macaroon_path) = env::var(ENV_LND_MACAROON_FILE) {
- self.macaroon_file = PathBuf::from(macaroon_path);
- }
- if let Ok(fee_str) = env::var(ENV_LND_FEE_PERCENT) {
- if let Ok(fee) = fee_str.parse() {
- self.fee_percent = fee;
- }
- }
- if let Ok(reserve_fee_str) = env::var(ENV_LND_RESERVE_FEE_MIN) {
- if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
- self.reserve_fee_min = reserve_fee.into();
- }
- }
- self
- }
- }
- impl Phoenixd {
- pub fn from_env(mut self) -> Self {
- if let Ok(password) = env::var(ENV_PHOENIXD_API_PASSWORD) {
- self.api_password = password;
- }
- if let Ok(url) = env::var(ENV_PHOENIXD_API_URL) {
- self.api_url = url;
- }
- if let Ok(bolt12_str) = env::var(ENV_PHOENIXD_BOLT12) {
- if let Ok(bolt12) = bolt12_str.parse() {
- self.bolt12 = bolt12;
- }
- }
- if let Ok(fee_str) = env::var(ENV_PHOENIXD_FEE_PERCENT) {
- if let Ok(fee) = fee_str.parse() {
- self.fee_percent = fee;
- }
- }
- if let Ok(reserve_fee_str) = env::var(ENV_PHOENIXD_RESERVE_FEE_MIN) {
- if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
- self.reserve_fee_min = reserve_fee.into();
- }
- }
- self
- }
- }
- impl LNbits {
- pub fn from_env(mut self) -> Self {
- if let Ok(admin_key) = env::var(ENV_LNBITS_ADMIN_API_KEY) {
- self.admin_api_key = admin_key;
- }
- if let Ok(invoice_key) = env::var(ENV_LNBITS_INVOICE_API_KEY) {
- self.invoice_api_key = invoice_key;
- }
- if let Ok(api) = env::var(ENV_LNBITS_API) {
- self.lnbits_api = api;
- }
- if let Ok(fee_str) = env::var(ENV_LNBITS_FEE_PERCENT) {
- if let Ok(fee) = fee_str.parse() {
- self.fee_percent = fee;
- }
- }
- if let Ok(reserve_fee_str) = env::var(ENV_LNBITS_RESERVE_FEE_MIN) {
- if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
- self.reserve_fee_min = reserve_fee.into();
- }
- }
- self
- }
- }
- impl FakeWallet {
- pub fn from_env(mut self) -> Self {
- // Supported Units - expects comma-separated list
- if let Ok(units_str) = env::var(ENV_FAKE_WALLET_SUPPORTED_UNITS) {
- if let Ok(units) = units_str
- .split(',')
- .map(|s| s.trim().parse())
- .collect::<Result<Vec<CurrencyUnit>, _>>()
- {
- self.supported_units = units;
- }
- }
- if let Ok(fee_str) = env::var(ENV_FAKE_WALLET_FEE_PERCENT) {
- if let Ok(fee) = fee_str.parse() {
- self.fee_percent = fee;
- }
- }
- if let Ok(reserve_fee_str) = env::var(ENV_FAKE_WALLET_RESERVE_FEE_MIN) {
- if let Ok(reserve_fee) = reserve_fee_str.parse::<u64>() {
- self.reserve_fee_min = reserve_fee.into();
- }
- }
- if let Ok(min_delay_str) = env::var(ENV_FAKE_WALLET_MIN_DELAY) {
- if let Ok(min_delay) = min_delay_str.parse() {
- self.min_delay_time = min_delay;
- }
- }
- if let Ok(max_delay_str) = env::var(ENV_FAKE_WALLET_MAX_DELAY) {
- if let Ok(max_delay) = max_delay_str.parse() {
- self.max_delay_time = max_delay;
- }
- }
- self
- }
- }
- #[cfg(feature = "management-rpc")]
- impl MintManagementRpc {
- pub fn from_env(mut self) -> Self {
- if let Ok(enabled) = env::var(ENV_MINT_MANAGEMENT_ENABLED) {
- if let Ok(enabled) = enabled.parse() {
- self.enabled = enabled;
- }
- }
- if let Ok(address) = env::var(ENV_MINT_MANAGEMENT_ADDRESS) {
- self.address = Some(address);
- }
- if let Ok(port) = env::var(ENV_MINT_MANAGEMENT_PORT) {
- if let Ok(port) = port.parse::<u16>() {
- self.port = Some(port);
- }
- }
- if let Ok(tls_path) = env::var(ENV_MINT_MANAGEMENT_TLS_DIR_PATH) {
- self.tls_dir_path = Some(tls_path.into());
- }
- self
- }
- }
|