Skip to content

Commit

Permalink
Place utoipa traits and structs behind swagger feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Oct 21, 2024
1 parent 702e667 commit 345f3ad
Show file tree
Hide file tree
Showing 24 changed files with 148 additions and 133 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
-p cdk --no-default-features,
-p cdk --no-default-features --features wallet,
-p cdk --no-default-features --features mint,
-p cdk --no-default-features --features "mint swagger",
-p cdk-redb,
-p cdk-sqlite,
-p cdk-axum,
Expand Down Expand Up @@ -143,6 +144,7 @@ jobs:
-p cdk --no-default-features,
-p cdk --no-default-features --features wallet,
-p cdk --no-default-features --features mint,
-p cdk --no-default-features --features "mint swagger",
-p cdk-axum,
-p cdk-strike,
-p cdk-lnbits,
Expand Down
5 changes: 4 additions & 1 deletion crates/cdk-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ axum = "0.6.20"
cdk = { path = "../cdk", version = "0.4.0", default-features = false, features = ["mint"] }
tokio = { version = "1", default-features = false }
tracing = { version = "0.1", default-features = false, features = ["attributes", "log"] }
utoipa = { version = "4", features = ["preserve_order", "preserve_path_order"] }
utoipa = { version = "4", features = ["preserve_order", "preserve_path_order"], optional = true }
futures = { version = "0.3.28", default-features = false }
moka = { version = "0.11.1", features = ["future"] }
serde_json = "1"
paste = "1.0.15"

[features]
swagger = ["cdk/swagger", "dep:utoipa"]
60 changes: 32 additions & 28 deletions crates/cdk-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,51 @@ use std::time::Duration;
use anyhow::Result;
use axum::routing::{get, post};
use axum::Router;
use cdk::amount::Amount;
use cdk::error::{ErrorCode, ErrorResponse};
use cdk::mint::Mint;
use cdk::nuts::nut00::{
BlindSignature, BlindedMessage, CurrencyUnit, PaymentMethod, Proof, Witness,
};
use cdk::nuts::nut01::{Keys, KeysResponse, PublicKey, SecretKey};
use cdk::nuts::nut02::{Id, KeySet, KeySetInfo, KeySetVersion, KeysetResponse};
use cdk::nuts::nut03::{SwapRequest, SwapResponse};
use cdk::nuts::nut04;
use cdk::nuts::nut04::{
MintBolt11Request, MintBolt11Response, MintMethodSettings, MintQuoteBolt11Request,
MintQuoteBolt11Response,
};
use cdk::nuts::nut05;
use cdk::nuts::nut05::{
MeltBolt11Request, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt11Response,
};
use cdk::nuts::nut06::{ContactInfo, MintInfo, MintVersion, Nuts, SupportedSettings};
use cdk::nuts::nut07::{CheckStateRequest, CheckStateResponse, ProofState, State};
use cdk::nuts::nut09::{RestoreRequest, RestoreResponse};
use cdk::nuts::nut11::P2PKWitness;
use cdk::nuts::nut12::{BlindSignatureDleq, ProofDleq};
use cdk::nuts::nut14::HTLCWitness;
use cdk::nuts::nut15;
use cdk::nuts::nut15::{Mpp, MppMethodSettings};
use cdk::nuts::{MeltQuoteState, MintQuoteState};
use moka::future::Cache;
use router_handlers::*;
use utoipa::OpenApi;

mod router_handlers;

#[cfg(feature = "swagger")]
mod swagger_imports {
pub use cdk::amount::Amount;
pub use cdk::error::{ErrorCode, ErrorResponse};
pub use cdk::nuts::nut00::{
BlindSignature, BlindedMessage, CurrencyUnit, PaymentMethod, Proof, Witness,
};
pub use cdk::nuts::nut01::{Keys, KeysResponse, PublicKey, SecretKey};
pub use cdk::nuts::nut02::{Id, KeySet, KeySetInfo, KeySetVersion, KeysetResponse};
pub use cdk::nuts::nut03::{SwapRequest, SwapResponse};
pub use cdk::nuts::nut04;
pub use cdk::nuts::nut04::{
MintBolt11Request, MintBolt11Response, MintMethodSettings, MintQuoteBolt11Request,
MintQuoteBolt11Response,
};
pub use cdk::nuts::nut05;
pub use cdk::nuts::nut05::{
MeltBolt11Request, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt11Response,
};
pub use cdk::nuts::nut06::{ContactInfo, MintInfo, MintVersion, Nuts, SupportedSettings};
pub use cdk::nuts::nut07::{CheckStateRequest, CheckStateResponse, ProofState, State};
pub use cdk::nuts::nut09::{RestoreRequest, RestoreResponse};
pub use cdk::nuts::nut11::P2PKWitness;
pub use cdk::nuts::nut12::{BlindSignatureDleq, ProofDleq};
pub use cdk::nuts::nut14::HTLCWitness;
pub use cdk::nuts::nut15;
pub use cdk::nuts::nut15::{Mpp, MppMethodSettings};
pub use cdk::nuts::{MeltQuoteState, MintQuoteState};
}

