EIP712.test.js 3.6 KB

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