solidity中将地址转化为字节码,进行判断地址的所属权

技术分享
1389 0

solidity中将地址转化为字节码,进行判断地址的所属权

例如可以进行参加领取礼物活动时只让用户领取,而恶意合约无法领取

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;

contract addressCode{
    function getCode(address add) public view returns(bytes memory) {
        return address(add).code;
    }

    function gift()  public view returns(uint256){
        bytes memory senderCode = getCode(msg.sender);
        require(uint8(senderCode.length) == 0 ,unicode"合约不能调用,只能用户调用");
        return senderCode.length;
    }
    function giftContruct(address add_) public view returns(uint256){
        bytes memory senderCode = getCode(add_);
        // require(uint8(senderCode.length) == 0 ,unicode"合约不能调用,只能用户调用");
        return senderCode.length;
    }
}
最后更新 2023-09-12
评论 ( 0 )
OωO
隐私评论