【问题标题】:Can you detect if a msg.sender is a smart contract?你能检测出 msg.sender 是否是智能合约吗?
【发布时间】:2019-02-27 10:18:18
【问题描述】:

如果我调用了一个可靠的函数,有没有办法让该函数知道msg.sender 是否是智能合约?

【问题讨论】:

标签: solidity


【解决方案1】:

是的,你可以:

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

【讨论】:

  • 当合约的构造函数正在执行时,它的代码大小为 0。(也许这也发生在 selfdestruct 调用之后?)所以如果问题是关于某种形式的安全性,请不要t 尝试区分。但如果是为了“如果收件人是一个合约,我们想首先尝试调用这个函数”,那么像这样的尽力而为的方法可能会有一个有效的用例。
【解决方案2】:

我认为没有一种安全的方法来检测 msg.sender 是否是智能合约,因为这可以通过从智能合约的构造函数中调用来破解。

Etherenaut 的第 14 级“守门人二”就是关于这个问题的。见解释: Ethernaut Lvl 14 Gatekeeper 2 Walkthrough: How contracts initialize (and how to do bitwise operations)

extcodesize(sender) 应该返回 0,如果 extcodesize 是发送者合约的原始构造函数中的子例程

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 2019-08-12
    • 2022-12-06
    • 2021-06-14
    • 1970-01-01
    • 2022-08-18
    • 2020-06-01
    • 2022-07-02
    • 2017-02-28
    相关资源
    最近更新 更多