EIP712.test.js 3.7 KB

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