Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/platform/identity/load_identity.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::context::AppContext;
use crate::model::qualified_identity::EncryptedPrivateKeyTarget::{
PrivateKeyOnMainIdentity, PrivateKeyOnVoterIdentity,
self, PrivateKeyOnMainIdentity, PrivateKeyOnVoterIdentity,
};
use crate::model::qualified_identity::{IdentityType, QualifiedIdentity};
use crate::platform::identity::{verify_key_input, IdentityInputToLoad};
use dash_sdk::dashcore_rpc::dashcore::key::Secp256k1;
use dash_sdk::dashcore_rpc::dashcore::PrivateKey;
use dash_sdk::dpp::identifier::MasternodeIdentifiers;
use dash_sdk::dpp::identity::accessors::IdentityGettersV0;
use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
use dash_sdk::dpp::platform_value::string_encoding::Encoding;
use dash_sdk::platform::{Fetch, Identifier, Identity};
Expand All @@ -26,7 +27,7 @@ impl AppContext {
alias_input,
owner_private_key_input,
payout_address_private_key_input,
keys_input: _,
keys_input,
} = input;

// Verify the voting private key
Expand Down Expand Up @@ -114,6 +115,29 @@ impl AppContext {
None
};

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()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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

};
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),
);
}
}
Comment on lines +118 to +139

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Suggested change
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),
);
}
}


let qualified_identity = QualifiedIdentity {
identity,
associated_voter_identity,
Expand Down
2 changes: 1 addition & 1 deletion src/ui/identities/add_existing_identity_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl AddExistingIdentityScreen {
// For User, show multiple key inputs
for (i, key) in self.keys_input.iter_mut().enumerate() {
ui.horizontal(|ui| {
ui.label(format!("Key {}:", i + 1));
ui.label(format!("Private Key {} (Hex or WIF):", i + 1));
ui.text_edit_singleline(key);
if ui.button("-").clicked() {
keys_to_remove.push(i);
Expand Down