-
-
Save rmeissner/26bae7deaac62db764abc9a03902094e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: LGPL-3.0-only | |
pragma solidity >=0.7.0 <0.9.0; | |
/// @notice More details at https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/introspection/IERC165.sol | |
interface IERC165 { | |
/** | |
* @dev Returns true if this contract implements the interface defined by | |
* `interfaceId`. See the corresponding | |
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section] | |
* to learn more about how these ids are created. | |
* | |
* This function call must use less than 30 000 gas. | |
*/ | |
function supportsInterface(bytes4 interfaceId) external view returns (bool); | |
} | |
interface Guard is IERC165 { | |
function checkTransaction( | |
address to, | |
uint256 value, | |
bytes memory data, | |
uint8 operation, | |
uint256 safeTxGas, | |
uint256 baseGas, | |
uint256 gasPrice, | |
address gasToken, | |
address payable refundReceiver, | |
bytes memory signatures, | |
address msgSender | |
) external; | |
function checkAfterExecution(bytes32 txHash, bool success) external; | |
} | |
abstract contract BaseGuard is Guard { | |
function supportsInterface(bytes4 interfaceId) external view virtual override returns (bool) { | |
return | |
interfaceId == type(Guard).interfaceId || // 0xe6d7a83a | |
interfaceId == type(IERC165).interfaceId; // 0x01ffc9a7 | |
} | |
} | |
contract SafeBlocker is BaseGuard { | |
address public controller; | |
bool public active; | |
constructor(address _controller) { | |
controller = _controller; | |
} | |
function setActive(bool _active) external { | |
require(msg.sender == controller, "Only controller chan change active state"); | |
active = _active; | |
} | |
/** | |
* Approach with a Guard | |
* Set this contract as a guard (available from 1.3.0 on) to check txs | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
function checkTransaction( | |
address, | |
uint256, | |
bytes memory, | |
uint8, | |
uint256, | |
uint256, | |
uint256, | |
address, | |
address payable, | |
bytes memory, | |
address | |
) external override { | |
require(active, "Currently no transactions allowed"); | |
} | |
function checkAfterExecution(bytes32 txHash, bool success) external override { | |
// DO NOTHING | |
} | |
/** | |
* Approach without a Guard | |
* Set this contract as an owner that is required to confirm transactions | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
bytes4 internal constant EIP1271_MAGIC_VALUE = 0x20c13b0b; | |
/** | |
* @dev Should return whether the signature provided is valid for the provided data | |
* | |
* MUST return the bytes4 magic value 0x20c13b0b when function passes. | |
*/ | |
function isValidSignature(bytes memory, bytes memory) public view virtual returns (bytes4) { | |
require(active, "Currently no transactions allowed"); | |
// We approve any transaction if in active state | |
return EIP1271_MAGIC_VALUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment