【发布时间】:2019-02-27 10:18:18
【问题描述】:
如果我调用了一个可靠的函数,有没有办法让该函数知道msg.sender 是否是智能合约?
【问题讨论】:
-
你可以检查
msg.sender == tx.origin。如果为 false,则调用者是智能合约。
标签: solidity
如果我调用了一个可靠的函数,有没有办法让该函数知道msg.sender 是否是智能合约?
【问题讨论】:
msg.sender == tx.origin。如果为 false,则调用者是智能合约。
标签: solidity
是的,你可以:
function isContract(address _address) returns (bool isContract){
uint32 size;
assembly {
size := extcodesize(_address)
}
return (size > 0);
}
【讨论】:
selfdestruct 调用之后?)所以如果问题是关于某种形式的安全性,请不要t 尝试区分。但如果是为了“如果收件人是一个合约,我们想首先尝试调用这个函数”,那么像这样的尽力而为的方法可能会有一个有效的用例。
我认为没有一种安全的方法来检测 msg.sender 是否是智能合约,因为这可以通过从智能合约的构造函数中调用来破解。
Etherenaut 的第 14 级“守门人二”就是关于这个问题的。见解释: Ethernaut Lvl 14 Gatekeeper 2 Walkthrough: How contracts initialize (and how to do bitwise operations)
extcodesize(sender) 应该返回 0,如果 extcodesize 是发送者合约的原始构造函数中的子例程
【讨论】: