Skip to content

Commit

Permalink
Allow for AIP-80 compliant strings in imports
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostWalker562 committed Oct 21, 2024
1 parent fbf09ce commit 8336d6c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/aptos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to the Aptos CLI will be captured in this file. This project
- Update the default version of `movefmt` to be installed from 1.0.4 to 1.0.5
- Update the local-testnet logs to use `println` for regular output and reserve `eprintln` for errors.
- Set compiler V2 as default when using `aptos move prove`.
- Add support for AIP-80 compliant strings when importing using the CLI arguments or manual input.

## [4.2.3] - 2024/09/20
- Fix the broken indexer in localnet in 4.2.2, which migrates table info from sycn to async ways.
Expand Down
12 changes: 8 additions & 4 deletions crates/aptos/src/common/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use crate::{
ConfigSearchMode, EncodingOptions, HardwareWalletOptions, PrivateKeyInputOptions,
ProfileConfig, ProfileOptions, PromptOptions, RngArgs, DEFAULT_PROFILE,
},
utils::{explorer_account_link, fund_account, prompt_yes_with_override, read_line},
utils::{
explorer_account_link, fund_account, prompt_yes_with_override, read_line,
strip_private_key_prefix,
},
},
};
use aptos_crypto::{ed25519::Ed25519PrivateKey, PrivateKey, ValidCryptoMaterialStringExt};
Expand Down Expand Up @@ -218,9 +221,10 @@ impl CliCommand<()> for InitTool {
.generate_ed25519_private_key()
}
} else {
Ed25519PrivateKey::from_encoded_string(input).map_err(|err| {
CliError::UnableToParse("Ed25519PrivateKey", err.to_string())
})?
Ed25519PrivateKey::from_encoded_string(&strip_private_key_prefix(
&input.to_string(),
))
.map_err(|err| CliError::UnableToParse("Ed25519PrivateKey", err.to_string()))?
}
};

Expand Down
4 changes: 2 additions & 2 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use super::utils::{explorer_transaction_link, fund_account};
use super::utils::{explorer_transaction_link, fund_account, strip_private_key_prefix};
use crate::{
common::{
init::Network,
Expand Down Expand Up @@ -675,7 +675,7 @@ pub trait ParsePrivateKey {
encoding.load_key("--private-key-file", file.as_path())?,
))
} else if let Some(ref key) = private_key {
let key = key.as_bytes().to_vec();
let key = strip_private_key_prefix(key).as_bytes().to_vec();
Ok(Some(encoding.decode_key("--private-key", key)?))
} else {
Ok(None)
Expand Down
12 changes: 12 additions & 0 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,15 @@ pub fn explorer_transaction_link(
)
}
}

/// Strips the private key prefix for a given key string if it is AIP-80 compliant.
///
/// [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
.to_string()
}

0 comments on commit 8336d6c

Please sign in to comment.