Skip to content

Instantly share code, notes, and snippets.

@airled
Created August 14, 2024 13:54
Show Gist options
  • Save airled/6e1b5ba7959bdfd3cd86cc2f4322c46e to your computer and use it in GitHub Desktop.
Save airled/6e1b5ba7959bdfd3cd86cc2f4322c46e to your computer and use it in GitHub Desktop.
Extract a signer address from signature of a personal signed message
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
using MessageHashUtils for bytes;
using ECDSA for bytes32;
contract AddressExtractor {
function extract(
string calldata message,
bytes calldata signature
) public pure returns (address) {
bytes memory msgBytes = bytes(message);
bytes32 signedMessageHash = msgBytes.toEthSignedMessageHash();
return signedMessageHash.recover(signature);
}
}
@airled
Copy link
Author

airled commented Dec 6, 2024

Sign message with ruby eth gem:

private_key = ''
key = Eth::Key.new(priv: private_key)
message = '123'
signature = '0x' + key.personal_sign(message)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment