ECDSA.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { toEthSignedMessageHash, fixSignature } = require('../helpers/sign');
  3. const { expect } = require('chai');
  4. const ECDSAMock = artifacts.require('ECDSAMock');
  5. const TEST_MESSAGE = web3.utils.sha3('OpenZeppelin');
  6. const WRONG_MESSAGE = web3.utils.sha3('Nope');
  7. contract('ECDSA', function (accounts) {
  8. const [ other ] = accounts;
  9. beforeEach(async function () {
  10. this.ecdsa = await ECDSAMock.new();
  11. });
  12. context('recover with invalid signature', function () {
  13. it('with short signature', async function () {
  14. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, '0x1234'), 'ECDSA: invalid signature length');
  15. });
  16. it('with long signature', async function () {
  17. await expectRevert(
  18. // eslint-disable-next-line max-len
  19. this.ecdsa.recover(TEST_MESSAGE, '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'),
  20. 'ECDSA: invalid signature length',
  21. );
  22. });
  23. });
  24. context('recover with valid signature', function () {
  25. context('with v0 signature', function () {
  26. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  27. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  28. // eslint-disable-next-line max-len
  29. const signatureWithoutVersion = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  30. context('with 00 as version value', function () {
  31. it('reverts', async function () {
  32. const version = '00';
  33. const signature = signatureWithoutVersion + version;
  34. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  35. });
  36. });
  37. context('with 27 as version value', function () {
  38. it('works', async function () {
  39. const version = '1b'; // 27 = 1b.
  40. const signature = signatureWithoutVersion + version;
  41. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  42. });
  43. });
  44. context('with wrong version', function () {
  45. it('reverts', async function () {
  46. // The last two hex digits are the signature version.
  47. // The only valid values are 0, 1, 27 and 28.
  48. const version = '02';
  49. const signature = signatureWithoutVersion + version;
  50. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  51. });
  52. });
  53. });
  54. context('with v1 signature', function () {
  55. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  56. // eslint-disable-next-line max-len
  57. const signatureWithoutVersion = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  58. context('with 01 as version value', function () {
  59. it('reverts', async function () {
  60. const version = '01';
  61. const signature = signatureWithoutVersion + version;
  62. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  63. });
  64. });
  65. context('with 28 as version value', function () {
  66. it('works', async function () {
  67. const version = '1c'; // 28 = 1c.
  68. const signature = signatureWithoutVersion + version;
  69. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  70. });
  71. });
  72. context('with wrong version', function () {
  73. it('reverts', async function () {
  74. // The last two hex digits are the signature version.
  75. // The only valid values are 0, 1, 27 and 28.
  76. const version = '02';
  77. const signature = signatureWithoutVersion + version;
  78. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  79. });
  80. });
  81. });
  82. context('with high-s value signature', function () {
  83. it('reverts', async function () {
  84. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  85. // eslint-disable-next-line max-len
  86. const highSSignature = '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  87. await expectRevert(this.ecdsa.recover(message, highSSignature), 'ECDSA: invalid signature \'s\' value');
  88. });
  89. });
  90. context('using web3.eth.sign', function () {
  91. context('with correct signature', function () {
  92. it('returns signer address', async function () {
  93. // Create the signature
  94. const signature = fixSignature(await web3.eth.sign(TEST_MESSAGE, other));
  95. // Recover the signer address from the generated message and signature.
  96. expect(await this.ecdsa.recover(
  97. toEthSignedMessageHash(TEST_MESSAGE),
  98. signature,
  99. )).to.equal(other);
  100. });
  101. });
  102. context('with wrong message', function () {
  103. it('returns a different address', async function () {
  104. const signature = fixSignature(await web3.eth.sign(TEST_MESSAGE, other));
  105. expect(await this.ecdsa.recover(WRONG_MESSAGE, signature)).to.not.equal(other);
  106. });
  107. });
  108. context('with invalid signature', function () {
  109. it('reverts', async function () {
  110. // eslint-disable-next-line max-len
  111. const signature = '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  112. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  113. });
  114. });
  115. });
  116. });
  117. context('toEthSignedMessage', function () {
  118. it('should prefix hashes correctly', async function () {
  119. expect(await this.ecdsa.toEthSignedMessageHash(TEST_MESSAGE)).to.equal(toEthSignedMessageHash(TEST_MESSAGE));
  120. });
  121. });
  122. });