Is a reliable way to identify the caller with the following code?

bool isContract = msg.sender != tx.origin;

marked as duplicate by eth Jan 5 at 21:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Please don't exclude contracts from interacting with your contract. If it's to avoid some security vulnerability, it's probably ineffective, and it means that no one using a multisig wallet can use your contract. tx.origin may also stop working in future Ethereum versions, especially as account abstraction work comes in. – smarx Jan 5 at 21:55

This should work. Also you can check the size of the code in the address if it is bigger than zero, is a contract

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

Hope this helps you

  • 1
    Note that during execution of a contract's constructor, that address's code size will be 0. – smarx Jan 5 at 21:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.