-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerifySignature.sol
45 lines (36 loc) · 1.33 KB
/
VerifySignature.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract VerifySignature {
function verify(address _signer, string memory _message, bytes memory _sig)
external pure returns (bool)
{
bytes32 messageHash = getMessageHash(_message);
bytes32 ethSignedMessageHash = getEthSignedMessageHash(messageHash);
return recover(ethSignedMessageHash, _sig) == _signer;
}
function getMessageHash(string memory _message) public pure returns (bytes32) {
return keccak256(abi.encodePacked(_message));
}
function getEthSignedMessageHash(bytes32 _messageHash) public pure returns (bytes32) {
return keccak256(abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
_messageHash
));
}
function recover(bytes32 _ethSignedMessageHash, bytes memory _sig)
public pure returns (address)
{
(bytes32 r, bytes32 s, uint8 v) = _split(_sig);
return ecrecover(_ethSignedMessageHash, v, r, s);
}
function _split(bytes memory _sig) internal pure
returns (bytes32 r, bytes32 s, uint8 v)
{
require(_sig.length == 65, "invalid signature length");
assembly {
r := mload(add(_sig, 32))
s := mload(add(_sig, 64))
v := byte(0, mload(add(_sig, 96)))
}
}
}