From 57180ff3c25cae7cd481a5106327100a1d7c9919 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Wed, 4 Dec 2024 00:07:16 +0000 Subject: [PATCH] feat: nut19 settings --- crates/cdk-mintd/src/main.rs | 18 ++++++++--- crates/cdk/src/mint/builder.rs | 13 ++++++++ crates/cdk/src/nuts/mod.rs | 1 + crates/cdk/src/nuts/nut06.rs | 18 ++++++++++- crates/cdk/src/nuts/nut19.rs | 55 ++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 crates/cdk/src/nuts/nut19.rs diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index 3d789c8d..a5b41954 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -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; @@ -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); @@ -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 diff --git a/crates/cdk/src/mint/builder.rs b/crates/cdk/src/mint/builder.rs index 989ddd99..d393d973 100644 --- a/crates/cdk/src/mint/builder.rs +++ b/crates/cdk/src/mint/builder.rs @@ -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}; @@ -212,6 +213,18 @@ impl MintBuilder { self } + /// Add support for NUT19 + pub fn add_cache(mut self, ttl: Option, cached_endpoints: Vec) -> 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 { Ok(Mint::new( diff --git a/crates/cdk/src/nuts/mod.rs b/crates/cdk/src/nuts/mod.rs index eb1f8170..7f913f49 100644 --- a/crates/cdk/src/nuts/mod.rs +++ b/crates/cdk/src/nuts/mod.rs @@ -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, diff --git a/crates/cdk/src/nuts/nut06.rs b/crates/cdk/src/nuts/nut06.rs index 56f91d24..f1667b48 100644 --- a/crates/cdk/src/nuts/nut06.rs +++ b/crates/cdk/src/nuts/nut06.rs @@ -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)] @@ -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 { @@ -340,6 +345,17 @@ impl Nuts { ..self } } + + /// Nut19 settings + pub fn nut19(self, ttl: Option, cached_endpoints: Vec) -> Self { + Self { + nut19: nut19::Settings { + ttl, + cached_endpoints, + }, + ..self + } + } } /// Check state Settings diff --git a/crates/cdk/src/nuts/nut19.rs b/crates/cdk/src/nuts/nut19.rs new file mode 100644 index 00000000..6d97b6be --- /dev/null +++ b/crates/cdk/src/nuts/nut19.rs @@ -0,0 +1,55 @@ +//! NUT-19: Cached Responses +//! +//! + +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, + /// Cached endpoints + pub cached_endpoints: Vec, +} + +/// 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, +}