Skip to content

Commit

Permalink
feat: quote ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Sep 26, 2024
1 parent 4290153 commit b691258
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
5 changes: 4 additions & 1 deletion crates/cdk-integration-tests/src/init_regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cdk::{
cdk_lightning::MintLightning,
mint::{FeeReserve, Mint},
nuts::{CurrencyUnit, MeltMethodSettings, MintInfo, MintMethodSettings},
types::LnKey,
types::{LnKey, QuoteTTL},
};
use cdk_cln::Cln as CdkCln;
use futures::StreamExt;
Expand Down Expand Up @@ -166,10 +166,13 @@ where
let mut supported_units: HashMap<CurrencyUnit, (u64, u8)> = HashMap::new();
supported_units.insert(CurrencyUnit::Sat, (0, 32));

let quote_ttl = QuoteTTL::new(10000, 10000);

let mint = Mint::new(
&get_mint_url(),
&mnemonic.to_seed_normalized(""),
mint_info,
quote_ttl,
Arc::new(database),
ln_backends,
supported_units,
Expand Down
5 changes: 4 additions & 1 deletion crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use cdk::nuts::{
CurrencyUnit, Id, KeySet, MeltMethodSettings, MintInfo, MintMethodSettings, MintQuoteState,
Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
};
use cdk::types::LnKey;
use cdk::types::{LnKey, QuoteTTL};
use cdk::wallet::client::HttpClient;
use cdk::{Mint, Wallet};
use cdk_fake_wallet::FakeWallet;
Expand Down Expand Up @@ -72,10 +72,13 @@ pub async fn start_mint(

let mnemonic = Mnemonic::generate(12)?;

let quote_ttl = QuoteTTL::new(10000, 10000);

let mint = Mint::new(
&get_mint_url(),
&mnemonic.to_seed_normalized(""),
mint_info,
quote_ttl,
Arc::new(MintMemoryDatabase::default()),
ln_backends.clone(),
supported_units,
Expand Down
4 changes: 4 additions & 0 deletions crates/cdk-integration-tests/tests/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cdk::nuts::{
CurrencyUnit, Id, MintBolt11Request, MintInfo, Nuts, PreMintSecrets, Proofs, SecretKey,
SpendingConditions, SwapRequest,
};
use cdk::types::QuoteTTL;
use cdk::util::unix_time;
use cdk::Mint;
use std::collections::HashMap;
Expand Down Expand Up @@ -36,10 +37,13 @@ async fn new_mint(fee: u64) -> Mint {

let mnemonic = Mnemonic::generate(12).unwrap();

let quote_ttl = QuoteTTL::new(10000, 10000);

Mint::new(
MINT_URL,
&mnemonic.to_seed_normalized(""),
mint_info,
quote_ttl,
Arc::new(MintMemoryDatabase::default()),
HashMap::new(),
supported_units,
Expand Down
5 changes: 4 additions & 1 deletion crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use cdk::nuts::{
nut04, nut05, ContactInfo, CurrencyUnit, MeltMethodSettings, MeltQuoteState, MintInfo,
MintMethodSettings, MintVersion, MppMethodSettings, Nuts, PaymentMethod,
};
use cdk::types::LnKey;
use cdk::types::{LnKey, QuoteTTL};
use cdk_cln::Cln;
use cdk_fake_wallet::FakeWallet;
use cdk_lnbits::LNbits;
Expand Down Expand Up @@ -425,10 +425,13 @@ async fn main() -> anyhow::Result<()> {

let mnemonic = Mnemonic::from_str(&settings.info.mnemonic)?;

let quote_ttl = QuoteTTL::new(10000, 10000);

let mint = Mint::new(
&settings.info.url,
&mnemonic.to_seed_normalized(""),
mint_info,
quote_ttl,
localstore,
ln_backends.clone(),
supported_units,
Expand Down
11 changes: 8 additions & 3 deletions crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::fees::calculate_fee;
use crate::mint_url::MintUrl;
use crate::nuts::nut11::enforce_sig_flag;
use crate::nuts::*;
use crate::types::LnKey;
use crate::types::{LnKey, QuoteTTL};
use crate::util::unix_time;
use crate::Amount;

Expand All @@ -37,6 +37,8 @@ pub struct Mint {
pub mint_url: MintUrl,
/// Mint Info
pub mint_info: MintInfo,
/// Quotes ttl
pub quote_ttl: QuoteTTL,
/// Mint Storage backend
pub localstore: Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync>,
/// Ln backends for mint
Expand All @@ -53,6 +55,7 @@ impl Mint {
mint_url: &str,
seed: &[u8],
mint_info: MintInfo,
quote_ttl: QuoteTTL,
localstore: Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync>,
ln: HashMap<LnKey, Arc<dyn MintLightning<Err = cdk_lightning::Error> + Send + Sync>>,
// Hashmap where the key is the unit and value is (input fee ppk, max_order)
Expand Down Expand Up @@ -165,6 +168,7 @@ impl Mint {
mint_url: MintUrl::from_str(mint_url)?,
keysets: Arc::new(RwLock::new(active_keysets)),
secp_ctx,
quote_ttl,
xpriv,
localstore,
mint_info,
Expand Down Expand Up @@ -277,8 +281,7 @@ impl Mint {
Error::UnitUnsupported
})?;

// TODO: Should get this from ln
let quote_expiry = unix_time() + 36000;
let quote_expiry = unix_time() + self.quote_ttl.mint_ttl;

if description.is_some() && !ln.get_settings().invoice_description {
tracing::error!("Backend does not support invoice description");
Expand Down Expand Up @@ -2002,6 +2005,7 @@ mod tests {
mint_info: MintInfo,
supported_units: HashMap<CurrencyUnit, (u64, u8)>,
melt_requests: Vec<(MeltBolt11Request, LnKey)>,
quote_ttl: QuoteTTL,
}

async fn create_mint(config: MintConfig<'_>) -> Result<Mint, Error> {
Expand All @@ -2025,6 +2029,7 @@ mod tests {
config.mint_url,
config.seed,
config.mint_info,
config.quote_ttl,
localstore,
HashMap::new(),
config.supported_units,
Expand Down
16 changes: 16 additions & 0 deletions crates/cdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ impl LnKey {
}
}

/// Secs wuotes are valid
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct QuoteTTL {
/// Seconds mint quote is valid
pub mint_ttl: u64,
/// Seconds melt quote is valid
pub melt_ttl: u64,
}

impl QuoteTTL {
/// Create new [`QuoteTTL`]
pub fn new(mint_ttl: u64, melt_ttl: u64) -> QuoteTTL {
Self { mint_ttl, melt_ttl }
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down

0 comments on commit b691258

Please sign in to comment.