Skip to content

Commit

Permalink
[compiler-v2] fixed authentication key process on prologue and eilpro…
Browse files Browse the repository at this point in the history
…logue (#4303)

* [compiler-v2 framework] fixed error for unittest stc_block.move -
test_block_metadata_bcs

* [compiler-v2 framework] add authentication key to prologue and eilprologue

* [compiler-v2 framework] add authentication key to prologue and eilprologue
  • Loading branch information
welbon authored and simonjiao committed Nov 28, 2024
1 parent b297739 commit 6840d8b
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 54 deletions.
90 changes: 90 additions & 0 deletions vm/framework/starcoin-framework/doc/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
- [Function `update_auth_key_and_originating_address_table`](#0x1_account_update_auth_key_and_originating_address_table)
- [Function `create_resource_address`](#0x1_account_create_resource_address)
- [Function `create_resource_account`](#0x1_account_create_resource_account)
- [Function `is_account_zero_auth_key`](#0x1_account_is_account_zero_auth_key)
- [Function `is_account_auth_key`](#0x1_account_is_account_auth_key)
- [Function `auth_key_to_address`](#0x1_account_auth_key_to_address)
- [Function `create_framework_reserved_account`](#0x1_account_create_framework_reserved_account)
- [Function `create_guid`](#0x1_account_create_guid)
- [Function `new_event_handle`](#0x1_account_new_event_handle)
Expand Down Expand Up @@ -2003,6 +2006,93 @@ than <code>(1/2)^(256)</code>.



</details>

<a id="0x1_account_is_account_zero_auth_key"></a>

## Function `is_account_zero_auth_key`

Check auth key is zero.


<pre><code><b>public</b> <b>fun</b> <a href="account.md#0x1_account_is_account_zero_auth_key">is_account_zero_auth_key</a>(account_addr: <b>address</b>): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="account.md#0x1_account_is_account_zero_auth_key">is_account_zero_auth_key</a>(account_addr: <b>address</b>): bool <b>acquires</b> <a href="account.md#0x1_account_Account">Account</a> {
<b>let</b> <a href="account.md#0x1_account">account</a> = <b>borrow_global</b>&lt;<a href="account.md#0x1_account_Account">Account</a>&gt;(account_addr);
<a href="account.md#0x1_account">account</a>.authentication_key == <a href="account.md#0x1_account_ZERO_AUTH_KEY">ZERO_AUTH_KEY</a>
}
</code></pre>



</details>

<a id="0x1_account_is_account_auth_key"></a>

## Function `is_account_auth_key`

Check is account auth key


<pre><code><b>public</b> <b>fun</b> <a href="account.md#0x1_account_is_account_auth_key">is_account_auth_key</a>(account_addr: <b>address</b>, auth_key: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="account.md#0x1_account_is_account_auth_key">is_account_auth_key</a>(account_addr: <b>address</b>, auth_key: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): bool <b>acquires</b> <a href="account.md#0x1_account_Account">Account</a> {
<b>let</b> <a href="account.md#0x1_account">account</a> = <b>borrow_global</b>&lt;<a href="account.md#0x1_account_Account">Account</a>&gt;(account_addr);
<a href="account.md#0x1_account">account</a>.authentication_key == auth_key
}
</code></pre>



</details>

<a id="0x1_account_auth_key_to_address"></a>

## Function `auth_key_to_address`

Convert from authentication key to address


<pre><code><b>public</b> <b>fun</b> <a href="account.md#0x1_account_auth_key_to_address">auth_key_to_address</a>(authentication_key: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="account.md#0x1_account_auth_key_to_address">auth_key_to_address</a>(authentication_key: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <b>address</b> {
<b>assert</b>!(<a href="../../move-stdlib/doc/vector.md#0x1_vector_length">vector::length</a>(&authentication_key) == 32, <a href="../../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="account.md#0x1_account_EMALFORMED_AUTHENTICATION_KEY">EMALFORMED_AUTHENTICATION_KEY</a>));
<b>let</b> address_bytes = <a href="../../move-stdlib/doc/vector.md#0x1_vector_empty">vector::empty</a>&lt;u8&gt;();

<b>let</b> i = 16;
<b>while</b> (i &lt; 32) {
<b>let</b> b = *<a href="../../move-stdlib/doc/vector.md#0x1_vector_borrow">vector::borrow</a>(&authentication_key, i);
<a href="../../move-stdlib/doc/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> address_bytes, b);
i = i + 1;
};

<a href="../../starcoin-stdlib/doc/from_bcs.md#0x1_from_bcs_to_address">from_bcs::to_address</a>(address_bytes)
}
</code></pre>



</details>

<a id="0x1_account_create_framework_reserved_account"></a>
Expand Down
2 changes: 1 addition & 1 deletion vm/framework/starcoin-framework/doc/stc_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ The runtime always runs this before executing the transactions in a block.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="stc_block.md#0x1_stc_block_block_prologue">block_prologue</a>(
<pre><code><b>public</b> <b>fun</b> <a href="stc_block.md#0x1_stc_block_block_prologue">block_prologue</a>(
<a href="account.md#0x1_account">account</a>: <a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
parent_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
<a href="timestamp.md#0x1_timestamp">timestamp</a>: u64,
Expand Down
56 changes: 31 additions & 25 deletions vm/framework/starcoin-framework/doc/stc_transaction_validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<b>use</b> <a href="create_signer.md#0x1_create_signer">0x1::create_signer</a>;
<b>use</b> <a href="../../starcoin-stdlib/doc/debug.md#0x1_debug">0x1::debug</a>;
<b>use</b> <a href="../../move-stdlib/doc/error.md#0x1_error">0x1::error</a>;
<b>use</b> <a href="../../move-stdlib/doc/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="../../move-stdlib/doc/signer.md#0x1_signer">0x1::signer</a>;
<b>use</b> <a href="starcoin_coin.md#0x1_starcoin_coin">0x1::starcoin_coin</a>;
<b>use</b> <a href="stc_transaction_fee.md#0x1_stc_transaction_fee">0x1::stc_transaction_fee</a>;
Expand All @@ -34,6 +35,7 @@
<b>use</b> <a href="../../move-stdlib/doc/string.md#0x1_string">0x1::string</a>;
<b>use</b> <a href="system_addresses.md#0x1_system_addresses">0x1::system_addresses</a>;
<b>use</b> <a href="stc_transaction_publish_option.md#0x1_transaction_publish_option">0x1::transaction_publish_option</a>;
<b>use</b> <a href="../../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>;
</code></pre>


Expand Down Expand Up @@ -371,7 +373,7 @@ It collects gas and bumps the sequence number
Migration from old StarcoinFramework Account::txn_prologue


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_validation.md#0x1_stc_transaction_validation_txn_prologue">txn_prologue</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, txn_sender: <b>address</b>, txn_sequence_number: u64, _txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, txn_gas_price: u64, txn_max_gas_units: u64)
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_validation.md#0x1_stc_transaction_validation_txn_prologue">txn_prologue</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, txn_sender: <b>address</b>, txn_sequence_number: u64, txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, txn_gas_price: u64, txn_max_gas_units: u64)
</code></pre>


Expand All @@ -384,7 +386,7 @@ Migration from old StarcoinFramework Account::txn_prologue
<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
txn_sender: <b>address</b>,
txn_sequence_number: u64,
_txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
txn_gas_price: u64,
txn_max_gas_units: u64,
) {
Expand All @@ -398,21 +400,22 @@ Migration from old StarcoinFramework Account::txn_prologue
<a href="../../move-stdlib/doc/error.md#0x1_error_invalid_state">error::invalid_state</a>(<a href="stc_transaction_validation.md#0x1_stc_transaction_validation_EPROLOGUE_SIGNER_ALREADY_DELEGATED">EPROLOGUE_SIGNER_ALREADY_DELEGATED</a>)
);

// TODO(BobOng): [framework upgrade] txn_authentication_key_preimage <b>to</b> be check
// // Load the transaction sender's <a href="account.md#0x1_account">account</a>
// <b>if</b> (is_dummy_auth_key(sender_account)){
// // <b>if</b> sender's auth key is empty, <b>use</b> <b>address</b> <b>as</b> auth key for check transaction.
// <b>assert</b>!(
// Authenticator::derived_address(Hash::sha3_256(txn_authentication_key_preimage)) == txn_sender,
// Errors::invalid_argument(<a href="stc_transaction_validation.md#0x1_stc_transaction_validation_EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY">EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY</a>)
// );
// }<b>else</b>{
// // Check that the <a href="../../move-stdlib/doc/hash.md#0x1_hash">hash</a> of the transaction's <b>public</b> key matches the <a href="account.md#0x1_account">account</a>'s auth key
// <b>assert</b>!(
// Hash::sha3_256(txn_authentication_key_preimage) == *&sender_account.authentication_key,
// Errors::invalid_argument(<a href="stc_transaction_validation.md#0x1_stc_transaction_validation_EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY">EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY</a>)
// );
// };
// txn_authentication_key_preimage <b>to</b> be check
// Load the transaction sender's <a href="account.md#0x1_account">account</a>
<b>if</b> (<a href="account.md#0x1_account_is_account_zero_auth_key">account::is_account_zero_auth_key</a>(txn_sender)) {
// <b>if</b> sender's auth key is empty, <b>use</b> <b>address</b> <b>as</b> auth key for check transaction.
<b>assert</b>!(
<a href="account.md#0x1_account_auth_key_to_address">account::auth_key_to_address</a>(<a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(txn_authentication_key_preimage)) == txn_sender,
<a href="../../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="stc_transaction_validation.md#0x1_stc_transaction_validation_EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY">EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY</a>)
);
} <b>else</b> {
// Check that the <a href="../../move-stdlib/doc/hash.md#0x1_hash">hash</a> of the transaction's <b>public</b> key matches the <a href="account.md#0x1_account">account</a>'s auth key
<b>assert</b>!(
//<a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(txn_authentication_key_preimage) == *&sender_account.authentication_key,
<a href="account.md#0x1_account_is_account_auth_key">account::is_account_auth_key</a>(txn_sender, <a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(txn_authentication_key_preimage)),
<a href="../../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="stc_transaction_validation.md#0x1_stc_transaction_validation_EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY">EPROLOGUE_INVALID_ACCOUNT_AUTH_KEY</a>)
);
};


<b>assert</b>!(
Expand Down Expand Up @@ -460,7 +463,7 @@ The epilogue is invoked at the end of transactions.
It collects gas and bumps the sequence number


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_validation.md#0x1_stc_transaction_validation_txn_epilogue">txn_epilogue</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, txn_sender: <b>address</b>, _txn_sequence_number: u64, _txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, txn_gas_price: u64, txn_max_gas_units: u64, gas_units_remaining: u64)
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_validation.md#0x1_stc_transaction_validation_txn_epilogue">txn_epilogue</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, txn_sender: <b>address</b>, _txn_sequence_number: u64, txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, txn_gas_price: u64, txn_max_gas_units: u64, gas_units_remaining: u64)
</code></pre>


Expand All @@ -473,7 +476,7 @@ It collects gas and bumps the sequence number
<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
txn_sender: <b>address</b>,
_txn_sequence_number: u64,
_txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
txn_gas_price: u64,
txn_max_gas_units: u64,
gas_units_remaining: u64,
Expand All @@ -482,7 +485,7 @@ It collects gas and bumps the sequence number


// Charge for gas
<b>let</b> transaction_fee_amount =(txn_gas_price * (txn_max_gas_units - gas_units_remaining) <b>as</b> u128);
<b>let</b> transaction_fee_amount = (txn_gas_price * (txn_max_gas_units - gas_units_remaining) <b>as</b> u128);
<b>assert</b>!(
<a href="coin.md#0x1_coin_balance">coin::balance</a>&lt;STC&gt;(txn_sender) &gt;= (transaction_fee_amount <b>as</b> u64),
<a href="../../move-stdlib/doc/error.md#0x1_error_out_of_range">error::out_of_range</a>(<a href="stc_transaction_validation.md#0x1_stc_transaction_validation_EINSUFFICIENT_BALANCE">EINSUFFICIENT_BALANCE</a>)
Expand All @@ -491,11 +494,14 @@ It collects gas and bumps the sequence number
// Bump the sequence number
<a href="account.md#0x1_account_increment_sequence_number">account::increment_sequence_number</a>(txn_sender);

// TODO(BobOng): [framework upgrade] txn_authentication_key_preimage <b>to</b> be check
// Set auth key when user send transaction first.
// <b>if</b> (is_dummy_auth_key(sender_account) && !<a href="../../move-stdlib/doc/vector.md#0x1_vector_is_empty">vector::is_empty</a>(&txn_authentication_key_preimage)){
// sender_account.authentication_key = Hash::sha3_256(txn_authentication_key_preimage);
// };
<b>if</b> (<a href="account.md#0x1_account_is_account_zero_auth_key">account::is_account_zero_auth_key</a>(txn_sender) &&
!<a href="../../move-stdlib/doc/vector.md#0x1_vector_is_empty">vector::is_empty</a>(&txn_authentication_key_preimage)) {
<a href="account.md#0x1_account_rotate_authentication_key_internal">account::rotate_authentication_key_internal</a>(
&<a href="create_signer.md#0x1_create_signer_create_signer">create_signer::create_signer</a>(txn_sender),
<a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(txn_authentication_key_preimage)
)
};

<b>if</b> (transaction_fee_amount &gt; 0) {
<b>let</b> <a href="transaction_fee.md#0x1_transaction_fee">transaction_fee</a> = <a href="coin.md#0x1_coin_withdraw">coin::withdraw</a>&lt;STC&gt;(
Expand Down Expand Up @@ -599,7 +605,7 @@ It collects gas and bumps the sequence number
### Function `txn_epilogue`


<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_validation.md#0x1_stc_transaction_validation_txn_epilogue">txn_epilogue</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, txn_sender: <b>address</b>, _txn_sequence_number: u64, _txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, txn_gas_price: u64, txn_max_gas_units: u64, gas_units_remaining: u64)
<pre><code><b>public</b> <b>fun</b> <a href="stc_transaction_validation.md#0x1_stc_transaction_validation_txn_epilogue">txn_epilogue</a>&lt;TokenType&gt;(<a href="account.md#0x1_account">account</a>: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, txn_sender: <b>address</b>, _txn_sequence_number: u64, txn_authentication_key_preimage: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, txn_gas_price: u64, txn_max_gas_units: u64, gas_units_remaining: u64)
</code></pre>


Expand Down
21 changes: 21 additions & 0 deletions vm/framework/starcoin-framework/sources/account.move
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,27 @@ module starcoin_framework::account {
(resource, signer_cap)
}

/// Check auth key is zero.
public fun is_account_zero_auth_key(account_addr: address): bool acquires Account {
let account = borrow_global<Account>(account_addr);
account.authentication_key == ZERO_AUTH_KEY
}

/// Convert from authentication key to address
public fun auth_key_to_address(authentication_key: vector<u8>): address {
assert!(vector::length(&authentication_key) == 32, error::invalid_argument(EMALFORMED_AUTHENTICATION_KEY));
let address_bytes = vector::empty<u8>();

let i = 16;
while (i < 32) {
let b = *vector::borrow(&authentication_key, i);
vector::push_back(&mut address_bytes, b);
i = i + 1;
};

from_bcs::to_address(address_bytes)
}

/// create the account for system reserved addresses
public(friend) fun create_framework_reserved_account(addr: address): (signer, SignerCapability) {
assert!(
Expand Down
10 changes: 6 additions & 4 deletions vm/framework/starcoin-framework/sources/stc/stc_block.move
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ module starcoin_framework::stc_block {
#[test_only]
use std::hash;
#[test_only]
use starcoin_framework::account::create_signer_for_test;
use starcoin_framework::account::{create_signer_for_test, create_account_if_does_not_exist};
#[test_only]
use starcoin_framework::bcs_util;
#[test_only]
use starcoin_framework::create_signer;
use starcoin_framework::starcoin_account::create_account;

const EPROLOGUE_BAD_CHAIN_ID: u64 = 6;

Expand Down Expand Up @@ -113,7 +113,7 @@ module starcoin_framework::stc_block {

/// Set the metadata for the current block and distribute transaction fees and block rewards.
/// The runtime always runs this before executing the transactions in a block.
public fun block_prologue(
public fun block_prologue(
account: signer,
parent_hash: vector<u8>,
timestamp: u64,
Expand Down Expand Up @@ -295,7 +295,9 @@ module starcoin_framework::stc_block {

#[test]
fun test_block_metadata_bcs() {
let test_framework = create_signer_for_test(system_addresses::get_starcoin_framework());
debug::print(&std::string::utf8(b"test_block_metadata_bcs | bcs"));

let test_framework = account::create_account_for_test(system_addresses::get_starcoin_framework());
let block_metadata = BlockMetadata {
number: 0,
parent_hash: vector::empty<u8>(),
Expand Down
Loading

0 comments on commit 6840d8b

Please sign in to comment.