Skip to content

Commit

Permalink
fix: itests
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Dec 16, 2024
1 parent e8bc365 commit b2ce729
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
59 changes: 42 additions & 17 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use bip39::Mnemonic;
use cdk::amount::SplitTarget;
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::nuts::{
CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintQuoteState, NotificationPayload,
PreMintSecrets, State,
CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintBolt11Request, MintQuoteState,
NotificationPayload, PreMintSecrets, SecretKey, State,
};
use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::{Wallet, WalletSubscription};
Expand Down Expand Up @@ -398,7 +398,6 @@ async fn test_fake_mint_with_witness() -> Result<()> {
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 @@ -413,13 +412,25 @@ async fn test_fake_mint_without_witness() -> Result<()> {

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

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await;
let http_client = HttpClient::new(MINT_URL.parse()?);

let active_keyset_id = wallet.get_active_mint_keyset().await?.id;

let premint_secrets =
PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::default()).unwrap();

match mint_amount {
Err(cdk::error::Error::SecretKeyNotProvided) => Ok(()),
_ => bail!("Wrong mint response for minting without witness"),
let request = MintBolt11Request {
quote: mint_quote.id,
outputs: premint_secrets.blinded_messages(),
signature: None,
};

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

match response {
Err(cdk::error::Error::SignatureMissingOrInvalid) => Ok(()),
Err(err) => bail!("Wrong mint response for minting without witness: {}", err),
Ok(_) => bail!("Minting should not have succeed without a witness"),
}
}

Expand All @@ -438,15 +449,29 @@ async fn test_fake_mint_with_wrong_witness() -> Result<()> {

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

let mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await;
let http_client = HttpClient::new(MINT_URL.parse()?);

match mint_amount {
Err(cdk::error::Error::IncorrectSecretKey) => Ok(()),
_ => {
bail!("Wrong mint response for minting without witness")
}
let active_keyset_id = wallet.get_active_mint_keyset().await?.id;

let premint_secrets =
PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::default()).unwrap();

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

let secret_key = SecretKey::generate();

request.sign(secret_key)?;

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

match response {
Err(cdk::error::Error::SignatureMissingOrInvalid) => Ok(()),
Err(err) => bail!("Wrong mint response for minting without witness: {}", err),
Ok(_) => bail!("Minting should not have succeed without a witness"),
}
}

Expand Down
10 changes: 4 additions & 6 deletions crates/cdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum Error {
/// Amount overflow
#[error("Amount Overflow")]
AmountOverflow,
/// Witness missing or invalid
#[error("Signature missing or invalid")]
SignatureMissingOrInvalid,

// Mint Errors
/// Minting is disabled
Expand Down Expand Up @@ -162,12 +165,6 @@ pub enum Error {
/// Invoice Description not supported
#[error("Invoice Description not supported")]
InvoiceDescriptionUnsupported,
/// Secretkey to sign mint quote not provided
#[error("Secretkey to sign mint quote not provided")]
SecretKeyNotProvided,
/// Incorrect secret key provided
#[error("Incorrect secretkey provided")]
IncorrectSecretKey,
/// Custom Error
#[error("`{0}`")]
Custom(String),
Expand Down Expand Up @@ -416,6 +413,7 @@ impl From<ErrorResponse> for Error {
Self::AmountOutofLimitRange(Amount::default(), Amount::default(), Amount::default())
}
ErrorCode::TokenPending => Self::TokenPending,
ErrorCode::WitnessMissingOrInvalid => Self::SignatureMissingOrInvalid,
_ => Self::UnknownErrorResponse(err.to_string()),
}
}
Expand Down

0 comments on commit b2ce729

Please sign in to comment.