EIP712.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { ethers } = require('hardhat');
  2. const { getDomain, domainType, domainSeparator, hashTypedData } = require('../../helpers/eip712');
  3. const { getChainId } = require('../../helpers/chainid');
  4. const { mapValues } = require('../../helpers/iterate');
  5. const EIP712Verifier = artifacts.require('$EIP712Verifier');
  6. const Clones = artifacts.require('$Clones');
  7. contract('EIP712', function (accounts) {
  8. const [mailTo] = accounts;
  9. const shortName = 'A Name';
  10. const shortVersion = '1';
  11. const longName = 'A'.repeat(40);
  12. const longVersion = 'B'.repeat(40);
  13. const cases = [
  14. ['short', shortName, shortVersion],
  15. ['long', longName, longVersion],
  16. ];
  17. for (const [shortOrLong, name, version] of cases) {
  18. describe(`with ${shortOrLong} name and version`, function () {
  19. beforeEach('deploying', async function () {
  20. this.eip712 = await EIP712Verifier.new(name, version);
  21. this.domain = {
  22. name,
  23. version,
  24. chainId: await getChainId(),
  25. verifyingContract: this.eip712.address,
  26. };
  27. this.domainType = domainType(this.domain);
  28. });
  29. describe('domain separator', function () {
  30. it('is internally available', async function () {
  31. const expected = await domainSeparator(this.domain);
  32. expect(await this.eip712.$_domainSeparatorV4()).to.equal(expected);
  33. });
  34. it("can be rebuilt using EIP-5267's eip712Domain", async function () {
  35. const rebuildDomain = await getDomain(this.eip712);
  36. expect(mapValues(rebuildDomain, String)).to.be.deep.equal(mapValues(this.domain, String));
  37. });
  38. if (shortOrLong === 'short') {
  39. // Long strings are in storage, and the proxy will not be properly initialized unless
  40. // the upgradeable contract variant is used and the initializer is invoked.
  41. it('adjusts when behind proxy', async function () {
  42. const factory = await Clones.new();
  43. const cloneReceipt = await factory.$clone(this.eip712.address);
  44. const cloneAddress = cloneReceipt.logs.find(({ event }) => event === 'return$clone').args.instance;
  45. const clone = new EIP712Verifier(cloneAddress);
  46. const cloneDomain = { ...this.domain, verifyingContract: clone.address };
  47. const reportedDomain = await getDomain(clone);
  48. expect(mapValues(reportedDomain, String)).to.be.deep.equal(mapValues(cloneDomain, String));
  49. const expectedSeparator = await domainSeparator(cloneDomain);
  50. expect(await clone.$_domainSeparatorV4()).to.equal(expectedSeparator);
  51. });
  52. }
  53. });
  54. it('hash digest', async function () {
  55. const structhash = web3.utils.randomHex(32);
  56. expect(await this.eip712.$_hashTypedDataV4(structhash)).to.be.equal(hashTypedData(this.domain, structhash));
  57. });
  58. it('digest', async function () {
  59. const message = {
  60. to: mailTo,
  61. contents: 'very interesting',
  62. };
  63. const types = {
  64. Mail: [
  65. { name: 'to', type: 'address' },
  66. { name: 'contents', type: 'string' },
  67. ],
  68. };
  69. const signer = ethers.Wallet.createRandom();
  70. const address = await signer.getAddress();
  71. const signature = await signer.signTypedData(this.domain, types, message);
  72. await this.eip712.verify(signature, address, message.to, message.contents);
  73. });
  74. it('name', async function () {
  75. expect(await this.eip712.$_EIP712Name()).to.be.equal(name);
  76. });
  77. it('version', async function () {
  78. expect(await this.eip712.$_EIP712Version()).to.be.equal(version);
  79. });
  80. });
  81. }
  82. });