Skip to content

Commit

Permalink
feat: add derivation path to config
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Feb 13, 2024
1 parent 2e78dc0 commit b40a585
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ MINT_DB_URL=postgres://postgres:[email protected]/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"
Expand Down
2 changes: 2 additions & 0 deletions moksha-mint/src/bin/moksha-mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub async fn main() -> anyhow::Result<()> {

let MintConfig {
privatekey,
derivation_path,
info,
lightning_fee,
server,
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions moksha-mint/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
#[clap(flatten)]
pub info: MintInfoConfig,
#[clap(flatten)]
Expand Down Expand Up @@ -57,6 +59,7 @@ impl FromStr for LightningTypeVariant {
#[derive(Debug, Clone, Default)]
pub struct MintConfig {
pub privatekey: String,
pub derivation_path: Option<String>,
pub info: MintInfoConfig,
pub lightning_fee: LightningFeeConfig,
pub server: ServerConfig,
Expand All @@ -69,6 +72,7 @@ impl From<(Opts, LightningType, Option<BtcOnchainConfig>)> for MintConfig {
fn from((opts, ln, btc): (Opts, LightningType, Option<BtcOnchainConfig>)) -> Self {
Self {
privatekey: opts.privatekey,
derivation_path: opts.derivation_path,
info: opts.info,
lightning_fee: opts.lightning_fee,
server: opts.server,
Expand Down Expand Up @@ -109,6 +113,7 @@ impl MintConfig {
impl MintConfig {
pub const fn new(
private_key: String,
derivation_path: Option<String>,
info: MintInfoConfig,
lightning_fee: LightningFeeConfig,
server: ServerConfig,
Expand All @@ -118,6 +123,7 @@ impl MintConfig {
) -> Self {
Self {
privatekey: private_key,
derivation_path,
info,
lightning_fee,
server,
Expand Down
35 changes: 24 additions & 11 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ pub struct Mint {

impl Mint {
pub fn new(
secret: String,
derivation_path: String,
lightning: Arc<dyn Lightning + Send + Sync>,
lightning_type: LightningType,
db: Arc<dyn Database + Send + Sync>,
Expand All @@ -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,
Expand Down Expand Up @@ -259,6 +264,7 @@ impl Mint {
#[derive(Debug, Default)]
pub struct MintBuilder {
private_key: Option<String>,
derivation_path: Option<String>,
lightning_type: Option<LightningType>,
db_config: Option<DatabaseConfig>,
fee_config: Option<LightningFeeConfig>,
Expand Down Expand Up @@ -287,6 +293,11 @@ impl MintBuilder {
self
}

pub fn with_derivation_path(mut self, derivation_path: Option<String>) -> Self {
self.derivation_path = derivation_path;
self
}

pub fn with_db(mut self, db_config: DatabaseConfig) -> Self {
self.db_config = Some(db_config);
self
Expand Down Expand Up @@ -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()
Expand All @@ -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(),
Expand All @@ -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;
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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())),
)
Expand Down
3 changes: 1 addition & 2 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit b40a585

Please sign in to comment.