Skip to content

Instantly share code, notes, and snippets.

@ydm
Created September 14, 2023 14:31
Show Gist options
  • Select an option

  • Save ydm/7074302b4fd295dbe21a302fbbd499e4 to your computer and use it in GitHub Desktop.

Select an option

Save ydm/7074302b4fd295dbe21a302fbbd499e4 to your computer and use it in GitHub Desktop.
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

ydm commented Sep 14, 2023

Copy link
Copy Markdown
Author
// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.9;

import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {console} from "hardhat/console.sol";

contract Playground {
    constructor() {
    }

    function f() external pure {
        bytes32 digest = hex"f7e8e0901d87de336fdb8980d1304428bb478998befb1bce253ca5c801846be1";
        bytes memory signature = hex"7b86032782c1e43385ac196e08257c88ce4fde490cc01ffe0155b1d2dad3b18e5d5f73a7b23d6b80a0fb58258d9a1fa4a165aa98606658c25b162a26d3217d591b";

        address recovered = ECDSA.recover(ECDSA.toEthSignedMessageHash(digest), signature);
        
        console.log("Recovered address:");
        console.logAddress(recovered);

        if (recovered != 0x5c63B71F47dfA807aa6a8705e5cf134f4c3Afd8a) {
            revert("nope");
        }
    }
}

@ydm

ydm commented Sep 14, 2023

Copy link
Copy Markdown
Author
function hashMessage(message) {
    if (typeof (message) === "string") {
        message = toUtf8Bytes(message);
    }
    return keccak256(concat([
        toUtf8Bytes(MessagePrefix),
        toUtf8Bytes(String(message.length)),
        message
    ]));
}

@ydm

ydm commented Sep 14, 2023

Copy link
Copy Markdown
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