Skip to content

Commit

Permalink
[asset-mapping] Since the parameters of the move entry function do no…
Browse files Browse the repository at this point in the history
…t support vector<vector<u8>>, we need to put all the data into a separate vector<u8> parameter when passing the proof parameter `sibilings`, and split it with `|` to ensure that the move layer can receive it smoothly.
  • Loading branch information
welbon committed Jan 7, 2025
1 parent 4587bca commit 6dfdb15
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 29 deletions.
22 changes: 14 additions & 8 deletions vm/framework/cached-packages/src/starcoin_framework_sdk_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ pub enum EntryFunctionCall {
AssetMappingAssignToAccountWithProof {
receiper: AccountAddress,
old_token_str: Vec<u8>,
proof_path: Vec<u8>,
proof_path_hash: Vec<u8>,
proof_value_hash: Vec<u8>,
proof_siblings: Vec<u8>,
amount: u64,
},
Expand Down Expand Up @@ -654,13 +655,15 @@ impl EntryFunctionCall {
AssetMappingAssignToAccountWithProof {
receiper,
old_token_str,
proof_path,
proof_path_hash,
proof_value_hash,
proof_siblings,
amount,
} => asset_mapping_assign_to_account_with_proof(
receiper,
old_token_str,
proof_path,
proof_path_hash,
proof_value_hash,
proof_siblings,
amount,
),
Expand Down Expand Up @@ -1281,7 +1284,8 @@ pub fn account_rotate_authentication_key_with_rotation_capability(
pub fn asset_mapping_assign_to_account_with_proof(
receiper: AccountAddress,
old_token_str: Vec<u8>,
proof_path: Vec<u8>,
proof_path_hash: Vec<u8>,
proof_value_hash: Vec<u8>,
proof_siblings: Vec<u8>,
amount: u64,
) -> TransactionPayload {
Expand All @@ -1295,7 +1299,8 @@ pub fn asset_mapping_assign_to_account_with_proof(
vec![
bcs::to_bytes(&receiper).unwrap(),
bcs::to_bytes(&old_token_str).unwrap(),
bcs::to_bytes(&proof_path).unwrap(),
bcs::to_bytes(&proof_path_hash).unwrap(),
bcs::to_bytes(&proof_value_hash).unwrap(),
bcs::to_bytes(&proof_siblings).unwrap(),
bcs::to_bytes(&amount).unwrap(),
],
Expand Down Expand Up @@ -2578,9 +2583,10 @@ mod decoder {
Some(EntryFunctionCall::AssetMappingAssignToAccountWithProof {
receiper: bcs::from_bytes(script.args().get(0)?).ok()?,
old_token_str: bcs::from_bytes(script.args().get(1)?).ok()?,
proof_path: bcs::from_bytes(script.args().get(2)?).ok()?,
proof_siblings: bcs::from_bytes(script.args().get(3)?).ok()?,
amount: bcs::from_bytes(script.args().get(4)?).ok()?,
proof_path_hash: bcs::from_bytes(script.args().get(2)?).ok()?,
proof_value_hash: bcs::from_bytes(script.args().get(3)?).ok()?,
proof_siblings: bcs::from_bytes(script.args().get(4)?).ok()?,
amount: bcs::from_bytes(script.args().get(5)?).ok()?,
})
} else {
None
Expand Down
17 changes: 8 additions & 9 deletions vm/framework/starcoin-framework/doc/asset_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ with proof verification.
- [Function `calculation_proof`](#0x1_asset_mapping_calculation_proof)


<pre><code><b>use</b> <a href="../../move-stdlib/doc/bcs.md#0x1_bcs">0x1::bcs</a>;
<b>use</b> <a href="coin.md#0x1_coin">0x1::coin</a>;
<pre><code><b>use</b> <a href="coin.md#0x1_coin">0x1::coin</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="fungible_asset.md#0x1_fungible_asset">0x1::fungible_asset</a>;
<b>use</b> <a href="../../move-stdlib/doc/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="object.md#0x1_object">0x1::object</a>;
<b>use</b> <a href="primary_fungible_store.md#0x1_primary_fungible_store">0x1::primary_fungible_store</a>;
<b>use</b> <a href="../../move-stdlib/doc/signer.md#0x1_signer">0x1::signer</a>;
Expand Down Expand Up @@ -369,7 +367,7 @@ Retrieves the balance for a specific token type



<pre><code><b>public</b> entry <b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_assign_to_account_with_proof">assign_to_account_with_proof</a>(token_issuer: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, receiper: <b>address</b>, old_token_str: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, proof_path: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, proof_siblings: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, amount: u64)
<pre><code><b>public</b> entry <b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_assign_to_account_with_proof">assign_to_account_with_proof</a>(token_issuer: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>, receiper: <b>address</b>, old_token_str: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, proof_path_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, proof_value_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, proof_siblings: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, amount: u64)
</code></pre>


Expand All @@ -382,7 +380,8 @@ Retrieves the balance for a specific token type
token_issuer: &<a href="../../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
receiper: <b>address</b>,
old_token_str: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
proof_path: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
proof_path_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
proof_value_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
proof_siblings: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
amount: u64
) <b>acquires</b> <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingPool">AssetMappingPool</a>, <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingCoinType">AssetMappingCoinType</a>, <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingProof">AssetMappingProof</a> {
Expand All @@ -393,7 +392,7 @@ Retrieves the balance for a specific token type

// Verify that the token type of the request mapping is the passed-in verification type
<b>assert</b>!(
<a href="asset_mapping.md#0x1_asset_mapping_calculation_proof">calculation_proof</a>(proof_path, amount, <a href="asset_mapping.md#0x1_asset_mapping_split_proof_siblings_from_vec">split_proof_siblings_from_vec</a>(proof_siblings)),
<a href="asset_mapping.md#0x1_asset_mapping_calculation_proof">calculation_proof</a>(proof_path_hash, proof_value_hash, <a href="asset_mapping.md#0x1_asset_mapping_split_proof_siblings_from_vec">split_proof_siblings_from_vec</a>(proof_siblings)),
<a href="../../move-stdlib/doc/error.md#0x1_error_unauthenticated">error::unauthenticated</a>(<a href="asset_mapping.md#0x1_asset_mapping_EINVALID_NOT_PROOF">EINVALID_NOT_PROOF</a>)
);

Expand Down Expand Up @@ -486,7 +485,7 @@ Requirements:
Computes and verifies the provided proof


<pre><code><b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_calculation_proof">calculation_proof</a>(proof_path_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, amount: u64, proof_siblings: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;): bool
<pre><code><b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_calculation_proof">calculation_proof</a>(proof_path_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, blob_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;, proof_siblings: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;): bool
</code></pre>


Expand All @@ -497,14 +496,14 @@ Computes and verifies the provided proof

<pre><code><b>fun</b> <a href="asset_mapping.md#0x1_asset_mapping_calculation_proof">calculation_proof</a>(
proof_path_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
amount: u64,
blob_hash: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
proof_siblings: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;
): bool <b>acquires</b> <a href="asset_mapping.md#0x1_asset_mapping_AssetMappingProof">AssetMappingProof</a> {
<b>let</b> expect_proof_root =
<b>borrow_global_mut</b>&lt;<a href="asset_mapping.md#0x1_asset_mapping_AssetMappingProof">AssetMappingProof</a>&gt;(<a href="system_addresses.md#0x1_system_addresses_get_starcoin_framework">system_addresses::get_starcoin_framework</a>()).proof_root;
<b>let</b> actual_root = <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_computer_root_hash">starcoin_proof_verifier::computer_root_hash</a>(
proof_path_hash,
<a href="../../move-stdlib/doc/hash.md#0x1_hash_sha3_256">hash::sha3_256</a>(<a href="../../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(&amount)),
blob_hash,
proof_siblings
);
expect_proof_root == actual_root
Expand Down
81 changes: 81 additions & 0 deletions vm/framework/starcoin-framework/doc/starcoin_proof.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
- [Function `verify_on`](#0x1_starcoin_proof_verifier_verify_on)
- [Function `verify`](#0x1_starcoin_proof_verifier_verify)
- [Function `computer_root_hash`](#0x1_starcoin_proof_verifier_computer_root_hash)
- [Function `splite_symbol`](#0x1_starcoin_proof_verifier_splite_symbol)
- [Function `split`](#0x1_starcoin_proof_verifier_split)


<pre><code><b>use</b> <a href="../../move-stdlib/doc/hash.md#0x1_hash">0x1::hash</a>;
<b>use</b> <a href="starcoin_proof_bit.md#0x1_starcoin_proof_bit">0x1::starcoin_proof_bit</a>;
<b>use</b> <a href="starcoin_proof_structured_hash.md#0x1_starcoin_proof_structured_hash">0x1::starcoin_proof_structured_hash</a>;
<b>use</b> <a href="../../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>;
</code></pre>


Expand Down Expand Up @@ -86,6 +89,15 @@
## Constants


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



<pre><code><b>const</b> <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_DELMITER">DELMITER</a>: u8 = 124;
</code></pre>



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


Expand Down Expand Up @@ -241,6 +253,75 @@



</details>

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

## Function `splite_symbol`



<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_splite_symbol">splite_symbol</a>(): u8
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_splite_symbol">splite_symbol</a>(): u8 {
<a href="starcoin_proof.md#0x1_starcoin_proof_verifier_DELMITER">DELMITER</a>
}
</code></pre>



</details>

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

## Function `split`



<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_split">split</a>(input: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_split">split</a>(input: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt; {
<b>let</b> result: <a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;<a href="../../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt; = <a href="../../move-stdlib/doc/vector.md#0x1_vector_empty">vector::empty</a>();
<b>let</b> current_segment = <a href="../../move-stdlib/doc/vector.md#0x1_vector_empty">vector::empty</a>&lt;u8&gt;();
<b>let</b> i = 0;
<b>let</b> len = <a href="../../move-stdlib/doc/vector.md#0x1_vector_length">vector::length</a>(&input);

<b>while</b> (i &lt; len) {
<b>let</b> current_byte = *<a href="../../move-stdlib/doc/vector.md#0x1_vector_borrow">vector::borrow</a>(&input, i);
<b>if</b> (current_byte == <a href="starcoin_proof.md#0x1_starcoin_proof_verifier_DELMITER">DELMITER</a>) {
<b>if</b> (!<a href="../../move-stdlib/doc/vector.md#0x1_vector_is_empty">vector::is_empty</a>(&current_segment)) {
<a href="../../move-stdlib/doc/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> result, current_segment);
current_segment = <a href="../../move-stdlib/doc/vector.md#0x1_vector_empty">vector::empty</a>();
};
} <b>else</b> {
<a href="../../move-stdlib/doc/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> current_segment, current_byte);
};
i = i + 1;
};

<b>if</b> (!<a href="../../move-stdlib/doc/vector.md#0x1_vector_is_empty">vector::is_empty</a>(&current_segment)) {
<a href="../../move-stdlib/doc/vector.md#0x1_vector_push_back">vector::push_back</a>(&<b>mut</b> result, current_segment);
};
result
}
</code></pre>



</details>


Expand Down
40 changes: 28 additions & 12 deletions vm/framework/starcoin-framework/sources/asset_mapping.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
/// with proof verification.
module starcoin_framework::asset_mapping {

use std::bcs;
use std::error;
use std::hash;
use std::signer;
use std::string;
use std::vector;
Expand All @@ -26,6 +24,8 @@ module starcoin_framework::asset_mapping {
#[test_only]
use starcoin_framework::starcoin_coin::{Self, STC};
#[test_only]
use starcoin_framework::starcoin_proof_verifier::splite_symbol;
#[test_only]
use starcoin_std::type_info;

/// AssetMappingStore represents a store for mapped assets
Expand Down Expand Up @@ -274,15 +274,32 @@ module starcoin_framework::asset_mapping {

#[test(framework= @starcoin_framework)]
fun test_asset_mapping_calculation_proof(framework: &signer) acquires AssetMappingProof {
let siblings = vector::empty<vector<u8>>();
vector::push_back(&mut siblings, x"cfb1462d4fc72f736eab2a56b2bf72ca6ad1c4e8c79557046a8b0adce047f007");
vector::push_back(&mut siblings, x"5350415253455f4d45524b4c455f504c414345484f4c4445525f484153480000");
vector::push_back(&mut siblings, x"5ca9febe74c7fde3fdcf2bd464de6d8899a0a13d464893aada2714c6fa774f9d");
vector::push_back(&mut siblings, x"1519a398fed69687cabf51adf831f0ee1650aaf79775d00135fc70f55a73e151");
vector::push_back(&mut siblings, x"50ce5c38983ba2eb196acd44e0aaedf040b1437ad1106e05ca452d7e27e4e03f");
vector::push_back(&mut siblings, x"55ed28435637a061a6dd9e20b72849199cd36184570f976b7e306a27bebf2fdf");
vector::push_back(&mut siblings, x"0dc23e31614798a6f67659b0b808b3eadc3b13a2a7bc03580a9e3004e45c2e6c");
vector::push_back(&mut siblings, x"83bed048bc0bc452c98cb0e9f1cc0f691919eaf756864fc44940c2d1e01da92a");
let siblings_data = vector::empty<u8>();
vector::append(&mut siblings_data, x"cfb1462d4fc72f736eab2a56b2bf72ca6ad1c4e8c79557046a8b0adce047f007");
vector::push_back(&mut siblings_data, splite_symbol());

vector::append(&mut siblings_data, x"5350415253455f4d45524b4c455f504c414345484f4c4445525f484153480000");
vector::push_back(&mut siblings_data, splite_symbol());

vector::append(&mut siblings_data, x"5ca9febe74c7fde3fdcf2bd464de6d8899a0a13d464893aada2714c6fa774f9d");
vector::push_back(&mut siblings_data, splite_symbol());

vector::append(&mut siblings_data, x"1519a398fed69687cabf51adf831f0ee1650aaf79775d00135fc70f55a73e151");
vector::push_back(&mut siblings_data, splite_symbol());

vector::append(&mut siblings_data, x"50ce5c38983ba2eb196acd44e0aaedf040b1437ad1106e05ca452d7e27e4e03f");
vector::push_back(&mut siblings_data, splite_symbol());

vector::append(&mut siblings_data, x"55ed28435637a061a6dd9e20b72849199cd36184570f976b7e306a27bebf2fdf");
vector::push_back(&mut siblings_data, splite_symbol());

vector::append(&mut siblings_data, x"0dc23e31614798a6f67659b0b808b3eadc3b13a2a7bc03580a9e3004e45c2e6c");
vector::push_back(&mut siblings_data, splite_symbol());

vector::append(&mut siblings_data, x"83bed048bc0bc452c98cb0e9f1cc0f691919eaf756864fc44940c2d1e01da92a");
vector::push_back(&mut siblings_data, splite_symbol());

let siblings = starcoin_proof_verifier::split(siblings_data);

let element_key = x"4cc8bd9df94b37c233555d9a3bba0a712c3c709f047486d1e624b2bcd3b83266";
Self::initialize(framework, x"f65860f575bf2a198c069adb4e7872037e3a329b63ef617e40afa39b87b067c8");
Expand Down Expand Up @@ -316,5 +333,4 @@ module starcoin_framework::asset_mapping {
);
debug::print(&std::string::utf8(b"asset_mapping::test_asset_mapping_coin_type_verify | exited"));
}

}
56 changes: 56 additions & 0 deletions vm/framework/starcoin-framework/sources/proof/starcoin_proof.move
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ module starcoin_framework::starcoin_proof_verifier {
current_hash
}


// ASCII for '|'
const DELMITER: u8 = 124;

public fun splite_symbol(): u8 {
DELMITER
}

public fun split(input: vector<u8>): vector<vector<u8>> {
let result: vector<vector<u8>> = vector::empty();
let current_segment = vector::empty<u8>();
let i = 0;
let len = vector::length(&input);

while (i < len) {
let current_byte = *vector::borrow(&input, i);
if (current_byte == DELMITER) {
if (!vector::is_empty(&current_segment)) {
vector::push_back(&mut result, current_segment);
current_segment = vector::empty();
};
} else {
vector::push_back(&mut current_segment, current_byte);
};
i = i + 1;
};

if (!vector::is_empty(&current_segment)) {
vector::push_back(&mut result, current_segment);
};
result
}

#[test]
public fun test_starcoin_proof_verify_is_expect_root() {
let siblings = vector::empty<vector<u8>>();
Expand All @@ -89,6 +122,29 @@ module starcoin_framework::starcoin_proof_verifier {
);
assert!(actual_root_hash == expect_root_hash, 1000);
}

#[test]
public fun test_starcoin_proof_split() {
// Test case 1: Normal split
let input1 = b"hello|world|test";
let result1 = split(input1);
assert!(vector::length(&result1) == 3, 10010);

// Test case 2: Empty segments
let input2 = b"||test||";
let result2 = split(input2);
assert!(vector::length(&result2) == 1, 10011);

// Test case 3: Single segment
let input3 = b"hello";
let result3 = split(input3);
assert!(vector::length(&result3) == 1, 10012);

// Test case 4: Empty input
let input4 = b"";
let result4 = split(input4);
assert!(vector::length(&result4) == 0, 10013);
}
}


0 comments on commit 6dfdb15

Please sign in to comment.