Skip to content

Commit

Permalink
feat: always sign mint quote
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Dec 16, 2024
1 parent 836d95d commit 4051e49
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 90 deletions.
6 changes: 2 additions & 4 deletions crates/cdk-cli/src/sub_commands/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub async fn mint(
};

let quote = wallet
.mint_quote(Amount::from(sub_command_args.amount), description, None)
.mint_quote(Amount::from(sub_command_args.amount), description)
.await?;

println!("Quote: {:#?}", quote);
Expand All @@ -71,9 +71,7 @@ pub async fn mint(
}
}

let receive_amount = wallet
.mint(&quote.id, SplitTarget::default(), None, None)
.await?;
let receive_amount = wallet.mint(&quote.id, SplitTarget::default(), None).await?;

println!("Received {receive_amount} from mint {mint_url}");

Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub async fn wallet_mint(
split_target: SplitTarget,
description: Option<String>,
) -> Result<()> {
let quote = wallet.mint_quote(amount, description, None).await?;
let quote = wallet.mint_quote(amount, description).await?;

let mut subscription = wallet
.subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
Expand All @@ -144,7 +144,7 @@ pub async fn wallet_mint(
}
}

let receive_amount = wallet.mint(&quote.id, split_target, None, None).await?;
let receive_amount = wallet.mint(&quote.id, split_target, None).await?;

println!("Minted: {}", receive_amount);

Expand Down
55 changes: 24 additions & 31 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cdk::amount::SplitTarget;
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::nuts::{
CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintQuoteState, NotificationPayload,
PreMintSecrets, SecretKey, State,
PreMintSecrets, State,
};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::{Wallet, WalletSubscription};
Expand All @@ -26,12 +26,12 @@ async fn test_fake_tokens_pending() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

