Skip to content

Commit

Permalink
[Framework] Support transfer coin to multichain address (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar authored Sep 28, 2023
1 parent b097c70 commit 43fb0e5
Show file tree
Hide file tree
Showing 22 changed files with 957 additions and 159 deletions.
1 change: 1 addition & 0 deletions crates/rooch-framework/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This is the reference documentation of the Rooch Framework.
- [`0x3::gas_coin`](gas_coin.md#0x3_gas_coin)
- [`0x3::genesis`](genesis.md#0x3_genesis)
- [`0x3::hash`](hash.md#0x3_hash)
- [`0x3::multichain_address`](multichain_address.md#0x3_multichain_address)
- [`0x3::native_validator`](native_validator.md#0x3_native_validator)
- [`0x3::schnorr`](schnorr.md#0x3_schnorr)
- [`0x3::session_key`](session_key.md#0x3_session_key)
Expand Down
129 changes: 28 additions & 101 deletions crates/rooch-framework/doc/address_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@



- [Struct `MultiChainAddress`](#0x3_address_mapping_MultiChainAddress)
- [Resource `AddressMapping`](#0x3_address_mapping_AddressMapping)
- [Constants](#@Constants_0)
- [Function `is_rooch_address`](#0x3_address_mapping_is_rooch_address)
- [Function `genesis_init`](#0x3_address_mapping_genesis_init)
- [Function `resolve`](#0x3_address_mapping_resolve)
- [Function `resolve_or_generate`](#0x3_address_mapping_resolve_or_generate)
- [Function `exists_mapping`](#0x3_address_mapping_exists_mapping)
Expand All @@ -20,49 +18,15 @@
<b>use</b> <a href="">0x1::signer</a>;
<b>use</b> <a href="">0x2::account_storage</a>;
<b>use</b> <a href="">0x2::bcs</a>;
<b>use</b> <a href="">0x2::signer</a>;
<b>use</b> <a href="">0x2::storage_context</a>;
<b>use</b> <a href="">0x2::table</a>;
<b>use</b> <a href="">0x2::tx_context</a>;
<b>use</b> <a href="core_addresses.md#0x3_core_addresses">0x3::core_addresses</a>;
<b>use</b> <a href="hash.md#0x3_hash">0x3::hash</a>;
<b>use</b> <a href="multichain_address.md#0x3_multichain_address">0x3::multichain_address</a>;
</code></pre>



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

## Struct `MultiChainAddress`



<pre><code><b>struct</b> <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a> <b>has</b> <b>copy</b>, drop, store
</code></pre>



<details>
<summary>Fields</summary>


<dl>
<dt>
<code>multichain_id: u64</code>
</dt>
<dd>

</dd>
<dt>
<code>raw_address: <a href="">vector</a>&lt;u8&gt;</code>
</dt>
<dd>

</dd>
</dl>


</details>

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

## Resource `AddressMapping`
Expand All @@ -80,7 +44,7 @@

<dl>
<dt>
<code>mapping: <a href="_Table">table::Table</a>&lt;<a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>, <b>address</b>&gt;</code>
<code>mapping: <a href="_Table">table::Table</a>&lt;<a href="multichain_address.md#0x3_multichain_address_MultiChainAddress">multichain_address::MultiChainAddress</a>, <b>address</b>&gt;</code>
</dt>
<dd>

Expand All @@ -90,54 +54,13 @@

</details>

<a name="@Constants_0"></a>

## Constants


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



<pre><code><b>const</b> <a href="address_mapping.md#0x3_address_mapping_MULTICHAIN_ID_BITCOIN">MULTICHAIN_ID_BITCOIN</a>: u64 = 0;
</code></pre>



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



<pre><code><b>const</b> <a href="address_mapping.md#0x3_address_mapping_MULTICHAIN_ID_ETHER">MULTICHAIN_ID_ETHER</a>: u64 = 60;
</code></pre>



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



<pre><code><b>const</b> <a href="address_mapping.md#0x3_address_mapping_MULTICHAIN_ID_NOSTR">MULTICHAIN_ID_NOSTR</a>: u64 = 1237;
</code></pre>



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



<pre><code><b>const</b> <a href="address_mapping.md#0x3_address_mapping_MULTICHAIN_ID_ROOCH">MULTICHAIN_ID_ROOCH</a>: u64 = 20230101;
</code></pre>



<a name="0x3_address_mapping_is_rooch_address"></a>
<a name="0x3_address_mapping_genesis_init"></a>

## Function `is_rooch_address`
## Function `genesis_init`



<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_is_rooch_address">is_rooch_address</a>(maddress: &<a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>): bool
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_genesis_init">genesis_init</a>(ctx: &<b>mut</b> <a href="_StorageContext">storage_context::StorageContext</a>, genesis_account: &<a href="">signer</a>)
</code></pre>


Expand All @@ -146,8 +69,12 @@
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_is_rooch_address">is_rooch_address</a>(maddress: &<a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>) : bool{
maddress.multichain_id == <a href="address_mapping.md#0x3_address_mapping_MULTICHAIN_ID_ROOCH">MULTICHAIN_ID_ROOCH</a>
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_genesis_init">genesis_init</a>(ctx: &<b>mut</b> StorageContext, genesis_account: &<a href="">signer</a>) {
<b>let</b> tx_ctx = <a href="_tx_context_mut">storage_context::tx_context_mut</a>(ctx);
<b>let</b> mapping = <a href="_new">table::new</a>&lt;MultiChainAddress, <b>address</b>&gt;(tx_ctx);
<a href="_global_move_to">account_storage::global_move_to</a>(ctx, genesis_account, <a href="address_mapping.md#0x3_address_mapping_AddressMapping">AddressMapping</a>{
mapping,
});
}
</code></pre>

Expand All @@ -162,7 +89,7 @@
Resolve a multi-chain address to a rooch address


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve">resolve</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>): <a href="_Option">option::Option</a>&lt;<b>address</b>&gt;
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve">resolve</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, maddress: <a href="multichain_address.md#0x3_multichain_address_MultiChainAddress">multichain_address::MultiChainAddress</a>): <a href="_Option">option::Option</a>&lt;<b>address</b>&gt;
</code></pre>


Expand All @@ -171,9 +98,9 @@ Resolve a multi-chain address to a rooch address
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve">resolve</a>(ctx: &StorageContext, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>): Option&lt;<b>address</b>&gt; {
<b>if</b> (<a href="address_mapping.md#0x3_address_mapping_is_rooch_address">is_rooch_address</a>(&maddress)) {
<b>return</b> <a href="_some">option::some</a>(moveos_std::bcs::to_address(maddress.raw_address))
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve">resolve</a>(ctx: &StorageContext, maddress: MultiChainAddress): Option&lt;<b>address</b>&gt; {
<b>if</b> (<a href="multichain_address.md#0x3_multichain_address_is_rooch_address">multichain_address::is_rooch_address</a>(&maddress)) {
<b>return</b> <a href="_some">option::some</a>(<a href="multichain_address.md#0x3_multichain_address_into_rooch_address">multichain_address::into_rooch_address</a>(maddress))
};
<b>let</b> am = <a href="_global_borrow">account_storage::global_borrow</a>&lt;<a href="address_mapping.md#0x3_address_mapping_AddressMapping">AddressMapping</a>&gt;(ctx, @rooch_framework);
<b>if</b>(<a href="_contains">table::contains</a>(&am.mapping, maddress)){
Expand All @@ -196,7 +123,7 @@ Resolve a multi-chain address to a rooch address
Resolve a multi-chain address to a rooch address, if not exists, generate a new rooch address


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve_or_generate">resolve_or_generate</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>): <b>address</b>
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve_or_generate">resolve_or_generate</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, maddress: <a href="multichain_address.md#0x3_multichain_address_MultiChainAddress">multichain_address::MultiChainAddress</a>): <b>address</b>
</code></pre>


Expand All @@ -205,10 +132,10 @@ Resolve a multi-chain address to a rooch address, if not exists, generate a new
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve_or_generate">resolve_or_generate</a>(ctx: &StorageContext, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>): <b>address</b> {
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve_or_generate">resolve_or_generate</a>(ctx: &StorageContext, maddress: MultiChainAddress): <b>address</b> {
<b>let</b> addr = <a href="address_mapping.md#0x3_address_mapping_resolve">resolve</a>(ctx, maddress);
<b>if</b>(<a href="_is_none">option::is_none</a>(&addr)){
<a href="address_mapping.md#0x3_address_mapping_generate_rooch_address">generate_rooch_address</a>(maddress)
<a href="address_mapping.md#0x3_address_mapping_generate_rooch_address">generate_rooch_address</a>(&maddress)
}<b>else</b>{
<a href="_extract">option::extract</a>(&<b>mut</b> addr)
}
Expand All @@ -226,7 +153,7 @@ Resolve a multi-chain address to a rooch address, if not exists, generate a new
Check if a multi-chain address is bound to a rooch address


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_exists_mapping">exists_mapping</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>): bool
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_exists_mapping">exists_mapping</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, maddress: <a href="multichain_address.md#0x3_multichain_address_MultiChainAddress">multichain_address::MultiChainAddress</a>): bool
</code></pre>


Expand All @@ -235,8 +162,8 @@ Check if a multi-chain address is bound to a rooch address
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_exists_mapping">exists_mapping</a>(ctx: &StorageContext, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>): bool {
<b>if</b> (<a href="address_mapping.md#0x3_address_mapping_is_rooch_address">is_rooch_address</a>(&maddress)) {
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_exists_mapping">exists_mapping</a>(ctx: &StorageContext, maddress: MultiChainAddress): bool {
<b>if</b> (<a href="multichain_address.md#0x3_multichain_address_is_rooch_address">multichain_address::is_rooch_address</a>(&maddress)) {
<b>return</b> <b>true</b>
};
<b>let</b> am = <a href="_global_borrow">account_storage::global_borrow</a>&lt;<a href="address_mapping.md#0x3_address_mapping_AddressMapping">AddressMapping</a>&gt;(ctx, @rooch_framework);
Expand All @@ -256,7 +183,7 @@ Bind a multi-chain address to the sender's rooch address
The caller need to ensure the relationship between the multi-chain address and the rooch address


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind">bind</a>(ctx: &<b>mut</b> <a href="_StorageContext">storage_context::StorageContext</a>, sender: &<a href="">signer</a>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>)
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind">bind</a>(ctx: &<b>mut</b> <a href="_StorageContext">storage_context::StorageContext</a>, sender: &<a href="">signer</a>, maddress: <a href="multichain_address.md#0x3_multichain_address_MultiChainAddress">multichain_address::MultiChainAddress</a>)
</code></pre>


Expand All @@ -265,7 +192,7 @@ The caller need to ensure the relationship between the multi-chain address and t
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind">bind</a>(ctx: &<b>mut</b> StorageContext, sender: &<a href="">signer</a>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>) {
<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind">bind</a>(ctx: &<b>mut</b> StorageContext, sender: &<a href="">signer</a>, maddress: MultiChainAddress) {
<a href="address_mapping.md#0x3_address_mapping_bind_no_check">bind_no_check</a>(ctx, <a href="_address_of">signer::address_of</a>(sender), maddress);
}
</code></pre>
Expand All @@ -281,7 +208,7 @@ The caller need to ensure the relationship between the multi-chain address and t
Bind a rooch address to a multi-chain address


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind_no_check">bind_no_check</a>(ctx: &<b>mut</b> <a href="_StorageContext">storage_context::StorageContext</a>, rooch_address: <b>address</b>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind_no_check">bind_no_check</a>(ctx: &<b>mut</b> <a href="_StorageContext">storage_context::StorageContext</a>, rooch_address: <b>address</b>, maddress: <a href="multichain_address.md#0x3_multichain_address_MultiChainAddress">multichain_address::MultiChainAddress</a>)
</code></pre>


Expand All @@ -290,14 +217,14 @@ Bind a rooch address to a multi-chain address
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind_no_check">bind_no_check</a>(ctx: &<b>mut</b> StorageContext, rooch_address: <b>address</b>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>) {
<b>if</b>(<a href="address_mapping.md#0x3_address_mapping_is_rooch_address">is_rooch_address</a>(&maddress)){
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_bind_no_check">bind_no_check</a>(ctx: &<b>mut</b> StorageContext, rooch_address: <b>address</b>, maddress: MultiChainAddress) {
<b>if</b>(<a href="multichain_address.md#0x3_multichain_address_is_rooch_address">multichain_address::is_rooch_address</a>(&maddress)){
//Do nothing <b>if</b> the multi-chain <b>address</b> is a rooch <b>address</b>
<b>return</b>
};
<b>let</b> am = <a href="_global_borrow_mut">account_storage::global_borrow_mut</a>&lt;<a href="address_mapping.md#0x3_address_mapping_AddressMapping">AddressMapping</a>&gt;(ctx, @rooch_framework);
<a href="_add">table::add</a>(&<b>mut</b> am.mapping, maddress, rooch_address);
//TODO matienance the reverse mapping rooch_address -&gt; <a href="">vector</a>&lt;<a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>&gt;
//TODO matienance the reverse mapping rooch_address -&gt; <a href="">vector</a>&lt;MultiChainAddress&gt;
}
</code></pre>

Expand Down
28 changes: 28 additions & 0 deletions crates/rooch-framework/doc/bitcoin_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Constants](#@Constants_0)
- [Function `new_legacy`](#0x3_bitcoin_address_new_legacy)
- [Function `new_bech32`](#0x3_bitcoin_address_new_bech32)
- [Function `from_bytes`](#0x3_bitcoin_address_from_bytes)
- [Function `as_bytes`](#0x3_bitcoin_address_as_bytes)
- [Function `into_bytes`](#0x3_bitcoin_address_into_bytes)
- [Function `create_p2pkh_address`](#0x3_bitcoin_address_create_p2pkh_address)
Expand Down Expand Up @@ -244,6 +245,33 @@ error code



</details>

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

## Function `from_bytes`



<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_address.md#0x3_bitcoin_address_from_bytes">from_bytes</a>(bytes: <a href="">vector</a>&lt;u8&gt;): <a href="bitcoin_address.md#0x3_bitcoin_address_BTCAddress">bitcoin_address::BTCAddress</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_address.md#0x3_bitcoin_address_from_bytes">from_bytes</a>(bytes: <a href="">vector</a>&lt;u8&gt;): <a href="bitcoin_address.md#0x3_bitcoin_address_BTCAddress">BTCAddress</a> {
//TODO check the <b>address</b> bytes.
<a href="bitcoin_address.md#0x3_bitcoin_address_BTCAddress">BTCAddress</a> {
bytes: bytes,
}
}
</code></pre>



</details>

<a name="0x3_bitcoin_address_as_bytes"></a>
Expand Down
40 changes: 40 additions & 0 deletions crates/rooch-framework/doc/ethereum_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Struct `ETHAddress`](#0x3_ethereum_address_ETHAddress)
- [Constants](#@Constants_0)
- [Function `new`](#0x3_ethereum_address_new)
- [Function `from_bytes`](#0x3_ethereum_address_from_bytes)
- [Function `as_bytes`](#0x3_ethereum_address_as_bytes)
- [Function `into_bytes`](#0x3_ethereum_address_into_bytes)

Expand Down Expand Up @@ -69,6 +70,15 @@



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



<pre><code><b>const</b> <a href="ethereum_address.md#0x3_ethereum_address_ErrorInvaidAddresBytes">ErrorInvaidAddresBytes</a>: u64 = 2;
</code></pre>



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

error code
Expand Down Expand Up @@ -132,6 +142,36 @@ error code



</details>

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

## Function `from_bytes`



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



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="ethereum_address.md#0x3_ethereum_address_from_bytes">from_bytes</a>(bytes: <a href="">vector</a>&lt;u8&gt;): <a href="ethereum_address.md#0x3_ethereum_address_ETHAddress">ETHAddress</a> {
<b>assert</b>!(
<a href="_length">vector::length</a>(&bytes) == <a href="ethereum_address.md#0x3_ethereum_address_ETHEREUM_ADDR_LENGTH">ETHEREUM_ADDR_LENGTH</a>,
<a href="_invalid_argument">error::invalid_argument</a>(<a href="ethereum_address.md#0x3_ethereum_address_ErrorInvaidAddresBytes">ErrorInvaidAddresBytes</a>)
);
<a href="ethereum_address.md#0x3_ethereum_address_ETHAddress">ETHAddress</a> {
bytes: bytes,
}
}
</code></pre>



</details>

<a name="0x3_ethereum_address_as_bytes"></a>
Expand Down
1 change: 1 addition & 0 deletions crates/rooch-framework/doc/genesis.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x2::storage_context</a>;
<b>use</b> <a href="account.md#0x3_account">0x3::account</a>;
<b>use</b> <a href="address_mapping.md#0x3_address_mapping">0x3::address_mapping</a>;
<b>use</b> <a href="auth_validator_registry.md#0x3_auth_validator_registry">0x3::auth_validator_registry</a>;
<b>use</b> <a href="builtin_validators.md#0x3_builtin_validators">0x3::builtin_validators</a>;
<b>use</b> <a href="chain_id.md#0x3_chain_id">0x3::chain_id</a>;
Expand Down
Loading

0 comments on commit 43fb0e5

Please sign in to comment.