Skip to content

Commit

Permalink
feat: nut19 settings
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Dec 4, 2024
1 parent 38b4c6d commit 57180ff
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
18 changes: 14 additions & 4 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use cdk::cdk_lightning;
use cdk::cdk_lightning::MintLightning;
use cdk::mint::{MintBuilder, MintMeltLimits};
use cdk::nuts::nut17::SupportedMethods;
use cdk::nuts::nut19::{CachedEndpoint, Method as NUT19Method, Path as NUT19Path};
use cdk::nuts::{ContactInfo, CurrencyUnit, MintVersion, PaymentMethod};
use cdk::types::LnKey;
use cdk_mintd::cli::CLIArgs;
Expand Down Expand Up @@ -285,6 +286,19 @@ async fn main() -> anyhow::Result<()> {
.with_quote_ttl(10000, 10000)
.with_seed(mnemonic.to_seed_normalized("").to_vec());

let cache_ttl = settings
.info
.seconds_to_cache_requests_for
.unwrap_or(DEFAULT_CACHE_TTL_SECS);

let cached_endpoints = vec![
CachedEndpoint::new(NUT19Method::Post, NUT19Path::MintBolt11),
CachedEndpoint::new(NUT19Method::Post, NUT19Path::MeltBolt11),
CachedEndpoint::new(NUT19Method::Post, NUT19Path::Swap),
];

mint_builder = mint_builder.add_cache(Some(cache_ttl), cached_endpoints);

let mint = mint_builder.build().await?;

let mint = Arc::new(mint);
Expand All @@ -306,10 +320,6 @@ async fn main() -> anyhow::Result<()> {
.info
.seconds_quote_is_valid_for
.unwrap_or(DEFAULT_QUOTE_TTL_SECS);
let cache_ttl = settings
.info
.seconds_to_cache_requests_for
.unwrap_or(DEFAULT_CACHE_TTL_SECS);
let cache_tti = settings
.info
.seconds_to_extend_cache_by
Expand Down
13 changes: 13 additions & 0 deletions crates/cdk/src/mint/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use anyhow::anyhow;

use super::nut17::SupportedMethods;
use super::nut19::{self, CachedEndpoint};
use super::Nuts;
use crate::amount::Amount;
use crate::cdk_database::{self, MintDatabase};
Expand Down Expand Up @@ -212,6 +213,18 @@ impl MintBuilder {
self
}

/// Add support for NUT19
pub fn add_cache(mut self, ttl: Option<u64>, cached_endpoints: Vec<CachedEndpoint>) -> Self {
let nut19_settings = nut19::Settings {
ttl,
cached_endpoints,
};

self.mint_info.nuts.nut19 = nut19_settings;

self
}

/// Build mint
pub async fn build(&self) -> anyhow::Result<Mint> {
Ok(Mint::new(
Expand Down
1 change: 1 addition & 0 deletions crates/cdk/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod nut15;
#[cfg(feature = "mint")]
pub mod nut17;
pub mod nut18;
pub mod nut19;

pub use nut00::{
BlindSignature, BlindedMessage, CurrencyUnit, PaymentMethod, PreMint, PreMintSecrets, Proof,
Expand Down
18 changes: 17 additions & 1 deletion crates/cdk/src/nuts/nut06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use super::nut01::PublicKey;
#[cfg(feature = "mint")]
use super::nut17::SupportedMethods;
use super::{nut04, nut05, nut15, MppMethodSettings};
use super::nut19::CachedEndpoint;
use super::{nut04, nut05, nut15, nut19, MppMethodSettings};

/// Mint Version
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -242,6 +243,10 @@ pub struct Nuts {
#[serde(rename = "17")]
#[cfg(feature = "mint")]
pub nut17: super::nut17::SupportedSettings,
/// NUT19 Settings
#[serde(default)]
#[serde(rename = "19")]
pub nut19: nut19::Settings,
}

impl Nuts {
Expand Down Expand Up @@ -340,6 +345,17 @@ impl Nuts {
..self
}
}

/// Nut19 settings
pub fn nut19(self, ttl: Option<u64>, cached_endpoints: Vec<CachedEndpoint>) -> Self {
Self {
nut19: nut19::Settings {
ttl,
cached_endpoints,
},
..self
}
}
}

/// Check state Settings
Expand Down
55 changes: 55 additions & 0 deletions crates/cdk/src/nuts/nut19.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! NUT-19: Cached Responses
//!
//! <https://github.com/cashubtc/nuts/blob/main/19.md>
use serde::{Deserialize, Serialize};

/// Mint settings
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
pub struct Settings {
/// Number of seconds the responses are cached for
pub ttl: Option<u64>,
/// Cached endpoints
pub cached_endpoints: Vec<CachedEndpoint>,
}

/// List of the methods and paths for which caching is enabled
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CachedEndpoint {
/// HTTP Method
pub method: Method,
/// Route path
pub path: Path,
}

impl CachedEndpoint {
/// Create [`CachedEndpoint`]
pub fn new(method: Method, path: Path) -> Self {
Self { method, path }
}
}

/// HTTP method
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Method {
/// Get
Get,
/// POST
Post,
}

/// Route path
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Path {
/// Bolt11 Mint
#[serde(rename = "/v1/mint/bolt11")]
MintBolt11,
/// Bolt11 Melt
#[serde(rename = "/v1/melt/bolt11")]
MeltBolt11,
/// Swap
#[serde(rename = "/v1/swap")]
Swap,
}

0 comments on commit 57180ff

Please sign in to comment.