Created
September 14, 2023 14:31
-
-
Save ydm/7074302b4fd295dbe21a302fbbd499e4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { ethers } from "hardhat"; | |
| import { Signer } from "ethers"; | |
| async function main() { | |
| let admin: Signer = await ethers.getSigners().then(xs => xs[0]); | |
| const message: Uint8Array = ethers.toUtf8Bytes("mindless"); | |
| const digest: Uint8Array = ethers.getBytes(ethers.keccak256(message)); | |
| const signature: string = await admin.signMessage(digest); | |
| const want = await admin.getAddress(); | |
| const have: string = ethers.recoverAddress(ethers.hashMessage(digest), signature); | |
| console.log("message :", ethers.hexlify(message)); | |
| console.log("digest :", ethers.hexlify(digest)); | |
| console.log("signature :", signature); | |
| console.log("recovered :", have); | |
| console.log("same :", want === have); | |
| } | |
| main().catch((error) => { | |
| console.error(error); | |
| process.exit(1); | |
| }); |
ydm
commented
Sep 14, 2023
Author
Author
function hashMessage(message) {
if (typeof (message) === "string") {
message = toUtf8Bytes(message);
}
return keccak256(concat([
toUtf8Bytes(MessagePrefix),
toUtf8Bytes(String(message.length)),
message
]));
}
Author
/**
* @dev Returns an Ethereum Signed Message, created from a `hash`. This
* produces hash corresponding to the one signed with the
* https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
* JSON-RPC method as part of EIP-191.
*
* See {recover}.
*/
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 message) {
// 32 is the length in bytes of hash,
// enforced by the type signature above
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, "\x19Ethereum Signed Message:\n32")
mstore(0x1c, hash)
message := keccak256(0x00, 0x3c)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment