feat: rotate next keyset

This commit is contained in:
thesimplekid
2025-02-02 14:30:59 +00:00
parent 65e07e37f1
commit df8291dc04
3 changed files with 50 additions and 15 deletions

View File

@@ -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?;

View File

@@ -96,8 +96,8 @@ impl Mint {
derivation_path_index: u32,
max_order: u8,
input_fee_ppk: u64,
custom_paths: HashMap<CurrencyUnit, DerivationPath>,
) -> Result<(), Error> {
custom_paths: &HashMap<CurrencyUnit, DerivationPath>,
) -> Result<MintKeySetInfo, Error> {
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<MintKeySetInfo, Error> {
let current_keyset_id = self
.localstore
.get_active_keyset_id(&unit)
.await?
.ok_or(Error::UnsupportedUnit)?;
let keyset_info = self
.localstore
.get_keyset_info(&current_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

View File

@@ -48,6 +48,7 @@ pub struct Mint {
secp_ctx: Secp256k1<secp256k1::All>,
xpriv: Xpriv,
keysets: Arc<RwLock<HashMap<Id, MintKeySet>>>,
custom_paths: HashMap<CurrencyUnit, DerivationPath>,
}
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();