fix: load private keys when loading a user identity#12
Conversation
WalkthroughThe changes in this pull request involve updates to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
src/ui/identities/add_existing_identity_screen.rs (1)
Line range hint
228-258: LGTM: Added methods for populating fields with random testnet data.The
fill_random_hpmnandfill_random_masternodemethods are great additions for testing purposes. They allow quick population of fields with valid data from testnet nodes.However, consider adding error handling in case the chosen node doesn't have all required fields. This would prevent potential panics or unexpected behavior.
Here's a suggestion for improving error handling in
fill_random_hpmn:fn fill_random_hpmn(&mut self) { if let Some(testnet_nodes) = &self.testnet_loaded_nodes { if let Some((name, hpmn)) = testnet_nodes.hp_masternodes.iter().choose(&mut thread_rng()) { self.identity_id_input = hpmn.protx_tx_hash.clone(); self.identity_type = IdentityType::Evonode; self.alias_input = name.clone(); self.voting_private_key_input = hpmn.voter.private_key.clone(); self.owner_private_key_input = hpmn.owner.private_key.clone(); self.payout_address_private_key_input = hpmn.payout.private_key.clone(); } else { // Handle the case where no HPMN is available println!("No HPMN available in testnet nodes"); } } else { // Handle the case where testnet nodes are not loaded println!("Testnet nodes not loaded"); } }Apply similar changes to
fill_random_masternodeas well.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- src/platform/identity/load_identity.rs (3 hunks)
- src/ui/identities/add_existing_identity_screen.rs (1 hunks)
🧰 Additional context used
🔇 Additional comments (5)
src/ui/identities/add_existing_identity_screen.rs (2)
162-162: LGTM: Improved label clarity for key input.The change from "Key" to "Private Key" enhances user understanding by clearly specifying the expected input type. This aligns well with the PR's objective of fixing private key loading.
Line range hint
270-346: LGTM: Enhanced UI with testing utilities and improved loading feedback.The additions to the
uimethod are well-implemented and provide valuable improvements:
- The new buttons for filling random HPMN and Masternode data enhance testing efficiency.
- The enhanced loading status display provides better user feedback, showing elapsed time in a user-friendly format.
These changes align well with the PR's objectives and improve overall user experience.
src/platform/identity/load_identity.rs (3)
3-3: Approved: ImportingPrivateKeyOnMainIdentityandPrivateKeyOnVoterIdentityThe imported variants are necessary for handling encrypted private keys associated with main and voter identities.
10-10: Approved: ImportingIdentityGettersV0Importing
IdentityGettersV0provides access to essential getter methods for identity operations.
30-30: Approved: Includingkeys_inputin parameter destructuringAdding
keys_inputto the input parameters allows the function to handle user identity keys effectively.
| let key_id = i as u32; | ||
| let public_key = match identity.public_keys().get(&key_id) { | ||
| Some(key) => key, | ||
| None => return Err("No public key matching key id {key_id}".to_string()), |
There was a problem hiding this comment.
Correct error messages to include actual key_id values
The error messages use {key_id} as a placeholder, but without variable interpolation, it will not display the actual key_id value.
Update the error messages using format! to properly include the key_id:
- None => return Err("No public key matching key id {key_id}".to_string()),
+ None => return Err(format!("No public key matching key ID {}", key_id)),
...
- return Err("Private key input length is 0 for key id {key_id}".to_string())
+ return Err(format!("Private key input length is 0 for key ID {}", key_id))Also applies to: 131-131
| if identity_type == IdentityType::User { | ||
| for (i, private_key_input) in keys_input.into_iter().enumerate() { | ||
| let key_id = i as u32; | ||
| let public_key = match identity.public_keys().get(&key_id) { | ||
| Some(key) => key, | ||
| None => return Err("No public key matching key id {key_id}".to_string()), | ||
| }; | ||
| let private_key_bytes = match verify_key_input( | ||
| private_key_input, | ||
| &public_key.key_type().to_string(), | ||
| )? { | ||
| Some(bytes) => bytes, | ||
| None => { | ||
| return Err("Private key input length is 0 for key id {key_id}".to_string()) | ||
| } | ||
| }; | ||
| encrypted_private_keys.insert( | ||
| (EncryptedPrivateKeyTarget::PrivateKeyOnMainIdentity, key_id), | ||
| (public_key.clone(), private_key_bytes), | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Ensure correct mapping between keys_input and identity public keys
The current implementation assumes that the key IDs are sequential and match the indices of keys_input. This may not always be the case if the identity has non-sequential or custom key IDs, leading to potential mismatches.
Consider modifying the code to explicitly map each provided private key to its corresponding key ID. This ensures accurate association between private keys and their respective public keys.
Proposed changes:
- for (i, private_key_input) in keys_input.into_iter().enumerate() {
- let key_id = i as u32;
+ for (key_id_str, private_key_input) in keys_input {
+ let key_id = key_id_str.parse::<u32>().map_err(|e| format!("Invalid key ID: {}", e))?;
let public_key = match identity.public_keys().get(&key_id) {
Some(key) => key,
None => return Err(format!("No public key matching key ID {}", key_id)),
};
let private_key_bytes = match verify_key_input(
private_key_input,
&public_key.key_type().to_string(),
)? {
Some(bytes) => bytes,
None => {
- return Err("Private key input length is 0 for key id {key_id}".to_string())
+ return Err(format!("Private key input length is 0 for key ID {}", key_id))
}
};
encrypted_private_keys.insert(
(EncryptedPrivateKeyTarget::PrivateKeyOnMainIdentity, key_id),
(public_key.clone(), private_key_bytes),
);
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if identity_type == IdentityType::User { | |
| for (i, private_key_input) in keys_input.into_iter().enumerate() { | |
| let key_id = i as u32; | |
| let public_key = match identity.public_keys().get(&key_id) { | |
| Some(key) => key, | |
| None => return Err("No public key matching key id {key_id}".to_string()), | |
| }; | |
| let private_key_bytes = match verify_key_input( | |
| private_key_input, | |
| &public_key.key_type().to_string(), | |
| )? { | |
| Some(bytes) => bytes, | |
| None => { | |
| return Err("Private key input length is 0 for key id {key_id}".to_string()) | |
| } | |
| }; | |
| encrypted_private_keys.insert( | |
| (EncryptedPrivateKeyTarget::PrivateKeyOnMainIdentity, key_id), | |
| (public_key.clone(), private_key_bytes), | |
| ); | |
| } | |
| } | |
| if identity_type == IdentityType::User { | |
| for (key_id_str, private_key_input) in keys_input { | |
| let key_id = key_id_str.parse::<u32>().map_err(|e| format!("Invalid key ID: {}", e))?; | |
| let public_key = match identity.public_keys().get(&key_id) { | |
| Some(key) => key, | |
| None => return Err(format!("No public key matching key ID {}", key_id)), | |
| }; | |
| let private_key_bytes = match verify_key_input( | |
| private_key_input, | |
| &public_key.key_type().to_string(), | |
| )? { | |
| Some(bytes) => bytes, | |
| None => { | |
| return Err(format!("Private key input length is 0 for key ID {}", key_id)) | |
| } | |
| }; | |
| encrypted_private_keys.insert( | |
| (EncryptedPrivateKeyTarget::PrivateKeyOnMainIdentity, key_id), | |
| (public_key.clone(), private_key_bytes), | |
| ); | |
| } | |
| } |
#12: Replace identities.as_ref().unwrap() with if-let-Some guard in the flush_inner identity top-level drops logging block. #13: Fix 5 stale doc comments: - Module header: updated scope from "Phase 9b" to current coverage (core, asset_locks, contacts, profiles, payments) - flush_inner: says "writes core + identity subset + asset locks + contacts" instead of "only core" - write_identity_dashpay_subset: removed duplicate first line - test_load_returns_empty_changeset: removed "no-op" description #14: Log dropped dashpay_profiles and dashpay_payments_overlay overlay fields in flush_inner for consistency with the platform_addresses and token_balances drop gates. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Loading the private keys when adding a new user identity was not implemented
Summary by CodeRabbit
New Features
Bug Fixes