Skip to content

Commit

Permalink
Fix bitcoin & eth validator (rooch-network#1660)
Browse files Browse the repository at this point in the history
  • Loading branch information
wow-sven authored May 10, 2024
1 parent d0ea730 commit 4a5e844
Show file tree
Hide file tree
Showing 23 changed files with 259 additions and 418 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frameworks/rooch-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ smallvec = { workspace = true }
hex = { workspace = true }
http = { workspace = true }
tracing = { workspace = true }
bitcoin = { workspace = true }

move-binary-format = { workspace = true }
move-bytecode-utils = { workspace = true }
Expand Down
13 changes: 0 additions & 13 deletions frameworks/rooch-framework/doc/auth_payload.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
- [Function `sign_info_prefix`](#0x3_auth_payload_sign_info_prefix)
- [Function `sign_info`](#0x3_auth_payload_sign_info)
- [Function `public_key`](#0x3_auth_payload_public_key)
- [Function `multi_address`](#0x3_auth_payload_multi_address)
- [Function `from_address`](#0x3_auth_payload_from_address)


<pre><code><b>use</b> <a href="">0x2::bcs</a>;
<b>use</b> <a href="multichain_address.md#0x3_multichain_address">0x3::multichain_address</a>;
</code></pre>


Expand Down Expand Up @@ -88,17 +86,6 @@



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

## Function `multi_address`



<pre><code><b>public</b> <b>fun</b> <a href="auth_payload.md#0x3_auth_payload_multi_address">multi_address</a>(payload: <a href="auth_payload.md#0x3_auth_payload_AuthPayload">auth_payload::AuthPayload</a>): <a href="multichain_address.md#0x3_multichain_address_MultiChainAddress">multichain_address::MultiChainAddress</a>
</code></pre>



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

## Function `from_address`
Expand Down
12 changes: 12 additions & 0 deletions frameworks/rooch-framework/doc/bitcoin_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Function `as_bytes`](#0x3_bitcoin_address_as_bytes)
- [Function `into_bytes`](#0x3_bitcoin_address_into_bytes)
- [Function `to_bech32`](#0x3_bitcoin_address_to_bech32)
- [Function `verify_with_pk`](#0x3_bitcoin_address_verify_with_pk)


<pre><code><b>use</b> <a href="">0x1::string</a>;
Expand Down Expand Up @@ -245,3 +246,14 @@ Empty address is a special address that is used to if we parse address failed fr

<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_address.md#0x3_bitcoin_address_to_bech32">to_bech32</a>(_addr: &<a href="bitcoin_address.md#0x3_bitcoin_address_BitcoinAddress">bitcoin_address::BitcoinAddress</a>): <a href="_String">string::String</a>
</code></pre>



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

## Function `verify_with_pk`



<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_address.md#0x3_bitcoin_address_verify_with_pk">verify_with_pk</a>(addr: &<a href="">vector</a>&lt;u8&gt;, pk: &<a href="">vector</a>&lt;u8&gt;): bool
</code></pre>
90 changes: 2 additions & 88 deletions frameworks/rooch-framework/doc/bitcoin_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,18 @@ This module implements Bitcoin validator with the ECDSA recoverable signature ov
- [Struct `BitcoinValidator`](#0x3_bitcoin_validator_BitcoinValidator)
- [Constants](#@Constants_0)
- [Function `auth_validator_id`](#0x3_bitcoin_validator_auth_validator_id)
- [Function `rotate_authentication_key_entry`](#0x3_bitcoin_validator_rotate_authentication_key_entry)
- [Function `remove_authentication_key_entry`](#0x3_bitcoin_validator_remove_authentication_key_entry)
- [Function `public_key_to_authentication_key`](#0x3_bitcoin_validator_public_key_to_authentication_key)
- [Function `get_authentication_key_option_from_account`](#0x3_bitcoin_validator_get_authentication_key_option_from_account)
- [Function `is_authentication_key_in_account`](#0x3_bitcoin_validator_is_authentication_key_in_account)
- [Function `get_authentication_key_from_account`](#0x3_bitcoin_validator_get_authentication_key_from_account)
- [Function `validate_signature`](#0x3_bitcoin_validator_validate_signature)
- [Function `validate`](#0x3_bitcoin_validator_validate)


<pre><code><b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x1::signer</a>;
<b>use</b> <a href="">0x1::vector</a>;
<pre><code><b>use</b> <a href="">0x1::vector</a>;
<b>use</b> <a href="">0x2::features</a>;
<b>use</b> <a href="">0x2::hash</a>;
<b>use</b> <a href="">0x2::hex</a>;
<b>use</b> <a href="">0x2::tx_context</a>;
<b>use</b> <a href="account_authentication.md#0x3_account_authentication">0x3::account_authentication</a>;
<b>use</b> <a href="auth_payload.md#0x3_auth_payload">0x3::auth_payload</a>;
<b>use</b> <a href="auth_validator.md#0x3_auth_validator">0x3::auth_validator</a>;
<b>use</b> <a href="bitcoin_address.md#0x3_bitcoin_address">0x3::bitcoin_address</a>;
<b>use</b> <a href="ecdsa_k1.md#0x3_ecdsa_k1">0x3::ecdsa_k1</a>;
<b>use</b> <a href="multichain_address.md#0x3_multichain_address">0x3::multichain_address</a>;
</code></pre>
Expand Down Expand Up @@ -61,15 +53,6 @@ there defines auth validator id for each blockchain



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



<pre><code><b>const</b> <a href="bitcoin_validator.md#0x3_bitcoin_validator_ErrorInvalidPublicKeyLength">ErrorInvalidPublicKeyLength</a>: u64 = 1;
</code></pre>



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

## Function `auth_validator_id`
Expand All @@ -81,75 +64,6 @@ there defines auth validator id for each blockchain



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

## Function `rotate_authentication_key_entry`



<pre><code><b>public</b> entry <b>fun</b> <a href="bitcoin_validator.md#0x3_bitcoin_validator_rotate_authentication_key_entry">rotate_authentication_key_entry</a>(<a href="">account</a>: &<a href="">signer</a>, public_key: <a href="">vector</a>&lt;u8&gt;)
</code></pre>



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

## Function `remove_authentication_key_entry`



<pre><code><b>public</b> entry <b>fun</b> <a href="bitcoin_validator.md#0x3_bitcoin_validator_remove_authentication_key_entry">remove_authentication_key_entry</a>(<a href="">account</a>: &<a href="">signer</a>)
</code></pre>



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

## Function `public_key_to_authentication_key`



<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_validator.md#0x3_bitcoin_validator_public_key_to_authentication_key">public_key_to_authentication_key</a>(public_key: <a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



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

## Function `get_authentication_key_option_from_account`

Get the authentication key option of the given account.


<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_validator.md#0x3_bitcoin_validator_get_authentication_key_option_from_account">get_authentication_key_option_from_account</a>(addr: <b>address</b>): <a href="_Option">option::Option</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;
</code></pre>



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

## Function `is_authentication_key_in_account`

The authentication key exists in account or not.


<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_validator.md#0x3_bitcoin_validator_is_authentication_key_in_account">is_authentication_key_in_account</a>(addr: <b>address</b>): bool
</code></pre>



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

## Function `get_authentication_key_from_account`

Extract the authentication key of the authentication key option.


<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_validator.md#0x3_bitcoin_validator_get_authentication_key_from_account">get_authentication_key_from_account</a>(addr: <b>address</b>): <a href="">vector</a>&lt;u8&gt;
</code></pre>



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

## Function `validate_signature`
Expand Down
104 changes: 1 addition & 103 deletions frameworks/rooch-framework/doc/ethereum_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,14 @@ This module implements Ethereum validator with the ECDSA recoverable signature o
- [Struct `EthereumValidator`](#0x3_ethereum_validator_EthereumValidator)
- [Constants](#@Constants_0)
- [Function `auth_validator_id`](#0x3_ethereum_validator_auth_validator_id)
- [Function `rotate_authentication_key_entry`](#0x3_ethereum_validator_rotate_authentication_key_entry)
- [Function `remove_authentication_key_entry`](#0x3_ethereum_validator_remove_authentication_key_entry)
- [Function `public_key_to_address`](#0x3_ethereum_validator_public_key_to_address)
- [Function `public_key_to_authentication_key`](#0x3_ethereum_validator_public_key_to_authentication_key)
- [Function `get_authentication_key_option_from_account`](#0x3_ethereum_validator_get_authentication_key_option_from_account)
- [Function `is_authentication_key_in_account`](#0x3_ethereum_validator_is_authentication_key_in_account)
- [Function `get_authentication_key_from_account`](#0x3_ethereum_validator_get_authentication_key_from_account)
- [Function `validate_signature`](#0x3_ethereum_validator_validate_signature)
- [Function `validate`](#0x3_ethereum_validator_validate)


<pre><code><b>use</b> <a href="">0x1::debug</a>;
<b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x1::signer</a>;
<b>use</b> <a href="">0x1::vector</a>;
<pre><code><b>use</b> <a href="">0x1::vector</a>;
<b>use</b> <a href="">0x2::features</a>;
<b>use</b> <a href="">0x2::hex</a>;
<b>use</b> <a href="">0x2::signer</a>;
<b>use</b> <a href="">0x2::tx_context</a>;
<b>use</b> <a href="account_authentication.md#0x3_account_authentication">0x3::account_authentication</a>;
<b>use</b> <a href="auth_payload.md#0x3_auth_payload">0x3::auth_payload</a>;
<b>use</b> <a href="auth_validator.md#0x3_auth_validator">0x3::auth_validator</a>;
<b>use</b> <a href="ecdsa_k1.md#0x3_ecdsa_k1">0x3::ecdsa_k1</a>;
Expand All @@ -54,15 +42,6 @@ This module implements Ethereum validator with the ECDSA recoverable signature o
## Constants


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



<pre><code><b>const</b> <a href="ethereum_validator.md#0x3_ethereum_validator_ErrorInvalidPublicKeyLength">ErrorInvalidPublicKeyLength</a>: u64 = 1;
</code></pre>



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

there defines auth validator id for each blockchain
Expand All @@ -84,87 +63,6 @@ there defines auth validator id for each blockchain



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

## Function `rotate_authentication_key_entry`



<pre><code><b>public</b> entry <b>fun</b> <a href="ethereum_validator.md#0x3_ethereum_validator_rotate_authentication_key_entry">rotate_authentication_key_entry</a>(<a href="">account</a>: &<a href="">signer</a>, public_key: <a href="">vector</a>&lt;u8&gt;)
</code></pre>



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

## Function `remove_authentication_key_entry`



<pre><code><b>public</b> entry <b>fun</b> <a href="ethereum_validator.md#0x3_ethereum_validator_remove_authentication_key_entry">remove_authentication_key_entry</a>(<a href="">account</a>: &<a href="">signer</a>)
</code></pre>



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

## Function `public_key_to_address`



<pre><code><b>public</b> <b>fun</b> <a href="ethereum_validator.md#0x3_ethereum_validator_public_key_to_address">public_key_to_address</a>(public_key: <a href="">vector</a>&lt;u8&gt;): <a href="ethereum_address.md#0x3_ethereum_address_ETHAddress">ethereum_address::ETHAddress</a>
</code></pre>



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

## Function `public_key_to_authentication_key`

Get the authentication key of the given public key.


<pre><code><b>public</b> <b>fun</b> <a href="ethereum_validator.md#0x3_ethereum_validator_public_key_to_authentication_key">public_key_to_authentication_key</a>(public_key: <a href="">vector</a>&lt;u8&gt;): <a href="">vector</a>&lt;u8&gt;
</code></pre>



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

## Function `get_authentication_key_option_from_account`

Get the authentication key option of the given account.


<pre><code><b>public</b> <b>fun</b> <a href="ethereum_validator.md#0x3_ethereum_validator_get_authentication_key_option_from_account">get_authentication_key_option_from_account</a>(addr: <b>address</b>): <a href="_Option">option::Option</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;
</code></pre>



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

## Function `is_authentication_key_in_account`

The authentication key exists in account or not.


<pre><code><b>public</b> <b>fun</b> <a href="ethereum_validator.md#0x3_ethereum_validator_is_authentication_key_in_account">is_authentication_key_in_account</a>(addr: <b>address</b>): bool
</code></pre>



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

## Function `get_authentication_key_from_account`

Extract the authentication key of the authentication key option.


<pre><code><b>public</b> <b>fun</b> <a href="ethereum_validator.md#0x3_ethereum_validator_get_authentication_key_from_account">get_authentication_key_from_account</a>(addr: <b>address</b>): <a href="">vector</a>&lt;u8&gt;
</code></pre>



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

## Function `validate_signature`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,39 @@ module rooch_framework::bitcoin_address {
//We need to add the network and address type
abort 0
}


native public fun verify_with_pk (addr: &vector<u8>, pk: &vector<u8>): bool;

#[test]
fun test_verify_with_pk_success() {
// p2tr
let addr = x"626331703878706a706b6339757a6a3264657863786a67397377386c786a6538357861343037307a7063797335383965337266366b3230716d36676a7274";
let pk = x"038e3d29b653e40f5b620f9443ee05222d1e40be58f544b6fed3d464edd54db883";

verify_with_pk(&addr, &pk);

// p2wpkh
let addr = x"6263317139796d6c6e6132656671783561727663737a75363333727a667871373763653963337a33346c";
let pk = x"02481521eb57656db4bc9ec81857e105cc7853fe8cad61be23667bb401840fc7f8";
verify_with_pk(&addr, &pk);

// p2pkh
let addr = x"313531364d67424b5a317438784641726d475a7a796e6354714142526f7665794c47";
let pk = x"02c3bc6ff4dec7f43dd4f587d4dc227fb171755779425ca032e0fcb2f0bb639cc2";
verify_with_pk(&addr, &pk);

// p2sh-p2wpkh
let addr = x"33385972544d547051345a55736a766373776557676d48696b556d67466356374435";
let pk = x"02ebdc1107552f81d188a2c63806cb6fa5d734eaa7316a85dc1f608fcaee412b72";
verify_with_pk(&addr, &pk);
}

#[test]
#[expected_failure(location=Self, abort_code = 3)]
fun test_validate_signature_fail() {
let addr = x"616331703878706a706b6339757a6a3264657863786a67397377386c786a6538357861343037307a7063797335383965337266366b3230716d36676a7274";
let pk = x"038e3d29b653e40f5b620f9443ee05222d1e40be58f544b6fed3d464edd54db883";

verify_with_pk(&addr, &pk);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
module rooch_framework::auth_payload {

use moveos_std::bcs;
use rooch_framework::multichain_address;

#[data_struct]
struct AuthPayload has copy, store, drop {
// msg sin
// Message sin
sign: vector<u8>,
// Some wallets add magic prefixes, such as unisat adding 'Bitcoin Signed Message:\n'
sign_info_prefix: vector<u8>,
// Description of a user-defined signature
sign_info: vector<u8>,
// Public key of address
public_key: vector<u8>,
multi_address: vector<u8>,
// TODO: use multi_address, currently, sdk does not support eth, so test use from_address
// Wallet address
from_address: vector<u8>
}

Expand All @@ -40,10 +39,6 @@ module rooch_framework::auth_payload {
payload.public_key
}

public fun multi_address(payload: AuthPayload): multichain_address::MultiChainAddress {
multichain_address::from_bytes(payload.multi_address)
}

public fun from_address(payload: AuthPayload): vector<u8> {
payload.from_address
}
Expand Down
Loading

0 comments on commit 4a5e844

Please sign in to comment.