Migrate EIP712 to ethersjs (#4750)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Renan Souza
2023-11-23 04:40:12 +00:00
committed by GitHub
parent 9702b67ce1
commit 6a56b3b08d
9 changed files with 145 additions and 123 deletions

View File

@ -1,38 +1,39 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { getDomain, domainType, domainSeparator, hashTypedData } = require('../../helpers/eip712');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { getDomain, domainSeparator, hashTypedData } = require('../../helpers/eip712');
const { getChainId } = require('../../helpers/chainid');
const { mapValues } = require('../../helpers/iterate');
const EIP712Verifier = artifacts.require('$EIP712Verifier');
const Clones = artifacts.require('$Clones');
const LENGTHS = {
short: ['A Name', '1'],
long: ['A'.repeat(40), 'B'.repeat(40)],
};
contract('EIP712', function (accounts) {
const [mailTo] = accounts;
const fixture = async () => {
const [from, to] = await ethers.getSigners();
const shortName = 'A Name';
const shortVersion = '1';
const lengths = {};
for (const [shortOrLong, [name, version]] of Object.entries(LENGTHS)) {
lengths[shortOrLong] = { name, version };
lengths[shortOrLong].eip712 = await ethers.deployContract('$EIP712Verifier', [name, version]);
lengths[shortOrLong].domain = {
name,
version,
chainId: await getChainId(),
verifyingContract: lengths[shortOrLong].eip712.target,
};
}
const longName = 'A'.repeat(40);
const longVersion = 'B'.repeat(40);
return { from, to, lengths };
};
const cases = [
['short', shortName, shortVersion],
['long', longName, longVersion],
];
for (const [shortOrLong, name, version] of cases) {
describe('EIP712', function () {
for (const [shortOrLong, [name, version]] of Object.entries(LENGTHS)) {
describe(`with ${shortOrLong} name and version`, function () {
beforeEach('deploying', async function () {
this.eip712 = await EIP712Verifier.new(name, version);
this.domain = {
name,
version,
chainId: await getChainId(),
verifyingContract: this.eip712.address,
};
this.domainType = domainType(this.domain);
Object.assign(this, await loadFixture(fixture));
Object.assign(this, this.lengths[shortOrLong]);
});
describe('domain separator', function () {
@ -44,7 +45,7 @@ contract('EIP712', function (accounts) {
it("can be rebuilt using EIP-5267's eip712Domain", async function () {
const rebuildDomain = await getDomain(this.eip712);
expect(mapValues(rebuildDomain, String)).to.be.deep.equal(mapValues(this.domain, String));
expect(rebuildDomain).to.be.deep.equal(this.domain);
});
if (shortOrLong === 'short') {
@ -52,33 +53,29 @@ contract('EIP712', function (accounts) {
// the upgradeable contract variant is used and the initializer is invoked.
it('adjusts when behind proxy', async function () {
const factory = await Clones.new();
const cloneReceipt = await factory.$clone(this.eip712.address);
const cloneAddress = cloneReceipt.logs.find(({ event }) => event === 'return$clone').args.instance;
const clone = new EIP712Verifier(cloneAddress);
const factory = await ethers.deployContract('$Clones');
const cloneDomain = { ...this.domain, verifyingContract: clone.address };
const clone = await factory
.$clone(this.eip712)
.then(tx => tx.wait())
.then(receipt => receipt.logs.find(ev => ev.fragment.name == 'return$clone').args.instance)
.then(address => ethers.getContractAt('$EIP712Verifier', address));
const reportedDomain = await getDomain(clone);
expect(mapValues(reportedDomain, String)).to.be.deep.equal(mapValues(cloneDomain, String));
const expectedDomain = { ...this.domain, verifyingContract: clone.target };
expect(await getDomain(clone)).to.be.deep.equal(expectedDomain);
const expectedSeparator = await domainSeparator(cloneDomain);
const expectedSeparator = await domainSeparator(expectedDomain);
expect(await clone.$_domainSeparatorV4()).to.equal(expectedSeparator);
});
}
});
it('hash digest', async function () {
const structhash = web3.utils.randomHex(32);
expect(await this.eip712.$_hashTypedDataV4(structhash)).to.be.equal(hashTypedData(this.domain, structhash));
const structhash = ethers.hexlify(ethers.randomBytes(32));
expect(await this.eip712.$_hashTypedDataV4(structhash)).to.equal(hashTypedData(this.domain, structhash));
});
it('digest', async function () {
const message = {
to: mailTo,
contents: 'very interesting',
};
const types = {
Mail: [
{ name: 'to', type: 'address' },
@ -86,19 +83,22 @@ contract('EIP712', function (accounts) {
],
};
const signer = ethers.Wallet.createRandom();
const address = await signer.getAddress();
const signature = await signer.signTypedData(this.domain, types, message);
const message = {
to: this.to.address,
contents: 'very interesting',
};
await this.eip712.verify(signature, address, message.to, message.contents);
const signature = await this.from.signTypedData(this.domain, types, message);
await expect(this.eip712.verify(signature, this.from.address, message.to, message.contents)).to.not.be.reverted;
});
it('name', async function () {
expect(await this.eip712.$_EIP712Name()).to.be.equal(name);
expect(await this.eip712.$_EIP712Name()).to.equal(name);
});
it('version', async function () {
expect(await this.eip712.$_EIP712Version()).to.be.equal(version);
expect(await this.eip712.$_EIP712Version()).to.equal(version);
});
});
}