Last active
August 27, 2023 07:23
-
-
Save Theo6890/a85d2f8cd672dfdde8dd11c3161cc360 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* | |
* @param {ethers.Wallet} wallet | |
* @param {ethers.BigNumber} chainId | |
* @param {string} verifyingContract | |
* @returns {string} full signature | |
* | |
* rewards is an object: { | |
id: ethers.BigNumber, | |
amount: ethers.BigNumber, | |
tokenContract: string, | |
recipient: string, | |
timestamp: ethers.BigNumber | |
* } | |
*/ | |
async function signEIP712(wallet, chainId, verifyingContract, rewards) { | |
const domain = { | |
name: 'Rewards', | |
version: '1', | |
chainId, | |
verifyingContract, | |
}; | |
// The named list of all type definitions | |
const types = { | |
Rewards: [ | |
{ name: 'id', type: 'uint256' }, | |
{ name: 'amount', type: 'uint256' }, | |
{ name: 'tokenContract', type: 'address' }, | |
{ name: 'recipient', type: 'address' }, | |
{ name: 'timestamp', type: 'uint256' }, | |
], | |
}; | |
return await wallet._signTypedData(domain, types, { ...rewards }); | |
} | |
module.exports = { | |
signEIP712, | |
}; | |
------------------ FOUNDRY mock up ------------------ | |
// // SPDX-License-Identifier: UNLICENSED | |
// pragma solidity ^0.8.13; | |
// import {Strings} from "openzeppelin-contracts/utils/Strings.sol"; | |
// import {SetUpRewards, MockRewards} from "./SetUpRewards.sol"; | |
// contract RewardsTest_claimRewards is SetUpRewards { | |
// function test_signature_SolidityVsJS() public { | |
// (uint8 _v, bytes32 _r, bytes32 _s) = vm.sign( | |
// privateKey, | |
// rewards.hashReward_EIP712Format(erc20Rewards) | |
// ); | |
// bytes memory soliditySignature = abi.encodePacked(_r, _s, _v); | |
// bytes memory jsSignature = __signJs( | |
// Strings.toHexString(uint160(address(rewards)), 20) | |
// ); | |
// assertEq(soliditySignature, jsSignature); | |
// } | |
// function __signJs(string memory rewardsContractAddr) | |
// private | |
// returns (bytes memory) | |
// { | |
// string[] memory cmd = new string[](3); | |
// cmd[0] = "node"; | |
// cmd[1] = "test/utils/test_ethersEIP712.js"; | |
// cmd[2] = rewardsContractAddr; | |
// return vm.ffi(cmd); | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment