SignatureChecker.test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const precompile = require('../../helpers/precompiles');
  5. const TEST_MESSAGE = ethers.id('OpenZeppelin');
  6. const TEST_MESSAGE_HASH = ethers.hashMessage(TEST_MESSAGE);
  7. const WRONG_MESSAGE = ethers.id('Nope');
  8. const WRONG_MESSAGE_HASH = ethers.hashMessage(WRONG_MESSAGE);
  9. async function fixture() {
  10. const [signer, other] = await ethers.getSigners();
  11. const mock = await ethers.deployContract('$SignatureChecker');
  12. const wallet = await ethers.deployContract('ERC1271WalletMock', [signer]);
  13. const malicious = await ethers.deployContract('ERC1271MaliciousMock');
  14. const signature = await signer.signMessage(TEST_MESSAGE);
  15. return { signer, other, mock, wallet, malicious, signature };
  16. }
  17. describe('SignatureChecker (ERC1271)', function () {
  18. before('deploying', async function () {
  19. Object.assign(this, await loadFixture(fixture));
  20. });
  21. describe('EOA account', function () {
  22. it('with matching signer and signature', async function () {
  23. await expect(this.mock.$isValidSignatureNow(this.signer, TEST_MESSAGE_HASH, this.signature)).to.eventually.be
  24. .true;
  25. });
  26. it('with invalid signer', async function () {
  27. await expect(this.mock.$isValidSignatureNow(this.other, TEST_MESSAGE_HASH, this.signature)).to.eventually.be
  28. .false;
  29. });
  30. it('with invalid signature', async function () {
  31. await expect(this.mock.$isValidSignatureNow(this.signer, WRONG_MESSAGE_HASH, this.signature)).to.eventually.be
  32. .false;
  33. });
  34. });
  35. describe('ERC1271 wallet', function () {
  36. for (const fn of ['isValidERC1271SignatureNow', 'isValidSignatureNow']) {
  37. describe(fn, function () {
  38. it('with matching signer and signature', async function () {
  39. await expect(this.mock.getFunction(`$${fn}`)(this.wallet, TEST_MESSAGE_HASH, this.signature)).to.eventually.be
  40. .true;
  41. });
  42. it('with invalid signer', async function () {
  43. await expect(this.mock.getFunction(`$${fn}`)(this.mock, TEST_MESSAGE_HASH, this.signature)).to.eventually.be
  44. .false;
  45. });
  46. it('with identity precompile', async function () {
  47. await expect(this.mock.getFunction(`$${fn}`)(precompile.identity, TEST_MESSAGE_HASH, this.signature)).to
  48. .eventually.be.false;
  49. });
  50. it('with invalid signature', async function () {
  51. await expect(this.mock.getFunction(`$${fn}`)(this.wallet, WRONG_MESSAGE_HASH, this.signature)).to.eventually
  52. .be.false;
  53. });
  54. it('with malicious wallet', async function () {
  55. await expect(this.mock.getFunction(`$${fn}`)(this.malicious, TEST_MESSAGE_HASH, this.signature)).to.eventually
  56. .be.false;
  57. });
  58. });
  59. }
  60. });
  61. });