SignatureChecker.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { toEthSignedMessageHash } = require('../../helpers/sign');
  2. const { expect } = require('chai');
  3. const SignatureCheckerMock = artifacts.require('SignatureCheckerMock');
  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 SignatureCheckerMock.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(await this.signaturechecker.isValidSignatureNow(
  19. signer,
  20. toEthSignedMessageHash(TEST_MESSAGE),
  21. this.signature,
  22. )).to.equal(true);
  23. });
  24. it('with invalid signer', async function () {
  25. expect(await this.signaturechecker.isValidSignatureNow(
  26. other,
  27. toEthSignedMessageHash(TEST_MESSAGE),
  28. this.signature,
  29. )).to.equal(false);
  30. });
  31. it('with invalid signature', async function () {
  32. expect(await this.signaturechecker.isValidSignatureNow(
  33. signer,
  34. toEthSignedMessageHash(WRONG_MESSAGE),
  35. this.signature,
  36. )).to.equal(false);
  37. });
  38. });
  39. context('ERC1271 wallet', function () {
  40. it('with matching signer and signature', async function () {
  41. expect(await this.signaturechecker.isValidSignatureNow(
  42. this.wallet.address,
  43. toEthSignedMessageHash(TEST_MESSAGE),
  44. this.signature,
  45. )).to.equal(true);
  46. });
  47. it('with invalid signer', async function () {
  48. expect(await this.signaturechecker.isValidSignatureNow(
  49. this.signaturechecker.address,
  50. toEthSignedMessageHash(TEST_MESSAGE),
  51. this.signature,
  52. )).to.equal(false);
  53. });
  54. it('with invalid signature', async function () {
  55. expect(await this.signaturechecker.isValidSignatureNow(
  56. this.wallet.address,
  57. toEthSignedMessageHash(WRONG_MESSAGE),
  58. this.signature,
  59. )).to.equal(false);
  60. });
  61. it('with malicious wallet', async function () {
  62. expect(await this.signaturechecker.isValidSignatureNow(
  63. this.malicious.address,
  64. toEthSignedMessageHash(TEST_MESSAGE),
  65. this.signature,
  66. )).to.equal(false);
  67. });
  68. });
  69. });