Skip to content

Commit

Permalink
feat: store quote for melt in db
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 5, 2023
1 parent 1cd2d2c commit d4ca37e
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 54 deletions.
2 changes: 2 additions & 0 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ pub struct PostMeltQuoteBolt11Response {
pub quote: String,
pub amount: u64,
pub fee_reserve: u64,
pub paid: bool,
pub expiry: u64,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
Expand Down
11 changes: 8 additions & 3 deletions moksha-mint/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,14 @@ mod tests {
let tmp_dir = tmp.path().to_str().expect("Could not create tmp dir");
let db = super::RocksDB::new(tmp_dir.to_owned());

let quote = Quote::new(Uuid::new_v4(), "12345678".to_owned());
let key = quote.quote_id.to_string();
db.add_quote(key.clone(), quote.clone())?;
let key = Uuid::new_v4();
let quote = Quote::Bolt11Mint {
quote_id: key,
payment_request: "12345678".to_owned(),
expiry: 12345678,
};

db.add_quote(key.to_string(), quote.clone())?;
let lookup_quote = db.get_quote(key.to_string())?;

assert_eq!(quote, lookup_quote);
Expand Down
6 changes: 3 additions & 3 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ impl Mint {

pub async fn mint_tokens(
&self,
invoice_hash: String,
key: String,
outputs: &[BlindedMessage],
) -> Result<Vec<BlindedSignature>, MokshaMintError> {
let invoice = self.db.get_pending_invoice(invoice_hash.clone())?;
let invoice = self.db.get_pending_invoice(key.clone())?;

let is_paid = self
.lightning
Expand All @@ -129,7 +129,7 @@ impl Mint {
return Err(MokshaMintError::InvoiceNotPaidYet);
}

self.db.remove_pending_invoice(invoice_hash)?;
self.db.remove_pending_invoice(key)?;
self.create_blinded_signatures(outputs)
}

Expand Down
39 changes: 27 additions & 12 deletions moksha-mint/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,32 @@ pub struct CreateInvoiceParams {
pub internal: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Quote {
pub quote_id: Uuid,
pub payment_request: String,
}
// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
// pub struct Quote {
// pub quote_id: Uuid,
// pub payment_request: String,
// }

impl Quote {
pub fn new(quote_id: Uuid, payment_request: String) -> Self {
Self {
quote_id,
payment_request,
}
}
// impl Quote {
// pub fn new(quote_id: Uuid, payment_request: String) -> Self {
// Self {
// quote_id,
// payment_request,
// }
// }
// }

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Quote {
Bolt11Mint {
quote_id: Uuid,
payment_request: String,
expiry: u64,
},
Bolt11Melt {
quote_id: Uuid,
amount: u64,
fee_reserve: u64,
expiry: u64,
},
}
131 changes: 95 additions & 36 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -
.route("/v1/keys", get(get_keys))
.route("/v1/keysets", get(get_keysets))
.route("/v1/mint/quote/bolt11", post(post_mint_quote_bolt11))
.route("/v1/mint/quote/bolt11/:quote_id", get(get_mint_quote))
.route("/v1/mint/quote/bolt11/:quote", get(get_mint_quote_bolt11))
.route("/v1/mint/bolt11", post(post_mint_bolt11))
.route("/v1/melt/quote/bolt11", post(post_melt_quote_bolt11))
.route("/v1/melt/quote/bolt11/:quote", get(get_melt_quote_bolt11))
.route("/v1/melt/bolt11", post(post_melt_bolt11))
.route("/v1/swap", post(post_split))
.route("/v1/info", get(get_legacy_info));
Expand Down Expand Up @@ -279,15 +280,20 @@ async fn post_mint_quote_bolt11(
let (pr, _hash) = mint.create_invoice(key.to_string(), request.amount).await?;
let invoice = mint.lightning.decode_invoice(pr.clone()).await?;

let quote = Quote::new(key, pr.clone());
let quote_id = quote.quote_id.to_string();
mint.db.add_quote(quote_id.clone(), quote)?;
let expiry = invoice.expiry_time().as_secs();
let quote = Quote::Bolt11Mint {
quote_id: key,
payment_request: pr.clone(),
expiry, // FIXME check if this is correct
};

mint.db.add_quote(key.to_string(), quote)?;

Ok(Json(PostMintQuoteBolt11Response {
quote: quote_id,
quote: key.to_string(),
request: pr,
paid: false,
expiry: invoice.expiry_time().as_secs(), // FIXME check if this is correct
expiry,
}))
}

Expand All @@ -300,10 +306,15 @@ async fn post_mint_bolt11(
.get(request.quote.as_str())
.ok_or_else(|| crate::error::MokshaMintError::InvalidQuote(request.quote.clone()))?;

let signatures = mint
.mint_tokens(quote.quote_id.to_string(), &request.outputs)
.await?;
Ok(Json(PostMintBolt11Response { signatures }))
match quote {
Quote::Bolt11Mint { .. } => {
let signatures = mint.mint_tokens(request.quote, &request.outputs).await?;
Ok(Json(PostMintBolt11Response { signatures }))
}
_ => Err(crate::error::MokshaMintError::InvalidQuote(
request.quote.clone(),
)),
}
}

async fn post_melt_quote_bolt11(
Expand All @@ -319,53 +330,101 @@ async fn post_melt_quote_bolt11(
.ok_or_else(|| crate::error::MokshaMintError::InvalidAmount)?;
let fee_reserve = mint.fee_reserve(amount);

// Store quote in db
let key = Uuid::new_v4();
let quote = Quote::Bolt11Melt {
quote_id: key,
amount,
fee_reserve,
expiry: invoice.expiry_time().as_secs(), // FIXME check if this is correct
};
mint.db.add_quote(key.to_string(), quote)?;

// TODO implement into for Quote
Ok(Json(PostMeltQuoteBolt11Response {
amount,
fee_reserve,
quote: melt_request.request.clone(), // FIXME use uuid as quote
paid: false,
expiry: invoice.expiry_time().as_secs(), // FIXME check if this is correct
}))
}

async fn post_melt_bolt11(
State(mint): State<Mint>,
Json(melt_request): Json<PostMeltBolt11Request>,
) -> Result<Json<PostMeltBolt11Response>, MokshaMintError> {
let quote = mint.db.get_quote(melt_request.quote)?;

let (paid, preimage, _change) = mint
.melt(quote.payment_request, &melt_request.inputs, &[])
.await?;

Ok(Json(PostMeltBolt11Response {
paid,
payment_preimage: preimage,
change: vec![], // FIXME return change
}))
let quote = mint.db.get_quote(melt_request.quote.clone())?;

match quote {
Quote::Bolt11Melt { .. } => {
let (paid, preimage, change) = mint
.melt(melt_request.quote, &melt_request.inputs, &[])
.await?;

Ok(Json(PostMeltBolt11Response {
paid,
payment_preimage: preimage,
change,
}))
}
_ => Err(crate::error::MokshaMintError::InvalidQuote(
melt_request.quote.clone(),
)),
}
}

async fn get_mint_quote(
async fn get_mint_quote_bolt11(
Path(quote_id): Path<String>,
State(mint): State<Mint>,
) -> Result<Json<PostMintQuoteBolt11Response>, MokshaMintError> {
info!("get_quote: {}", quote_id);
let quote = mint.db.get_quote(quote_id.clone())?;

let invoice = mint
.lightning
.decode_invoice(quote.payment_request.clone())
.await?;
match quote {
Quote::Bolt11Mint {
quote_id,
payment_request,
expiry,
} => {
let paid = mint
.lightning
.is_invoice_paid(payment_request.clone())
.await?;

Ok(Json(PostMintQuoteBolt11Response {
quote: quote_id.to_string(),
request: payment_request,
paid,
expiry,
}))
}
_ => Err(crate::error::MokshaMintError::InvalidQuote(quote_id)),
}
}

let is_paid = mint
.lightning
.is_invoice_paid(quote.payment_request.clone())
.await?;
async fn get_melt_quote_bolt11(
Path(quote_id): Path<String>,
State(mint): State<Mint>,
) -> Result<Json<PostMeltQuoteBolt11Response>, MokshaMintError> {
info!("get_melt_quote: {}", quote_id);
let quote = mint.db.get_quote(quote_id.clone())?;

Ok(Json(PostMintQuoteBolt11Response {
quote: quote_id,
request: quote.payment_request,
paid: is_paid,
expiry: invoice.expiry_time().as_secs(), // FIXME check if this is correct
}))
match quote {
Quote::Bolt11Melt {
quote_id,
amount,
fee_reserve,
expiry,
} => Ok(Json(PostMeltQuoteBolt11Response {
amount,
fee_reserve,
quote: quote_id.to_string(),
paid: false, // FIXME check if paid
expiry,
})),
_ => Err(crate::error::MokshaMintError::InvalidQuote(quote_id)),
}
}

#[cfg(test)]
Expand Down

0 comments on commit d4ca37e

Please sign in to comment.