SignatureChecker.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 TEST_MESSAGE = web3.utils.sha3('OpenZeppelin');
  6. const WRONG_MESSAGE = web3.utils.sha3('Nope');
  7. contract('SignatureChecker (ERC1271)', function (accounts) {
  8. const [signer, other] = accounts;
  9. before('deploying', async function () {
  10. this.signaturechecker = await SignatureCheckerMock.new();
  11. this.wallet = await ERC1271WalletMock.new(signer);
  12. this.signature = await web3.eth.sign(TEST_MESSAGE, signer);
  13. });
  14. context('EOA account', function () {
  15. it('with matching signer and signature', async function () {
  16. expect(await this.signaturechecker.isValidSignatureNow(
  17. signer,
  18. toEthSignedMessageHash(TEST_MESSAGE),
  19. this.signature,
  20. )).to.equal(true);
  21. });
  22. it('with invalid signer', async function () {
  23. expect(await this.signaturechecker.isValidSignatureNow(
  24. other,
  25. toEthSignedMessageHash(TEST_MESSAGE),
  26. this.signature,
  27. )).to.equal(false);
  28. });
  29. it('with invalid signature', async function () {
  30. expect(await this.signaturechecker.isValidSignatureNow(
  31. signer,
  32. toEthSignedMessageHash(WRONG_MESSAGE),
  33. this.signature,
  34. )).to.equal(false);
  35. });
  36. });
  37. context('ERC1271 wallet', function () {
  38. it('with matching signer and signature', async function () {
  39. expect(await this.signaturechecker.isValidSignatureNow(
  40. this.wallet.address,
  41. toEthSignedMessageHash(TEST_MESSAGE),
  42. this.signature,
  43. )).to.equal(true);
  44. });
  45. it('with invalid signer', async function () {
  46. expect(await this.signaturechecker.isValidSignatureNow(
  47. this.signaturechecker.address,
  48. toEthSignedMessageHash(TEST_MESSAGE),
  49. this.signature,
  50. )).to.equal(false);
  51. });
  52. it('with invalid signature', async function () {
  53. expect(await this.signaturechecker.isValidSignatureNow(
  54. this.wallet.address,
  55. toEthSignedMessageHash(WRONG_MESSAGE),
  56. this.signature,
  57. )).to.equal(false);
  58. });
  59. });
  60. });