Skip to content

Instantly share code, notes, and snippets.

@whyvrafvr
Created November 13, 2021 13:43
Show Gist options
  • Save whyvrafvr/5d07b0c5ec7df64bc482190dbae5e7ac to your computer and use it in GitHub Desktop.
Save whyvrafvr/5d07b0c5ec7df64bc482190dbae5e7ac to your computer and use it in GitHub Desktop.
OwnerZero.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.9.0;
contract Test {
address public regMsgSender = address(0);
function setMsgSender () external
{
regMsgSender = msg.sender;
}
function getMsgSender () public view returns (address)
{
return regMsgSender;
}
function setMsgSender2 () public
{
string memory s = string(abi.encodePacked("Violation: msg.sender is not the sender before: ", toAsciiString(regMsgSender), " - ", toAsciiString(msg.sender)));
require(regMsgSender == msg.sender, s);
regMsgSender = msg.sender;
}
function toAsciiString(address x) internal pure returns (string memory) {
bytes memory s = new bytes(40);
for (uint i = 0; i < 20; i++) {
bytes1 b = bytes1(uint8(uint(uint160(x)) / (2**(8*(19 - i)))));
bytes1 hi = bytes1(uint8(b) / 16);
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
s[2*i] = char(hi);
s[2*i+1] = char(lo);
}
return string(s);
}
function char(bytes1 b) internal pure returns (bytes1 c) {
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment