Skip to content

Commit

Permalink
Simple EIP712 example
Browse files Browse the repository at this point in the history
  • Loading branch information
XieJunhua committed May 10, 2024
1 parent 76ac55d commit a283b6d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ contract NFTMarketDeploy is BaseScript {
uint256 deployPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployPrivateKey);
BaseERC20 token = new BaseERC20(vm.envAddress("ETH_FROM"));
MyERC721 myNFT = new MyERC721();
// MyERC721 myNFT = new MyERC721();

NFTMarket nft = new NFTMarket(address(myNFT), address(token));
// NFTMarket nft = new NFTMarket(address(myNFT), address(token));

vm.stopBroadcast();
}
Expand Down
32 changes: 32 additions & 0 deletions src/examples/SimpleEIP712.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

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

contract SimpleEIP712 is EIP712 {
constructor() EIP712("test", "1.0") { }

struct Mail {
address to;
string contents;
}

function verify(
bytes memory signature,
address signer,
address mailTo,
string memory mailContents
)
external
view
returns (address)
{
bytes32 digest = _hashTypedDataV4(
keccak256(abi.encode(keccak256("Mail(address to,string contents)"), mailTo, keccak256(bytes(mailContents))))
);
address recoveredSigner = ECDSA.recover(digest, signature);
require(recoveredSigner == signer, "signers not equal");
return recoveredSigner;
}
}
36 changes: 36 additions & 0 deletions test/examples/EIP712.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import { Test, console2 } from "forge-std/src/Test.sol";
import { SimpleEIP712 } from "../../src/examples/SimpleEIP712.sol";
import { MessageHashUtils } from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";

contract EIP172Test is Test {
using MessageHashUtils for bytes32;

SimpleEIP712 simpleEIP712;
address alice;
uint256 alicePk;

function setUp() public {
simpleEIP712 = new SimpleEIP712();
(alice, alicePk) = makeAddrAndKey("alice");
}

function testVerify() public {
// bytes memory signature,
// address signer,
// address mailTo,
// string memory mailContents

vm.startPrank(alice);
uint256 price = 1e6;
uint256 tokenId = 0;
bytes32 hash = keccak256(abi.encodePacked(tokenId + price)).toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePk, hash);

simpleEIP712.verify(tokenId, price, v, r, s);

vm.stopPrank();
}
}

0 comments on commit a283b6d

Please sign in to comment.