Last active
October 22, 2021 12:32
-
-
Save xtremetom/1da3118dec7b1e327e653c1e89057d11 to your computer and use it in GitHub Desktop.
SigTestContract
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: MIT | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; | |
contract SigTest { | |
using ECDSA for bytes32; | |
address private systemAddress; | |
constructor (address _systemAddress) { | |
systemAddress = _systemAddress; | |
} | |
function isValidSignature(bytes32 hash, bytes memory signature) internal view returns (bool isValid) { | |
bytes32 signedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)); | |
return signedHash.recover(signature) == systemAddress; | |
} | |
function isDataValid(uint256 timestamp, bytes memory signature) public { | |
// Build the hash and check the sig | |
// We only accept sigs from the system | |
bytes32 msgHash = keccak256(abi.encodePacked(msg.sender, timestamp)); | |
require( | |
isValidSignature(msgHash, signature), | |
"Invalid signature" | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment