Skip to content

Commit 85867b7

Browse files
authored
Fix keygen (#76)
* sync keygen with chain update KAT test * add comment * revert some of the changes * update dilithium crypto version update keys
1 parent 6d58010 commit 85867b7

4 files changed

Lines changed: 23 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ aes-gcm = "0.10" # AES-256-GCM (quantum-safe with 256-bit keys)
5151
# Quantus crypto dependencies (aligned with chain)
5252
qp-rusty-crystals-dilithium = { version = "2.4.0" }
5353
qp-rusty-crystals-hdwallet = { version = "2.3.0" }
54-
qp-dilithium-crypto = { version = "0.2.5", features = ["serde"] }
54+
qp-dilithium-crypto = { version = "0.3.0", features = ["serde"] }
5555
qp-poseidon = { version = "1.4.0" }
5656

5757
# HTTP client for Subsquid queries

src/wallet/keystore.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ impl QuantumKeyPair {
6262
.map_err(|_| WalletError::KeyGeneration.into())
6363
}
6464

65-
#[allow(dead_code)]
6665
pub fn from_resonance_pair(keypair: &DilithiumPair) -> Self {
6766
use sp_core::Pair;
6867
Self {

src/wallet/mod.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ pub mod password;
1010

1111
use crate::error::{Result, WalletError};
1212
pub use keystore::{Keystore, QuantumKeyPair, WalletData};
13-
use qp_rusty_crystals_hdwallet::{derive_key_from_mnemonic, generate_mnemonic, SensitiveBytes32};
13+
use qp_dilithium_crypto::DilithiumPair;
14+
use qp_rusty_crystals_hdwallet::{
15+
derive_key_from_mnemonic, generate_mnemonic, mnemonic_to_seed, SensitiveBytes32,
16+
};
1417
use rand::{rng, RngCore};
1518
use serde::{Deserialize, Serialize};
1619
use sp_runtime::traits::IdentifyAccount;
@@ -75,13 +78,10 @@ impl WalletManager {
7578
.map_err(|_| WalletError::KeyGeneration)?;
7679
let quantum_keypair = QuantumKeyPair::from_dilithium_keypair(&dilithium_keypair);
7780

78-
// Create wallet data
7981
let mut metadata = std::collections::HashMap::new();
8082
metadata.insert("version".to_string(), "1.0.0".to_string());
8183
metadata.insert("algorithm".to_string(), "ML-DSA-87".to_string());
8284
metadata.insert("derivation_path".to_string(), derivation_path.to_string());
83-
84-
// Generate address from public key (simplified version)
8585
let address = quantum_keypair.to_account_id_ss58check();
8686

8787
let wallet_data = WalletData {
@@ -217,15 +217,15 @@ impl WalletManager {
217217
return Err(WalletError::AlreadyExists.into());
218218
}
219219

220-
// Generate new mnemonic and use master seed directly (no derivation path)
221220
let mut seed = [0u8; 32];
222221
rng().fill_bytes(&mut seed);
223222
let sensitive_seed = SensitiveBytes32::from(&mut seed);
224223
let mnemonic = generate_mnemonic(sensitive_seed).map_err(|_| WalletError::KeyGeneration)?;
225-
// For "no derivation" mode, we use the root path m/
226-
let dilithium_keypair = derive_key_from_mnemonic(&mnemonic, None, "m/44'/189189'/0'")
227-
.map_err(|_| WalletError::KeyGeneration)?;
228-
let quantum_keypair = QuantumKeyPair::from_dilithium_keypair(&dilithium_keypair);
224+
let seed64 =
225+
mnemonic_to_seed(mnemonic.clone(), None).map_err(|_| WalletError::KeyGeneration)?;
226+
let dilithium_pair =
227+
DilithiumPair::from_seed(&seed64).map_err(|_| WalletError::KeyGeneration)?;
228+
let quantum_keypair = QuantumKeyPair::from_resonance_pair(&dilithium_pair);
229229

230230
// Create wallet data
231231
let mut metadata = std::collections::HashMap::new();
@@ -271,10 +271,12 @@ impl WalletManager {
271271
return Err(WalletError::AlreadyExists.into());
272272
}
273273

274-
// Use mnemonic to generate keys directly (no derivation path)
275-
let dilithium_keypair = derive_key_from_mnemonic(mnemonic, None, "m/44'/189189'/0'")
274+
// No derivation path - get the seed and create a key from the seed
275+
let seed64 = mnemonic_to_seed(mnemonic.to_string(), None)
276276
.map_err(|_| WalletError::InvalidMnemonic)?;
277-
let quantum_keypair = QuantumKeyPair::from_dilithium_keypair(&dilithium_keypair);
277+
let dilithium_pair =
278+
DilithiumPair::from_seed(&seed64).map_err(|_| WalletError::KeyGeneration)?;
279+
let quantum_keypair = QuantumKeyPair::from_resonance_pair(&dilithium_pair);
278280

279281
// Create wallet data
280282
let mut metadata = std::collections::HashMap::new();
@@ -322,10 +324,9 @@ impl WalletManager {
322324
return Err(WalletError::AlreadyExists.into());
323325
}
324326

325-
// Validate and import from mnemonic using derivation path
326-
let dilithium_keypair = derive_key_from_mnemonic(mnemonic, None, derivation_path)
327+
let dilithium_pair = derive_key_from_mnemonic(mnemonic, None, derivation_path)
327328
.map_err(|_| WalletError::InvalidMnemonic)?;
328-
let quantum_keypair = QuantumKeyPair::from_dilithium_keypair(&dilithium_keypair);
329+
let quantum_keypair = QuantumKeyPair::from_dilithium_keypair(&dilithium_pair);
329330

330331
// Create wallet data
331332
let mut metadata = std::collections::HashMap::new();
@@ -773,8 +774,8 @@ mod tests {
773774

774775
let (wallet_manager, _temp_dir) = create_test_wallet_manager().await;
775776
let test_mnemonic = "orchard answer curve patient visual flower maze noise retreat penalty cage small earth domain scan pitch bottom crunch theme club client swap slice raven";
776-
let expected_address = "qzpJj8HRv7m9Ur9fYRsGFMggz4FLtMQEEi1rTPsKQZJUttjkV";
777-
let expected_address_no_derive = "qzpFin6y47r9MDdH2cxhGDENagYKVTn7Fkvy7XRJh7f7L9qvJ";
777+
let expected_address_no_derive = "qzmTAz3UUw1WGUuVh8nbFmPwcftomduwy6twq6NDR6y9qqtEs";
778+
let expected_address_hd_0 = "qzm5QCox8Dp5A3oSXZZYHD8YoYgPz7enykZb6RPUropdCyN5h";
778779

779780
let imported_wallet = wallet_manager
780781
.import_wallet("imported-test-wallet", test_mnemonic, Some("import-password"))
@@ -790,7 +791,7 @@ mod tests {
790791
.await
791792
.expect("Failed to import wallet");
792793

793-
assert_eq!(imported_wallet.address, expected_address, "address at index 0 is wrong");
794+
assert_eq!(imported_wallet.address, expected_address_hd_0, "address at index 0 is wrong");
794795
assert_eq!(
795796
imported_wallet_no_derive.address, expected_address_no_derive,
796797
"no-derivation address is wrong"

0 commit comments

Comments
 (0)