Skip to content

Commit

Permalink
Add ProfileConfig deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostWalker562 committed Oct 28, 2024
1 parent a57bdfc commit e0380d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
9 changes: 5 additions & 4 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use crate::{
init::Network,
local_simulation,
utils::{
check_if_file_exists, create_dir_if_not_exist, dir_default_to_current,
get_account_with_state, get_auth_key, get_sequence_number, parse_json_file,
prompt_yes_with_override, read_from_file, start_logger, to_common_result,
to_common_success_result, write_to_file, write_to_file_with_opts,
check_if_file_exists, create_dir_if_not_exist, deserialize_private_key_with_prefix,
dir_default_to_current, get_account_with_state, get_auth_key, get_sequence_number,
parse_json_file, prompt_yes_with_override, read_from_file, start_logger,
to_common_result, to_common_success_result, write_to_file, write_to_file_with_opts,
write_to_user_only_file,
},
},
Expand Down Expand Up @@ -247,6 +247,7 @@ pub struct ProfileConfig {
pub network: Option<Network>,
/// Private key for commands.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(deserialize_with = "deserialize_private_key_with_prefix")]
pub private_key: Option<Ed25519PrivateKey>,
/// Public key for commands
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
41 changes: 35 additions & 6 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use aptos_types::{
use itertools::Itertools;
use move_core_types::{account_address::AccountAddress, language_storage::CORE_CODE_ADDRESS};
use reqwest::Url;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use std::{
Expand Down Expand Up @@ -602,10 +602,39 @@ pub fn explorer_transaction_link(
///
/// [Read about AIP-80](https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-80.md)
pub fn strip_private_key_prefix(key: &String) -> String {
let prefixes = ["ed25519-priv-", "secp256k1-priv-"];
prefixes
.iter()
.find_map(|prefix| key.strip_prefix(prefix))
.unwrap_or(key) // If no prefix is found, return the original key
key.strip_prefix("ed25519-priv-")
.unwrap_or(key) // If the prefix is not found, return the original key
.to_string()
}

/// Deserializes an Ed25519 private key with a prefix AIP-80 prefix if present.
///
/// [Read about AIP-80](https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-80.md)
pub fn deserialize_private_key_with_prefix<'de, D>(
deserializer: D,
) -> Result<Option<Ed25519PrivateKey>, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;

// Deserialize the field as an Option<String>
let opt: Option<String> = Option::deserialize(deserializer)?;

// Transform Option<String> into Option<Ed25519PrivateKey>
opt.map_or(Ok(None), |s| {
if let Some(stripped) = s.strip_prefix("ed25519-priv-") {
// Deserialize using Ed25519PrivateKey's DeserializeKey implementation
Ed25519PrivateKey::deserialize(serde::de::value::StrDeserializer::<D::Error>::new(
stripped,
))
.map(Some)
.map_err(D::Error::custom)
} else {
// Attempt normal deserialization
Ed25519PrivateKey::deserialize(serde::de::value::StrDeserializer::<D::Error>::new(&s))
.map(Some)
.map_err(D::Error::custom)
}
})
}

0 comments on commit e0380d3

Please sign in to comment.