Browse Source

feat: nut19 settings

thesimplekid 4 months ago
parent
commit
57180ff3c2

+ 14 - 4
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

+ 13 - 0
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<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(

+ 1 - 0
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,

+ 17 - 1
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<u64>, cached_endpoints: Vec<CachedEndpoint>) -> Self {
+        Self {
+            nut19: nut19::Settings {
+                ttl,
+                cached_endpoints,
+            },
+            ..self
+        }
+    }
 }
 
 /// Check state Settings

+ 55 - 0
crates/cdk/src/nuts/nut19.rs

@@ -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,
+}