Skip to content

Commit

Permalink
Rename derive_account_id_from_public_key -> derive_near_implicit_acco…
Browse files Browse the repository at this point in the history
…unt_id
  • Loading branch information
staffik committed Oct 30, 2023
1 parent 42974dd commit 4319933
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions core/primitives/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ where

/// Derives `AccountId` from `PublicKey``.
/// If the key type is ED25519, returns hex-encoded copy of the key.
pub fn derive_account_id_from_public_key(public_key: &PublicKey) -> AccountId {
pub fn derive_near_implicit_account_id(public_key: &PublicKey) -> AccountId {
match public_key.key_type() {
KeyType::ED25519 => hex::encode(public_key.key_data()).parse().unwrap(),
_ => unimplemented!(),
Expand All @@ -487,7 +487,7 @@ mod tests {
let public_key = PublicKey::from_seed(KeyType::ED25519, "test");
let expected: AccountId =
"bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0".parse().unwrap();
assert_eq!(derive_account_id_from_public_key(&public_key), expected);
assert_eq!(derive_near_implicit_account_id(&public_key), expected);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use near_primitives::shard_layout::ShardLayout;
use near_primitives::sharding::ChunkHash;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::{AccountId, BlockHeight};
use near_primitives::utils::derive_account_id_from_public_key;
use near_primitives::utils::derive_near_implicit_account_id;
use near_primitives::version::{ProtocolFeature, ProtocolVersion};
use near_primitives::views::FinalExecutionStatus;
use nearcore::config::GenesisExt;
Expand Down Expand Up @@ -203,7 +203,7 @@ fn get_status_of_tx_hash_collision_for_implicit_account(
fn test_transaction_hash_collision_for_implicit_account_fail() {
let protocol_version = ProtocolFeature::AccessKeyNonceForImplicitAccounts.protocol_version();
let secret_key = SecretKey::from_seed(KeyType::ED25519, "test");
let implicit_account_id = derive_account_id_from_public_key(&secret_key.public_key());
let implicit_account_id = derive_near_implicit_account_id(&secret_key.public_key());
let implicit_account_signer = InMemorySigner::from_secret_key(implicit_account_id, secret_key);
assert_matches!(
get_status_of_tx_hash_collision_for_implicit_account(
Expand All @@ -220,7 +220,7 @@ fn test_transaction_hash_collision_for_implicit_account_ok() {
let protocol_version =
ProtocolFeature::AccessKeyNonceForImplicitAccounts.protocol_version() - 1;
let secret_key = SecretKey::from_seed(KeyType::ED25519, "test");
let implicit_account_id = derive_account_id_from_public_key(&secret_key.public_key());
let implicit_account_id = derive_near_implicit_account_id(&secret_key.public_key());
let implicit_account_signer = InMemorySigner::from_secret_key(implicit_account_id, secret_key);
assert_matches!(
get_status_of_tx_hash_collision_for_implicit_account(
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/src/tests/standard_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use near_primitives::errors::{
};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::types::{AccountId, Balance, TrieNodesCount};
use near_primitives::utils::derive_account_id_from_public_key;
use near_primitives::utils::derive_near_implicit_account_id;
use near_primitives::views::{
AccessKeyView, AccountView, ExecutionMetadataView, FinalExecutionOutcomeView,
FinalExecutionStatus,
Expand Down Expand Up @@ -336,7 +336,7 @@ pub fn transfer_tokens_implicit_account(node: impl Node, public_key: PublicKey)
let root = node_user.get_state_root();
let tokens_used = 10u128.pow(25);
let fee_helper = fee_helper(&node);
let receiver_id = derive_account_id_from_public_key(&public_key);
let receiver_id = derive_near_implicit_account_id(&public_key);

let transfer_cost = match receiver_id.get_account_type() {
AccountType::NearImplicitAccount => fee_helper.create_account_transfer_full_key_cost(),
Expand Down Expand Up @@ -401,7 +401,7 @@ pub fn trying_to_create_implicit_account(node: impl Node, public_key: PublicKey)
let root = node_user.get_state_root();
let tokens_used = 10u128.pow(25);
let fee_helper = fee_helper(&node);
let receiver_id = derive_account_id_from_public_key(&public_key);
let receiver_id = derive_near_implicit_account_id(&public_key);

let transaction_result = node_user
.create_account(
Expand Down
9 changes: 4 additions & 5 deletions runtime/runtime/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,7 @@ pub(crate) fn action_implicit_account_creation_transfer(
* near_primitives::account::AccessKey::ACCESS_KEY_NONCE_RANGE_MULTIPLIER;
}

// Invariant: The account_id is hex like (implicit account id).
// It holds because in the only calling site, we've checked the permissions before.
// unwrap: Can only fail if `account_id` is not NEAR-implicit.
// unwrap: here it's safe because the `account_id` has already been determined to be implicit by `get_account_type`
let public_key = PublicKey::from_near_implicit_account(account_id).unwrap();

*account = Some(Account::new(
Expand All @@ -472,8 +470,9 @@ pub(crate) fn action_implicit_account_creation_transfer(

set_access_key(state_update, account_id.clone(), public_key, &access_key);
}
AccountType::EthImplicitAccount => panic!("must be implicit"),
AccountType::NamedAccount => panic!("must be implicit"),
// Invariant: The `account_id` is implicit.
// It holds because in the only calling site, we've checked the permissions before.
AccountType::EthImplicitAccount | AccountType::NamedAccount => panic!("must be implicit"),
}
}

Expand Down
4 changes: 2 additions & 2 deletions tools/mirror/src/key_mapping.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use hkdf::Hkdf;
use near_crypto::{ED25519PublicKey, ED25519SecretKey, PublicKey, Secp256K1PublicKey, SecretKey};
use near_primitives::types::AccountId;
use near_primitives::utils::derive_account_id_from_public_key;
use near_primitives::utils::derive_near_implicit_account_id;
use near_primitives_core::account::id::AccountType;
use sha2::Sha256;

Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn map_account(
let public_key =
PublicKey::from_near_implicit_account(account_id).expect("must be implicit");
let mapped_key = map_key(&public_key, secret);
derive_account_id_from_public_key(&mapped_key.public_key())
derive_near_implicit_account_id(&mapped_key.public_key())
}
AccountType::EthImplicitAccount => account_id.clone(),
AccountType::NamedAccount => account_id.clone(),
Expand Down

0 comments on commit 4319933

Please sign in to comment.