Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet): mint and melt quote status #131

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading