From b511dd79be502fb6bfe4dd072c969175c5ebe99e Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Tue, 16 Jul 2024 12:59:24 +0200 Subject: [PATCH] feat: change contact-info in /info endpoint fixes #303 --- .../src/fixtures/nutshell_mint_info.json | 15 +++++-- moksha-core/src/primitives.rs | 39 ++++++++++++++++--- moksha-mint/src/config.rs | 14 ++++++- moksha-mint/src/routes/default.rs | 19 +-------- 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/moksha-core/src/fixtures/nutshell_mint_info.json b/moksha-core/src/fixtures/nutshell_mint_info.json index 4711bf05..16e54ee2 100644 --- a/moksha-core/src/fixtures/nutshell_mint_info.json +++ b/moksha-core/src/fixtures/nutshell_mint_info.json @@ -5,9 +5,18 @@ "description": "The short mint description", "description_long": "A long mint description that can be a long piece of text.", "contact": [ - ["email", "contact@me.com"], - ["twitter", "@me"], - ["nostr", "npub..."] + { + "method": "email", + "info": "contact@me.com" + }, + { + "method": "twitter", + "info": "@me" + }, + { + "method": "nostr", + "info": "npub..." + } ], "motd": "Message to users", "nuts": { diff --git a/moksha-core/src/primitives.rs b/moksha-core/src/primitives.rs index f9c6c1d8..1e02473d 100644 --- a/moksha-core/src/primitives.rs +++ b/moksha-core/src/primitives.rs @@ -220,11 +220,40 @@ pub struct MintInfoResponse { pub version: Option, pub description: Option, pub description_long: Option, - pub contact: Option>>, + pub contact: Option>, pub motd: Option, pub nuts: Nuts, } +#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ToSchema)] +pub struct ContactInfoResponse { + pub method: String, + pub info: String, +} + +impl ContactInfoResponse { + pub fn email(info: String) -> Self { + Self { + method: "email".to_string(), + info, + } + } + + pub fn twitter(info: String) -> Self { + Self { + method: "twitter".to_string(), + info, + } + } + + pub fn nostr(info: String) -> Self { + Self { + method: "twitter".to_string(), + info, + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct BtcOnchainMintQuote { pub quote_id: Uuid, @@ -536,7 +565,7 @@ mod tests { use crate::{ dhke::public_key_from_hex, fixture::read_fixture, - primitives::{KeyResponse, MintInfoResponse, Nuts, PostSwapResponse}, + primitives::{ContactInfoResponse, KeyResponse, MintInfoResponse, Nuts, PostSwapResponse}, }; #[test] @@ -570,9 +599,9 @@ mod tests { description: Some("The short mint description".to_string()), description_long: Some("A description that can be a long piece of text.".to_string()), contact: Some(vec![ - vec!["email".to_string(), "contact@me.com".to_string()], - vec!["twitter".to_string(), "@me".to_string()], - vec!["nostr".to_string(), "npub...".to_string()], + ContactInfoResponse::email("contact@me.com".to_string()), + ContactInfoResponse::twitter("@me".to_string()), + ContactInfoResponse::nostr("npub...".to_string()), ]), nuts: Nuts::default(), motd: Some("Message to display to users.".to_string()), diff --git a/moksha-mint/src/config.rs b/moksha-mint/src/config.rs index 060f42c3..48f447a3 100644 --- a/moksha-mint/src/config.rs +++ b/moksha-mint/src/config.rs @@ -2,7 +2,7 @@ use std::{env, net::SocketAddr, path::PathBuf, str::FromStr}; use clap::Parser; use moksha_core::primitives::{ - CurrencyUnit, Nut17, Nut18, PaymentMethod, PaymentMethodConfigBtcOnchain, + ContactInfoResponse, CurrencyUnit, Nut17, Nut18, PaymentMethod, PaymentMethodConfigBtcOnchain, }; use serde::{Deserialize, Serialize}; @@ -302,6 +302,18 @@ pub struct MintInfoConfig { // FIXME add missing fields for v1/info endpoint nut4/nut5 payment_methods, nut4 disabled flag } +impl From for Vec { + fn from(info: MintInfoConfig) -> Vec { + [ + info.contact_email.map(ContactInfoResponse::email), + info.contact_twitter.map(ContactInfoResponse::twitter), + info.contact_nostr.map(ContactInfoResponse::nostr), + ] + .iter() + .filter_map(|contact| contact.to_owned()) + .collect() + } +} #[derive(Deserialize, Serialize, Debug, Clone, Default)] pub struct BuildParams { pub commit_hash: Option, diff --git a/moksha-mint/src/routes/default.rs b/moksha-mint/src/routes/default.rs index a8cc3036..8107a505 100644 --- a/moksha-mint/src/routes/default.rs +++ b/moksha-mint/src/routes/default.rs @@ -341,25 +341,8 @@ pub async fn get_melt_quote_bolt11( )] #[instrument(name = "get_info", skip(mint), err)] pub async fn get_info(State(mint): State) -> Result, MokshaMintError> { - // TODO implement From-trait - let mint_info = mint.config.info.clone(); - let contact = Some( - vec![ - mint_info - .contact_email - .map(|email| vec!["email".to_owned(), email]), - mint_info - .contact_twitter - .map(|twitter| vec!["twitter".to_owned(), twitter]), - mint_info - .contact_nostr - .map(|nostr| vec!["nostr".to_owned(), nostr]), - ] - .into_iter() - .flatten() - .collect::>>(), - ); + let contact = Some(mint_info.into()); let mint_info = MintInfoResponse { nuts: get_nuts(&mint.config),