Skip to content

Commit

Permalink
test: first mint unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vnprc committed Aug 6, 2024
1 parent 6adf326 commit cd2b07f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ thiserror.workspace = true
futures = { workspace = true, optional = true }
url = "2.3"
uuid.workspace = true
chrono = "0.4"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { workspace = true, features = [
Expand Down
78 changes: 78 additions & 0 deletions crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,3 +1296,81 @@ fn derivation_path_from_unit(unit: CurrencyUnit, index: u32) -> DerivationPath {
ChildNumber::from_hardened_idx(index).expect("0 is a valid index"),
])
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use chrono::{Duration, Utc};
use secp256k1::Secp256k1;

use super::*;

#[test]
fn test_generate_keyset_from_seed() {
let seed = "test_seed".as_bytes();
let secp256k1 = Secp256k1::new();

let info = MintKeySetInfo {
max_order: 5,
unit: CurrencyUnit::Sat,
derivation_path: derivation_path_from_unit(CurrencyUnit::Sat, 0),
// TODO resolve chicken/egg problem: how do we know the ID before the keyset is generated?
id: Id::from_str("009a1f293253e41e").unwrap(),
active: true,
valid_from: Utc::now().timestamp() as u64,
valid_to: Some((Utc::now() + Duration::weeks(1)).timestamp() as u64),
derivation_path_index: None,
input_fee_ppk: 1_u64,
};

// Generate the MintKeySet using the seed
let keyset = MintKeySet::generate_from_seed(&secp256k1, seed, info);

assert_eq!(keyset.unit, CurrencyUnit::Sat);
assert_eq!(keyset.keys.len(), 5);
//TODO assert hard coded pubkeys and amounts
}

use cdk_database::mint_memory::MintMemoryDatabase;

#[tokio::test]
async fn test_new_mint() {
// mock DB settings
let active_keysets: HashMap<CurrencyUnit, Id> = HashMap::new();
let keysets: Vec<MintKeySetInfo> = Vec::new();
let mint_quotes: Vec<MintQuote> = Vec::new();
let melt_quotes: Vec<MeltQuote> = Vec::new();
let pending_proofs: Proofs = Proofs::new();
let spent_proofs: Proofs = Proofs::new();
let blinded_signatures: HashMap<[u8; 33], BlindSignature> = HashMap::new();

// Mock data
let mint_url = "http://example.com";
let seed = b"some_random_seed";
let mint_info = MintInfo::default();
let localstore = Arc::new(
MintMemoryDatabase::new(
active_keysets,
keysets,
mint_quotes,
melt_quotes,
pending_proofs,
spent_proofs,
blinded_signatures,
)
.unwrap(),
);
let supported_units: HashMap<CurrencyUnit, (u64, u8)> = HashMap::new();

// Instantiate a new Mint
let mint = Mint::new(mint_url, seed, mint_info, localstore, supported_units).await;

// Assert the mint was created successfully
assert!(mint.is_ok());
let mint = mint.unwrap();

// Additional assertions can be added here
assert_eq!(mint.get_mint_url().to_string(), "http://example.com");
}
}

0 comments on commit cd2b07f

Please sign in to comment.