Skip to content

Commit

Permalink
fix: Don't use env::signer_account_id
Browse files Browse the repository at this point in the history
Instead use env::predecessor_account_id. Using signer_account_id
has the same problems of using tx.origin in Ethereum. See link
below for more details:

ethereum/solidity#683
  • Loading branch information
mfornet committed Mar 11, 2021
1 parent ac1c156 commit 0bb6b45
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
9 changes: 4 additions & 5 deletions contracts/near/admin-controlled/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
pub mod macros;
pub use macros::*;

use near_sdk::env;

pub type Mask = u128;

pub trait AdminControlled {
fn is_owner(&self) -> bool {
env::current_account_id() == env::signer_account_id()
}

fn assert_owner(&self) {
assert!(self.is_owner());
env::current_account_id() == env::predecessor_account_id()
}

/// Return the current mask representing all paused events.
Expand Down
21 changes: 21 additions & 0 deletions contracts/near/admin-controlled/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[macro_export]
macro_rules! impl_admin_controlled {
($contract: ident, $paused: ident) => {
use admin_controlled::{AdminControlled as AdminControlledInner, Mask as MaskInner};
use near_sdk as near_sdk_inner;

#[near_bindgen]
impl AdminControlledInner for $contract {
#[result_serializer(borsh)]
fn get_paused(&self) -> MaskInner {
self.$paused
}

#[result_serializer(borsh)]
fn set_paused(&mut self, #[serializer(borsh)] paused: MaskInner) {
near_sdk_inner::assert_self();
self.$paused = paused;
}
}
};
}
18 changes: 3 additions & 15 deletions contracts/near/eth-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use admin_controlled::{AdminControlled, Mask};
use admin_controlled::Mask;
use borsh::{BorshDeserialize, BorshSerialize};
use eth_types::*;
use near_sdk::collections::UnorderedMap;
Expand Down Expand Up @@ -111,20 +111,6 @@ impl Default for EthClient {
}
}

#[near_bindgen]
impl AdminControlled for EthClient {
#[result_serializer(borsh)]
fn get_paused(&self) -> Mask {
self.paused
}

#[result_serializer(borsh)]
fn set_paused(&mut self, #[serializer(borsh)] paused: Mask) {
self.assert_owner();
self.paused = paused;
}
}

#[near_bindgen]
impl EthClient {
#[init]
Expand Down Expand Up @@ -441,3 +427,5 @@ impl EthClient {
(H256(pair.0), H256(pair.1))
}
}

admin_controlled::impl_admin_controlled!(EthClient, paused);
16 changes: 2 additions & 14 deletions contracts/near/eth-prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use admin_controlled::{AdminControlled, Mask};
use admin_controlled::Mask;
use borsh::{BorshDeserialize, BorshSerialize};
use eth_types::*;
use near_sdk::{env, ext_contract, near_bindgen, PromiseOrValue};
Expand Down Expand Up @@ -320,16 +320,4 @@ impl EthProver {
}
}

#[near_bindgen]
impl AdminControlled for EthProver {
#[result_serializer(borsh)]
fn get_paused(&self) -> Mask {
self.paused
}

#[result_serializer(borsh)]
fn set_paused(&mut self, #[serializer(borsh)] paused: Mask) {
self.assert_owner();
self.paused = paused;
}
}
admin_controlled::impl_admin_controlled!(EthProver, paused);

0 comments on commit 0bb6b45

Please sign in to comment.