EIP712.test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const ethSigUtil = require('eth-sig-util');
  2. const Wallet = require('ethereumjs-wallet').default;
  3. const { EIP712Domain, domainSeparator } = require('../helpers/eip712');
  4. const EIP712 = artifacts.require('EIP712External');
  5. contract('EIP712', function (accounts) {
  6. const [mailTo] = accounts;
  7. const name = 'A Name';
  8. const version = '1';
  9. beforeEach('deploying', async function () {
  10. this.eip712 = await EIP712.new(name, version);
  11. // We get the chain id from the contract because Ganache (used for coverage) does not return the same chain id
  12. // from within the EVM as from the JSON RPC interface.
  13. // See https://github.com/trufflesuite/ganache-core/issues/515
  14. this.chainId = await this.eip712.getChainId();
  15. });
  16. it('domain separator', async function () {
  17. expect(
  18. await this.eip712.domainSeparator(),
  19. ).to.equal(
  20. await domainSeparator(name, version, this.chainId, this.eip712.address),
  21. );
  22. });
  23. it('digest', async function () {
  24. const chainId = this.chainId;
  25. const verifyingContract = this.eip712.address;
  26. const message = {
  27. to: mailTo,
  28. contents: 'very interesting',
  29. };
  30. const data = {
  31. types: {
  32. EIP712Domain,
  33. Mail: [
  34. { name: 'to', type: 'address' },
  35. { name: 'contents', type: 'string' },
  36. ],
  37. },
  38. domain: { name, version, chainId, verifyingContract },
  39. primaryType: 'Mail',
  40. message,
  41. };
  42. const wallet = Wallet.generate();
  43. const signature = ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data });
  44. await this.eip712.verify(signature, wallet.getAddressString(), message.to, message.contents);
  45. });
  46. });