Skip to content

Instantly share code, notes, and snippets.

@Hero-Development
Last active June 30, 2022 17:39
Show Gist options
  • Save Hero-Development/40a3a14a547ccd6d5b2b69122d52fe05 to your computer and use it in GitHub Desktop.
Save Hero-Development/40a3a14a547ccd6d5b2b69122d52fe05 to your computer and use it in GitHub Desktop.
The base contract for other contracts that need to receive and verify struct data and its signature
// SPDX-License-Identifier: BSD-3
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
contract Signed is Ownable{
using ECDSA for bytes32;
address internal _signer;
constructor( address signer ){
setSigner( signer );
}
function setSigner( address signer ) public onlyOwner{
_signer = signer;
}
function _createHash( bytes memory data ) internal virtual view returns ( bytes32 ){
return keccak256( abi.encodePacked( address(this), msg.sender, data ) );
}
function _isAuthorizedSigner( bytes memory data, bytes calldata signature ) internal view virtual returns( bool ){
return _signer == _recoverSigner( _createHash( data ), signature );
}
function _recoverSigner( bytes32 hashed, bytes memory signature ) internal pure returns( address ){
return hashed.toEthSignedMessageHash().recover( signature );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment