SignatureChecker.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const { toEthSignedMessageHash } = require('../../helpers/sign');
  2. const { expect } = require('chai');
  3. const SignatureChecker = artifacts.require('$SignatureChecker');
  4. const ERC1271WalletMock = artifacts.require('ERC1271WalletMock');
  5. const ERC1271MaliciousMock = artifacts.require('ERC1271MaliciousMock');
  6. const TEST_MESSAGE = web3.utils.sha3('OpenZeppelin');
  7. const WRONG_MESSAGE = web3.utils.sha3('Nope');
  8. contract('SignatureChecker (ERC1271)', function (accounts) {
  9. const [signer, other] = accounts;
  10. before('deploying', async function () {
  11. this.signaturechecker = await SignatureChecker.new();
  12. this.wallet = await ERC1271WalletMock.new(signer);
  13. this.malicious = await ERC1271MaliciousMock.new();
  14. this.signature = await web3.eth.sign(TEST_MESSAGE, signer);
  15. });
  16. context('EOA account', function () {
  17. it('with matching signer and signature', async function () {
  18. expect(
  19. await this.signaturechecker.$isValidSignatureNow(signer, toEthSignedMessageHash(TEST_MESSAGE), this.signature),
  20. ).to.equal(true);
  21. });
  22. it('with invalid signer', async function () {
  23. expect(
  24. await this.signaturechecker.$isValidSignatureNow(other, toEthSignedMessageHash(TEST_MESSAGE), this.signature),
  25. ).to.equal(false);
  26. });
  27. it('with invalid signature', async function () {
  28. expect(
  29. await this.signaturechecker.$isValidSignatureNow(signer, toEthSignedMessageHash(WRONG_MESSAGE), this.signature),
  30. ).to.equal(false);
  31. });
  32. });
  33. context('ERC1271 wallet', function () {
  34. it('with matching signer and signature', async function () {
  35. expect(
  36. await this.signaturechecker.$isValidSignatureNow(
  37. this.wallet.address,
  38. toEthSignedMessageHash(TEST_MESSAGE),
  39. this.signature,
  40. ),
  41. ).to.equal(true);
  42. });
  43. it('with invalid signer', async function () {
  44. expect(
  45. await this.signaturechecker.$isValidSignatureNow(
  46. this.signaturechecker.address,
  47. toEthSignedMessageHash(TEST_MESSAGE),
  48. this.signature,
  49. ),
  50. ).to.equal(false);
  51. });
  52. it('with invalid signature', async function () {
  53. expect(
  54. await this.signaturechecker.$isValidSignatureNow(
  55. this.wallet.address,
  56. toEthSignedMessageHash(WRONG_MESSAGE),
  57. this.signature,
  58. ),
  59. ).to.equal(false);
  60. });
  61. it('with malicious wallet', async function () {
  62. expect(
  63. await this.signaturechecker.$isValidSignatureNow(
  64. this.malicious.address,
  65. toEthSignedMessageHash(TEST_MESSAGE),
  66. this.signature,
  67. ),
  68. ).to.equal(false);
  69. });
  70. });
  71. });