Skip to content

Instantly share code, notes, and snippets.

@fredlacs
Last active March 20, 2022 13:30
Show Gist options
  • Select an option

  • Save fredlacs/a01e04041d515c74e2e0cfbf6ad007ac to your computer and use it in GitHub Desktop.

Select an option

Save fredlacs/a01e04041d515c74e2e0cfbf6ad007ac to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.0;
import { ISignatureValidatorConstants, ISignatureValidator } from "https://github.com/gnosis/safe-contracts/blob/v1.3.0/contracts/interfaces/ISignatureValidator.sol";
import { IERC721 } from "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.5.0/contracts/token/ERC721/IERC721.sol";
enum Flag { Nil, Validated, NotValidated }
/// @notice allows a 1 of 1 gnosis safe to be controlled by a NFT
contract SafeOwner is ISignatureValidator {
IERC721 immutable public nft;
uint256 immutable public id;
Flag internal flag;
constructor(IERC721 _nft, uint256 _id) {
assert(
ISignatureValidatorConstants.EIP1271_MAGIC_VALUE ==
bytes4(keccak256("isValidSignature(bytes,bytes)"))
);
nft = _nft;
id = _id;
flag = Flag.NotValidated;
}
function owner() public view returns (address) {
return nft.ownerOf(id);
}
modifier ownerInitiated {
require(msg.sender == owner(), "NOT_NFT_OWNER");
require(flag == Flag.NotValidated, "NO_REENTRANCY_PLS");
flag = Flag.Validated;
_;
flag = Flag.NotValidated;
}
modifier onlyIfPrevOwnerInitiated {
require(flag == Flag.Validated, "NOT_OWNER_INITIATED");
_;
}
/// @notice all calls that come from this will be allowed. watch out if there are other contracts that might pass
/// the `isValidSignature` when not intended
function sendTx(address to, bytes calldata data) external payable ownerInitiated {
(bool res, ) = to.call{ value: msg.value }(data);
require(res, "CALL_FAIL");
}
function isValidSignature(bytes memory /* _data */ , bytes memory /* _signature */ )
public
view
override
onlyIfPrevOwnerInitiated
returns (bytes4)
{
return ISignatureValidatorConstants.EIP1271_MAGIC_VALUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment