MessageHashUtils.test.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { domainSeparator, hashTypedData } = require('../../helpers/eip712');
  5. async function fixture() {
  6. const mock = await ethers.deployContract('$MessageHashUtils');
  7. return { mock };
  8. }
  9. describe('MessageHashUtils', function () {
  10. beforeEach(async function () {
  11. Object.assign(this, await loadFixture(fixture));
  12. });
  13. describe('toEthSignedMessageHash', function () {
  14. it('prefixes bytes32 data correctly', async function () {
  15. const message = ethers.randomBytes(32);
  16. const expectedHash = ethers.hashMessage(message);
  17. await expect(this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message)).to.eventually.equal(
  18. expectedHash,
  19. );
  20. });
  21. it('prefixes dynamic length data correctly', async function () {
  22. const message = ethers.randomBytes(128);
  23. const expectedHash = ethers.hashMessage(message);
  24. await expect(this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message)).to.eventually.equal(expectedHash);
  25. });
  26. it('version match for bytes32', async function () {
  27. const message = ethers.randomBytes(32);
  28. const fixed = await this.mock.getFunction('$toEthSignedMessageHash(bytes32)')(message);
  29. const dynamic = await this.mock.getFunction('$toEthSignedMessageHash(bytes)')(message);
  30. expect(fixed).to.equal(dynamic);
  31. });
  32. });
  33. describe('toDataWithIntendedValidatorHash', function () {
  34. it('returns the digest of `bytes32 messageHash` correctly', async function () {
  35. const verifier = ethers.Wallet.createRandom().address;
  36. const message = ethers.randomBytes(32);
  37. const expectedHash = ethers.solidityPackedKeccak256(
  38. ['string', 'address', 'bytes32'],
  39. ['\x19\x00', verifier, message],
  40. );
  41. await expect(
  42. this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes32)')(verifier, message),
  43. ).to.eventually.equal(expectedHash);
  44. });
  45. it('returns the digest of `bytes memory message` correctly', async function () {
  46. const verifier = ethers.Wallet.createRandom().address;
  47. const message = ethers.randomBytes(128);
  48. const expectedHash = ethers.solidityPackedKeccak256(
  49. ['string', 'address', 'bytes'],
  50. ['\x19\x00', verifier, message],
  51. );
  52. await expect(
  53. this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes)')(verifier, message),
  54. ).to.eventually.equal(expectedHash);
  55. });
  56. it('version match for bytes32', async function () {
  57. const verifier = ethers.Wallet.createRandom().address;
  58. const message = ethers.randomBytes(32);
  59. const fixed = await this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes)')(verifier, message);
  60. const dynamic = await this.mock.getFunction('$toDataWithIntendedValidatorHash(address,bytes32)')(
  61. verifier,
  62. message,
  63. );
  64. expect(fixed).to.equal(dynamic);
  65. });
  66. });
  67. describe('toTypedDataHash', function () {
  68. it('returns the digest correctly', async function () {
  69. const domain = {
  70. name: 'Test',
  71. version: '1',
  72. chainId: 1n,
  73. verifyingContract: ethers.Wallet.createRandom().address,
  74. };
  75. const structhash = ethers.randomBytes(32);
  76. const expectedHash = hashTypedData(domain, structhash);
  77. await expect(this.mock.$toTypedDataHash(domainSeparator(domain), structhash)).to.eventually.equal(expectedHash);
  78. });
  79. });
  80. });