MessageHashUtils.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. require('@openzeppelin/test-helpers');
  2. const { toEthSignedMessageHash, toDataWithIntendedValidatorHash } = require('../../helpers/sign');
  3. const { domainSeparator, hashTypedData } = require('../../helpers/eip712');
  4. const { expect } = require('chai');
  5. const MessageHashUtils = artifacts.require('$MessageHashUtils');
  6. contract('MessageHashUtils', function () {
  7. beforeEach(async function () {
  8. this.messageHashUtils = await MessageHashUtils.new();
  9. this.message = '0x' + Buffer.from('abcd').toString('hex');
  10. this.messageHash = web3.utils.sha3(this.message);
  11. this.verifyingAddress = web3.utils.toChecksumAddress(web3.utils.randomHex(20));
  12. });
  13. context('toEthSignedMessageHash', function () {
  14. it('prefixes bytes32 data correctly', async function () {
  15. expect(await this.messageHashUtils.methods['$toEthSignedMessageHash(bytes32)'](this.messageHash)).to.equal(
  16. toEthSignedMessageHash(this.messageHash),
  17. );
  18. });
  19. it('prefixes dynamic length data correctly', async function () {
  20. expect(await this.messageHashUtils.methods['$toEthSignedMessageHash(bytes)'](this.message)).to.equal(
  21. toEthSignedMessageHash(this.message),
  22. );
  23. });
  24. });
  25. context('toDataWithIntendedValidatorHash', function () {
  26. it('returns the digest correctly', async function () {
  27. expect(
  28. await this.messageHashUtils.$toDataWithIntendedValidatorHash(this.verifyingAddress, this.message),
  29. ).to.equal(toDataWithIntendedValidatorHash(this.verifyingAddress, this.message));
  30. });
  31. });
  32. context('toTypedDataHash', function () {
  33. it('returns the digest correctly', async function () {
  34. const domain = {
  35. name: 'Test',
  36. version: 1,
  37. chainId: 1,
  38. verifyingContract: this.verifyingAddress,
  39. };
  40. const structhash = web3.utils.randomHex(32);
  41. const expectedDomainSeparator = await domainSeparator(domain);
  42. expect(await this.messageHashUtils.$toTypedDataHash(expectedDomainSeparator, structhash)).to.equal(
  43. hashTypedData(domain, structhash),
  44. );
  45. });
  46. });
  47. });