SignatureChecker.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. for (const signature of ['isValidERC1271SignatureNow', 'isValidSignatureNow']) {
  35. context(signature, function () {
  36. it('with matching signer and signature', async function () {
  37. expect(
  38. await this.signaturechecker[`$${signature}`](
  39. this.wallet.address,
  40. toEthSignedMessageHash(TEST_MESSAGE),
  41. this.signature,
  42. ),
  43. ).to.equal(true);
  44. });
  45. it('with invalid signer', async function () {
  46. expect(
  47. await this.signaturechecker[`$${signature}`](
  48. this.signaturechecker.address,
  49. toEthSignedMessageHash(TEST_MESSAGE),
  50. this.signature,
  51. ),
  52. ).to.equal(false);
  53. });
  54. it('with invalid signature', async function () {
  55. expect(
  56. await this.signaturechecker[`$${signature}`](
  57. this.wallet.address,
  58. toEthSignedMessageHash(WRONG_MESSAGE),
  59. this.signature,
  60. ),
  61. ).to.equal(false);
  62. });
  63. it('with malicious wallet', async function () {
  64. expect(
  65. await this.signaturechecker[`$${signature}`](
  66. this.malicious.address,
  67. toEthSignedMessageHash(TEST_MESSAGE),
  68. this.signature,
  69. ),
  70. ).to.equal(false);
  71. });
  72. });
  73. }
  74. });
  75. });