/// CDK Mint State
#[derive(Clone)]
pub struct MintState {
mint: Arc<Mint>,
cache: Cache<String, String>,
}

#[derive(OpenApi)]
#[cfg(feature = "swagger")]
#[derive(utoipa::OpenApi)]
#[openapi(
components(schemas(
Amount,
Expand Down
52 changes: 26 additions & 26 deletions crates/cdk-axum/src/router_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ post_cache_wrapper!(post_swap, SwapRequest, SwapResponse);
post_cache_wrapper!(post_mint_bolt11, MintBolt11Request, MintBolt11Response);
post_cache_wrapper!(post_melt_bolt11, MeltBolt11Request, MeltQuoteBolt11Response);

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/keys",
responses(
(status = 200, description = "Successful response", body = KeysResponse, content_type = "application/json")
)
)]
))]
/// Get the public keys of the newest mint keyset
///
/// This endpoint returns a dictionary of all supported token values of the mint and their associated public key.
Expand All @@ -69,7 +69,7 @@ pub async fn get_keys(State(state): State<MintState>) -> Result<Json<KeysRespons
Ok(Json(pubkeys))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/keys/{keyset_id}",
Expand All @@ -80,7 +80,7 @@ pub async fn get_keys(State(state): State<MintState>) -> Result<Json<KeysRespons
(status = 200, description = "Successful response", body = KeysResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Get the public keys of a specific keyset
///
/// Get the public keys of the mint from a specific keyset ID.
Expand All @@ -96,15 +96,15 @@ pub async fn get_keyset_pubkeys(
Ok(Json(pubkeys))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/keysets",
responses(
(status = 200, description = "Successful response", body = KeysetResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Get all active keyset IDs of the mint
///
/// This endpoint returns a list of keysets that the mint currently supports and will accept tokens from.
Expand All @@ -117,7 +117,7 @@ pub async fn get_keysets(State(state): State<MintState>) -> Result<Json<KeysetRe
Ok(Json(keysets))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/mint/quote/bolt11",
Expand All @@ -126,7 +126,7 @@ pub async fn get_keysets(State(state): State<MintState>) -> Result<Json<KeysetRe
(status = 200, description = "Successful response", body = MintQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Request a quote for minting of new tokens
///
/// Request minting of new tokens. The mint responds with a Lightning invoice. This endpoint can be used for a Lightning invoice UX flow.
Expand All @@ -143,7 +143,7 @@ pub async fn get_mint_bolt11_quote(
Ok(Json(quote))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/mint/quote/bolt11/{quote_id}",
Expand All @@ -154,7 +154,7 @@ pub async fn get_mint_bolt11_quote(
(status = 200, description = "Successful response", body = MintQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Get mint quote by ID
///
/// Get mint quote state.
Expand All @@ -174,7 +174,7 @@ pub async fn get_check_mint_bolt11_quote(
Ok(Json(quote))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/mint/bolt11",
Expand All @@ -183,7 +183,7 @@ pub async fn get_check_mint_bolt11_quote(
(status = 200, description = "Successful response", body = MintBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Mint tokens by paying a BOLT11 Lightning invoice.
///
/// Requests the minting of tokens belonging to a paid payment request.
Expand All @@ -205,7 +205,7 @@ pub async fn post_mint_bolt11(
Ok(Json(res))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/melt/quote/bolt11",
Expand All @@ -214,7 +214,7 @@ pub async fn post_mint_bolt11(
(status = 200, description = "Successful response", body = MeltQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Request a quote for melting tokens
pub async fn get_melt_bolt11_quote(
State(state): State<MintState>,
Expand All @@ -229,7 +229,7 @@ pub async fn get_melt_bolt11_quote(
Ok(Json(quote))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/melt/quote/bolt11/{quote_id}",
Expand All @@ -240,7 +240,7 @@ pub async fn get_melt_bolt11_quote(
(status = 200, description = "Successful response", body = MeltQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Get melt quote by ID
///
/// Get melt quote state.
Expand All @@ -260,7 +260,7 @@ pub async fn get_check_melt_bolt11_quote(
Ok(Json(quote))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/melt/bolt11",
Expand All @@ -269,7 +269,7 @@ pub async fn get_check_melt_bolt11_quote(
(status = 200, description = "Successful response", body = MeltQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Melt tokens for a Bitcoin payment that the mint will make for the user in exchange
///
/// Requests tokens to be destroyed and sent out via Lightning.
Expand All @@ -286,7 +286,7 @@ pub async fn post_melt_bolt11(
Ok(Json(res))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/checkstate",
Expand All @@ -295,7 +295,7 @@ pub async fn post_melt_bolt11(
(status = 200, description = "Successful response", body = CheckStateResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Check whether a proof is spent already or is pending in a transaction
///
/// Check whether a secret has been spent already or not.
Expand All @@ -311,20 +311,20 @@ pub async fn post_check(
Ok(Json(state))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/info",
responses(
(status = 200, description = "Successful response", body = MintInfo)
)
)]
))]
/// Mint information, operator contact information, and other info
pub async fn get_mint_info(State(state): State<MintState>) -> Result<Json<MintInfo>, Response> {
Ok(Json(state.mint.mint_info().clone().time(unix_time())))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/swap",
Expand All @@ -333,7 +333,7 @@ pub async fn get_mint_info(State(state): State<MintState>) -> Result<Json<MintIn
(status = 200, description = "Successful response", body = SwapResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Swap inputs for outputs of the same value
///
/// Requests a set of Proofs to be swapped for another set of BlindSignatures.
Expand All @@ -354,7 +354,7 @@ pub async fn post_swap(
Ok(Json(swap_response))
}

#[utoipa::path(
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/restore",
Expand All @@ -363,7 +363,7 @@ pub async fn post_swap(
(status = 200, description = "Successful response", body = RestoreResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
)]
))]
/// Restores blind signature for a set of outputs.
pub async fn post_restore(
State(state): State<MintState>,
Expand Down
7 changes: 5 additions & 2 deletions crates/cdk-mintd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ tower-http = { version = "0.4.4", features = ["cors"] }
lightning-invoice = { version = "0.32.0", features = ["serde", "std"] }
home = "0.5.5"
url = "2.3"
utoipa = "4"
utoipa-swagger-ui = { version = "4", features = ["axum"] }
utoipa = { version = "4", optional = true }
utoipa-swagger-ui = { version = "4", features = ["axum"], optional = true }

[features]
swagger = ["cdk-axum/swagger", "dep:utoipa", "dep:utoipa-swagger-ui"]
14 changes: 8 additions & 6 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ use tokio::sync::{Mutex, Notify};
use tower_http::cors::CorsLayer;
use tracing_subscriber::EnvFilter;
use url::Url;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

mod cli;
mod config;
Expand Down Expand Up @@ -474,13 +472,17 @@ async fn main() -> anyhow::Result<()> {
let v1_service = cdk_axum::create_mint_router(Arc::clone(&mint), cache_ttl, cache_tti).await?;

let mut mint_service = Router::new()
.merge(
SwaggerUi::new("/swagger-ui")
.url("/api-docs/openapi.json", cdk_axum::ApiDocV1::openapi()),
)
.merge(v1_service)
.layer(CorsLayer::permissive());

#[cfg(feature = "swagger")]
{
mint_service = mint_service.merge(
utoipa_swagger_ui::SwaggerUi::new("/swagger-ui")
.url("/api-docs/openapi.json", cdk_axum::ApiDocV1::openapi()),
);
}

for router in ln_routers {
mint_service = mint_service.merge(router);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ license = "MIT"

[features]
default = ["mint", "wallet"]
mint = ["dep:futures", "dep:utoipa"]
mint = ["dep:futures"]
swagger = ["mint", "dep:utoipa"]
wallet = ["dep:reqwest"]
bench = []

Expand Down
Loading

0 comments on commit 345f3ad

Please sign in to comment.