Skip to content

Commit

Permalink
fix: json serialization of /v1/info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 11, 2023
1 parent 2707b01 commit beba5d5
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 90 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 110 additions & 60 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub struct KeyResponse {
pub keys: HashMap<u64, PublicKey>,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, ToSchema)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, ToSchema, Hash)]
#[serde(rename_all = "lowercase")]
pub enum CurrencyUnit {
Sat,
Expand All @@ -138,7 +138,7 @@ impl Display for CurrencyUnit {
}
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, ToSchema)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, ToSchema, Hash)]
#[serde(rename_all = "lowercase")]
pub enum PaymentMethod {
Bolt11,
Expand Down Expand Up @@ -272,68 +272,138 @@ pub struct MintInfoResponse {
pub description_long: Option<String>,
pub contact: Option<Vec<Vec<String>>>,
pub motd: Option<String>,
pub nuts: Vec<MintInfoNut>,
pub nuts: Nuts,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ToSchema)]
pub enum MintInfoNut {
/// Cryptography and Models
#[serde(rename = "0")]
Nut0 { disabled: bool },

/// Mint public keys
#[serde(rename = "1")]
Nut1 { disabled: bool },

/// Keysets and keyset IDs
#[serde(rename = "2")]
Nut2 { disabled: bool },

/// Swapping tokens
#[serde(rename = "3")]
Nut3 { disabled: bool },

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, ToSchema)]
pub struct Nuts {
// /// Cryptography and Models
// #[serde(rename = "0")]
// pub nut0: bool,

// /// Mint public keys
// #[serde(rename = "1")]
// pub nut1: bool,

// /// Keysets and keyset IDs
// #[serde(rename = "2")]
// pub nut2: bool,

// /// Swapping tokens
// #[serde(rename = "3")]
// pub nut3: bool,
/// Minting tokens
#[serde(rename = "4")]
Nut4 {
methods: Vec<(PaymentMethod, CurrencyUnit)>,
disabled: bool,
},
pub nut4: Nut4,

/// Melting tokens
#[serde(rename = "5")]
Nut5 {
methods: Vec<(PaymentMethod, CurrencyUnit)>,
disabled: bool,
},
pub nut5: Nut5,

/// Mint info
#[serde(rename = "6")]
Nut6 { disabled: bool },
pub nut6: Nut6,

/// Token state check
#[serde(rename = "7")]
Nut7 { supported: bool },
pub nut7: Nut7,

/// Overpaid Lightning fees
#[serde(rename = "8")]
Nut8 { supported: bool },
pub nut8: Nut8,

/// Deterministic backup and restore
#[serde(rename = "9")]
Nut9 { supported: bool },
pub nut9: Nut9,

/// Spending conditions
#[serde(rename = "10")]
Nut10 { supported: bool },
pub nut10: Nut10,

/// Pay-To-Pubkey (P2PK)
#[serde(rename = "11")]
Nut11 { supported: bool },
pub nut11: Nut11,

/// DLEQ proofs
#[serde(rename = "12")]
Nut12 { supported: bool },
/// DLEQ proofs
pub nut12: Nut12,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, ToSchema)]
pub struct Nut4 {
pub methods: Vec<(PaymentMethod, CurrencyUnit)>,
pub disabled: bool,
}

impl Default for Nut4 {
fn default() -> Self {
Self {
methods: vec![(PaymentMethod::Bolt11, CurrencyUnit::Sat)],
disabled: false,
}
}
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, ToSchema)]
pub struct Nut5 {
pub methods: Vec<(PaymentMethod, CurrencyUnit)>,
pub disabled: bool,
}

impl Default for Nut5 {
fn default() -> Self {
Self {
methods: vec![(PaymentMethod::Bolt11, CurrencyUnit::Sat)],
disabled: false,
}
}
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, ToSchema)]
pub struct Nut6 {
pub supported: bool,
}

impl Default for Nut6 {
fn default() -> Self {
Self { supported: true }
}
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, ToSchema)]
pub struct Nut7 {
pub supported: bool,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, ToSchema)]
pub struct Nut8 {
pub supported: bool,
}

impl Default for Nut8 {
fn default() -> Self {
Self { supported: true }
}
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, ToSchema)]
pub struct Nut9 {
pub supported: bool,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, ToSchema)]
pub struct Nut10 {
pub supported: bool,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, ToSchema)]
pub struct Nut11 {
pub supported: bool,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, ToSchema)]
pub struct Nut12 {
pub supported: bool,
}

