Skip to content

Commit

Permalink
Embed mint_url as a field of HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Nov 16, 2024
1 parent 4e7f55e commit f506e6d
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 191 deletions.
7 changes: 4 additions & 3 deletions crates/cdk-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,17 @@ async fn main() -> Result<()> {

let mints = localstore.get_mints().await?;

for (mint, _) in mints {
for (mint_url, _) in mints {
let mut wallet = Wallet::new(
&mint.to_string(),
&mint_url.to_string(),
cdk::nuts::CurrencyUnit::Sat,
localstore.clone(),
&mnemonic.to_seed_normalized(""),
None,
)?;
if let Some(proxy_url) = args.proxy.as_ref() {
wallet.set_client(HttpClient::with_proxy(proxy_url.clone(), None, true)?);
let http_client = HttpClient::with_proxy(mint_url, proxy_url.clone(), None, true)?;
wallet.set_client(Arc::from(http_client));
}

wallets.push(wallet);
Expand Down
11 changes: 5 additions & 6 deletions crates/cdk-cli/src/sub_commands/mint_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use cdk::mint_url::MintUrl;
use cdk::wallet::client::HttpClientMethods;
use cdk::wallet::client::MintConnector;
use cdk::HttpClient;
use clap::Args;
use url::Url;
Expand All @@ -11,14 +11,13 @@ pub struct MintInfoSubcommand {
}

pub async fn mint_info(proxy: Option<Url>, sub_command_args: &MintInfoSubcommand) -> Result<()> {
let mint_url = sub_command_args.mint_url.clone();
let client = match proxy {
Some(proxy) => HttpClient::with_proxy(proxy, None, true)?,
None => HttpClient::new(),
Some(proxy) => HttpClient::with_proxy(mint_url, proxy, None, true)?,
None => HttpClient::new(mint_url),
};

let info = client
.get_mint_info(sub_command_args.mint_url.clone())
.await?;
let info = client.get_mint_info().await?;

println!("{:#?}", info);

Expand Down
14 changes: 7 additions & 7 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;

Expand All @@ -10,12 +11,13 @@ use cdk::cdk_database::mint_memory::MintMemoryDatabase;
use cdk::cdk_lightning::MintLightning;
use cdk::dhke::construct_proofs;
use cdk::mint::FeeReserve;
use cdk::mint_url::MintUrl;
use cdk::nuts::{
CurrencyUnit, Id, KeySet, MintBolt11Request, MintInfo, MintQuoteBolt11Request, MintQuoteState,
Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
};
use cdk::types::{LnKey, QuoteTTL};
use cdk::wallet::client::{HttpClient, HttpClientMethods};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::{Mint, Wallet};
use cdk_fake_wallet::FakeWallet;
use init_regtest::{get_mint_addr, get_mint_port, get_mint_url};
Expand Down Expand Up @@ -155,23 +157,21 @@ pub async fn mint_proofs(
println!("Minting for ecash");
println!();

let wallet_client = HttpClient::new();
let wallet_client = HttpClient::new(MintUrl::from_str(mint_url)?);

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

let mint_quote = wallet_client
.post_mint_quote(mint_url.parse()?, request)
.await?;
let mint_quote = wallet_client.post_mint_quote(request).await?;

println!("Please pay: {}", mint_quote.request);

loop {
let status = wallet_client
.get_mint_quote_status(mint_url.parse()?, &mint_quote.quote)
.get_mint_quote_status(&mint_quote.quote)
.await?;

if status.state == MintQuoteState::Paid {
Expand All @@ -189,7 +189,7 @@ pub async fn mint_proofs(
outputs: premint_secrets.blinded_messages(),
};

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

let pre_swap_proofs = construct_proofs(
mint_response.signatures,
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cdk::cdk_database::WalletMemoryDatabase;
use cdk::nuts::{
CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintQuoteState, PreMintSecrets, State,
};
use cdk::wallet::client::{HttpClient, HttpClientMethods};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::Wallet;
use cdk_fake_wallet::{create_fake_invoice, FakeInvoiceDescription};
use cdk_integration_tests::attempt_to_swap_pending;
Expand Down Expand Up @@ -354,15 +354,15 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {

let premint_secrets = PreMintSecrets::random(keyset.id, 100.into(), &SplitTarget::default())?;

let client = HttpClient::new();
let client = HttpClient::new(MINT_URL.parse()?);

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?;
let melt_response = client.post_melt(melt_request).await?;

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

Expand Down
12 changes: 4 additions & 8 deletions crates/cdk-integration-tests/tests/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cdk::nuts::{
CurrencyUnit, MeltQuoteState, MintBolt11Request, MintQuoteState, NotificationPayload,
PreMintSecrets, State,
};
use cdk::wallet::client::{HttpClient, HttpClientMethods};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::Wallet;
use cdk_integration_tests::init_regtest::{
get_mint_url, get_mint_ws_url, init_cln_client, init_lnd_client,
Expand Down Expand Up @@ -374,7 +374,7 @@ async fn test_cached_mint() -> Result<()> {
}

let active_keyset_id = wallet.get_active_mint_keyset().await?.id;
let http_client = HttpClient::new();
let http_client = HttpClient::new(get_mint_url().as_str().parse()?);
let premint_secrets =
PreMintSecrets::random(active_keyset_id, 31.into(), &SplitTarget::default()).unwrap();

Expand All @@ -383,12 +383,8 @@ async fn test_cached_mint() -> Result<()> {
outputs: premint_secrets.blinded_messages(),
};

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

assert!(response == response1);
Ok(())
Expand Down
Loading

0 comments on commit f506e6d

Please sign in to comment.