diff --git a/Cargo.lock b/Cargo.lock index 442a4bb..a694067 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,15 +13,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "aho-corasick" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" -dependencies = [ - "memchr", -] - [[package]] name = "anyhow" version = "1.0.71" @@ -84,15 +75,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "const-oid" -version = "0.9.2" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "520fbf3c07483f94e3e3ca9d0cfd913d7718ef2483d2cfd91c0d9e91474ab913" +checksum = "6340df57935414636969091153f35f68d9f00bbc8fb4a9c6054706c213e6c6bc" [[package]] name = "cosmwasm-crypto" -version = "1.2.6" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41c0e41be7e6c7d7ab3c61cdc32fcfaa14f948491a401cbc1c74bb33b6f4b851" +checksum = "bb64554a91d6a9231127f4355d351130a0b94e663d5d9dc8b3a54ca17d83de49" dependencies = [ "digest 0.10.7", "ed25519-zebra", @@ -103,18 +94,18 @@ dependencies = [ [[package]] name = "cosmwasm-derive" -version = "1.2.6" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a7ee2798c92c00dd17bebb4210f81d5f647e5e92d847959b7977e0fd29a3500" +checksum = "a0fb2ce09f41a3dae1a234d56a9988f9aff4c76441cd50ef1ee9a4f20415b028" dependencies = [ "syn 1.0.109", ] [[package]] name = "cosmwasm-schema" -version = "1.2.6" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "407aca6f1671a08b60db8167f03bb7cb6b2378f0ddd9a030367b66ba33c2fd41" +checksum = "230e5d1cefae5331db8934763c81b9c871db6a2cd899056a5694fa71d292c815" dependencies = [ "cosmwasm-schema-derive", "schemars", @@ -125,9 +116,9 @@ dependencies = [ [[package]] name = "cosmwasm-schema-derive" -version = "1.2.6" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6d1e00b8fd27ff923c10303023626358e23a6f9079f8ebec23a8b4b0bfcd4b3" +checksum = "43dadf7c23406cb28079d69e6cb922c9c29b9157b0fe887e3b79c783b7d4bcb8" dependencies = [ "proc-macro2", "quote", @@ -136,9 +127,9 @@ dependencies = [ [[package]] name = "cosmwasm-std" -version = "1.2.6" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d5fdfd112b070055f068fad079d490117c8e905a588b92a5a7c9276d029930" +checksum = "4337eef8dfaf8572fe6b6b415d6ec25f9308c7bb09f2da63789209fb131363be" dependencies = [ "base64", "cosmwasm-crypto", @@ -149,16 +140,16 @@ dependencies = [ "schemars", "serde", "serde-json-wasm", - "sha2 0.10.6", + "sha2 0.10.7", "thiserror", "uint", ] [[package]] name = "cosmwasm-storage" -version = "1.2.6" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af9e21c4f58986fd20184d7685e1c43c5732c9309337b09307d5952fd34dba6e" +checksum = "e8601d284db8776e39fe99b3416516c5636ca73cef14666b7bb9648ca32c4b89" dependencies = [ "cosmwasm-std", "serde", @@ -166,9 +157,9 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58" +checksum = "03e69e28e9f7f77debdedbaafa2866e1de9ba56df55a8bd7cfc724c25a09987c" dependencies = [ "libc", ] @@ -224,7 +215,6 @@ dependencies = [ "cw-storage-plus", "cw20", "hex", - "regex", "rlp", "rlp-derive", "rustc-hex", @@ -256,9 +246,9 @@ dependencies = [ [[package]] name = "cw-multi-test" -version = "0.16.4" +version = "0.16.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a18afd2e201221c6d72a57f0886ef2a22151bbc9e6db7af276fde8a91081042" +checksum = "127c7bb95853b8e828bdab97065c81cb5ddc20f7339180b61b2300565aaa99d1" dependencies = [ "anyhow", "cosmwasm-std", @@ -301,22 +291,23 @@ dependencies = [ [[package]] name = "cw2" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fb70cee2cf0b4a8ff7253e6bc6647107905e8eb37208f87d54f67810faa62f8" +checksum = "29ac2dc7a55ad64173ca1e0a46697c31b7a5c51342f55a1e84a724da4eb99908" dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-storage-plus", "schemars", "serde", + "thiserror", ] [[package]] name = "cw20" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91666da6c7b40c8dd5ff94df655a28114efc10c79b70b4d06f13c31e37d60609" +checksum = "011c45920f8200bd5d32d4fe52502506f64f2f75651ab408054d4cfc75ca3a9b" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -327,9 +318,9 @@ dependencies = [ [[package]] name = "cw20-base" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afcd279230b08ed8afd8be5828221622bd5b9ce25d0b01d58bad626c6ce0169c" +checksum = "0b3ad456059901a36cfa68b596d85d579c3df2b797dae9950dc34c27e14e995f" dependencies = [ "cosmwasm-schema", "cosmwasm-std", @@ -477,9 +468,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.9" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", "libc", @@ -532,9 +523,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.6" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +checksum = "62b02a5381cc465bd3041d84623d0fa3b66738b52b8e2fc3bab8ad63ab032f4a" [[package]] name = "k256" @@ -545,20 +536,14 @@ dependencies = [ "cfg-if", "ecdsa", "elliptic-curve", - "sha2 0.10.6", + "sha2 0.10.7", ] [[package]] name = "libc" -version = "0.2.145" +version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc86cde3ff845662b8f4ef6cb50ea0e20c524eb3d29ae048287e06a1b3fa6a81" - -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "once_cell" @@ -584,9 +569,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.59" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b" +checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb" dependencies = [ "unicode-ident", ] @@ -616,9 +601,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.28" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" +checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105" dependencies = [ "proc-macro2", ] @@ -638,23 +623,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "regex" -version = "1.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" - [[package]] name = "rfc6979" version = "0.3.1" @@ -695,9 +663,9 @@ checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" [[package]] name = "ryu" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +checksum = "fe232bdf6be8c8de797b22184ee71118d63780ea42ac85b61d1baa6d3b782ae9" [[package]] name = "schemars" @@ -745,9 +713,9 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" [[package]] name = "serde" -version = "1.0.163" +version = "1.0.166" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2113ab51b87a539ae008b5c6c02dc020ffa39afd2d83cffcb3f4eb2722cebec2" +checksum = "d01b7404f9d441d3ad40e6a636a7782c377d2abdbe4fa2440e2edcc2f4f10db8" dependencies = [ "serde_derive", ] @@ -763,13 +731,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.163" +version = "1.0.166" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" +checksum = "5dd83d6dde2b6b2d466e14d9d1acce8816dedee94f735eac6395808b3483c6d6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.23", ] [[package]] @@ -785,9 +753,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.96" +version = "1.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" +checksum = "0f1e14e89be7aa4c4b78bdbdc9eb5bf8517829a600ae8eaa39a6e1d960b5185c" dependencies = [ "itoa", "ryu", @@ -809,9 +777,9 @@ dependencies = [ [[package]] name = "sha2" -version = "0.10.6" +version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" dependencies = [ "cfg-if", "cpufeatures", @@ -863,9 +831,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.18" +version = "2.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +checksum = "59fb7d6d8281a51045d62b8eb3a7d1ce347b76f312af50cd3dc0af39c87c1737" dependencies = [ "proc-macro2", "quote", @@ -874,22 +842,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.40" +version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +checksum = "c16a64ba9387ef3fdae4f9c1a7f07a0997fce91985c0336f1ddc1822b3b37802" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.40" +version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +checksum = "d14928354b01c4d6a4f0e549069adef399a284e7995c7ccca94e8a07a5346c59" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn 2.0.23", ] [[package]] @@ -912,9 +880,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" +checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73" [[package]] name = "version_check" diff --git a/contracts/cw-common/Cargo.toml b/contracts/cw-common/Cargo.toml index 9aa609c..42e8339 100644 --- a/contracts/cw-common/Cargo.toml +++ b/contracts/cw-common/Cargo.toml @@ -16,6 +16,5 @@ cosmwasm-std = { version = "1.2.5", default-features = false } rlp = { version = "0.5.2", default-features = false } cosmwasm-schema = "1.2.6" cw-storage-plus = "1.1.0" -regex = "1.5.4" diff --git a/contracts/cw-common/src/network_address.rs b/contracts/cw-common/src/network_address.rs index 1bd5ec6..0a759ba 100644 --- a/contracts/cw-common/src/network_address.rs +++ b/contracts/cw-common/src/network_address.rs @@ -1,4 +1,3 @@ -use regex::Regex; use std::str::FromStr; use cosmwasm_schema::cw_serde; @@ -93,23 +92,13 @@ impl NetworkAddress { } pub fn validate(&self) -> bool { - let address = self.get_parts()[1]; - if !address.is_ascii() { - return false; + let parts = self.get_parts(); + let net_id = parts[0].to_string(); + let address = parts[1]; + match net_id { + s if s.contains("icon") => validate_icon_address(address), + _ => false, } - let lowercase_address = address.to_lowercase(); - if !(lowercase_address.starts_with("hx") || lowercase_address.starts_with("cx")) { - return false; - } - let address_without_prefix = &lowercase_address[2..]; - let address_length = address_without_prefix.len(); - - if address_length == 40 { - // Check if the address contains only valid characters [0-9, a-f] - let regex = Regex::new("^[0-9a-f]+$").unwrap(); - return regex.is_match(address_without_prefix); - } - false } } @@ -134,14 +123,61 @@ impl FromStr for NetworkAddress { } } +fn validate_icon_address(address: &str) -> bool { + let lowercase_address = address.to_lowercase(); + + if !lowercase_address.starts_with("hx") && !lowercase_address.starts_with("cx") { + return false; + } + + lowercase_address.len() == 42 && is_valid_character_set(&lowercase_address[2..]) +} + +fn is_valid_character_set(address: &str) -> bool { + address.chars().all(|c| matches!(c, '0'..='9' | 'a'..='f')) +} + #[test] fn test_parse_btp_address() { - let btp_address = - NetworkAddress("0x38.bsc/0x034AaDE86BF402F023Aa17E5725fABC4ab9E9798".to_string()); - let (network, account) = btp_address.parse_parts(); - assert_eq!(network, NetId("0x38.bsc".to_string())); + let network_address = + NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_string()); + let (network, account) = network_address.parse_parts(); + assert_eq!(network, NetId("0x01.icon".to_string())); assert_eq!( account, - Addr::unchecked("0x034AaDE86BF402F023Aa17E5725fABC4ab9E9798") + Addr::unchecked("cx9876543210fedcba9876543210fedcba98765432") ); } + +#[test] +fn address_validation_test() { + let network_address = + NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_string()); + let res = network_address.validate(); + assert!(res); + + let network_address = + NetworkAddress("0x01.icon/hx9876543210fedcba9876543210fedcba98765432".to_string()); + let res = network_address.validate(); + assert!(res); + + let network_address = + NetworkAddress("0x01.bsc/cx9876543210fedcba9876543210fedcba98765432".to_string()); + let res = network_address.validate(); + assert!(!res); + + let network_address = + NetworkAddress("0x01.icon/wx9876543210fedcba9876543210fedcba98765432".to_string()); + let res = network_address.validate(); + assert!(!res); + + let network_address = + NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba9876542".to_string()); + let res = network_address.validate(); + assert!(!res); + + let network_address = + NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba9876543_".to_string()); + let res = network_address.validate(); + assert!(!res); +} diff --git a/contracts/mock-contracts/x-call-mock/src/contract.rs b/contracts/mock-contracts/x-call-mock/src/contract.rs index 7e9dd22..0ab8773 100644 --- a/contracts/mock-contracts/x-call-mock/src/contract.rs +++ b/contracts/mock-contracts/x-call-mock/src/contract.rs @@ -73,7 +73,7 @@ pub fn execute( pub fn query(_deps: Deps, _env: Env, _msg: XCallQuery) -> StdResult { match _msg { XCallQuery::GetNetworkAddress {} => Ok(to_binary( - "0x1.icon/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e", + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432", )?), } } diff --git a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs index 6211957..113c26a 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/contract.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/contract.rs @@ -1,8 +1,3 @@ -#[cfg(not(feature = "library"))] -use cosmwasm_std::entry_point; -use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdError, StdResult}; -use cw2::set_contract_version; -// use cw2::set_contract_version; use crate::constants::{ REPLY_MSG_SUCCESS, TOKEN_DECIMALS, TOKEN_NAME, TOKEN_SYMBOL, TOKEN_TOTAL_SUPPLY, X_CROSS_TRANSFER, X_CROSS_TRANSFER_REVERT, @@ -11,6 +6,10 @@ use crate::error::ContractError; use crate::state::{ DESTINATION_TOKEN_ADDRESS, DESTINATION_TOKEN_NET, NID, OWNER, X_CALL, X_CALL_NETWORK_ADDRESS, }; +#[cfg(not(feature = "library"))] +use cosmwasm_std::entry_point; +use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdError, StdResult}; +use cw2::set_contract_version; use cw_common::hub_token_msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; use cw_common::x_call_msg::{XCallMsg, XCallQuery}; @@ -123,6 +122,10 @@ mod execute { .addr_validate(x_call.as_ref()) .map_err(ContractError::Std)?; + if !hub_network_address.validate() { + return Err(ContractError::InvalidNetworkAddress); + } + X_CALL .save(deps.storage, &x_call) .map_err(ContractError::Std)?; @@ -159,21 +162,26 @@ mod execute { from: NetworkAddress, data: Vec, ) -> Result { + if !from.validate() { + return Err(ContractError::InvalidNetworkAddress); + } + let xcall = X_CALL.load(deps.storage)?; if info.sender != xcall { return Err(ContractError::OnlyCallService); } + let rlp: Rlp = Rlp::new(&data); let data_list: Vec = rlp.as_list().unwrap(); if data_list.len() <= 2 { return Err(ContractError::InvalidData); } - debug_println!("this is {:?}", data_list); + debug_println!("datalist {:?}", data_list); let data_list = &data_list[0].to_vec(); let method = from_utf8(data_list).unwrap(); - debug_println!("this is {:?}", method); + debug_println!("method {:?}", method); match method { X_CROSS_TRANSFER => { let cross_transfer_data: CrossTransfer = decode(&data).unwrap(); @@ -199,6 +207,9 @@ mod execute { amount: u128, data: Vec, ) -> Result { + if !to.validate() { + return Err(ContractError::InvalidNetworkAddress); + } let funds = info.funds.clone(); let nid = NID.load(deps.storage)?; let hub_net: NetId = DESTINATION_TOKEN_NET.load(deps.storage)?; @@ -212,7 +223,7 @@ mod execute { from: from.clone(), to: to.clone(), value: amount, - data, + data: data.clone(), }; let rollback_data = CrossTransferRevert { method: X_CROSS_TRANSFER_REVERT.to_string(), @@ -237,16 +248,21 @@ mod execute { let sub_message = SubMsg::reply_always(wasm_execute_message, REPLY_MSG_SUCCESS); debug_println!("this is {:?}", info.sender); - let _result = + debug_println!("burn from {:?}", sub_message); + + let result = execute_burn(deps, env, info, amount.into()).map_err(ContractError::Cw20BaseError)?; + debug_println!("burn from {:?}", sub_message); + //TODO: emit a event log for cross transfer let event = Event::new("CrossTransfer") .add_attribute("from", from.to_string()) .add_attribute("to", to.to_string()) - .add_attribute("value", amount.to_string()); - - Ok(Response::new() + .add_attribute("value", amount.to_string()) + .add_attribute("data", hex_encode(data)); + debug_println!("this is {:?}", event); + Ok(result .add_submessage(sub_message) .add_attribute("method", "cross_transfer") .add_event(event)) @@ -259,7 +275,10 @@ mod execute { from: NetworkAddress, cross_transfer_data: CrossTransfer, ) -> Result { - debug_println!("this is {:?}", cross_transfer_data); + debug_println!("xcrosstransfer {:?}", cross_transfer_data); + if !cross_transfer_data.from.validate() || !cross_transfer_data.to.validate() { + return Err(ContractError::InvalidNetworkAddress); + } let nid = NID.load(deps.storage)?; let hub_net: NetId = DESTINATION_TOKEN_NET.load(deps.storage)?; @@ -268,14 +287,14 @@ mod execute { let network_address = NetworkAddress::new(&hub_net.to_string(), destination_network_address.as_ref()); - debug_println!("this is {:?},{:?}", network_address, from); + debug_println!("before network addr==from {:?},{:?}", network_address, from); if from != network_address { return Err(ContractError::WrongAddress {}); } //TODO: add a validation check for ICON address in network address library let (net, account) = NetworkAddress::parse_parts(&cross_transfer_data.to); - debug_println!("this is {:?},{:?}", net, nid); + debug_println!("net nid comparision {:?},{:?}", net, nid); if net != nid { return Err(ContractError::WrongNetwork); } @@ -283,7 +302,7 @@ mod execute { deps.api .addr_validate(account.as_ref()) .map_err(ContractError::Std)?; - + debug_println!("mint to {:?}", account); let res = execute_mint( deps, env, @@ -296,7 +315,8 @@ mod execute { let event = Event::new("XCrossTransfer") .add_attribute("from", cross_transfer_data.from.to_string()) .add_attribute("to", cross_transfer_data.to.to_string()) - .add_attribute("value", cross_transfer_data.value.to_string()); + .add_attribute("value", cross_transfer_data.value.to_string()) + .add_attribute("data", hex_encode(cross_transfer_data.data)); //TODO: add event for cross transfer with relevant parameters Ok(res @@ -312,6 +332,9 @@ mod execute { cross_transfer_revert_data: CrossTransferRevert, ) -> Result { debug_println!("this is {:?},{:?}", cross_transfer_revert_data, from); + if !from.validate() { + return Err(ContractError::InvalidNetworkAddress); + } deps.api .addr_validate(cross_transfer_revert_data.from.as_ref()) .map_err(ContractError::Std)?; @@ -331,6 +354,18 @@ mod execute { .add_attribute("method", "x_cross_transfer_revert") .add_event(event)) } + + fn hex_encode(data: Vec) -> String { + debug_println!("this is {:?}", data); + if data.is_empty() { + debug_println!("this is empty"); + "null".to_string() + } else { + let data = hex::encode(data); + debug_println!("this is not empty, {}", data); + data + } + } } #[cfg(test)] @@ -345,10 +380,8 @@ mod rlp_test { fn encodetest() { let call_data = CrossTransfer { method: "xCrossTransfer".to_string(), - from: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), - ), - to: NetworkAddress("0x38.bsc/archway123fdth".to_string()), + from: NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned()), + to: NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_string()), value: 1000, data: vec![ 118, 101, 99, 33, 91, 49, 44, 32, 50, 44, 32, 51, 44, 32, 52, 44, 32, 53, 93, @@ -382,29 +415,33 @@ mod tests { testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier}, to_binary, Addr, ContractResult, MemoryStorage, OwnedDeps, SystemResult, WasmQuery, }; + use debug_print::debug_println; use rlp::encode; use super::*; - fn setup() -> ( + fn setup( + sender: &str, + ) -> ( OwnedDeps, Env, MessageInfo, ) { let mut deps: OwnedDeps = mock_dependencies(); let env = mock_env(); - let info = mock_info("archway123fdth", &[]); + let info = mock_info(sender, &[]); let msg = InstantiateMsg { x_call: "archway123fdth".to_owned(), - hub_address: "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), + hub_address: "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), }; - let _res: Response = instantiate(deps.as_mut(), env.clone(), info.clone(), msg).unwrap(); + let res = instantiate(deps.as_mut(), env.clone(), info.clone(), msg); + assert!(res.is_ok()); let setup_message = ExecuteMsg::Setup { x_call: Addr::unchecked("archway123fdth".to_owned()), hub_address: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), ), }; @@ -413,31 +450,30 @@ mod tests { contract_addr: _, msg: _, } => SystemResult::Ok(ContractResult::Ok( - to_binary("0x38.bsc/archway192kfvz2vrxv4hhaz3tjdk39maa69xs75n5cea8").unwrap(), + to_binary("0x01.icon/cx9876543210fedcba9876543210fedcba98765432").unwrap(), )), _ => todo!(), }); - execute(deps.as_mut(), env.clone(), info.clone(), setup_message).unwrap(); + let res = execute(deps.as_mut(), env.clone(), info.clone(), setup_message); + assert!(res.is_ok()); (deps, env, info) } #[test] fn instantiate_test() { - setup(); + setup("archway123fdth"); } #[test] fn execute_handle_call_xcrosstransfer_test() { - let (mut deps, env, info) = setup(); + let (mut deps, env, info) = setup("archway123fdth"); let call_data = CrossTransfer { method: "xCrossTransfer".to_string(), - from: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), - ), - to: NetworkAddress("0x38.bsc/archway123fdth".to_string()), + from: NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned()), + to: NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_string()), value: 1000, data: vec![ 118, 101, 99, 33, 91, 49, 44, 32, 50, 44, 32, 51, 44, 32, 52, 44, 32, 53, 93, @@ -447,30 +483,28 @@ mod tests { // let mut stream = RlpStream::new(); let data = encode(&call_data).to_vec(); - let _res: Response = execute( + let res = execute( deps.as_mut(), env, info, ExecuteMsg::HandleCallMessage { from: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), ), data, }, - ) - .unwrap(); + ); + assert!(res.is_ok()); } #[test] fn cross_transfer_test() { - let (mut deps, env, info) = setup(); + let (mut deps, env, info) = setup("archway123fdth"); let call_data = CrossTransfer { method: "xCrossTransfer".to_string(), - from: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), - ), - to: NetworkAddress("0x38.bsc/archway123fdth".to_string()), + from: NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned()), + to: NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765452".to_string()), value: 1000, data: vec![ 118, 101, 99, 33, 91, 49, 44, 32, 50, 44, 32, 51, 44, 32, 52, 44, 32, 53, 93, @@ -483,39 +517,44 @@ mod tests { let _res: Response = execute( deps.as_mut(), env.clone(), - info.clone(), + info, ExecuteMsg::HandleCallMessage { from: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), ), data, }, ) .unwrap(); - let _res: Response = execute( + let info = mock_info("cx9876543210fedcba9876543210fedcba98765452", &[]); + + // execute_mint(deps, env, info, info.sender.to_string(), 1000); + + let res = execute( deps.as_mut(), env, info, ExecuteMsg::CrossTransfer { to: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), ), amount: 1000, data: vec![1, 2, 3, 4, 5], }, - ) - .unwrap(); + ); + debug_println!("this is {:?}", _res); + assert!(res.is_ok()); } #[test] fn execute_handle_call_test_xcrossrevert() { - let (mut deps, env, info) = setup(); + let (mut deps, env, info) = setup("archway123fdth"); let call_data = CrossTransferRevert { method: "xCrossTransferRevert".to_string(), from: Addr::unchecked( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), ), value: 1000, }; @@ -523,17 +562,17 @@ mod tests { // let mut stream = RlpStream::new(); let data = encode(&call_data).to_vec(); - let _res: Response = execute( + let res = execute( deps.as_mut(), env, info, ExecuteMsg::HandleCallMessage { from: NetworkAddress( - "0x38.bsc/archway192kfvz2vrxv4hhaz3tjdk39maa69xs75n5cea8".to_owned(), + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), ), data, }, - ) - .unwrap(); + ); + assert!(res.is_ok()); } } diff --git a/contracts/token-contracts/cw-hub-bnusd/src/error.rs b/contracts/token-contracts/cw-hub-bnusd/src/error.rs index c79bf75..7bb6eac 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/error.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/error.rs @@ -10,7 +10,7 @@ pub enum ContractError { Unauthorized {}, #[error("Wrong Address")] WrongAddress, - #[error("Invalid BTP Address")] + #[error("Invalid Network Address according to Network ID")] InvalidNetworkAddress, #[error("Wrong Network")] WrongNetwork, diff --git a/contracts/token-contracts/cw-hub-bnusd/src/state.rs b/contracts/token-contracts/cw-hub-bnusd/src/state.rs index 5ad8ff8..8058ee8 100644 --- a/contracts/token-contracts/cw-hub-bnusd/src/state.rs +++ b/contracts/token-contracts/cw-hub-bnusd/src/state.rs @@ -1,13 +1,6 @@ use cosmwasm_std::Addr; use cw_common::network_address::{NetId, NetworkAddress}; -use cw_storage_plus::{Item, Map}; - -pub const CONNECTED_CHAINS: &str = "connected_chains"; -pub const SPOKE_CONTRACTS: &str = "spoke_contract"; -pub const CROSS_CHAIN_SUPPLY: &str = "cross_chain_supply"; - -pub const CROSSCHAINSUPPLY: Map<&String, u128> = Map::new(CROSS_CHAIN_SUPPLY); -pub const CONNECTEDCHAINS: Item> = Item::new(CONNECTED_CHAINS); +use cw_storage_plus::Item; pub const OWNER: Item = Item::new("owner"); pub const X_CALL: Item = Item::new("xCall"); diff --git a/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs b/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs index 34f041e..b12e127 100644 --- a/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs +++ b/contracts/token-contracts/cw-hub-bnusd/tests/setup.rs @@ -97,8 +97,7 @@ mod instantiate_test { ctx.sender.clone(), &InstantiateMsg { x_call: Addr::unchecked(_x_call_address).into_string(), - hub_address: "0x1.icon/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e" - .to_owned(), + hub_address: "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), }, &[], "HubToken", @@ -118,7 +117,7 @@ mod instantiate_test { &ExecuteMsg::Setup { x_call: Addr::unchecked(ctx.get_xcall_app()), hub_address: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), + "0x01.icon/cx7866543210fedcba9876543210fedcba987654df".to_owned(), ), }, &[], @@ -131,10 +130,8 @@ mod instantiate_test { fn handle_call_message(mut ctx: TestContext) -> TestContext { let call_data = CrossTransfer { method: "xCrossTransfer".to_string(), - from: NetworkAddress( - "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), - ), - to: NetworkAddress("0x1.icon/archway123fdth".to_string()), + from: NetworkAddress("0x01.icon/cx7866543210fedcba9876543210fedcba987654df".to_owned()), + to: NetworkAddress("0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_string()), value: 1000, data: vec![ 118, 101, 99, 33, 91, 49, 44, 32, 50, 44, 32, 51, 44, 32, 52, 44, 32, 53, 93, @@ -144,19 +141,18 @@ mod instantiate_test { // let mut stream = RlpStream::new(); let data = encode(&call_data).to_vec(); - let _resp = ctx - .app - .execute_contract( - ctx.sender.clone(), - ctx.get_xcall_app(), - &XCallMsg::TestHandleCallMessage { - from: "0x38.bsc/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), - data, - hub_token: ctx.get_hubtoken_app().into_string(), - }, - &[], - ) - .unwrap(); + let _resp = ctx.app.execute_contract( + ctx.sender.clone(), + ctx.get_xcall_app(), + &XCallMsg::TestHandleCallMessage { + from: "0x01.icon/cx7866543210fedcba9876543210fedcba987654df".to_owned(), + data, + hub_token: ctx.get_hubtoken_app().into_string(), + }, + &[], + ); + println!("Respose Please{:?}", _resp); + assert!(_resp.is_ok()); let call_data = CrossTransferRevert { method: "xCrossTransferRevert".to_string(), @@ -167,19 +163,18 @@ mod instantiate_test { // let mut stream = RlpStream::new(); let data = encode(&call_data).to_vec(); - let _resp = ctx - .app - .execute_contract( - ctx.sender.clone(), - ctx.get_xcall_app(), - &XCallMsg::TestHandleCallMessage { - from: "0x1.icon/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_owned(), - data, - hub_token: ctx.get_hubtoken_app().into_string(), - }, - &[], - ) - .unwrap(); + let _resp = ctx.app.execute_contract( + ctx.sender.clone(), + ctx.get_xcall_app(), + &XCallMsg::TestHandleCallMessage { + from: "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_owned(), + data, + hub_token: ctx.get_hubtoken_app().into_string(), + }, + &[], + ); + println!("{:?}", _resp); + assert!(_resp.is_ok()); ctx } @@ -192,7 +187,7 @@ mod instantiate_test { ctx.get_hubtoken_app(), &ExecuteMsg::CrossTransfer { to: NetworkAddress( - "0x1.icon/archway1qvqas572t6fx7af203mzygn7lgw5ywjt4y6q8e".to_string(), + "0x01.icon/cx9876543210fedcba9876543210fedcba98765432".to_string(), ), amount: 100, data: vec![], @@ -200,6 +195,7 @@ mod instantiate_test { &[], ) .unwrap(); + println!("{:?}", _resp); ctx }