diff --git a/.env.example b/.env.example index da1af8e9..0fe598bd 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,10 @@ MINT_DB_URL=postgres://postgres:postgres@127.0.0.1/moksha-mint # the private key of the mint MINT_PRIVATE_KEY=superprivatesecretkey +# the derivation path for the mint (optional) +MINT_DERIVATION_PATH="/0/0/0/0" + + # the host and port the mint will listen on int the format https://doc.rust-lang.org/std/net/enum.SocketAddr.html # if the variable is not set the mint will listen on all interfaces on port 3338 MINT_HOST_PORT="[::]:3338" diff --git a/moksha-mint/src/bin/moksha-mint.rs b/moksha-mint/src/bin/moksha-mint.rs index 495a5d8b..ebc3e21e 100644 --- a/moksha-mint/src/bin/moksha-mint.rs +++ b/moksha-mint/src/bin/moksha-mint.rs @@ -19,6 +19,7 @@ pub async fn main() -> anyhow::Result<()> { let MintConfig { privatekey, + derivation_path, info, lightning_fee, server, @@ -31,6 +32,7 @@ pub async fn main() -> anyhow::Result<()> { .with_mint_info(Some(info)) .with_server(Some(server)) .with_private_key(privatekey) + .with_derivation_path(derivation_path) .with_db(database) .with_lightning(lightning_backend.expect("lightning not set")) .with_btc_onchain(btconchain_backend) diff --git a/moksha-mint/src/config.rs b/moksha-mint/src/config.rs index 13375899..56252856 100644 --- a/moksha-mint/src/config.rs +++ b/moksha-mint/src/config.rs @@ -14,6 +14,8 @@ use crate::lightning::{ pub struct Opts { #[clap(long, env = "MINT_PRIVATE_KEY")] pub privatekey: String, + #[clap(long, env = "MINT_DERIVATION_PATH")] + pub derivation_path: Option, #[clap(flatten)] pub info: MintInfoConfig, #[clap(flatten)] @@ -57,6 +59,7 @@ impl FromStr for LightningTypeVariant { #[derive(Debug, Clone, Default)] pub struct MintConfig { pub privatekey: String, + pub derivation_path: Option, pub info: MintInfoConfig, pub lightning_fee: LightningFeeConfig, pub server: ServerConfig, @@ -69,6 +72,7 @@ impl From<(Opts, LightningType, Option)> for MintConfig { fn from((opts, ln, btc): (Opts, LightningType, Option)) -> Self { Self { privatekey: opts.privatekey, + derivation_path: opts.derivation_path, info: opts.info, lightning_fee: opts.lightning_fee, server: opts.server, @@ -109,6 +113,7 @@ impl MintConfig { impl MintConfig { pub const fn new( private_key: String, + derivation_path: Option, info: MintInfoConfig, lightning_fee: LightningFeeConfig, server: ServerConfig, @@ -118,6 +123,7 @@ impl MintConfig { ) -> Self { Self { privatekey: private_key, + derivation_path, info, lightning_fee, server, diff --git a/moksha-mint/src/mint.rs b/moksha-mint/src/mint.rs index 89d4957b..35d2671e 100644 --- a/moksha-mint/src/mint.rs +++ b/moksha-mint/src/mint.rs @@ -42,8 +42,6 @@ pub struct Mint { impl Mint { pub fn new( - secret: String, - derivation_path: String, lightning: Arc, lightning_type: LightningType, db: Arc, @@ -54,8 +52,15 @@ impl Mint { Self { lightning, lightning_type, - keyset_legacy: MintKeyset::legacy_new(&secret, &derivation_path), - keyset: MintKeyset::new(&secret, &derivation_path), + keyset_legacy: MintKeyset::legacy_new( + // FIXME + &config.privatekey.clone(), + &config.derivation_path.clone().unwrap_or_default(), + ), + keyset: MintKeyset::new( + &config.privatekey.clone(), + &config.derivation_path.clone().unwrap_or_default(), + ), db, dhke: Dhke::new(), config, @@ -259,6 +264,7 @@ impl Mint { #[derive(Debug, Default)] pub struct MintBuilder { private_key: Option, + derivation_path: Option, lightning_type: Option, db_config: Option, fee_config: Option, @@ -287,6 +293,11 @@ impl MintBuilder { self } + pub fn with_derivation_path(mut self, derivation_path: Option) -> Self { + self.derivation_path = derivation_path; + self + } + pub fn with_db(mut self, db_config: DatabaseConfig) -> Self { self.db_config = Some(db_config); self @@ -365,8 +376,6 @@ impl MintBuilder { }; Ok(Mint::new( - self.private_key.clone().expect("MINT_PRIVATE_KEY not set"), - "".to_string(), ln, self.lightning_type .clone() @@ -375,6 +384,7 @@ impl MintBuilder { // FIXME simplify config creation MintConfig::new( self.private_key.expect("private-key not set"), + self.derivation_path, self.mint_info_settings.unwrap_or_default(), self.fee_config.expect("fee-config not set"), self.server_config.unwrap_or_default(), @@ -391,6 +401,7 @@ impl MintBuilder { #[cfg(test)] mod tests { use crate::btconchain::MockBtcOnchain; + use crate::config::MintConfig; use crate::lightning::error::LightningError; use crate::lightning::{LightningType, MockLightning}; use crate::mint::Mint; @@ -542,8 +553,8 @@ mod tests { }); let mint = Mint::new( - "TEST_PRIVATE_KEY".to_string(), - "0/0/0/0".to_string(), + // "TEST_PRIVATE_KEY".to_string(), + // "0/0/0/0".to_string(), Arc::new(lightning), LightningType::Lnbits(Default::default()), Arc::new(create_mock_db_get_used_proofs()), @@ -602,12 +613,14 @@ mod tests { }; Mint::new( - "TEST_PRIVATE_KEY".to_string(), - "0/0/0/0".to_string(), lightning, LightningType::Lnbits(Default::default()), db, - Default::default(), + MintConfig { + privatekey: "TEST_PRIVATE_KEY".to_string(), + derivation_path: Some("0/0/0/0".to_string()), + ..Default::default() + }, Default::default(), Some(Arc::new(MockBtcOnchain::default())), ) diff --git a/moksha-mint/src/server.rs b/moksha-mint/src/server.rs index 3a9ed6c6..deed5dd7 100644 --- a/moksha-mint/src/server.rs +++ b/moksha-mint/src/server.rs @@ -406,13 +406,12 @@ mod tests { let lightning = Arc::new(MockLightning::new()); Mint::new( - "mytestsecret".to_string(), - "".to_string(), lightning, LightningType::Lnbits(Default::default()), db, MintConfig { info, + privatekey: "mytestsecret".to_string(), ..Default::default() }, Default::default(),