SignatureChecker.test.js 2.4 KB

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