From df8291dc04cff69ea3140ab0a2b1c09e1675123a Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 2 Feb 2025 14:30:59 +0000 Subject: [PATCH] feat: rotate next keyset --- crates/cdk-integration-tests/tests/mint.rs | 10 +---- crates/cdk/src/mint/keysets.rs | 47 ++++++++++++++++++++-- crates/cdk/src/mint/mod.rs | 8 ++-- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/crates/cdk-integration-tests/tests/mint.rs b/crates/cdk-integration-tests/tests/mint.rs index e4dbcfbf..265c031b 100644 --- a/crates/cdk-integration-tests/tests/mint.rs +++ b/crates/cdk-integration-tests/tests/mint.rs @@ -17,7 +17,6 @@ use cdk::nuts::{ PreMintSecrets, ProofState, Proofs, SecretKey, SpendingConditions, State, SwapRequest, }; use cdk::subscription::{IndexableParams, Params}; -use cdk::types::QuoteTTL; use cdk::util::unix_time; use cdk::Mint; use cdk_fake_wallet::FakeWallet; @@ -337,7 +336,7 @@ async fn test_swap_unbalanced() -> Result<()> { async fn test_swap_overpay_underpay_fee() -> Result<()> { let mint = new_mint(1).await; - mint.rotate_keyset(CurrencyUnit::Sat, 1, 32, 1, HashMap::new()) + mint.rotate_keyset(CurrencyUnit::Sat, 1, 32, 1, &HashMap::new()) .await?; let keys = mint.pubkeys().await?.keysets.first().unwrap().clone().keys; @@ -466,16 +465,11 @@ async fn test_correct_keyset() -> Result<()> { mint_builder = mint_builder .with_name("regtest mint".to_string()) .with_description("regtest mint".to_string()) + .with_quote_ttl(10000, 1000) .with_seed(mnemonic.to_seed_normalized("").to_vec()); let mint = mint_builder.build().await?; - localstore - .set_mint_info(mint_builder.mint_info.clone()) - .await?; - let quote_ttl = QuoteTTL::new(10000, 10000); - localstore.set_quote_ttl(quote_ttl).await?; - mint.rotate_next_keyset(CurrencyUnit::Sat, 32, 0).await?; mint.rotate_next_keyset(CurrencyUnit::Sat, 32, 0).await?; diff --git a/crates/cdk/src/mint/keysets.rs b/crates/cdk/src/mint/keysets.rs index c099381c..7dd96bee 100644 --- a/crates/cdk/src/mint/keysets.rs +++ b/crates/cdk/src/mint/keysets.rs @@ -96,8 +96,8 @@ impl Mint { derivation_path_index: u32, max_order: u8, input_fee_ppk: u64, - custom_paths: HashMap, - ) -> Result<(), Error> { + custom_paths: &HashMap, + ) -> Result { let derivation_path = match custom_paths.get(&unit) { Some(path) => path.clone(), None => derivation_path_from_unit(unit.clone(), derivation_path_index) @@ -114,13 +114,52 @@ impl Mint { input_fee_ppk, ); let id = keyset_info.id; - self.localstore.add_keyset_info(keyset_info).await?; + self.localstore.add_keyset_info(keyset_info.clone()).await?; self.localstore.set_active_keyset(unit, id).await?; let mut keysets = self.keysets.write().await; keysets.insert(id, keyset); - Ok(()) + Ok(keyset_info) + } + + /// Rotate to next keyset for unit + #[instrument(skip(self))] + pub async fn rotate_next_keyset( + &self, + unit: CurrencyUnit, + max_order: u8, + input_fee_ppk: u64, + ) -> Result { + let current_keyset_id = self + .localstore + .get_active_keyset_id(&unit) + .await? + .ok_or(Error::UnsupportedUnit)?; + + let keyset_info = self + .localstore + .get_keyset_info(¤t_keyset_id) + .await? + .ok_or(Error::UnknownKeySet)?; + + tracing::debug!( + "Current active keyset {} path index {:?}", + keyset_info.id, + keyset_info.derivation_path_index + ); + + let keyset_info = self + .rotate_keyset( + unit, + keyset_info.derivation_path_index.unwrap_or(1) + 1, + max_order, + input_fee_ppk, + &self.custom_paths, + ) + .await?; + + Ok(keyset_info) } /// Ensure Keyset is loaded in mint diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index a41a3c72..64417ba6 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -48,6 +48,7 @@ pub struct Mint { secp_ctx: Secp256k1, xpriv: Xpriv, keysets: Arc>>, + custom_paths: HashMap, } impl Mint { @@ -184,6 +185,7 @@ impl Mint { localstore, ln, keysets, + custom_paths, }) } @@ -745,7 +747,7 @@ mod tests { assert!(keysets.keysets.is_empty()); // generate the first keyset and set it to active - mint.rotate_keyset(CurrencyUnit::default(), 0, 1, 1, HashMap::new()) + mint.rotate_keyset(CurrencyUnit::default(), 0, 1, 1, &HashMap::new()) .await?; let keysets = mint.keysets().await.unwrap(); @@ -754,7 +756,7 @@ mod tests { let first_keyset_id = keysets.keysets[0].id; // set the first keyset to inactive and generate a new keyset - mint.rotate_keyset(CurrencyUnit::default(), 1, 1, 1, HashMap::new()) + mint.rotate_keyset(CurrencyUnit::default(), 1, 1, 1, &HashMap::new()) .await?; let keysets = mint.keysets().await.unwrap(); @@ -786,7 +788,7 @@ mod tests { }; let mint = create_mint(config).await?; - mint.rotate_keyset(CurrencyUnit::default(), 0, 32, 1, HashMap::new()) + mint.rotate_keyset(CurrencyUnit::default(), 0, 32, 1, &HashMap::new()) .await?; let keys = mint.keysets.read().await.clone();