Skip to content

Commit

Permalink
feat: add swagger for mint and melt
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 10, 2023
1 parent d0d4920 commit 8368bd4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
7 changes: 5 additions & 2 deletions moksha-core/src/blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@
use secp256k1::{PublicKey, SecretKey};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::{
amount::{generate_random_string, Amount},
dhke::Dhke,
error::MokshaCoreError,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct BlindedSignature {
pub amount: u64,
#[serde(rename = "C_")]
#[schema(value_type=String)]
pub c_: PublicKey,
pub id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct BlindedMessage {
pub amount: u64,
#[serde(rename = "B_")]
#[schema(value_type=String)]
pub b_: PublicKey,
}

Expand Down
16 changes: 8 additions & 8 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ pub enum PaymentMethod {
Bolt11,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMintQuoteBolt11Request {
pub amount: u64,
pub unit: CurrencyUnit,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMintQuoteBolt11Response {
pub quote: String,
#[serde(rename = "request")]
Expand Down Expand Up @@ -179,25 +179,25 @@ impl TryFrom<Quote> for PostMintQuoteBolt11Response {
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMintBolt11Request {
pub quote: String,
pub outputs: Vec<BlindedMessage>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMintBolt11Response {
pub signatures: Vec<BlindedSignature>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMeltQuoteBolt11Request {
/// payment request
pub request: String,
pub unit: CurrencyUnit,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMeltQuoteBolt11Response {
pub quote: String,
pub amount: u64,
Expand Down Expand Up @@ -247,14 +247,14 @@ impl TryFrom<Quote> for PostMeltQuoteBolt11Response {
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMeltBolt11Request {
pub quote: String,
pub inputs: Proofs,
pub outputs: Vec<BlindedMessage>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMeltBolt11Response {
pub paid: bool,
pub payment_preimage: String,
Expand Down
6 changes: 4 additions & 2 deletions moksha-core/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
use secp256k1::PublicKey;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use utoipa::ToSchema;

use crate::error::MokshaCoreError;

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
pub struct Proof {
pub amount: u64,
pub secret: String,
#[serde(rename = "C")]
#[schema(value_type=String)]
pub c: PublicKey,
pub id: String, // FIXME use keysetID as specific type / consider making this non optional and brake backwards compatibility
pub script: Option<P2SHScript>,
Expand All @@ -42,7 +44,7 @@ impl Proof {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct P2SHScript;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, ToSchema)]
pub struct Proofs(pub(super) Vec<Proof>);

impl Proofs {
Expand Down
75 changes: 73 additions & 2 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ use axum::routing::{get_service, post};
use axum::{middleware, Router};
use axum::{routing::get, Json};
use moksha_core::keyset::{generate_hash, Keysets, V1Keyset, V1Keysets};
use moksha_core::proof::Proofs;
use utoipa_swagger_ui::SwaggerUi;
use uuid::Uuid;

use crate::mint::Mint;
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,
Expand Down Expand Up @@ -78,7 +81,16 @@ pub async fn run_server(

#[derive(OpenApi)]
#[openapi(
paths(get_info, get_keys, get_keys_by_id, get_keysets),
paths(
get_keys,
get_keys_by_id,
get_keysets,
post_mint_bolt11,
post_mint_quote_bolt11,
get_mint_quote_bolt11,
post_melt_bolt11,
get_info,
),
components(schemas(
MintInfoResponse,
MintInfoNut,
Expand All @@ -87,7 +99,18 @@ pub async fn run_server(
KeysResponse,
KeyResponse,
V1Keysets,
V1Keyset
V1Keyset,
BlindedMessage,
BlindedSignature,
Proofs,
PostMintQuoteBolt11Request,
PostMintQuoteBolt11Response,
PostMeltQuoteBolt11Request,
PostMeltQuoteBolt11Response,
PostMeltBolt11Request,
PostMeltBolt11Response,
PostMintBolt11Request,
PostMintBolt11Response,
))
)]
struct ApiDoc;
Expand Down Expand Up @@ -358,6 +381,14 @@ async fn get_keysets(State(mint): State<Mint>) -> Result<Json<V1Keysets>, Moksha
)))
}

#[utoipa::path(
post,
path = "/v1/mint/quote/bolt11",
request_body = PostMintQuoteBolt11Request,
responses(
(status = 200, description = "post mint quote", body = [PostMintQuoteBolt11Response])
),
)]
async fn post_mint_quote_bolt11(
State(mint): State<Mint>,
Json(request): Json<PostMintQuoteBolt11Request>,
Expand Down Expand Up @@ -385,6 +416,17 @@ async fn post_mint_quote_bolt11(
}))
}

#[utoipa::path(
post,
path = "/v1/mint/bolt11/{quote_id}",
request_body = PostMintBolt11Request,
responses(
(status = 200, description = "post mint quote", body = [PostMintBolt11Response])
),
params(
("quote_id" = String, Path, description = "quote id"),
)
)]
async fn post_mint_bolt11(
State(mint): State<Mint>,
Json(request): Json<PostMintBolt11Request>,
Expand All @@ -407,6 +449,14 @@ async fn post_mint_bolt11(
}
}

#[utoipa::path(
post,
path = "/v1/melt/quote/bolt11/{quote}",
request_body = PostMeltQuoteBolt11Request,
responses(
(status = 200, description = "post mint quote", body = [PostMeltQuoteBolt11Response])
),
)]
async fn post_melt_quote_bolt11(
State(mint): State<Mint>,
Json(melt_request): Json<PostMeltQuoteBolt11Request>,
Expand Down Expand Up @@ -438,6 +488,17 @@ async fn post_melt_quote_bolt11(
})?))
}

#[utoipa::path(
post,
path = "/v1/melt/bolt11/{quote_id}",
request_body = PostMeltBolt11Request,
responses(
(status = 200, description = "post melt", body = [PostMeltBolt11Response])
),
params(
("quote_id" = String, Path, description = "quote id"),
)
)]
async fn post_melt_bolt11(
State(mint): State<Mint>,
Json(melt_request): Json<PostMeltBolt11Request>,
Expand Down Expand Up @@ -485,6 +546,16 @@ async fn post_melt_bolt11(
}
}

#[utoipa::path(
get,
path = "/v1/mint/quote/bolt11/{quote_id}",
responses(
(status = 200, description = "get mint quote by id", body = [PostMintQuoteBolt11Response])
),
params(
("quote_id" = String, Path, description = "quote id"),
)
)]
async fn get_mint_quote_bolt11(
Path(quote_id): Path<String>,
State(mint): State<Mint>,
Expand Down

0 comments on commit 8368bd4

Please sign in to comment.