From 558aedaff16ed34a03ec22c38c3075f8b2c2eec5 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Mon, 18 Nov 2024 10:20:06 +0000 Subject: [PATCH] feat: rename to 20 and 21 --- crates/cdk-axum/src/bolt12_router.rs | 3 +- crates/cdk/src/cdk_lightning/bolt12.rs | 3 +- crates/cdk/src/error.rs | 4 +- crates/cdk/src/mint/mint_18.rs | 2 +- crates/cdk/src/nuts/mod.rs | 5 +- crates/cdk/src/nuts/nut19.rs | 66 ------------------ crates/cdk/src/nuts/nut20.rs | 97 ++++++++++++-------------- crates/cdk/src/nuts/nut21.rs | 77 ++++++++++++++++++++ crates/cdk/src/wallet/client.rs | 6 +- crates/cdk/src/wallet/mint_bolt12.rs | 4 +- 10 files changed, 133 insertions(+), 134 deletions(-) delete mode 100644 crates/cdk/src/nuts/nut19.rs create mode 100644 crates/cdk/src/nuts/nut21.rs diff --git a/crates/cdk-axum/src/bolt12_router.rs b/crates/cdk-axum/src/bolt12_router.rs index 963c9260..25a83e64 100644 --- a/crates/cdk-axum/src/bolt12_router.rs +++ b/crates/cdk-axum/src/bolt12_router.rs @@ -1,10 +1,9 @@ use anyhow::Result; use axum::extract::{Json, Path, State}; use axum::response::Response; -use cdk::nuts::nut19::{MintQuoteBolt12Request, MintQuoteBolt12Response}; use cdk::nuts::{ MeltBolt12Request, MeltQuoteBolt11Response, MeltQuoteBolt12Request, MintBolt11Request, - MintBolt11Response, + MintBolt11Response, MintQuoteBolt12Request, MintQuoteBolt12Response, }; use crate::{into_response, MintState}; diff --git a/crates/cdk/src/cdk_lightning/bolt12.rs b/crates/cdk/src/cdk_lightning/bolt12.rs index 0a760da5..4eb7cdaa 100644 --- a/crates/cdk/src/cdk_lightning/bolt12.rs +++ b/crates/cdk/src/cdk_lightning/bolt12.rs @@ -9,8 +9,7 @@ use serde::{Deserialize, Serialize}; use super::{ Bolt12PaymentQuoteResponse, CreateOfferResponse, Error, PayInvoiceResponse, WaitInvoiceResponse, }; -use crate::nuts::nut20::MeltQuoteBolt12Request; -use crate::nuts::CurrencyUnit; +use crate::nuts::{CurrencyUnit, MeltQuoteBolt12Request}; use crate::{mint, Amount}; /// MintLighting Bolt12 Trait diff --git a/crates/cdk/src/error.rs b/crates/cdk/src/error.rs index 844eeacd..a76ce9dc 100644 --- a/crates/cdk/src/error.rs +++ b/crates/cdk/src/error.rs @@ -247,10 +247,10 @@ pub enum Error { NUT18(#[from] crate::nuts::nut18::Error), /// NUT19 Error #[error(transparent)] - NUT19(#[from] crate::nuts::nut19::Error), + NUT19(#[from] crate::nuts::nut20::Error), /// NUT18 Error #[error(transparent)] - NUT20(#[from] crate::nuts::nut20::Error), + NUT20(#[from] crate::nuts::nut21::Error), /// Database Error #[cfg(any(feature = "wallet", feature = "mint"))] #[error(transparent)] diff --git a/crates/cdk/src/mint/mint_18.rs b/crates/cdk/src/mint/mint_18.rs index 447a6da3..56f03bda 100644 --- a/crates/cdk/src/mint/mint_18.rs +++ b/crates/cdk/src/mint/mint_18.rs @@ -1,6 +1,6 @@ use tracing::instrument; -use super::nut19::{MintQuoteBolt12Request, MintQuoteBolt12Response}; +use super::nut20::{MintQuoteBolt12Request, MintQuoteBolt12Response}; use super::{Mint, MintQuote, PaymentMethod}; use crate::util::unix_time; use crate::{Amount, Error}; diff --git a/crates/cdk/src/nuts/mod.rs b/crates/cdk/src/nuts/mod.rs index b99b207e..5c5e5ce8 100644 --- a/crates/cdk/src/nuts/mod.rs +++ b/crates/cdk/src/nuts/mod.rs @@ -21,8 +21,8 @@ pub mod nut15; #[cfg(feature = "mint")] pub mod nut17; pub mod nut18; -pub mod nut19; pub mod nut20; +pub mod nut21; pub use nut00::{ BlindSignature, BlindedMessage, CurrencyUnit, PaymentMethod, PreMint, PreMintSecrets, Proof, @@ -54,4 +54,5 @@ pub use nut15::{Mpp, MppMethodSettings, Settings as NUT15Settings}; #[cfg(feature = "mint")] pub use nut17::{NotificationPayload, PubSubManager}; pub use nut18::{PaymentRequest, PaymentRequestPayload, Transport}; -pub use nut20::{MeltBolt12Request, MeltQuoteBolt12Request}; +pub use nut20::{MintQuoteBolt12Request, MintQuoteBolt12Response}; +pub use nut21::{MeltBolt12Request, MeltQuoteBolt12Request}; diff --git a/crates/cdk/src/nuts/nut19.rs b/crates/cdk/src/nuts/nut19.rs deleted file mode 100644 index a1552900..00000000 --- a/crates/cdk/src/nuts/nut19.rs +++ /dev/null @@ -1,66 +0,0 @@ -//! NUT-17: Mint Tokens via Bolt11 -//! -//! - -use serde::{Deserialize, Serialize}; -use thiserror::Error; - -use super::nut00::CurrencyUnit; -use crate::Amount; - -/// NUT04 Error -#[derive(Debug, Error)] -pub enum Error { - /// Unknown Quote State - #[error("Unknown Quote State")] - UnknownState, - /// Amount overflow - #[error("Amount overflow")] - AmountOverflow, -} - -/// Mint quote request [NUT-19] -#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] -pub struct MintQuoteBolt12Request { - /// Amount - pub amount: Option, - /// Unit wallet would like to pay with - pub unit: CurrencyUnit, - /// Memo to create the invoice with - pub description: Option, - /// Single use - pub single_use: bool, - /// Expiry - pub expiry: Option, -} - -/// Mint quote response [NUT-19] -#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] -pub struct MintQuoteBolt12Response { - /// Quote Id - pub quote: String, - /// Payment request to fulfil - pub request: String, - /// Single use - pub single_use: bool, - /// Unix timestamp until the quote is valid - pub expiry: Option, - /// Amount that has been paid - pub amount_paid: Amount, - /// Amount that has been issued - pub amount_issued: Amount, -} - -#[cfg(feature = "mint")] -impl From for MintQuoteBolt12Response { - fn from(mint_quote: crate::mint::MintQuote) -> MintQuoteBolt12Response { - MintQuoteBolt12Response { - quote: mint_quote.id, - request: mint_quote.request, - expiry: Some(mint_quote.expiry), - amount_paid: mint_quote.amount_paid, - amount_issued: mint_quote.amount_issued, - single_use: mint_quote.single_use, - } - } -} diff --git a/crates/cdk/src/nuts/nut20.rs b/crates/cdk/src/nuts/nut20.rs index 30af3ac5..a1552900 100644 --- a/crates/cdk/src/nuts/nut20.rs +++ b/crates/cdk/src/nuts/nut20.rs @@ -1,77 +1,66 @@ -//! Bolt12 +//! NUT-17: Mint Tokens via Bolt11 +//! +//! + use serde::{Deserialize, Serialize}; use thiserror::Error; -use super::nut05::MeltRequestTrait; -use super::{BlindedMessage, CurrencyUnit, PaymentMethod, Proofs}; +use super::nut00::CurrencyUnit; use crate::Amount; -/// NUT18 Error +/// NUT04 Error #[derive(Debug, Error)] pub enum Error { /// Unknown Quote State - #[error("Unknown quote state")] + #[error("Unknown Quote State")] UnknownState, /// Amount overflow - #[error("Amount Overflow")] + #[error("Amount overflow")] AmountOverflow, } -/// Melt quote request [NUT-18] +/// Mint quote request [NUT-19] #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] -pub struct MeltQuoteBolt12Request { - /// Bolt12 invoice to be paid - pub request: String, +pub struct MintQuoteBolt12Request { + /// Amount + pub amount: Option, /// Unit wallet would like to pay with pub unit: CurrencyUnit, - /// Payment Options - pub amount: Option, + /// Memo to create the invoice with + pub description: Option, + /// Single use + pub single_use: bool, + /// Expiry + pub expiry: Option, } -/// Melt Bolt12 Request [NUT-18] -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct MeltBolt12Request { - /// Quote ID +/// Mint quote response [NUT-19] +#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] +pub struct MintQuoteBolt12Response { + /// Quote Id pub quote: String, - /// Proofs - pub inputs: Proofs, - /// Blinded Message that can be used to return change [NUT-08] - /// Amount field of BlindedMessages `SHOULD` be set to zero - pub outputs: Option>, + /// Payment request to fulfil + pub request: String, + /// Single use + pub single_use: bool, + /// Unix timestamp until the quote is valid + pub expiry: Option, + /// Amount that has been paid + pub amount_paid: Amount, + /// Amount that has been issued + pub amount_issued: Amount, } -impl MeltRequestTrait for MeltBolt12Request { - type Err = Error; - - fn get_quote_id(&self) -> &str { - &self.quote - } - - fn get_inputs(&self) -> &Proofs { - &self.inputs - } - - fn get_outputs(&self) -> &Option> { - &self.outputs - } - - fn inputs_amount(&self) -> Result { - Amount::try_sum(self.inputs.iter().map(|proof| proof.amount)) - .map_err(|_| Error::AmountOverflow) - } - - fn outputs_amount(&self) -> Result { - Amount::try_sum( - self.outputs - .as_ref() - .unwrap_or(&vec![]) - .iter() - .map(|proof| proof.amount), - ) - .map_err(|_| Error::AmountOverflow) - } - - fn get_payment_method(&self) -> PaymentMethod { - PaymentMethod::Bolt12 +#[cfg(feature = "mint")] +impl From for MintQuoteBolt12Response { + fn from(mint_quote: crate::mint::MintQuote) -> MintQuoteBolt12Response { + MintQuoteBolt12Response { + quote: mint_quote.id, + request: mint_quote.request, + expiry: Some(mint_quote.expiry), + amount_paid: mint_quote.amount_paid, + amount_issued: mint_quote.amount_issued, + single_use: mint_quote.single_use, + } } } diff --git a/crates/cdk/src/nuts/nut21.rs b/crates/cdk/src/nuts/nut21.rs new file mode 100644 index 00000000..30af3ac5 --- /dev/null +++ b/crates/cdk/src/nuts/nut21.rs @@ -0,0 +1,77 @@ +//! Bolt12 +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use super::nut05::MeltRequestTrait; +use super::{BlindedMessage, CurrencyUnit, PaymentMethod, Proofs}; +use crate::Amount; + +/// NUT18 Error +#[derive(Debug, Error)] +pub enum Error { + /// Unknown Quote State + #[error("Unknown quote state")] + UnknownState, + /// Amount overflow + #[error("Amount Overflow")] + AmountOverflow, +} + +/// Melt quote request [NUT-18] +#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] +pub struct MeltQuoteBolt12Request { + /// Bolt12 invoice to be paid + pub request: String, + /// Unit wallet would like to pay with + pub unit: CurrencyUnit, + /// Payment Options + pub amount: Option, +} + +/// Melt Bolt12 Request [NUT-18] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct MeltBolt12Request { + /// Quote ID + pub quote: String, + /// Proofs + pub inputs: Proofs, + /// Blinded Message that can be used to return change [NUT-08] + /// Amount field of BlindedMessages `SHOULD` be set to zero + pub outputs: Option>, +} + +impl MeltRequestTrait for MeltBolt12Request { + type Err = Error; + + fn get_quote_id(&self) -> &str { + &self.quote + } + + fn get_inputs(&self) -> &Proofs { + &self.inputs + } + + fn get_outputs(&self) -> &Option> { + &self.outputs + } + + fn inputs_amount(&self) -> Result { + Amount::try_sum(self.inputs.iter().map(|proof| proof.amount)) + .map_err(|_| Error::AmountOverflow) + } + + fn outputs_amount(&self) -> Result { + Amount::try_sum( + self.outputs + .as_ref() + .unwrap_or(&vec![]) + .iter() + .map(|proof| proof.amount), + ) + .map_err(|_| Error::AmountOverflow) + } + + fn get_payment_method(&self) -> PaymentMethod { + PaymentMethod::Bolt12 + } +} diff --git a/crates/cdk/src/wallet/client.rs b/crates/cdk/src/wallet/client.rs index d8d4e4b6..5429fbc6 100644 --- a/crates/cdk/src/wallet/client.rs +++ b/crates/cdk/src/wallet/client.rs @@ -11,13 +11,13 @@ use url::Url; use super::Error; use crate::error::ErrorResponse; use crate::mint_url::MintUrl; -use crate::nuts::nut19::{MintQuoteBolt12Request, MintQuoteBolt12Response}; use crate::nuts::{ BlindedMessage, CheckStateRequest, CheckStateResponse, CurrencyUnit, Id, KeySet, KeysResponse, KeysetResponse, MeltBolt11Request, MeltBolt12Request, MeltQuoteBolt11Request, MeltQuoteBolt11Response, MeltQuoteBolt12Request, MintBolt11Request, MintBolt11Response, - MintInfo, MintQuoteBolt11Request, MintQuoteBolt11Response, PreMintSecrets, Proof, - RestoreRequest, RestoreResponse, SwapRequest, SwapResponse, + MintInfo, MintQuoteBolt11Request, MintQuoteBolt11Response, MintQuoteBolt12Request, + MintQuoteBolt12Response, PreMintSecrets, Proof, RestoreRequest, RestoreResponse, SwapRequest, + SwapResponse, }; use crate::Amount; diff --git a/crates/cdk/src/wallet/mint_bolt12.rs b/crates/cdk/src/wallet/mint_bolt12.rs index 3111b74f..1341ac3c 100644 --- a/crates/cdk/src/wallet/mint_bolt12.rs +++ b/crates/cdk/src/wallet/mint_bolt12.rs @@ -4,9 +4,9 @@ use super::MintQuote; use crate::amount::SplitTarget; use crate::dhke::construct_proofs; use crate::nuts::nut00::ProofsMethods; -use crate::nuts::nut19::{MintQuoteBolt12Request, MintQuoteBolt12Response}; use crate::nuts::{ - nut12, MintBolt11Request, PaymentMethod, PreMintSecrets, SpendingConditions, State, + nut12, MintBolt11Request, MintQuoteBolt12Request, MintQuoteBolt12Response, PaymentMethod, + PreMintSecrets, SpendingConditions, State, }; use crate::types::ProofInfo; use crate::util::unix_time;