Skip to content

Commit

Permalink
Merge pull request #23 from multiversx/banned-endpoint-names
Browse files Browse the repository at this point in the history
refactor + banned endpoint names
  • Loading branch information
dorin-iancu authored Jan 23, 2024
2 parents 41c8bc5 + 4164f5d commit c6db74e
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions chain-factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion chain-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 17 additions & 0 deletions chain-factory/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions chain-factory/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
)
}

Expand Down
28 changes: 4 additions & 24 deletions common/token-whitelist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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<BlsSignature<Self::Api>>,
tokens: MultiValueEncoded<TokenIdentifier>,
) -> ManagedVec<TokenIdentifier> {
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<TokenIdentifier>;
Expand Down
3 changes: 3 additions & 0 deletions common/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
23 changes: 22 additions & 1 deletion common/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![no_std]

use bls_signature::BlsSignature;

multiversx_sc::imports!();

pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;

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),
Expand Down Expand Up @@ -44,4 +46,23 @@ pub trait UtilsModule {

first_payment
}

fn verfiy_items_signature<T: TopDecode + NestedEncode + ManagedVecItem>(
&self,
opt_signature: Option<BlsSignature<Self::Api>>,
items: MultiValueEncoded<T>,
) -> ManagedVec<T> {
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
}
}
48 changes: 46 additions & 2 deletions esdt-safe/src/to_sovereign/create_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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<BlsSignature<Self::Api>>,
names: MultiValueEncoded<ManagedBuffer>,
) {
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<BlsSignature<Self::Api>>,
names: MultiValueEncoded<ManagedBuffer>,
) {
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]
Expand All @@ -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,
Expand Down Expand Up @@ -184,4 +225,7 @@ pub trait CreateTxModule:

#[storage_mapper("burnTokens")]
fn burn_tokens(&self) -> UnorderedSetMapper<TokenIdentifier>;

#[storage_mapper("bannedEndpointNames")]
fn banned_endpoint_names(&self) -> UnorderedSetMapper<ManagedBuffer>;
}
2 changes: 2 additions & 0 deletions esdt-safe/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions esdt-safe/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions fee-market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion fee-market/src/fee_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum FeeType<M: ManagedTypeApi> {
}

#[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<Self::Api>) {
Expand Down
1 change: 1 addition & 0 deletions fee-market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions fee-market/src/subtract_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub trait SubtractFeeModule:
+ crate::fee_common::CommonFeeModule
+ crate::pairs::PairsModule
+ utils::UtilsModule
+ bls_signature::BlsSignatureModule
{
#[only_owner]
#[endpoint(addUsersToWhitelist)]
Expand Down
10 changes: 10 additions & 0 deletions fee-market/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c6db74e

Please sign in to comment.