Skip to content

Commit

Permalink
[asset-mapping] 1. append starcoin verify into asset_mapping; 2. bind…
Browse files Browse the repository at this point in the history
… old token string with Object<Metadata>; 3. Append test `test_asset_mapping_proof_coin_type_name`
  • Loading branch information
welbon committed Jan 6, 2025
1 parent c71ae37 commit 21f57e3
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 85 deletions.
33 changes: 31 additions & 2 deletions executor/tests/executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use anyhow::anyhow;
use anyhow::Result;
use forkable_jellyfish_merkle::node_type::SparseMerkleLeafNode;
use forkable_jellyfish_merkle::RawKey;
use sha3::{Digest, Sha3_256};
use starcoin_crypto::hash::PlainCryptoHash;
use starcoin_crypto::HashValue;
Expand All @@ -21,7 +22,7 @@ use starcoin_types::account::peer_to_peer_txn;
use starcoin_types::account::Account;
use starcoin_types::account_config::G_STC_TOKEN_CODE;
use starcoin_types::identifier::Identifier;
use starcoin_types::language_storage::{ModuleId, StructTag, CORE_CODE_ADDRESS};
use starcoin_types::language_storage::{ModuleId, StructTag, TypeTag, CORE_CODE_ADDRESS};
use starcoin_types::transaction::{EntryFunction, RawUserTransaction, TransactionArgument};
use starcoin_types::{
account_config, block_metadata::BlockMetadata, transaction::Transaction,
Expand Down Expand Up @@ -1200,14 +1201,42 @@ fn test_sha3_256_diffrent_with_crypto_macro() -> Result<()> {
HashValue::sha3_256_of(STARCOIN_HASH_PREFIX).as_slice(),
ser.as_slice(),
]
.concat();
.concat();
let move_hash = HashValue::sha3_256_of(&hash_vec[..]);
println!(
"test_sha3_256_diffrent_with_crypto_macro | sha3 crypto {:?}",
move_hash,
);

assert_eq!(move_hash, smt_hash, "Failed to get the same hash");
Ok(())
}

#[test]
fn test_asset_mapping_for_specified_coin_type() -> Result<()> {
let (_chain_state, _net) = prepare_genesis();
let chain_id_struct_tag = StructTag {
address: CORE_CODE_ADDRESS,
module: Identifier::new("coin").unwrap(),
name: Identifier::new("CoinStore").unwrap(),
type_args: vec![TypeTag::Struct(Box::new(StructTag {
address: CORE_CODE_ADDRESS,
module: Identifier::new("starcoin_coin").unwrap(),
name: Identifier::new("STC").unwrap(),
type_args: vec![],
}))],
};

let access_path = AccessPath::resource_access_path(genesis_address(), chain_id_struct_tag);
let (account_address, data_path) = access_path.into_inner();

println!(
"test_asset_mapping_for_specified_coin_type | account {:?}, data_path: {:?}, data_path key hash: {:?}",
account_address,
data_path.encode_key()?,
data_path.key_hash()
);


Ok(())
}
68 changes: 32 additions & 36 deletions vm/framework/cached-packages/src/starcoin_framework_sdk_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,11 @@ pub enum EntryFunctionCall {
cap_update_table: Vec<u8>,
},

/// Assigns tokens to a recipient account with proof verification
/// @param token_issuer - The token issuer signer
/// @param receiper - Recipient address
/// @param proove - Proof data for verification
/// @param amount - Amount of tokens to assign
/// Requirements:
/// - Valid proof must be provided
/// - Sufficient balance must exist
AssetMappingAssignToAccount {
t: TypeTag,
AssetMappingAssignToAccountWithProof {
receiper: AccountAddress,
proove: Vec<u8>,
old_token_str: Vec<u8>,
proof_path: Vec<u8>,
proof_siblings: Vec<u8>,
amount: u64,
},

Expand Down Expand Up @@ -658,12 +651,19 @@ impl EntryFunctionCall {
new_public_key_bytes,
cap_update_table,
),
AssetMappingAssignToAccount {
t,
AssetMappingAssignToAccountWithProof {
receiper,
proove,
old_token_str,
proof_path,
proof_siblings,
amount,
} => asset_mapping_assign_to_account(t, receiper, proove, amount),
} => asset_mapping_assign_to_account_with_proof(
receiper,
old_token_str,
proof_path,
proof_siblings,
amount,
),
CoinCreateCoinConversionMap {} => coin_create_coin_conversion_map(),
CoinCreatePairing { coin_type } => coin_create_pairing(coin_type),
CoinMigrateToFungibleStore { coin_type } => coin_migrate_to_fungible_store(coin_type),
Expand Down Expand Up @@ -1278,30 +1278,25 @@ pub fn account_rotate_authentication_key_with_rotation_capability(
))
}

/// Assigns tokens to a recipient account with proof verification
/// @param token_issuer - The token issuer signer
/// @param receiper - Recipient address
/// @param proove - Proof data for verification
/// @param amount - Amount of tokens to assign
/// Requirements:
/// - Valid proof must be provided
/// - Sufficient balance must exist
pub fn asset_mapping_assign_to_account(
t: TypeTag,
pub fn asset_mapping_assign_to_account_with_proof(
receiper: AccountAddress,
proove: Vec<u8>,
old_token_str: Vec<u8>,
proof_path: Vec<u8>,
proof_siblings: Vec<u8>,
amount: u64,
) -> TransactionPayload {
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(
AccountAddress::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
ident_str!("asset_mapping").to_owned(),
),
ident_str!("assign_to_account").to_owned(),
vec![t],
ident_str!("assign_to_account_with_proof").to_owned(),
vec![],
vec![
bcs::to_bytes(&receiper).unwrap(),
bcs::to_bytes(&proove).unwrap(),
bcs::to_bytes(&old_token_str).unwrap(),
bcs::to_bytes(&proof_path).unwrap(),
bcs::to_bytes(&proof_siblings).unwrap(),
bcs::to_bytes(&amount).unwrap(),
],
))
Expand Down Expand Up @@ -2576,15 +2571,16 @@ mod decoder {
}
}

pub fn asset_mapping_assign_to_account(
pub fn asset_mapping_assign_to_account_with_proof(
payload: &TransactionPayload,
) -> Option<EntryFunctionCall> {
if let TransactionPayload::EntryFunction(script) = payload {
Some(EntryFunctionCall::AssetMappingAssignToAccount {
t: script.ty_args().get(0)?.clone(),
Some(EntryFunctionCall::AssetMappingAssignToAccountWithProof {
receiper: bcs::from_bytes(script.args().get(0)?).ok()?,
proove: bcs::from_bytes(script.args().get(1)?).ok()?,
amount: bcs::from_bytes(script.args().get(2)?).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()?,
})
} else {
None
Expand Down Expand Up @@ -3479,8 +3475,8 @@ static SCRIPT_FUNCTION_DECODER_MAP: once_cell::sync::Lazy<EntryFunctionDecoderMa
Box::new(decoder::account_rotate_authentication_key_with_rotation_capability),
);
map.insert(
"asset_mapping_assign_to_account".to_string(),
Box::new(decoder::asset_mapping_assign_to_account),
"asset_mapping_assign_to_account_with_proof".to_string(),
Box::new(decoder::asset_mapping_assign_to_account_with_proof),
);
map.insert(
"coin_create_coin_conversion_map".to_string(),
Expand Down
Loading

0 comments on commit 21f57e3

Please sign in to comment.