Skip to content

Commit

Permalink
[gh-609] refactor validators for other schemes. (#610)
Browse files Browse the repository at this point in the history
* [gh-609] refactor validators for other schemes.

* [gh-609] determine whether auth key is in account.

* [gh-609] unify assert_execute_success.

* [gh-609] clear code and comments for address types

* [gh-609] use generic T type in ed25519 validator.

* [gh-609] regenerate genesis.

* [gh-609] update genesis.

---------

Co-authored-by: Feliciss <[email protected]>
  • Loading branch information
feliciss and Feliciss authored Aug 13, 2023
1 parent 83580b5 commit 58b8b79
Show file tree
Hide file tree
Showing 18 changed files with 779 additions and 465 deletions.
24 changes: 22 additions & 2 deletions crates/rooch-framework/doc/account_authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,32 @@ A resource tha holds the auth validator ids for this account has installed.



<a name="0x3_account_authentication_EAuthenticationKeyAlreadyExists"></a>

The authentication key already exists in the specified validator


<pre><code><b>const</b> <a href="account_authentication.md#0x3_account_authentication_EAuthenticationKeyAlreadyExists">EAuthenticationKeyAlreadyExists</a>: u64 = 5;
</code></pre>



<a name="0x3_account_authentication_EAuthenticationKeyNotFound"></a>

The authentication key has not been found for the specified validator


<pre><code><b>const</b> <a href="account_authentication.md#0x3_account_authentication_EAuthenticationKeyNotFound">EAuthenticationKeyNotFound</a>: u64 = 3;
<pre><code><b>const</b> <a href="account_authentication.md#0x3_account_authentication_EAuthenticationKeyNotFound">EAuthenticationKeyNotFound</a>: u64 = 4;
</code></pre>



<a name="0x3_account_authentication_EAuthenticationKeysResourceNotFound"></a>

The authentication keys resource has not been found for the account address


<pre><code><b>const</b> <a href="account_authentication.md#0x3_account_authentication_EAuthenticationKeysResourceNotFound">EAuthenticationKeysResourceNotFound</a>: u64 = 3;
</code></pre>


Expand Down Expand Up @@ -282,7 +302,7 @@ This function is used to remove a resource account's authentication key, only th
<pre><code><b>public</b> <b>fun</b> <a href="account_authentication.md#0x3_account_authentication_remove_authentication_key">remove_authentication_key</a>&lt;ValidatorType&gt;(ctx: &<b>mut</b> StorageContext, account_addr: <b>address</b>): <a href="account_authentication.md#0x3_account_authentication_AuthenticationKey">AuthenticationKey</a>&lt;ValidatorType&gt; {
<b>assert</b>!(
<a href="_global_exists">account_storage::global_exists</a>&lt;<a href="account_authentication.md#0x3_account_authentication_AuthenticationKeys">AuthenticationKeys</a>&gt;(ctx, account_addr),
<a href="_not_found">error::not_found</a>(<a href="account_authentication.md#0x3_account_authentication_EAuthenticationKeyNotFound">EAuthenticationKeyNotFound</a>)
<a href="_not_found">error::not_found</a>(<a href="account_authentication.md#0x3_account_authentication_EAuthenticationKeysResourceNotFound">EAuthenticationKeysResourceNotFound</a>)
);
<b>let</b> authentication_keys = <a href="_global_borrow_mut">account_storage::global_borrow_mut</a>&lt;<a href="account_authentication.md#0x3_account_authentication_AuthenticationKeys">AuthenticationKeys</a>&gt;(ctx, account_addr);
<b>assert</b>!(
Expand Down
5 changes: 2 additions & 3 deletions crates/rooch-framework/doc/auth_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,8 @@ Only can be called after the transaction is validated


<pre><code><b>public</b> <b>fun</b> <a href="auth_validator.md#0x3_auth_validator_get_session_key_from_tx_ctx">get_session_key_from_tx_ctx</a>(ctx: &StorageContext): <a href="">vector</a>&lt;u8&gt; {
<b>let</b> sesion_key_option = <a href="auth_validator.md#0x3_auth_validator_get_session_key_from_tx_ctx_option">get_session_key_from_tx_ctx_option</a>(ctx);
<b>assert</b>!(<a href="_is_some">option::is_some</a>(&sesion_key_option), <a href="_invalid_state">error::invalid_state</a>(<a href="auth_validator.md#0x3_auth_validator_EMustExecuteAfterValidate">EMustExecuteAfterValidate</a>));
<a href="_extract">option::extract</a>(&<b>mut</b> sesion_key_option)
<b>assert</b>!(<a href="auth_validator.md#0x3_auth_validator_is_validate_via_session_key">is_validate_via_session_key</a>(ctx), <a href="_invalid_state">error::invalid_state</a>(<a href="auth_validator.md#0x3_auth_validator_EMustExecuteAfterValidate">EMustExecuteAfterValidate</a>));
<a href="_extract">option::extract</a>(&<b>mut</b> <a href="auth_validator.md#0x3_auth_validator_get_session_key_from_tx_ctx_option">get_session_key_from_tx_ctx_option</a>(ctx))
}
</code></pre>

Expand Down
242 changes: 164 additions & 78 deletions crates/rooch-framework/doc/ecdsa_k1_recoverable_validator.md

Large diffs are not rendered by default.

240 changes: 165 additions & 75 deletions crates/rooch-framework/doc/ecdsa_k1_validator.md

Large diffs are not rendered by default.

63 changes: 27 additions & 36 deletions crates/rooch-framework/doc/ed25519_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ This module implements the ed25519 validator scheme.
- [Function `scheme`](#0x3_ed25519_validator_scheme)
- [Function `rotate_authentication_key_entry`](#0x3_ed25519_validator_rotate_authentication_key_entry)
- [Function `remove_authentication_key_entry`](#0x3_ed25519_validator_remove_authentication_key_entry)
- [Function `get_public_key_from_payload`](#0x3_ed25519_validator_get_public_key_from_payload)
- [Function `get_signature_from_payload`](#0x3_ed25519_validator_get_signature_from_payload)
- [Function `get_authentication_key_from_payload`](#0x3_ed25519_validator_get_authentication_key_from_payload)
- [Function `get_public_key_from_authenticator_payload`](#0x3_ed25519_validator_get_public_key_from_authenticator_payload)
- [Function `get_signature_from_authenticator_payload`](#0x3_ed25519_validator_get_signature_from_authenticator_payload)
- [Function `get_authentication_key_from_authenticator_payload`](#0x3_ed25519_validator_get_authentication_key_from_authenticator_payload)
- [Function `public_key_to_address`](#0x3_ed25519_validator_public_key_to_address)
- [Function `public_key_to_authentication_key`](#0x3_ed25519_validator_public_key_to_authentication_key)
- [Function `get_authentication_key_with_default`](#0x3_ed25519_validator_get_authentication_key_with_default)
Expand Down Expand Up @@ -68,21 +68,12 @@ This module implements the ed25519 validator scheme.
## Constants


<a name="0x3_ed25519_validator_EMalformedAuthenticationKey"></a>



<pre><code><b>const</b> <a href="ed25519_validator.md#0x3_ed25519_validator_EMalformedAuthenticationKey">EMalformedAuthenticationKey</a>: u64 = 1002;
</code></pre>



<a name="0x3_ed25519_validator_EMalformedAccount"></a>
<a name="0x3_ed25519_validator_EInvalidPublicKeyLength"></a>

error code


<pre><code><b>const</b> <a href="ed25519_validator.md#0x3_ed25519_validator_EMalformedAccount">EMalformedAccount</a>: u64 = 1001;
<pre><code><b>const</b> <a href="ed25519_validator.md#0x3_ed25519_validator_EInvalidPublicKeyLength">EInvalidPublicKeyLength</a>: u64 = 0;
</code></pre>


Expand Down Expand Up @@ -153,7 +144,7 @@ error code



<pre><code><b>public</b> entry <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_rotate_authentication_key_entry">rotate_authentication_key_entry</a>&lt;<a href="ed25519_validator.md#0x3_ed25519_validator_Ed25519Validator">Ed25519Validator</a>&gt;(ctx: &<b>mut</b> <a href="_StorageContext">storage_context::StorageContext</a>, <a href="account.md#0x3_account">account</a>: &<a href="">signer</a>, public_key: <a href="">vector</a>&lt;u8&gt;)
<pre><code><b>public</b> entry <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_rotate_authentication_key_entry">rotate_authentication_key_entry</a>&lt;T&gt;(ctx: &<b>mut</b> <a href="_StorageContext">storage_context::StorageContext</a>, <a href="account.md#0x3_account">account</a>: &<a href="">signer</a>, public_key: <a href="">vector</a>&lt;u8&gt;)
</code></pre>


Expand All @@ -162,15 +153,15 @@ error code
<summary>Implementation</summary>


<pre><code><b>public</b> entry <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_rotate_authentication_key_entry">rotate_authentication_key_entry</a>&lt;<a href="ed25519_validator.md#0x3_ed25519_validator_Ed25519Validator">Ed25519Validator</a>&gt;(
<pre><code><b>public</b> entry <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_rotate_authentication_key_entry">rotate_authentication_key_entry</a>&lt;T&gt;(
ctx: &<b>mut</b> StorageContext,
<a href="account.md#0x3_account">account</a>: &<a href="">signer</a>,
public_key: <a href="">vector</a>&lt;u8&gt;
) {
// compare newly passed <b>public</b> key <b>with</b> <a href="ed25519.md#0x3_ed25519">ed25519</a> <b>public</b> key length <b>to</b> ensure it's compatible
<b>assert</b>!(
<a href="_length">vector::length</a>(&public_key) == <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_PUBKEY_LENGTH">V_ED25519_PUBKEY_LENGTH</a>,
<a href="_invalid_argument">error::invalid_argument</a>(<a href="ed25519_validator.md#0x3_ed25519_validator_EMalformedAuthenticationKey">EMalformedAuthenticationKey</a>)
<a href="_invalid_argument">error::invalid_argument</a>(<a href="ed25519_validator.md#0x3_ed25519_validator_EInvalidPublicKeyLength">EInvalidPublicKeyLength</a>)
);

// User can rotate the authentication key arbitrarily, so we do not need <b>to</b> check the new <b>public</b> key <b>with</b> the <a href="account.md#0x3_account">account</a> <b>address</b>.
Expand Down Expand Up @@ -200,21 +191,21 @@ error code


<pre><code><b>public</b> entry <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_remove_authentication_key_entry">remove_authentication_key_entry</a>&lt;T&gt;(ctx: &<b>mut</b> StorageContext, <a href="account.md#0x3_account">account</a>: &<a href="">signer</a>) {
<a href="account_authentication.md#0x3_account_authentication_remove_authentication_key">account_authentication::remove_authentication_key</a>&lt;<a href="ed25519_validator.md#0x3_ed25519_validator_Ed25519Validator">Ed25519Validator</a>&gt;(ctx, <a href="_address_of">signer::address_of</a>(<a href="account.md#0x3_account">account</a>));
<a href="account_authentication.md#0x3_account_authentication_remove_authentication_key">account_authentication::remove_authentication_key</a>&lt;<a href="ed25519_validator.md#0x3_ed25519_validator_Ed25519Validator">Ed25519Validator</a>&gt;(ctx, <a href="_address_of">signer::address_of</a>(<a href="account.md#0x3_account">account</a>));
}
</code></pre>



</details>

<a name="0x3_ed25519_validator_get_public_key_from_payload"></a>
<a name="0x3_ed25519_validator_get_public_key_from_authenticator_payload"></a>

## Function `get_public_key_from_payload`
## Function `get_public_key_from_authenticator_payload`



<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_payload">get_public_key_from_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_authenticator_payload">get_public_key_from_authenticator_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>


Expand All @@ -223,7 +214,7 @@ error code
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_payload">get_public_key_from_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt; {
<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_authenticator_payload">get_public_key_from_authenticator_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt; {
<b>let</b> public_key = <a href="_empty">vector::empty</a>&lt;u8&gt;();
<b>let</b> i = <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_SCHEME_LENGTH">V_ED25519_SCHEME_LENGTH</a> + <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_SIG_LENGTH">V_ED25519_SIG_LENGTH</a>;
<b>while</b> (i &lt; <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_SCHEME_LENGTH">V_ED25519_SCHEME_LENGTH</a> + <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_SIG_LENGTH">V_ED25519_SIG_LENGTH</a> + <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_PUBKEY_LENGTH">V_ED25519_PUBKEY_LENGTH</a>) {
Expand All @@ -240,13 +231,13 @@ error code

</details>

<a name="0x3_ed25519_validator_get_signature_from_payload"></a>
<a name="0x3_ed25519_validator_get_signature_from_authenticator_payload"></a>

## Function `get_signature_from_payload`
## Function `get_signature_from_authenticator_payload`



<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_signature_from_payload">get_signature_from_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_signature_from_authenticator_payload">get_signature_from_authenticator_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>


Expand All @@ -255,7 +246,7 @@ error code
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_signature_from_payload">get_signature_from_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt; {
<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_signature_from_authenticator_payload">get_signature_from_authenticator_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt; {
<b>let</b> sign = <a href="_empty">vector::empty</a>&lt;u8&gt;();
<b>let</b> i = <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_SCHEME_LENGTH">V_ED25519_SCHEME_LENGTH</a>;
<b>while</b> (i &lt; <a href="ed25519_validator.md#0x3_ed25519_validator_V_ED25519_SIG_LENGTH">V_ED25519_SIG_LENGTH</a> + 1) {
Expand All @@ -272,14 +263,14 @@ error code

</details>

<a name="0x3_ed25519_validator_get_authentication_key_from_payload"></a>
<a name="0x3_ed25519_validator_get_authentication_key_from_authenticator_payload"></a>

## Function `get_authentication_key_from_payload`
## Function `get_authentication_key_from_authenticator_payload`

Get the authentication key of the given authenticator authenticator_payload.
Get the authentication key of the given authenticator from authenticator_payload.


<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_authentication_key_from_payload">get_authentication_key_from_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_authentication_key_from_authenticator_payload">get_authentication_key_from_authenticator_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>


Expand All @@ -288,8 +279,8 @@ Get the authentication key of the given authenticator authenticator_payload.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_authentication_key_from_payload">get_authentication_key_from_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt; {
<b>let</b> public_key = <a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_payload">get_public_key_from_payload</a>(authenticator_payload);
<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_get_authentication_key_from_authenticator_payload">get_authentication_key_from_authenticator_payload</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt; {
<b>let</b> public_key = <a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_authenticator_payload">get_public_key_from_authenticator_payload</a>(authenticator_payload);
<b>let</b> addr = <a href="ed25519_validator.md#0x3_ed25519_validator_public_key_to_address">public_key_to_address</a>(public_key);
moveos_std::bcs::to_bytes(&addr)
}
Expand Down Expand Up @@ -423,8 +414,8 @@ Only validate the authenticator's signature.
<pre><code><b>public</b> <b>fun</b> <a href="ed25519_validator.md#0x3_ed25519_validator_validate_signature">validate_signature</a>(authenticator_payload: &<a href="">vector</a>&lt;u8&gt;, tx_hash: &<a href="">vector</a>&lt;u8&gt;) {
<b>assert</b>!(
<a href="ed25519.md#0x3_ed25519_verify">ed25519::verify</a>(
&<a href="ed25519_validator.md#0x3_ed25519_validator_get_signature_from_payload">get_signature_from_payload</a>(authenticator_payload),
&<a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_payload">get_public_key_from_payload</a>(authenticator_payload),
&<a href="ed25519_validator.md#0x3_ed25519_validator_get_signature_from_authenticator_payload">get_signature_from_authenticator_payload</a>(authenticator_payload),
&<a href="ed25519_validator.md#0x3_ed25519_validator_get_public_key_from_authenticator_payload">get_public_key_from_authenticator_payload</a>(authenticator_payload),
tx_hash
),
<a href="auth_validator.md#0x3_auth_validator_error_invalid_authenticator">auth_validator::error_invalid_authenticator</a>()
Expand Down Expand Up @@ -455,10 +446,10 @@ Only validate the authenticator's signature.
<b>let</b> tx_hash = <a href="_tx_hash">storage_context::tx_hash</a>(ctx);
<a href="ed25519_validator.md#0x3_ed25519_validator_validate_signature">validate_signature</a>(&authenticator_payload, &tx_hash);

<b>let</b> auth_key = <a href="ed25519_validator.md#0x3_ed25519_validator_get_authentication_key_from_payload">get_authentication_key_from_payload</a>(&authenticator_payload);
<b>let</b> auth_key_from_authenticator_payload = <a href="ed25519_validator.md#0x3_ed25519_validator_get_authentication_key_from_authenticator_payload">get_authentication_key_from_authenticator_payload</a>(&authenticator_payload);
<b>let</b> auth_key_in_account = <a href="ed25519_validator.md#0x3_ed25519_validator_get_authentication_key_with_default">get_authentication_key_with_default</a>(ctx, <a href="_sender">storage_context::sender</a>(ctx));
<b>assert</b>!(
auth_key_in_account == auth_key,
auth_key_in_account == auth_key_from_authenticator_payload,
<a href="auth_validator.md#0x3_auth_validator_error_invalid_account_auth_key">auth_validator::error_invalid_account_auth_key</a>()
);
}
Expand Down
Loading

0 comments on commit 58b8b79

Please sign in to comment.