From d0d49204a6a7a8c401b6fe5b4ae7cdeeb6259bc1 Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Sun, 10 Dec 2023 11:05:55 +0100 Subject: [PATCH] feat: add swagger for keys and keysets --- moksha-core/src/keyset.rs | 5 +++-- moksha-core/src/primitives.rs | 5 +++-- moksha-mint/src/server.rs | 40 ++++++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/moksha-core/src/keyset.rs b/moksha-core/src/keyset.rs index 850ba3bd..930447c0 100644 --- a/moksha-core/src/keyset.rs +++ b/moksha-core/src/keyset.rs @@ -13,6 +13,7 @@ use hex::ToHex; use serde::{Deserialize, Serialize}; use std::collections::HashMap; +use utoipa::ToSchema; use base64::{engine::general_purpose, Engine as _}; use bitcoin_hashes::{sha256, Hash}; @@ -87,12 +88,12 @@ impl Keysets { } } -#[derive(Clone, Debug, Serialize, Deserialize, Default)] +#[derive(Clone, Debug, Serialize, Deserialize, Default, ToSchema)] pub struct V1Keysets { pub keysets: Vec, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)] pub struct V1Keyset { pub id: String, pub unit: CurrencyUnit, diff --git a/moksha-core/src/primitives.rs b/moksha-core/src/primitives.rs index 9f6759ee..a457bf70 100644 --- a/moksha-core/src/primitives.rs +++ b/moksha-core/src/primitives.rs @@ -109,15 +109,16 @@ pub struct Parameter { pub peg_out_only: bool, } -#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default)] +#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default, ToSchema)] pub struct KeysResponse { pub keysets: Vec, } -#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq)] +#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq, ToSchema)] pub struct KeyResponse { pub id: String, pub unit: CurrencyUnit, + #[schema(value_type = HashMap)] pub keys: HashMap, } diff --git a/moksha-mint/src/server.rs b/moksha-mint/src/server.rs index 8f7fc8f9..905a76ff 100644 --- a/moksha-mint/src/server.rs +++ b/moksha-mint/src/server.rs @@ -10,7 +10,7 @@ use axum::response::IntoResponse; use axum::routing::{get_service, post}; use axum::{middleware, Router}; use axum::{routing::get, Json}; -use moksha_core::keyset::{generate_hash, Keysets, V1Keysets}; +use moksha_core::keyset::{generate_hash, Keysets, V1Keyset, V1Keysets}; use utoipa_swagger_ui::SwaggerUi; use uuid::Uuid; @@ -78,8 +78,17 @@ pub async fn run_server( #[derive(OpenApi)] #[openapi( - paths(get_info,), - components(schemas(MintInfoResponse, MintInfoNut, CurrencyUnit, PaymentMethod)) + paths(get_info, get_keys, get_keys_by_id, get_keysets), + components(schemas( + MintInfoResponse, + MintInfoNut, + CurrencyUnit, + PaymentMethod, + KeysResponse, + KeyResponse, + V1Keysets, + V1Keyset + )) )] struct ApiDoc; @@ -293,6 +302,13 @@ async fn post_swap( })) } +#[utoipa::path( + get, + path = "/v1/keys", + responses( + (status = 200, description = "get keys", body = [KeysResponse]) + ) + )] async fn get_keys(State(mint): State) -> Result, MokshaMintError> { Ok(Json(KeysResponse { keysets: vec![KeyResponse { @@ -303,10 +319,21 @@ async fn get_keys(State(mint): State) -> Result, Moksha })) } +#[utoipa::path( + get, + path = "/v1/keys/{id}", + responses( + (status = 200, description = "get keys by id", body = [KeysResponse]) + ), + params( + ("id" = String, Path, description = "keyset id"), + ) + )] async fn get_keys_by_id( Path(_id): Path, State(mint): State, ) -> Result, MokshaMintError> { + // FIXME check if id is valid Ok(Json(KeysResponse { keysets: vec![KeyResponse { id: mint.keyset.keyset_id.clone(), @@ -316,6 +343,13 @@ async fn get_keys_by_id( })) } +#[utoipa::path( + get, + path = "/v1/keysets", + responses( + (status = 200, description = "get keysets", body = [V1Keysets]) + ), + )] async fn get_keysets(State(mint): State) -> Result, MokshaMintError> { Ok(Json(V1Keysets::new( mint.keyset.keyset_id,