Skip to content

Commit

Permalink
Standardize HttpClient post function structs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcaseria committed Oct 31, 2024
1 parent ba643ff commit 6b8d225
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 104 deletions.
25 changes: 15 additions & 10 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use cdk::cdk_lightning::MintLightning;
use cdk::dhke::construct_proofs;
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, Id, KeySet, MeltMethodSettings, MintInfo, MintMethodSettings, MintQuoteState,
Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
CurrencyUnit, Id, KeySet, MeltMethodSettings, MintBolt11Request, MintInfo, MintMethodSettings,
MintQuoteBolt11Request, MintQuoteState, Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
};
use cdk::types::{LnKey, QuoteTTL};
use cdk::wallet::client::{HttpClient, HttpClientMethods};
Expand Down Expand Up @@ -158,8 +158,14 @@ pub async fn mint_proofs(

let wallet_client = HttpClient::new();

let request = MintQuoteBolt11Request {
amount,
unit: CurrencyUnit::Sat,
description,
};

let mint_quote = wallet_client
.post_mint_quote(mint_url.parse()?, 1.into(), CurrencyUnit::Sat, description)
.post_mint_quote(mint_url.parse()?, request)
.await?;

println!("Please pay: {}", mint_quote.request);
Expand All @@ -179,13 +185,12 @@ pub async fn mint_proofs(

let premint_secrets = PreMintSecrets::random(keyset_id, amount, &SplitTarget::default())?;

let mint_response = wallet_client
.post_mint(
mint_url.parse()?,
&mint_quote.quote,
premint_secrets.clone(),
)
.await?;
let request = MintBolt11Request {
quote: mint_quote.quote,
outputs: premint_secrets.blinded_messages(),
};

let mint_response = wallet_client.post_mint(mint_url.parse()?, request).await?;

let pre_swap_proofs = construct_proofs(
mint_response.signatures,
Expand Down
17 changes: 8 additions & 9 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bip39::Mnemonic;
use cdk::{
amount::SplitTarget,
cdk_database::WalletMemoryDatabase,
nuts::{CurrencyUnit, MeltQuoteState, PreMintSecrets, State},
nuts::{CurrencyUnit, MeltBolt11Request, MeltQuoteState, PreMintSecrets, State},
wallet::{
client::{HttpClient, HttpClientMethods},
Wallet,
Expand Down Expand Up @@ -357,14 +357,13 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {

let client = HttpClient::new();

let melt_response = client
.post_melt(
MINT_URL.parse()?,
melt_quote.id.clone(),
proofs.clone(),
Some(premint_secrets.blinded_messages()),
)
.await?;
let melt_request = MeltBolt11Request {
quote: melt_quote.id.clone(),
inputs: proofs.clone(),
outputs: Some(premint_secrets.blinded_messages()),
};

let melt_response = client.post_melt(MINT_URL.parse()?, melt_request).await?;

assert!(melt_response.change.is_some());

Expand Down
17 changes: 10 additions & 7 deletions crates/cdk-integration-tests/tests/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use bip39::Mnemonic;
use cdk::{
amount::{Amount, SplitTarget},
cdk_database::WalletMemoryDatabase,
nuts::{CurrencyUnit, MeltQuoteState, MintQuoteState, PreMintSecrets, State},
nuts::{
CurrencyUnit, MeltQuoteState, MintBolt11Request, MintQuoteState, PreMintSecrets, State,
},
wallet::{
client::{HttpClient, HttpClientMethods},
Wallet,
Expand Down Expand Up @@ -292,15 +294,16 @@ async fn test_cached_mint() -> Result<()> {
let premint_secrets =
PreMintSecrets::random(active_keyset_id, 31.into(), &SplitTarget::default()).unwrap();

let request = MintBolt11Request {
quote: quote.id,
outputs: premint_secrets.blinded_messages(),
};

let response = http_client
.post_mint(
get_mint_url().as_str().parse()?,
&quote.id,
premint_secrets.clone(),
)
.post_mint(get_mint_url().as_str().parse()?, request.clone())
.await?;
let response1 = http_client
.post_mint(get_mint_url().as_str().parse()?, &quote.id, premint_secrets)
.post_mint(get_mint_url().as_str().parse()?, request)
.await?;

assert!(response == response1);
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk/src/nuts/nut03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ pub struct PreSwap {
pub fee: Amount,
}

/// Split Request [NUT-06]
/// Swap Request [NUT-03]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
pub struct SwapRequest {
/// Proofs that are to be spent in `Split`
/// Proofs that are to be spent in a `Swap`
#[cfg_attr(feature = "swagger", schema(value_type = Vec<Proof>))]
pub inputs: Proofs,
/// Blinded Messages for Mint to sign
Expand Down
81 changes: 19 additions & 62 deletions crates/cdk/src/wallet/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ use url::Url;
use super::Error;
use crate::error::ErrorResponse;
use crate::mint_url::MintUrl;
use crate::nuts::nut15::Mpp;
use crate::nuts::{
BlindedMessage, CheckStateRequest, CheckStateResponse, CurrencyUnit, Id, KeySet, KeysResponse,
KeysetResponse, MeltBolt11Request, MeltQuoteBolt11Request, MeltQuoteBolt11Response,
MintBolt11Request, MintBolt11Response, MintInfo, MintQuoteBolt11Request,
MintQuoteBolt11Response, PreMintSecrets, Proof, PublicKey, RestoreRequest, RestoreResponse,
SwapRequest, SwapResponse,
CheckStateRequest, CheckStateResponse, Id, KeySet, KeysResponse, KeysetResponse,
MeltBolt11Request, MeltQuoteBolt11Request, MeltQuoteBolt11Response, MintBolt11Request,
MintBolt11Response, MintInfo, MintQuoteBolt11Request, MintQuoteBolt11Response, RestoreRequest,
RestoreResponse, SwapRequest, SwapResponse,
};
use crate::{Amount, Bolt11Invoice};

/// Http Client
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -115,18 +112,10 @@ impl HttpClientMethods for HttpClient {
async fn post_mint_quote(
&self,
mint_url: MintUrl,
amount: Amount,
unit: CurrencyUnit,
description: Option<String>,
request: MintQuoteBolt11Request,
) -> Result<MintQuoteBolt11Response, Error> {
let url = mint_url.join_paths(&["v1", "mint", "quote", "bolt11"])?;

let request = MintQuoteBolt11Request {
amount,
unit,
description,
};

let res = self
.inner
.post(url)
Expand Down Expand Up @@ -166,20 +155,14 @@ impl HttpClientMethods for HttpClient {
}

/// Mint Tokens [NUT-04]
#[instrument(skip(self, quote, premint_secrets), fields(mint_url = %mint_url))]
#[instrument(skip(self, request), fields(mint_url = %mint_url))]
async fn post_mint(
&self,
mint_url: MintUrl,
quote: &str,
premint_secrets: PreMintSecrets,
request: MintBolt11Request,
) -> Result<MintBolt11Response, Error> {
let url = mint_url.join_paths(&["v1", "mint", "bolt11"])?;

let request = MintBolt11Request {
quote: quote.to_string(),
outputs: premint_secrets.blinded_messages(),
};

let res = self
.inner
.post(url)
Expand All @@ -200,20 +183,10 @@ impl HttpClientMethods for HttpClient {
async fn post_melt_quote(
&self,
mint_url: MintUrl,
unit: CurrencyUnit,
request: Bolt11Invoice,
mpp_amount: Option<Amount>,
request: MeltQuoteBolt11Request,
) -> Result<MeltQuoteBolt11Response, Error> {
let url = mint_url.join_paths(&["v1", "melt", "quote", "bolt11"])?;

let options = mpp_amount.map(|amount| Mpp { amount });

let request = MeltQuoteBolt11Request {
request,
unit,
options,
};

let res = self
.inner
.post(url)
Expand Down Expand Up @@ -248,22 +221,14 @@ impl HttpClientMethods for HttpClient {

/// Melt [NUT-05]
/// [Nut-08] Lightning fee return if outputs defined
#[instrument(skip(self, quote, inputs, outputs), fields(mint_url = %mint_url))]
#[instrument(skip(self, request), fields(mint_url = %mint_url))]
async fn post_melt(
&self,
mint_url: MintUrl,
quote: String,
inputs: Vec<Proof>,
outputs: Option<Vec<BlindedMessage>>,
request: MeltBolt11Request,
) -> Result<MeltQuoteBolt11Response, Error> {
let url = mint_url.join_paths(&["v1", "melt", "bolt11"])?;

let request = MeltBolt11Request {
quote,
inputs,
outputs,
};

let res = self
.inner
.post(url)
Expand All @@ -284,7 +249,7 @@ impl HttpClientMethods for HttpClient {
}
}

/// Split Token [NUT-06]
/// Swap Token [NUT-03]
#[instrument(skip(self, swap_request), fields(mint_url = %mint_url))]
async fn post_swap(
&self,
Expand Down Expand Up @@ -325,14 +290,13 @@ impl HttpClientMethods for HttpClient {
}

/// Spendable check [NUT-07]
#[instrument(skip(self), fields(mint_url = %mint_url))]
#[instrument(skip(self, request), fields(mint_url = %mint_url))]
async fn post_check_state(
&self,
mint_url: MintUrl,
ys: Vec<PublicKey>,
request: CheckStateRequest,
) -> Result<CheckStateResponse, Error> {
let url = mint_url.join_paths(&["v1", "checkstate"])?;
let request = CheckStateRequest { ys };

let res = self
.inner
Expand Down Expand Up @@ -390,9 +354,7 @@ pub trait HttpClientMethods: Debug {
async fn post_mint_quote(
&self,
mint_url: MintUrl,
amount: Amount,
unit: CurrencyUnit,
description: Option<String>,
request: MintQuoteBolt11Request,
) -> Result<MintQuoteBolt11Response, Error>;

/// Mint Quote status
Expand All @@ -406,17 +368,14 @@ pub trait HttpClientMethods: Debug {
async fn post_mint(
&self,
mint_url: MintUrl,
quote: &str,
premint_secrets: PreMintSecrets,
request: MintBolt11Request,
) -> Result<MintBolt11Response, Error>;

/// Melt Quote [NUT-05]
async fn post_melt_quote(
&self,
mint_url: MintUrl,
unit: CurrencyUnit,
request: Bolt11Invoice,
mpp_amount: Option<Amount>,
request: MeltQuoteBolt11Request,
) -> Result<MeltQuoteBolt11Response, Error>;

/// Melt Quote Status
Expand All @@ -431,16 +390,14 @@ pub trait HttpClientMethods: Debug {
async fn post_melt(
&self,
mint_url: MintUrl,
quote: String,
inputs: Vec<Proof>,
outputs: Option<Vec<BlindedMessage>>,
request: MeltBolt11Request,
) -> Result<MeltQuoteBolt11Response, Error>;

/// Split Token [NUT-06]
async fn post_swap(
&self,
mint_url: MintUrl,
swap_request: SwapRequest,
request: SwapRequest,
) -> Result<SwapResponse, Error>;

/// Get Mint Info [NUT-06]
Expand All @@ -450,7 +407,7 @@ pub trait HttpClientMethods: Debug {
async fn post_check_state(
&self,
mint_url: MintUrl,
ys: Vec<PublicKey>,
request: CheckStateRequest,
) -> Result<CheckStateResponse, Error>;

/// Restore request [NUT-13]
Expand Down
27 changes: 17 additions & 10 deletions crates/cdk/src/wallet/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use lightning_invoice::Bolt11Invoice;
use tracing::instrument;

use crate::nuts::nut00::ProofsMethods;
use crate::nuts::{MeltBolt11Request, MeltQuoteBolt11Request, Mpp};
use crate::{
dhke::construct_proofs,
nuts::{CurrencyUnit, MeltQuoteBolt11Response, PreMintSecrets, Proofs, State},
Expand Down Expand Up @@ -57,9 +58,17 @@ impl Wallet {
_ => return Err(Error::UnitUnsupported),
};

let options = mpp.map(|amount| Mpp { amount });

let quote_request = MeltQuoteBolt11Request {
request: Bolt11Invoice::from_str(&request)?,
unit: self.unit,
options,
};

let quote_res = self
.client
.post_melt_quote(self.mint_url.clone(), self.unit, invoice, mpp)
.post_melt_quote(self.mint_url.clone(), quote_request)
.await?;

if quote_res.amount != amount {
Expand Down Expand Up @@ -146,15 +155,13 @@ impl Wallet {
proofs_total - quote_info.amount,
)?;

let melt_response = self
.client
.post_melt(
self.mint_url.clone(),
quote_id.to_string(),
proofs.clone(),
Some(premint_secrets.blinded_messages()),
)
.await;
let request = MeltBolt11Request {
quote: quote_id.to_string(),
inputs: proofs.clone(),
outputs: Some(premint_secrets.blinded_messages()),
};

let melt_response = self.client.post_melt(self.mint_url.clone(), request).await;

let melt_response = match melt_response {
Ok(melt_response) => melt_response,
Expand Down
Loading

0 comments on commit 6b8d225

Please sign in to comment.