#[cfg(test)]
Expand All @@ -342,8 +412,8 @@ mod tests {
use crate::{
dhke::public_key_from_hex,
primitives::{
CurrencyUnit, KeyResponse, MintInfoNut, MintInfoResponse, MintLegacyInfoResponse,
Parameter, PaymentMethod, PostSwapResponse,
KeyResponse, MintInfoResponse, MintLegacyInfoResponse, Nuts, Parameter,
PostSwapResponse,
},
};

Expand Down Expand Up @@ -414,27 +484,7 @@ mod tests {
vec!["twitter".to_string(), "@me".to_string()],
vec!["nostr".to_string(), "npub...".to_string()],
]),
nuts: vec![
MintInfoNut::Nut0 { disabled: false },
MintInfoNut::Nut1 { disabled: false },
MintInfoNut::Nut2 { disabled: false },
MintInfoNut::Nut3 { disabled: false },
MintInfoNut::Nut4 {
methods: vec![(PaymentMethod::Bolt11, CurrencyUnit::Sat)],
disabled: false,
},
MintInfoNut::Nut5 {
methods: vec![(PaymentMethod::Bolt11, CurrencyUnit::Sat)],
disabled: false,
},
MintInfoNut::Nut6 { disabled: false },
MintInfoNut::Nut7 { supported: false },
MintInfoNut::Nut8 { supported: false },
MintInfoNut::Nut9 { supported: false },
MintInfoNut::Nut10 { supported: false },
MintInfoNut::Nut11 { supported: false },
MintInfoNut::Nut12 { supported: false },
],
nuts: Nuts::default(),
motd: Some("Message to display to users.".to_string()),
};
let out = serde_json::to_string_pretty(&mint_info)?;
Expand Down
1 change: 0 additions & 1 deletion moksha-mint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ hyper = "0.14.27"
serde = { version = "1.0.193", features = ["derive"] }
serde_derive = "1.0.171"
serde_json = "1.0.108"
serde_with = "3.4.0"
tokio = { version = "1.34.0", features = ["full"] }
tower-http = { version = "0.5.0", features = ["trace", "cors", "fs", "set-header"] }
tower-service = { version = "0.3.2" }
Expand Down
46 changes: 18 additions & 28 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ use crate::model::{GetMintQuery, PostMintQuery};
use moksha_core::blind::BlindedMessage;
use moksha_core::blind::BlindedSignature;
use moksha_core::primitives::{
CheckFeesRequest, CheckFeesResponse, CurrencyUnit, KeyResponse, KeysResponse, MintInfoNut,
MintInfoResponse, MintLegacyInfoResponse, PaymentMethod, PaymentRequest, PostMeltBolt11Request,
PostMeltBolt11Response, PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response,
PostMeltRequest, PostMeltResponse, PostMintBolt11Request, PostMintBolt11Response,
PostMintQuoteBolt11Request, PostMintQuoteBolt11Response, PostMintRequest, PostMintResponse,
PostSplitRequest, PostSplitResponse, PostSwapRequest, PostSwapResponse, Quote,
CheckFeesRequest, CheckFeesResponse, CurrencyUnit, KeyResponse, KeysResponse, MintInfoResponse,
MintLegacyInfoResponse, Nut10, Nut11, Nut12, Nut4, Nut5, Nut6, Nut7, Nut8, Nut9, Nuts,
PaymentMethod, PaymentRequest, PostMeltBolt11Request, PostMeltBolt11Response,
PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response, PostMeltRequest, PostMeltResponse,
PostMintBolt11Request, PostMintBolt11Response, PostMintQuoteBolt11Request,
PostMintQuoteBolt11Response, PostMintRequest, PostMintResponse, PostSplitRequest,
PostSplitResponse, PostSwapRequest, PostSwapResponse, Quote,
};
use secp256k1::PublicKey;

Expand Down Expand Up @@ -94,7 +95,16 @@ pub async fn run_server(
),
components(schemas(
MintInfoResponse,
MintInfoNut,
Nuts,
Nut4,
Nut5,
Nut6,
Nut7,
Nut8,
Nut9,
Nut10,
Nut11,
Nut12,
CurrencyUnit,
PaymentMethod,
KeysResponse,
Expand Down Expand Up @@ -641,27 +651,7 @@ async fn get_info(State(mint): State<Mint>) -> Result<Json<MintInfoResponse>, Mo
description: mint.mint_info.description,
description_long: mint.mint_info.description_long,
contact: mint.mint_info.contact,
nuts: vec![
MintInfoNut::Nut0 { disabled: false },
MintInfoNut::Nut1 { disabled: false },
MintInfoNut::Nut2 { disabled: false },
MintInfoNut::Nut3 { disabled: false },
MintInfoNut::Nut4 {
methods: vec![(PaymentMethod::Bolt11, CurrencyUnit::Sat)],
disabled: false,
},
MintInfoNut::Nut5 {
methods: vec![(PaymentMethod::Bolt11, CurrencyUnit::Sat)],
disabled: false,
},
MintInfoNut::Nut6 { disabled: false },
MintInfoNut::Nut7 { supported: false },
MintInfoNut::Nut8 { supported: false },
MintInfoNut::Nut9 { supported: false },
MintInfoNut::Nut10 { supported: true },
MintInfoNut::Nut11 { supported: true },
MintInfoNut::Nut12 { supported: true },
],
nuts: Nuts::default(),
motd: mint.mint_info.motd,
};
Ok(Json(mint_info))
Expand Down

0 comments on commit beba5d5

Please sign in to comment.