Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Enigmatic331/1074b422415d72a6ed9a9c7a6db6cf89 to your computer and use it in GitHub Desktop.
Save Enigmatic331/1074b422415d72a6ed9a9c7a6db6cf89 to your computer and use it in GitHub Desktop.
did something
pragma solidity ^0.8.0;
contract VerifySignature {
function verifySignedMessage(string memory _originalMessage, bytes memory _signature) public pure returns (address) {
bytes memory prefix = "\x19Ethereum Signed Message:\n";
bytes memory messageLength = bytes(uintToString(bytes(_originalMessage).length));
bytes memory prefixedMessage = abi.encodePacked(prefix, messageLength, _originalMessage);
bytes32 messageHash = keccak256(prefixedMessage);
return recoverSigner(messageHash, _signature);
}
function recoverSigner(bytes32 _messageHash, bytes memory _signature) internal pure returns (address) {
(bytes32 r, bytes32 s, uint8 v) = splitSignature(_signature);
// try thisssssss
if (v < 27) {
v += 27;
}
return ecrecover(_messageHash, v, r, s);
}
function splitSignature(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)))
}
}
// so we can get the length proper in bytes
function uintToString(uint _i) public pure returns (string memory) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment