3

When the user calls the delegatecallSetN function of contract D below, the msg.sender in contract E is the address of the user (as shown in this example).

contract D {
  uint public n;
  address public sender;

  function delegatecallSetN(address _e, uint _n) {
    _e.delegatecall(bytes4(sha3("setN(uint256)")), _n); // D's storage is set, E is not modified 
  }
}

contract E {
  uint public n;
  address public sender;
  function setN(uint _n) {
    n = _n;
    sender = msg.sender;
  }
}

My question is: How can we check in contract E that the call was done through contract D and not directly through the user?

Mike B
  • 131
  • 5
  • https://stackoverflow.com/questions/37644395/how-to-find-out-if-an-ethereum-address-is-a-contract .. Please look at Manuel Aráoz Answer.. I think you can make use of Iscontract function mentioned and check only Contract D has invoked Contract E and not by user. – Rangesh Sep 11 '17 at 17:35

2 Answers2

2

Should work if you use tx.origin. However this is usually advised against.

1

See this answer, https://stackoverflow.com/a/40939341/7482810

using some EVM assembly code to get the address' code size:

 function isContract(address addr) returns (bool) {
 uint size;
 assembly { size := extcodesize(addr) }
 return size > 0;}

As to why this function works, see https://ethereum.stackexchange.com/a/14016/19016

Zacharin
  • 311
  • 1
  • 3