From 4164f5d5f549ecc2b8adfc540e7cef0a0ebe851e Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Thu, 18 Jan 2024 14:16:09 +0200 Subject: [PATCH] refactor + banned endpoint names --- Cargo.lock | 3 ++ chain-factory/Cargo.toml | 3 ++ chain-factory/src/lib.rs | 2 +- chain-factory/wasm/Cargo.lock | 17 +++++++++ chain-factory/wasm/src/lib.rs | 7 ++-- common/token-whitelist/src/lib.rs | 28 +++------------ common/utils/Cargo.toml | 3 ++ common/utils/src/lib.rs | 23 +++++++++++- esdt-safe/src/to_sovereign/create_tx.rs | 48 +++++++++++++++++++++++-- esdt-safe/wasm/Cargo.lock | 2 ++ esdt-safe/wasm/src/lib.rs | 6 ++-- fee-market/Cargo.toml | 3 ++ fee-market/src/fee_type.rs | 2 +- fee-market/src/lib.rs | 1 + fee-market/src/subtract_fee.rs | 1 + fee-market/wasm/Cargo.lock | 10 ++++++ fee-market/wasm/src/lib.rs | 7 ++-- 17 files changed, 131 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82db1a5e..348f5e4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -249,6 +249,7 @@ dependencies = [ name = "chain-factory" version = "0.0.0" dependencies = [ + "bls-signature", "chain-config", "multiversx-sc", "multiversx-sc-scenario", @@ -551,6 +552,7 @@ checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" name = "fee-market" version = "0.0.0" dependencies = [ + "bls-signature", "multiversx-sc", "multiversx-sc-scenario", "num-bigint", @@ -2086,6 +2088,7 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" name = "utils" version = "0.0.0" dependencies = [ + "bls-signature", "multiversx-sc", ] diff --git a/chain-factory/Cargo.toml b/chain-factory/Cargo.toml index eb252080..74244f64 100644 --- a/chain-factory/Cargo.toml +++ b/chain-factory/Cargo.toml @@ -20,5 +20,8 @@ path = "../common/utils" [dependencies.chain-config] path = "../chain-config" +[dependencies.bls-signature] +path = "../common/bls-signature" + [dev-dependencies.multiversx-sc-scenario] version = "=0.46.1" diff --git a/chain-factory/src/lib.rs b/chain-factory/src/lib.rs index 41bd11d9..12328181 100644 --- a/chain-factory/src/lib.rs +++ b/chain-factory/src/lib.rs @@ -7,7 +7,7 @@ pub mod slash; #[multiversx_sc::contract] pub trait ChainFactoryContract: - factory::FactoryModule + slash::SlashModule + utils::UtilsModule + factory::FactoryModule + slash::SlashModule + utils::UtilsModule + bls_signature::BlsSignatureModule { #[init] fn init( diff --git a/chain-factory/wasm/Cargo.lock b/chain-factory/wasm/Cargo.lock index cbd9b390..78d8642b 100644 --- a/chain-factory/wasm/Cargo.lock +++ b/chain-factory/wasm/Cargo.lock @@ -20,6 +20,14 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bls-signature" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "transaction", +] + [[package]] name = "chain-config" version = "0.0.0" @@ -32,6 +40,7 @@ dependencies = [ name = "chain-factory" version = "0.0.0" dependencies = [ + "bls-signature", "chain-config", "multiversx-sc", "utils", @@ -192,6 +201,13 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "transaction" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -202,5 +218,6 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" name = "utils" version = "0.0.0" dependencies = [ + "bls-signature", "multiversx-sc", ] diff --git a/chain-factory/wasm/src/lib.rs b/chain-factory/wasm/src/lib.rs index d3151042..c31e7931 100644 --- a/chain-factory/wasm/src/lib.rs +++ b/chain-factory/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 6 +// Endpoints: 9 // Async Callback (empty): 1 -// Total number of exported functions: 8 +// Total number of exported functions: 11 #![no_std] #![allow(internal_features)] @@ -26,6 +26,9 @@ multiversx_sc_wasm_adapter::endpoints! { getDeployCost => deploy_cost slash => slash distributeSlashed => distribute_slashed + setMinValidSigners => set_min_valid_signers + addSigners => add_signers + removeSigners => remove_signers ) } diff --git a/common/token-whitelist/src/lib.rs b/common/token-whitelist/src/lib.rs index d827d9a4..3586ed32 100644 --- a/common/token-whitelist/src/lib.rs +++ b/common/token-whitelist/src/lib.rs @@ -22,7 +22,7 @@ pub trait TokenWhitelistModule: return; } - let token_list = self.verfiy_tokens_signature(opt_signature, tokens); + let token_list = self.verfiy_items_signature(opt_signature, tokens); self.token_whitelist().extend(&token_list); } @@ -39,7 +39,7 @@ pub trait TokenWhitelistModule: return; } - let token_list = self.verfiy_tokens_signature(opt_signature, tokens); + let token_list = self.verfiy_items_signature(opt_signature, tokens); self.remove_items(&mut self.token_whitelist(), &token_list); } @@ -64,7 +64,7 @@ pub trait TokenWhitelistModule: return; } - let token_list = self.verfiy_tokens_signature(opt_signature, tokens); + let token_list = self.verfiy_items_signature(opt_signature, tokens); self.token_blacklist().extend(&token_list); } @@ -81,30 +81,10 @@ pub trait TokenWhitelistModule: return; } - let token_list = self.verfiy_tokens_signature(opt_signature, tokens); + let token_list = self.verfiy_items_signature(opt_signature, tokens); self.remove_items(&mut self.token_blacklist(), &token_list); } - fn verfiy_tokens_signature( - &self, - opt_signature: Option>, - tokens: MultiValueEncoded, - ) -> ManagedVec { - require!(opt_signature.is_some(), "Must provide signature"); - - let signature = unsafe { opt_signature.unwrap_unchecked() }; - let mut signature_data = ManagedBuffer::new(); - let mut token_list = ManagedVec::new(); - for token in tokens { - let _ = token.dep_encode(&mut signature_data); - token_list.push(token); - } - - self.multi_verify_signature(&signature_data, &signature); - - token_list - } - #[view(getTokenWhitelist)] #[storage_mapper("tokenWhitelist")] fn token_whitelist(&self) -> UnorderedSetMapper; diff --git a/common/utils/Cargo.toml b/common/utils/Cargo.toml index 61764d10..24d0dc12 100644 --- a/common/utils/Cargo.toml +++ b/common/utils/Cargo.toml @@ -7,6 +7,9 @@ edition = "2021" [lib] path = "src/lib.rs" +[dependencies.bls-signature] +path = "../bls-signature" + [dependencies.multiversx-sc] version = "=0.46.1" features = ["esdt-token-payment-legacy-decode"] diff --git a/common/utils/src/lib.rs b/common/utils/src/lib.rs index ffa92062..53ef0d98 100644 --- a/common/utils/src/lib.rs +++ b/common/utils/src/lib.rs @@ -1,5 +1,7 @@ #![no_std] +use bls_signature::BlsSignature; + multiversx_sc::imports!(); pub type PaymentsVec = ManagedVec>; @@ -7,7 +9,7 @@ pub type PaymentsVec = ManagedVec>; static ERR_EMPTY_PAYMENTS: &[u8] = b"No payments"; #[multiversx_sc::module] -pub trait UtilsModule { +pub trait UtilsModule: bls_signature::BlsSignatureModule { fn require_sc_address(&self, address: &ManagedAddress) { require!( !address.is_zero() && self.blockchain().is_smart_contract(address), @@ -44,4 +46,23 @@ pub trait UtilsModule { first_payment } + + fn verfiy_items_signature( + &self, + opt_signature: Option>, + items: MultiValueEncoded, + ) -> ManagedVec { + require!(opt_signature.is_some(), "Must provide signature"); + + let list = items.to_vec(); + let signature = unsafe { opt_signature.unwrap_unchecked() }; + let mut signature_data = ManagedBuffer::new(); + for token in &list { + let _ = token.dep_encode(&mut signature_data); + } + + self.multi_verify_signature(&signature_data, &signature); + + list + } } diff --git a/esdt-safe/src/to_sovereign/create_tx.rs b/esdt-safe/src/to_sovereign/create_tx.rs index c4a4f8f7..dbdde9e6 100644 --- a/esdt-safe/src/to_sovereign/create_tx.rs +++ b/esdt-safe/src/to_sovereign/create_tx.rs @@ -57,7 +57,7 @@ pub trait CreateTxModule: return; } - let all_tokens = self.verfiy_tokens_signature(opt_signature, tokens); + let all_tokens = self.verfiy_items_signature(opt_signature, tokens); self.burn_tokens().extend(&all_tokens); } @@ -74,10 +74,44 @@ pub trait CreateTxModule: return; } - let all_tokens = self.verfiy_tokens_signature(opt_signature, tokens); + let all_tokens = self.verfiy_items_signature(opt_signature, tokens); self.remove_items(&mut self.burn_tokens(), &all_tokens); } + #[endpoint(addBannedEndpointNames)] + fn add_banned_endpoint_names( + &self, + opt_signature: Option>, + names: MultiValueEncoded, + ) { + if !self.is_setup_phase_complete() { + self.require_caller_initiator(); + self.banned_endpoint_names().extend(names); + + return; + } + + let all_names = self.verfiy_items_signature(opt_signature, names); + self.banned_endpoint_names().extend(&all_names); + } + + #[endpoint(removeBannedEndpointNames)] + fn remove_banned_endpoint_names( + &self, + opt_signature: Option>, + names: MultiValueEncoded, + ) { + if !self.is_setup_phase_complete() { + self.require_caller_initiator(); + self.remove_items(&mut self.banned_endpoint_names(), names); + + return; + } + + let all_names = self.verfiy_items_signature(opt_signature, names); + self.remove_items(&mut self.banned_endpoint_names(), &all_names); + } + /// Create an Elrond -> Sovereign transaction. #[payable("*")] #[endpoint] @@ -102,6 +136,13 @@ pub trait CreateTxModule: "Gas limit too high" ); + require!( + !self + .banned_endpoint_names() + .contains(&transfer_data.function), + "Banned endpoint name" + ); + OptionalValue::Some(transfer_data.gas_limit) } OptionalValue::None => OptionalValue::None, @@ -184,4 +225,7 @@ pub trait CreateTxModule: #[storage_mapper("burnTokens")] fn burn_tokens(&self) -> UnorderedSetMapper; + + #[storage_mapper("bannedEndpointNames")] + fn banned_endpoint_names(&self) -> UnorderedSetMapper; } diff --git a/esdt-safe/wasm/Cargo.lock b/esdt-safe/wasm/Cargo.lock index 03a52ea4..b39786e8 100644 --- a/esdt-safe/wasm/Cargo.lock +++ b/esdt-safe/wasm/Cargo.lock @@ -62,6 +62,7 @@ dependencies = [ name = "fee-market" version = "0.0.0" dependencies = [ + "bls-signature", "multiversx-sc", "transaction", "utils", @@ -257,5 +258,6 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" name = "utils" version = "0.0.0" dependencies = [ + "bls-signature", "multiversx-sc", ] diff --git a/esdt-safe/wasm/src/lib.rs b/esdt-safe/wasm/src/lib.rs index 6ece10a6..1804fa8c 100644 --- a/esdt-safe/wasm/src/lib.rs +++ b/esdt-safe/wasm/src/lib.rs @@ -5,10 +5,10 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 34 +// Endpoints: 36 // Async Callback: 1 // Promise callbacks: 1 -// Total number of exported functions: 37 +// Total number of exported functions: 39 #![no_std] #![allow(internal_features)] @@ -26,6 +26,8 @@ multiversx_sc_wasm_adapter::endpoints! { setMaxUserTxGasLimit => set_max_user_tx_gas_limit setBurnAndMint => set_burn_and_mint removeBurnAndMint => remove_burn_and_mint + addBannedEndpointNames => add_banned_endpoint_names + removeBannedEndpointNames => remove_banned_endpoint_names deposit => deposit claimRefund => claim_refund setTransactionBatchStatus => set_transaction_batch_status diff --git a/fee-market/Cargo.toml b/fee-market/Cargo.toml index 98afbfdf..943b729b 100644 --- a/fee-market/Cargo.toml +++ b/fee-market/Cargo.toml @@ -14,6 +14,9 @@ path = "../common/utils" [dependencies.transaction] path = "../common/transaction" +[dependencies.bls-signature] +path = "../common/bls-signature" + [dependencies.multiversx-sc] version = "=0.46.1" diff --git a/fee-market/src/fee_type.rs b/fee-market/src/fee_type.rs index 5fef300d..77248d12 100644 --- a/fee-market/src/fee_type.rs +++ b/fee-market/src/fee_type.rs @@ -17,7 +17,7 @@ pub enum FeeType { } #[multiversx_sc::module] -pub trait FeeTypeModule: utils::UtilsModule { +pub trait FeeTypeModule: utils::UtilsModule + bls_signature::BlsSignatureModule { #[only_owner] #[endpoint(addFee)] fn add_fee(&self, base_token: TokenIdentifier, fee_type: FeeType) { diff --git a/fee-market/src/lib.rs b/fee-market/src/lib.rs index a7b18db9..60e501ec 100644 --- a/fee-market/src/lib.rs +++ b/fee-market/src/lib.rs @@ -16,6 +16,7 @@ pub trait FeeMarket: + subtract_fee::SubtractFeeModule + pairs::PairsModule + utils::UtilsModule + + bls_signature::BlsSignatureModule { #[init] fn init(&self, esdt_safe_address: ManagedAddress, pair_for_query: ManagedAddress) { diff --git a/fee-market/src/subtract_fee.rs b/fee-market/src/subtract_fee.rs index e559a0e2..0c9cf93b 100644 --- a/fee-market/src/subtract_fee.rs +++ b/fee-market/src/subtract_fee.rs @@ -35,6 +35,7 @@ pub trait SubtractFeeModule: + crate::fee_common::CommonFeeModule + crate::pairs::PairsModule + utils::UtilsModule + + bls_signature::BlsSignatureModule { #[only_owner] #[endpoint(addUsersToWhitelist)] diff --git a/fee-market/wasm/Cargo.lock b/fee-market/wasm/Cargo.lock index cffa1416..d1903592 100644 --- a/fee-market/wasm/Cargo.lock +++ b/fee-market/wasm/Cargo.lock @@ -20,6 +20,14 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bls-signature" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "transaction", +] + [[package]] name = "endian-type" version = "0.1.2" @@ -30,6 +38,7 @@ checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" name = "fee-market" version = "0.0.0" dependencies = [ + "bls-signature", "multiversx-sc", "transaction", "utils", @@ -192,5 +201,6 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" name = "utils" version = "0.0.0" dependencies = [ + "bls-signature", "multiversx-sc", ] diff --git a/fee-market/wasm/src/lib.rs b/fee-market/wasm/src/lib.rs index 1e8f6caa..e7eef57d 100644 --- a/fee-market/wasm/src/lib.rs +++ b/fee-market/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 13 +// Endpoints: 16 // Async Callback (empty): 1 -// Total number of exported functions: 15 +// Total number of exported functions: 18 #![no_std] #![allow(internal_features)] @@ -33,6 +33,9 @@ multiversx_sc_wasm_adapter::endpoints! { getUsersWhitelist => users_whitelist addPairs => add_pairs removePairs => remove_pairs + setMinValidSigners => set_min_valid_signers + addSigners => add_signers + removeSigners => remove_signers ) }