let fake_description = FakeInvoiceDescription {
Expand Down Expand Up @@ -66,12 +66,12 @@ async fn test_fake_melt_payment_fail() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

let fake_description = FakeInvoiceDescription {
Expand Down Expand Up @@ -129,12 +129,12 @@ async fn test_fake_melt_payment_fail_and_check() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

let fake_description = FakeInvoiceDescription {
Expand Down Expand Up @@ -174,12 +174,12 @@ async fn test_fake_melt_payment_return_fail_status() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

let fake_description = FakeInvoiceDescription {
Expand Down Expand Up @@ -234,12 +234,12 @@ async fn test_fake_melt_payment_error_unknown() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

let fake_description = FakeInvoiceDescription {
Expand Down Expand Up @@ -295,12 +295,12 @@ async fn test_fake_melt_payment_err_paid() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

let fake_description = FakeInvoiceDescription {
Expand Down Expand Up @@ -333,12 +333,12 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

let fake_description = FakeInvoiceDescription::default();
Expand Down Expand Up @@ -385,22 +385,20 @@ async fn test_fake_mint_with_witness() -> Result<()> {
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
let secret = SecretKey::generate();
let mint_quote = wallet
.mint_quote(100.into(), None, Some(secret.public_key()))
.await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, Some(secret))
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

assert!(mint_amount == 100.into());

Ok(())
}

// TODO: Rewrite this test to not include witness
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fake_mint_without_witness() -> Result<()> {
let wallet = Wallet::new(
Expand All @@ -411,15 +409,12 @@ async fn test_fake_mint_without_witness() -> Result<()> {
None,
)?;

let secret = SecretKey::generate();
let mint_quote = wallet
.mint_quote(100.into(), None, Some(secret.public_key()))
.await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await;

match mint_amount {
Expand All @@ -428,6 +423,7 @@ async fn test_fake_mint_without_witness() -> Result<()> {
}
}

// TODO: Rewrite this test to include witness wrong
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fake_mint_with_wrong_witness() -> Result<()> {
let wallet = Wallet::new(
Expand All @@ -437,16 +433,13 @@ async fn test_fake_mint_with_wrong_witness() -> Result<()> {
&Mnemonic::generate(12)?.to_seed_normalized(""),
None,
)?;
let secret = SecretKey::generate();
let mint_quote = wallet
.mint_quote(100.into(), None, Some(secret.public_key()))
.await?;

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
let secret = SecretKey::generate();

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, Some(secret))
.mint(&mint_quote.id, SplitTarget::default(), None)
.await;

match mint_amount {
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk-integration-tests/tests/integration_tests_pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ mod integration_tests_pure {
/// amount is minted.
async fn receive(wallet: Arc<Wallet>, amount: u64) -> anyhow::Result<Amount> {
let desired_amount = Amount::from(amount);
let quote = wallet.mint_quote(desired_amount, None, None).await?;
let quote = wallet.mint_quote(desired_amount, None).await?;

loop {
let status = wallet.mint_quote_state(&quote.id).await?;
Expand All @@ -216,7 +216,7 @@ mod integration_tests_pure {
}

wallet
.mint(&quote.id, SplitTarget::default(), None, None)
.mint(&quote.id, SplitTarget::default(), None)
.await
.map_err(Into::into)
}
Expand Down
32 changes: 18 additions & 14 deletions crates/cdk-integration-tests/tests/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ async fn test_regtest_mint_melt_round_trip() -> Result<()> {
.expect("Failed to connect");
let (mut write, mut reader) = ws_stream.split();

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

lnd_client.pay_invoice(mint_quote.request).await?;

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

assert!(mint_amount == 100.into());
Expand Down Expand Up @@ -152,14 +152,14 @@ async fn test_regtest_mint_melt() -> Result<()> {

let mint_amount = Amount::from(100);

let mint_quote = wallet.mint_quote(mint_amount, None, None).await?;
let mint_quote = wallet.mint_quote(mint_amount, None).await?;

assert_eq!(mint_quote.amount, mint_amount);

lnd_client.pay_invoice(mint_quote.request).await?;

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

assert!(mint_amount == 100.into());
Expand All @@ -180,12 +180,12 @@ async fn test_restore() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

lnd_client.pay_invoice(mint_quote.request).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

assert!(wallet.total_balance().await? == 100.into());
Expand Down Expand Up @@ -236,12 +236,12 @@ async fn test_pay_invoice_twice() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

lnd_client.pay_invoice(mint_quote.request).await?;

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

assert_eq!(mint_amount, 100.into());
Expand Down Expand Up @@ -288,12 +288,12 @@ async fn test_internal_payment() -> Result<()> {
None,
)?;

let mint_quote = wallet.mint_quote(100.into(), None, None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

lnd_client.pay_invoice(mint_quote.request).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;

assert!(wallet.total_balance().await? == 100.into());
Expand All @@ -308,7 +308,7 @@ async fn test_internal_payment() -> Result<()> {
None,
)?;

let mint_quote = wallet_2.mint_quote(10.into(), None, None).await?;
let mint_quote = wallet_2.mint_quote(10.into(), None).await?;

let melt = wallet.melt_quote(mint_quote.request.clone(), None).await?;

Expand All @@ -317,7 +317,7 @@ async fn test_internal_payment() -> Result<()> {
let _melted = wallet.melt(&melt.id).await.unwrap();

let _wallet_2_mint = wallet_2
.mint(&mint_quote.id, SplitTarget::default(), None, None)
.mint(&mint_quote.id, SplitTarget::default(), None)
.await
.unwrap();

Expand Down Expand Up @@ -359,7 +359,7 @@ async fn test_cached_mint() -> Result<()> {

let mint_amount = Amount::from(100);

let quote = wallet.mint_quote(mint_amount, None, None).await?;
let quote = wallet.mint_quote(mint_amount, None).await?;
lnd_client.pay_invoice(quote.request).await?;

let mut subscription = wallet
Expand All @@ -381,12 +381,16 @@ async fn test_cached_mint() -> Result<()> {
let premint_secrets =
PreMintSecrets::random(active_keyset_id, 31.into(), &SplitTarget::default()).unwrap();

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

let secret_key = quote.secret_key;

request.sign(secret_key.expect("Secret key on quote"))?;

let response = http_client.post_mint(request.clone()).await?;
let response1 = http_client.post_mint(request).await?;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE mint_quote ADD secret_key TEXT;
Loading

0 comments on commit 4051e49

Please sign in to comment.