Skip to content

Commit

Permalink
feat: v1 mint tokens by paying bolt11
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Nov 30, 2023
1 parent 3050e80 commit 5197c0a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
33 changes: 28 additions & 5 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ impl PostSplitResponse {
}
}

#[derive(serde::Deserialize, Debug)]
#[derive(Deserialize, Debug)]
pub struct CashuErrorResponse {
pub code: u64,
pub error: String,
}

#[skip_serializing_none]
#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
pub struct MintInfoResponse {
pub name: Option<String>,
pub pubkey: PublicKey,
Expand All @@ -90,12 +90,12 @@ pub struct MintInfoResponse {
pub parameter: Parameter,
}

#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq, Default)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default)]
pub struct Parameter {
pub peg_out_only: bool,
}

#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq, Default)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default)]
pub struct KeysResponse {
pub keysets: Vec<KeyResponse>,
}
Expand All @@ -107,7 +107,7 @@ pub struct KeyResponse {
pub keys: HashMap<u64, PublicKey>,
}

#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
pub enum CurrencyUnit {
Sat,
}
Expand All @@ -120,6 +120,29 @@ impl Display for CurrencyUnit {
}
}

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

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct PostMintQuoteBolt11Response {
pub quote: String,
pub request: String,
}

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

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

#[cfg(test)]
mod tests {
use crate::{
Expand Down
40 changes: 31 additions & 9 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use crate::mint::Mint;
use crate::model::{GetMintQuery, PostMintQuery};
use moksha_core::primitives::{
CheckFeesRequest, CheckFeesResponse, CurrencyUnit, KeyResponse, KeysResponse, MintInfoResponse,
PaymentRequest, PostMeltRequest, PostMeltResponse, PostMintRequest, PostMintResponse,
PostSplitRequest, PostSplitResponse,
PaymentRequest, PostMeltRequest, PostMeltResponse, PostMintBolt11Request,
PostMintBolt11Response, PostMintQuoteBolt11Request, PostMintQuoteBolt11Response,
PostMintRequest, PostMintResponse, PostSplitRequest, PostSplitResponse,
};
use secp256k1::PublicKey;

Expand Down Expand Up @@ -73,19 +74,19 @@ fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -
let legacy_routes = Router::new()
.route("/keys", get(get_legacy_keys))
.route("/keysets", get(get_legacy_keysets))
.route("/mint", get(get_mint).post(post_legacy_mint))
.route("/mint", get(get_legacy_mint).post(post_legacy_mint))
.route("/checkfees", post(post_legacy_check_fees))
.route("/melt", post(post_legacy_melt))
.route("/split", post(post_legacy_split))
.route("/split", post(post_split))
.route("/info", get(get_legacy_info));

let routes = Router::new()
.route("/v1/keys", get(get_keys))
.route("/v1/keysets", get(get_keysets))
.route("/v1/mint", get(get_mint).post(post_legacy_mint))
.route("/v1/checkfees", post(post_legacy_check_fees))
.route("/v1/mint/quote/bolt11", post(post_mint_quote_bolt11))
.route("/v1/mint/bolt11", post(post_mint_bolt11))
.route("/v1/melt", post(post_legacy_melt))
.route("/v1/split", post(post_legacy_split))
.route("/v1/split", post(post_split))
.route("/v1/info", get(get_legacy_info));

let prefix = prefix.unwrap_or_else(|| "".to_owned());
Expand Down Expand Up @@ -139,7 +140,7 @@ async fn add_response_headers(
Ok(res)
}

async fn post_legacy_split(
async fn post_split(
State(mint): State<Mint>,
Json(split_request): Json<PostSplitRequest>,
) -> Result<Json<PostSplitResponse>, MokshaMintError> {
Expand Down Expand Up @@ -209,7 +210,7 @@ async fn get_legacy_info(
Ok(Json(mint_info))
}

async fn get_mint(
async fn get_legacy_mint(
State(mint): State<Mint>,
Query(mint_query): Query<GetMintQuery>,
) -> Result<Json<PaymentRequest>, MokshaMintError> {
Expand Down Expand Up @@ -263,6 +264,27 @@ async fn get_keysets(State(mint): State<Mint>) -> Result<Json<V1Keysets>, Moksha
)))
}

async fn post_mint_quote_bolt11(
State(mint): State<Mint>,
Json(request): Json<PostMintQuoteBolt11Request>,
) -> Result<Json<PostMintQuoteBolt11Response>, MokshaMintError> {
// FIXME check currency unit
let (pr, hash) = mint.create_invoice(request.amount).await?;
Ok(Json(PostMintQuoteBolt11Response {
quote: hash,
request: pr,
}))
}

async fn post_mint_bolt11(
State(mint): State<Mint>,
Json(request): Json<PostMintBolt11Request>,
) -> Result<Json<PostMintBolt11Response>, MokshaMintError> {
// FIXME don't use hash as quote
let signatures = mint.mint_tokens(request.quote, &request.outputs).await?;
Ok(Json(PostMintBolt11Response { signatures }))
}

#[cfg(test)]
mod tests {
use std::{collections::HashMap, sync::Arc};
Expand Down

0 comments on commit 5197c0a

Please sign in to comment.