Skip to content

Commit

Permalink
chore: add v1 structs and routes for melt
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 1, 2023
1 parent 5197c0a commit 7b1e924
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
27 changes: 27 additions & 0 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ pub struct PostMintBolt11Response {
pub signatures: Vec<BlindedSignature>,
}

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

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct PostMeltQuoteBolt11Response {
pub quote: String,
pub amount: u64,
pub fee_reserve: u64,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct PostMeltBolt11Request {
pub quote: String,
pub inputs: Proofs,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct PostMeltBolt11Response {
pub paid: bool,
/// payment preimage
pub proof: String,
}

#[cfg(test)]
mod tests {
use crate::{
Expand Down
41 changes: 39 additions & 2 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use crate::mint::Mint;
use crate::model::{GetMintQuery, PostMintQuery};
use moksha_core::primitives::{
CheckFeesRequest, CheckFeesResponse, CurrencyUnit, KeyResponse, KeysResponse, MintInfoResponse,
PaymentRequest, PostMeltRequest, PostMeltResponse, PostMintBolt11Request,
PaymentRequest, PostMeltBolt11Request, PostMeltBolt11Response, PostMeltQuoteBolt11Request,
PostMeltQuoteBolt11Response, PostMeltRequest, PostMeltResponse, PostMintBolt11Request,
PostMintBolt11Response, PostMintQuoteBolt11Request, PostMintQuoteBolt11Response,
PostMintRequest, PostMintResponse, PostSplitRequest, PostSplitResponse,
};
Expand Down Expand Up @@ -85,7 +86,8 @@ fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -
.route("/v1/keysets", get(get_keysets))
.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/melt/quote/bolt11", post(post_melt_quote_bolt11))
.route("/v1/melt/bolt11", post(post_melt_bolt11))
.route("/v1/split", post(post_split))
.route("/v1/info", get(get_legacy_info));

Expand Down Expand Up @@ -285,6 +287,41 @@ async fn post_mint_bolt11(
Ok(Json(PostMintBolt11Response { signatures }))
}

async fn post_melt_quote_bolt11(
State(mint): State<Mint>,
Json(melt_request): Json<PostMeltQuoteBolt11Request>,
) -> Result<Json<PostMeltQuoteBolt11Response>, MokshaMintError> {
let invoice = mint
.lightning
.decode_invoice(melt_request.request.clone())
.await?;
let amount = invoice
.amount_milli_satoshis()
.ok_or_else(|| crate::error::MokshaMintError::InvalidAmount)?;
let fee_reserve = mint.fee_reserve(amount);

Ok(Json(PostMeltQuoteBolt11Response {
amount,
fee_reserve,
quote: melt_request.request.clone(), // FIXME use uuid as quote
}))
}

async fn post_melt_bolt11(
State(mint): State<Mint>,
Json(melt_request): Json<PostMeltBolt11Request>,
) -> Result<Json<PostMeltBolt11Response>, MokshaMintError> {
// FIXME get quote from db
let (paid, preimage, _change) = mint
.melt(melt_request.quote, &melt_request.inputs, &[])
.await?;

Ok(Json(PostMeltBolt11Response {
paid,
proof: preimage,
}))
}

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

0 comments on commit 7b1e924

Please sign in to comment.