Skip to content

Commit

Permalink
[framework] massage byte_conversions into util
Browse files Browse the repository at this point in the history
this is a bit cleaner way of representing this
  • Loading branch information
davidiw committed Aug 26, 2022
1 parent b23cb7c commit 44c8212
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 43 deletions.
16 changes: 8 additions & 8 deletions aptos-move/framework/aptos-framework/sources/account.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module aptos_framework::account {
use std::signer;
use std::vector;
use aptos_std::type_info::{Self, TypeInfo};
use aptos_framework::byte_conversions;
use aptos_framework::event::{Self, EventHandle};
use aptos_framework::guid;
use aptos_framework::system_addresses;
use aptos_framework::util;
use aptos_std::table::{Self, Table};
use aptos_std::ed25519;

Expand Down Expand Up @@ -120,7 +120,7 @@ module aptos_framework::account {

fun create_account_unchecked(new_address: address): signer {
let new_account = create_signer(new_address);
let authentication_key = byte_conversions::from_address(&new_address);
let authentication_key = bcs::to_bytes(&new_address);
assert!(
vector::length(&authentication_key) == 32,
error::invalid_argument(EMALFORMED_AUTHENTICATION_KEY)
Expand Down Expand Up @@ -222,7 +222,7 @@ module aptos_framework::account {
let account_resource = borrow_global_mut<Account>(addr);
assert!(verify_authentication_key_matches_ed25519_public_key(account_resource.authentication_key, curr_pk_bytes), std::error::unauthenticated(EWRONG_CURRENT_PUBLIC_KEY));

let curr_auth_key = byte_conversions::to_address(account_resource.authentication_key);
let curr_auth_key = util::address_from_bytes(account_resource.authentication_key);
// Construct a RotationProofChallenge struct
let challenge = RotationProofChallenge {
sequence_number: account_resource.sequence_number,
Expand All @@ -246,7 +246,7 @@ module aptos_framework::account {
// Derive the authentication key of the new PK
vector::push_back(&mut new_pk_bytes, 0);
let new_auth_key = hash::sha3_256(new_pk_bytes);
let new_address = byte_conversions::to_address(new_auth_key);
let new_address = util::address_from_bytes(new_auth_key);

// Update the originating address map
table::add(address_map, new_address, addr);
Expand Down Expand Up @@ -317,7 +317,7 @@ module aptos_framework::account {
public fun create_resource_account(source: &signer, seed: vector<u8>): (signer, SignerCapability) {
let bytes = bcs::to_bytes(&signer::address_of(source));
vector::append(&mut bytes, seed);
let addr = byte_conversions::to_address(hash::sha3_256(bytes));
let addr = util::address_from_bytes(hash::sha3_256(bytes));

let signer = create_account_unchecked(copy addr);
let signer_cap = SignerCapability { account: addr };
Expand Down Expand Up @@ -476,7 +476,7 @@ module aptos_framework::account {
public entry fun test_invalid_offer_rotation_capability(bob: signer) acquires Account {
let pk_with_scheme = x"f66bf0ce5ceb582b93d6780820c2025b9967aedaa259bdbb9f3d0297eced0e18";
vector::push_back(&mut pk_with_scheme, 0);
let alice_address = byte_conversions::to_address(hash::sha3_256(pk_with_scheme));
let alice_address = util::address_from_bytes(hash::sha3_256(pk_with_scheme));
let alice = create_account_unchecked(alice_address);
create_account(signer::address_of(&bob));

Expand All @@ -490,7 +490,7 @@ module aptos_framework::account {
let pk = x"f66bf0ce5ceb582b93d6780820c2025b9967aedaa259bdbb9f3d0297eced0e18";
let pk_with_scheme = copy pk;
vector::push_back(&mut pk_with_scheme, 0);
let alice_address = byte_conversions::to_address(hash::sha3_256(pk_with_scheme));
let alice_address = util::address_from_bytes(hash::sha3_256(pk_with_scheme));
let alice = create_account_unchecked(alice_address);
create_account(signer::address_of(&bob));

Expand All @@ -510,7 +510,7 @@ module aptos_framework::account {
let pk = x"f66bf0ce5ceb582b93d6780820c2025b9967aedaa259bdbb9f3d0297eced0e18";
let pk_with_scheme = copy pk;
vector::push_back(&mut pk_with_scheme, 0);
let alice_address = byte_conversions::to_address(hash::sha3_256(pk_with_scheme));
let alice_address = util::address_from_bytes(hash::sha3_256(pk_with_scheme));
let alice = create_account_unchecked(alice_address);
create_account(signer::address_of(&bob));
create_account(signer::address_of(&charlie));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ module aptos_framework::aptos_account {
#[test(alice = @0xa11ce, core = @0x1)]
public fun test_transfer(alice: signer, core: signer) {
use std::signer;
use aptos_framework::byte_conversions;
use aptos_framework::util;

let bob = byte_conversions::to_address(x"0000000000000000000000000000000000000000000000000000000000000b0b");
let carol = byte_conversions::to_address(x"00000000000000000000000000000000000000000000000000000000000ca501");
let bob = util::address_from_bytes(x"0000000000000000000000000000000000000000000000000000000000000b0b");
let carol = util::address_from_bytes(x"00000000000000000000000000000000000000000000000000000000000ca501");

let (burn_cap, mint_cap) = aptos_framework::aptos_coin::initialize_for_test(&core);
create_account(signer::address_of(&alice));
Expand Down
30 changes: 0 additions & 30 deletions aptos-move/framework/aptos-framework/sources/byte_conversions.move

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module aptos_framework::resource_account {
let seed = x"01";
let bytes = bcs::to_bytes(&user_addr);
vector::append(&mut bytes, copy seed);
let resource_addr = aptos_framework::byte_conversions::to_address(hash::sha3_256(bytes));
let resource_addr = aptos_framework::util::address_from_bytes(hash::sha3_256(bytes));

create_resource_account(&user, seed, vector::empty());
let container = borrow_global<Container>(user_addr);
Expand Down
5 changes: 4 additions & 1 deletion aptos-move/framework/aptos-framework/sources/util.move
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// Utility functions used by the framework modules.
module aptos_framework::util {
friend aptos_framework::code;
friend aptos_framework::byte_conversions;
friend aptos_framework::gas_schedule;

/// Native function to deserialize a type T.
Expand All @@ -10,4 +9,8 @@ module aptos_framework::util {
/// deserialized a linear value, its their responsibility that the data they deserialize is
/// owned.
public(friend) native fun from_bytes<T>(bytes: vector<u8>): T;

public fun address_from_bytes(bytes: vector<u8>): address {
from_bytes(bytes)
}
}

0 comments on commit 44c8212

Please sign in to comment.