Skip to content

Instantly share code, notes, and snippets.

@ChristianOConnor
Last active May 28, 2023 01:34
Show Gist options
  • Save ChristianOConnor/6a8c2b22c2a388275fae2b881602c21f to your computer and use it in GitHub Desktop.
Save ChristianOConnor/6a8c2b22c2a388275fae2b881602c21f to your computer and use it in GitHub Desktop.
GetAddressFromSig which gets the address in an ethers split sig
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
contract GetAddressFromSig is ERC721, EIP712 {
struct Request {
address minter;
uint256 nonce;
uint256 deadline;
}
bytes32 public constant REQUEST_TYPEHASH = keccak256("Request(address minter,uint256 nonce,uint256 deadline)");
// Initialize _DOMAIN_SEPARATOR directly with static values
bytes32 private immutable _DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes("RandomReachDebug5Local")), // static name
keccak256(bytes("1")), // static version
31337, // static chainId
0x8464135c8F25Da09e49BC8782676a84730C318bC // static verifyingContract
)
);
constructor(string memory name, string memory symbol) ERC721(name, symbol) EIP712(name, "1") {}
function domainSeparator() public view returns (bytes32) {
return _DOMAIN_SEPARATOR;
}
function recoverSigner(Request memory request, uint8 v, bytes32 r, bytes32 s) public view returns (address) {
console.log("Domain: ");
console.log("Name: ", "RandomReachDebug5Local");
console.log("Version: ", "1");
console.log("ChainId: ", 31337);
console.log("VerifyingContract: ", "0x8464135c8F25Da09e49BC8782676a84730C318bC");
console.log("Types: ");
console.log("minter: ", "address");
console.log("nonce: ", "uint256");
console.log("deadline: ", "uint256");
console.log("Values: ");
console.log("minter: ", request.minter);
console.log("nonce: ", request.nonce);
console.log("deadline: ", request.deadline);
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01",
_DOMAIN_SEPARATOR,
keccak256(
abi.encode(
REQUEST_TYPEHASH,
request.minter,
request.nonce,
request.deadline
)
)
)
);
console.logBytes32(digest);
// ECDSA.recover returns the address that is associated with the public key
// that was used to sign the given data, in this case, the digest.
return ECDSA.recover(digest, v, r, s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment