EIP712.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { getDomain, domainSeparator, hashTypedData } = require('../../helpers/eip712');
  5. const { formatType } = require('../../helpers/eip712-types');
  6. const { getChainId } = require('../../helpers/chainid');
  7. const LENGTHS = {
  8. short: ['A Name', '1'],
  9. long: ['A'.repeat(40), 'B'.repeat(40)],
  10. };
  11. const fixture = async () => {
  12. const [from, to] = await ethers.getSigners();
  13. const lengths = {};
  14. for (const [shortOrLong, [name, version]] of Object.entries(LENGTHS)) {
  15. lengths[shortOrLong] = { name, version };
  16. lengths[shortOrLong].eip712 = await ethers.deployContract('$EIP712Verifier', [name, version]);
  17. lengths[shortOrLong].domain = {
  18. name,
  19. version,
  20. chainId: await getChainId(),
  21. verifyingContract: lengths[shortOrLong].eip712.target,
  22. };
  23. }
  24. return { from, to, lengths };
  25. };
  26. describe('EIP712', function () {
  27. for (const [shortOrLong, [name, version]] of Object.entries(LENGTHS)) {
  28. describe(`with ${shortOrLong} name and version`, function () {
  29. beforeEach('deploying', async function () {
  30. Object.assign(this, await loadFixture(fixture));
  31. Object.assign(this, this.lengths[shortOrLong]);
  32. });
  33. describe('domain separator', function () {
  34. it('is internally available', async function () {
  35. const expected = await domainSeparator(this.domain);
  36. expect(await this.eip712.$_domainSeparatorV4()).to.equal(expected);
  37. });
  38. it("can be rebuilt using EIP-5267's eip712Domain", async function () {
  39. const rebuildDomain = await getDomain(this.eip712);
  40. expect(rebuildDomain).to.be.deep.equal(this.domain);
  41. });
  42. if (shortOrLong === 'short') {
  43. // Long strings are in storage, and the proxy will not be properly initialized unless
  44. // the upgradeable contract variant is used and the initializer is invoked.
  45. it('adjusts when behind proxy', async function () {
  46. const factory = await ethers.deployContract('$Clones');
  47. const clone = await factory
  48. .$clone(this.eip712)
  49. .then(tx => tx.wait())
  50. .then(receipt => receipt.logs.find(ev => ev.fragment.name == 'return$clone').args.instance)
  51. .then(address => ethers.getContractAt('$EIP712Verifier', address));
  52. const expectedDomain = { ...this.domain, verifyingContract: clone.target };
  53. expect(await getDomain(clone)).to.be.deep.equal(expectedDomain);
  54. const expectedSeparator = await domainSeparator(expectedDomain);
  55. expect(await clone.$_domainSeparatorV4()).to.equal(expectedSeparator);
  56. });
  57. }
  58. });
  59. it('hash digest', async function () {
  60. const structhash = ethers.hexlify(ethers.randomBytes(32));
  61. expect(await this.eip712.$_hashTypedDataV4(structhash)).to.equal(hashTypedData(this.domain, structhash));
  62. });
  63. it('digest', async function () {
  64. const types = {
  65. Mail: formatType({
  66. to: 'address',
  67. contents: 'string',
  68. }),
  69. };
  70. const message = {
  71. to: this.to.address,
  72. contents: 'very interesting',
  73. };
  74. const signature = await this.from.signTypedData(this.domain, types, message);
  75. await expect(this.eip712.verify(signature, this.from.address, message.to, message.contents)).to.not.be.reverted;
  76. });
  77. it('name', async function () {
  78. expect(await this.eip712.$_EIP712Name()).to.equal(name);
  79. });
  80. it('version', async function () {
  81. expect(await this.eip712.$_EIP712Version()).to.equal(version);
  82. });
  83. });
  84. }
  85. });