Skip to content

Commit

Permalink
feat(wallet): mint and melt quote status
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed May 13, 2024
1 parent 91e1823 commit a9f0116
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 3 deletions.
36 changes: 36 additions & 0 deletions bindings/cdk-js/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use cdk_rexie::RexieWalletDatabase;
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
use crate::nuts::nut04::JsMintQuoteBolt11Response;
use crate::nuts::nut05::JsMeltQuoteBolt11Response;
use crate::nuts::nut11::JsP2PKSpendingConditions;
use crate::nuts::nut14::JsHTLCSpendingConditions;
use crate::nuts::{JsCurrencyUnit, JsMintInfo, JsProof};
Expand Down Expand Up @@ -95,6 +97,23 @@ impl JsWallet {
Ok(quote.into())
}

#[wasm_bindgen(js_name = mintQuoteStatus)]
pub async fn mint_quote_status(
&self,
mint_url: String,
quote_id: String,
) -> Result<JsMintQuoteBolt11Response> {
let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?;

let quote = self
.inner
.mint_quote_status(mint_url, &quote_id)
.await
.map_err(into_err)?;

Ok(quote.into())
}

#[wasm_bindgen(js_name = mint)]
pub async fn mint(&mut self, mint_url: String, quote_id: String) -> Result<JsAmount> {
let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?;
Expand Down Expand Up @@ -124,6 +143,23 @@ impl JsWallet {
Ok(melt_quote.into())
}

#[wasm_bindgen(js_name = meltQuoteStatus)]
pub async fn melt_quote_status(
&self,
mint_url: String,
quote_id: String,
) -> Result<JsMeltQuoteBolt11Response> {
let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?;

let quote = self
.inner
.melt_quote_status(mint_url, &quote_id)
.await
.map_err(into_err)?;

Ok(quote.into())
}

#[wasm_bindgen(js_name = melt)]
pub async fn melt(&mut self, mint_url: String, quote_id: String) -> Result<JsMelted> {
let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?;
Expand Down
42 changes: 42 additions & 0 deletions crates/cdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ impl HttpClient {
}
}

/// Mint Quote status
pub async fn get_mint_quote_status(
&self,
mint_url: Url,
quote_id: &str,
) -> Result<MintQuoteBolt11Response, Error> {
let url = join_url(mint_url, &["v1", "mint", "quote", "bolt11", quote_id])?;

let res = self.inner.get(url).send().await?;

let status = res.status();

let response: Result<MintQuoteBolt11Response, serde_json::Error> =
serde_json::from_value(res.json().await?);

match response {
Ok(res) => Ok(res),
Err(_) => Err(ErrorResponse::from_json(&status.to_string())?.into()),
}
}

/// Mint Tokens [NUT-04]
pub async fn post_mint(
&self,
Expand Down Expand Up @@ -192,6 +213,27 @@ impl HttpClient {
}
}

/// Melt Quote Status
pub async fn get_melt_quote_status(
&self,
mint_url: Url,
quote_id: &str,
) -> Result<MeltQuoteBolt11Response, Error> {
let url = join_url(mint_url, &["v1", "melt", "quote", "bolt11", quote_id])?;

let res = self.inner.get(url).send().await?;

let status = res.status();

let response: Result<MeltQuoteBolt11Response, serde_json::Error> =
serde_json::from_value(res.json().await?);

match response {
Ok(res) => Ok(res),
Err(_) => Err(ErrorResponse::from_json(&status.to_string())?.into()),
}
}

/// Melt [NUT-05]
/// [Nut-08] Lightning fee return if outputs defined
pub async fn post_melt(
Expand Down
59 changes: 56 additions & 3 deletions crates/cdk/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use crate::cdk_database::{self, WalletDatabase};
use crate::client::HttpClient;
use crate::dhke::{construct_proofs, hash_to_curve};
use crate::nuts::{
nut10, nut12, Conditions, CurrencyUnit, Id, KeySet, KeySetInfo, Keys, Kind, MintInfo,
PreMintSecrets, PreSwap, Proof, ProofState, Proofs, PublicKey, RestoreRequest, SigFlag,
SigningKey, SpendingConditions, State, SwapRequest, Token, VerifyingKey,
nut10, nut12, Conditions, CurrencyUnit, Id, KeySet, KeySetInfo, Keys, Kind,
MeltQuoteBolt11Response, MintInfo, MintQuoteBolt11Response, PreMintSecrets, PreSwap, Proof,
ProofState, Proofs, PublicKey, RestoreRequest, SigFlag, SigningKey, SpendingConditions, State,
SwapRequest, Token, VerifyingKey,
};
use crate::types::{MeltQuote, Melted, MintQuote};
use crate::url::UncheckedUrl;
Expand Down Expand Up @@ -321,6 +322,32 @@ impl Wallet {
Ok(quote)
}

/// Mint quote status
pub async fn mint_quote_status(
&self,
mint_url: UncheckedUrl,
quote_id: &str,
) -> Result<MintQuoteBolt11Response, Error> {
let response = self
.client
.get_mint_quote_status(mint_url.try_into()?, quote_id)
.await?;

match self.localstore.get_mint_quote(quote_id).await? {
Some(quote) => {
let mut quote = quote;

quote.paid = response.paid;
self.localstore.add_mint_quote(quote).await?;
}
None => {
tracing::info!("Quote mint {} unknown", quote_id);
}
}

Ok(response)
}

async fn active_mint_keyset(
&mut self,
mint_url: &UncheckedUrl,
Expand Down Expand Up @@ -786,6 +813,32 @@ impl Wallet {
Ok(quote)
}

/// Melt quote status
pub async fn melt_quote_status(
&self,
mint_url: UncheckedUrl,
quote_id: &str,
) -> Result<MeltQuoteBolt11Response, Error> {
let response = self
.client
.get_melt_quote_status(mint_url.try_into()?, quote_id)
.await?;

match self.localstore.get_melt_quote(quote_id).await? {
Some(quote) => {
let mut quote = quote;

quote.paid = response.paid;
self.localstore.add_melt_quote(quote).await?;
}
None => {
tracing::info!("Quote melt {} unknown", quote_id);
}
}

Ok(response)
}

// Select proofs
pub async fn select_proofs(
&self,
Expand Down

0 comments on commit a9f0116

Please sign in to comment.