Skip to content

Commit

Permalink
fix: store invoice with uuid as key
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 5, 2023
1 parent ba6e007 commit 1cd2d2c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion moksha-mint/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ mod tests {
dhke,
proof::{Proof, Proofs},
};
use uuid::Uuid;

use crate::{
database::Database,
Expand Down Expand Up @@ -263,7 +264,7 @@ 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("12345678".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 lookup_quote = db.get_quote(key.to_string())?;
Expand Down
9 changes: 6 additions & 3 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashSet, sync::Arc};
use moksha_core::{
blind::{BlindedMessage, BlindedSignature, TotalAmount},
dhke::Dhke,
keyset::{generate_hash, MintKeyset},
keyset::MintKeyset,
primitives::PostSplitResponse,
proof::Proofs,
};
Expand Down Expand Up @@ -102,9 +102,12 @@ impl Mint {
Ok(promises)
}

pub async fn create_invoice(&self, amount: u64) -> Result<(String, String), MokshaMintError> {
pub async fn create_invoice(
&self,
key: String,
amount: u64,
) -> Result<(String, String), MokshaMintError> {
let pr = self.lightning.create_invoice(amount).await?.payment_request;
let key = generate_hash();
self.db
.add_pending_invoice(key.clone(), Invoice::new(amount, pr.clone()))?;
Ok((pr, key))
Expand Down
4 changes: 2 additions & 2 deletions moksha-mint/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ pub struct Quote {
}

impl Quote {
pub fn new(payment_request: String) -> Self {
pub fn new(quote_id: Uuid, payment_request: String) -> Self {
Self {
quote_id: Uuid::new_v4(),
quote_id,
payment_request,
}
}
Expand Down
16 changes: 9 additions & 7 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use axum::response::IntoResponse;
use axum::routing::{get_service, post};
use axum::{middleware, Router};
use axum::{routing::get, Json};
use moksha_core::keyset::{Keysets, V1Keysets};
use moksha_core::keyset::{generate_hash, Keysets, V1Keysets};
use uuid::Uuid;

use crate::mint::Mint;
use crate::model::{GetMintQuery, PostMintQuery, Quote};
Expand Down Expand Up @@ -217,7 +218,9 @@ async fn get_legacy_mint(
State(mint): State<Mint>,
Query(mint_query): Query<GetMintQuery>,
) -> Result<Json<PaymentRequest>, MokshaMintError> {
let (pr, hash) = mint.create_invoice(mint_query.amount).await?;
let (pr, hash) = mint
.create_invoice(generate_hash(), mint_query.amount)
.await?;
Ok(Json(PaymentRequest { pr, hash }))
}

Expand Down Expand Up @@ -272,12 +275,11 @@ async fn post_mint_quote_bolt11(
Json(request): Json<PostMintQuoteBolt11Request>,
) -> Result<Json<PostMintQuoteBolt11Response>, MokshaMintError> {
// FIXME check currency unit
// FIXME use uuid to store invoice
let (pr, _hash) = mint.create_invoice(request.amount).await?;

let key = Uuid::new_v4();
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(pr.clone());
let quote = Quote::new(key, pr.clone());
let quote_id = quote.quote_id.to_string();
mint.db.add_quote(quote_id.clone(), quote)?;

Expand All @@ -299,7 +301,7 @@ async fn post_mint_bolt11(
.ok_or_else(|| crate::error::MokshaMintError::InvalidQuote(request.quote.clone()))?;

let signatures = mint
.mint_tokens(quote.payment_request.clone(), &request.outputs)
.mint_tokens(quote.quote_id.to_string(), &request.outputs)
.await?;
Ok(Json(PostMintBolt11Response { signatures }))
}
Expand Down

0 comments on commit 1cd2d2c

Please sign in to comment.