Skip to content

Commit

Permalink
feat: add swagger for keys and keysets
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 10, 2023
1 parent 19c9c7e commit d0d4920
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
5 changes: 3 additions & 2 deletions moksha-core/src/keyset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<V1Keyset>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct V1Keyset {
pub id: String,
pub unit: CurrencyUnit,
Expand Down
5 changes: 3 additions & 2 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyResponse>,
}

#[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<u64, String>)]
pub keys: HashMap<u64, PublicKey>,
}

Expand Down
40 changes: 37 additions & 3 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<Mint>) -> Result<Json<KeysResponse>, MokshaMintError> {
Ok(Json(KeysResponse {
keysets: vec![KeyResponse {
Expand All @@ -303,10 +319,21 @@ async fn get_keys(State(mint): State<Mint>) -> Result<Json<KeysResponse>, 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<String>,
State(mint): State<Mint>,
) -> Result<Json<KeysResponse>, MokshaMintError> {
// FIXME check if id is valid
Ok(Json(KeysResponse {
keysets: vec![KeyResponse {
id: mint.keyset.keyset_id.clone(),
Expand All @@ -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<Mint>) -> Result<Json<V1Keysets>, MokshaMintError> {
Ok(Json(V1Keysets::new(
mint.keyset.keyset_id,
Expand Down

0 comments on commit d0d4920

Please sign in to comment.