ECDSA.test.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { toEthSignedMessageHash } = 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. const NON_HASH_MESSAGE = '0x' + Buffer.from('abcd').toString('hex');
  8. function to2098Format (signature) {
  9. const long = web3.utils.hexToBytes(signature);
  10. if (long.length !== 65) {
  11. throw new Error('invalid signature length (expected long format)');
  12. }
  13. if (long[32] >> 7 === 1) {
  14. throw new Error('invalid signature \'s\' value');
  15. }
  16. const short = long.slice(0, 64);
  17. short[32] |= (long[64] % 27) << 7; // set the first bit of the 32nd byte to the v parity bit
  18. return web3.utils.bytesToHex(short);
  19. }
  20. function split (signature) {
  21. const raw = web3.utils.hexToBytes(signature);
  22. switch (raw.length) {
  23. case 64:
  24. return [
  25. web3.utils.bytesToHex(raw.slice(0, 32)), // r
  26. web3.utils.bytesToHex(raw.slice(32, 64)), // vs
  27. ];
  28. case 65:
  29. return [
  30. raw[64], // v
  31. web3.utils.bytesToHex(raw.slice(0, 32)), // r
  32. web3.utils.bytesToHex(raw.slice(32, 64)), // s
  33. ];
  34. default:
  35. expect.fail('Invalid signature length, cannot split');
  36. }
  37. }
  38. contract('ECDSA', function (accounts) {
  39. const [ other ] = accounts;
  40. beforeEach(async function () {
  41. this.ecdsa = await ECDSAMock.new();
  42. });
  43. context('recover with invalid signature', function () {
  44. it('with short signature', async function () {
  45. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, '0x1234'), 'ECDSA: invalid signature length');
  46. });
  47. it('with long signature', async function () {
  48. await expectRevert(
  49. // eslint-disable-next-line max-len
  50. this.ecdsa.recover(TEST_MESSAGE, '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'),
  51. 'ECDSA: invalid signature length',
  52. );
  53. });
  54. });
  55. context('recover with valid signature', function () {
  56. context('using web3.eth.sign', function () {
  57. it('returns signer address with correct signature', async function () {
  58. // Create the signature
  59. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  60. // Recover the signer address from the generated message and signature.
  61. expect(await this.ecdsa.recover(
  62. toEthSignedMessageHash(TEST_MESSAGE),
  63. signature,
  64. )).to.equal(other);
  65. });
  66. it('returns signer address with correct signature for arbitrary length message', async function () {
  67. // Create the signature
  68. const signature = await web3.eth.sign(NON_HASH_MESSAGE, other);
  69. // Recover the signer address from the generated message and signature.
  70. expect(await this.ecdsa.recover(
  71. toEthSignedMessageHash(NON_HASH_MESSAGE),
  72. signature,
  73. )).to.equal(other);
  74. });
  75. it('returns a different address', async function () {
  76. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  77. expect(await this.ecdsa.recover(WRONG_MESSAGE, signature)).to.not.equal(other);
  78. });
  79. it('reverts with invalid signature', async function () {
  80. // eslint-disable-next-line max-len
  81. const signature = '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  82. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  83. });
  84. });
  85. context('with v=27 signature', function () {
  86. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  87. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  88. // eslint-disable-next-line max-len
  89. const signatureWithoutV = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  90. it('works with correct v value', async function () {
  91. const v = '1b'; // 27 = 1b.
  92. const signature = signatureWithoutV + v;
  93. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  94. expect(await this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature))).to.equal(signer);
  95. expect(await this.ecdsa.recover_r_vs(TEST_MESSAGE, ...split(to2098Format(signature)))).to.equal(signer);
  96. });
  97. it('rejects incorrect v value', async function () {
  98. const v = '1c'; // 28 = 1c.
  99. const signature = signatureWithoutV + v;
  100. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  101. expect(await this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature))).to.not.equal(signer);
  102. expect(await this.ecdsa.recover_r_vs(TEST_MESSAGE, ...split(to2098Format(signature)))).to.not.equal(signer);
  103. });
  104. it('reverts wrong v values', async function () {
  105. for (const v of ['00', '01']) {
  106. const signature = signatureWithoutV + v;
  107. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  108. await expectRevert(this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature)), 'ECDSA: invalid signature');
  109. }
  110. });
  111. it('rejects short EIP2098 format', async function () {
  112. const v = '1b'; // 27 = 1b.
  113. const signature = signatureWithoutV + v;
  114. await expectRevert(
  115. this.ecdsa.recover(TEST_MESSAGE, to2098Format(signature)),
  116. 'ECDSA: invalid signature length',
  117. );
  118. });
  119. });
  120. context('with v=28 signature', function () {
  121. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  122. // eslint-disable-next-line max-len
  123. const signatureWithoutV = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  124. it('works with correct v value', async function () {
  125. const v = '1c'; // 28 = 1c.
  126. const signature = signatureWithoutV + v;
  127. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  128. expect(await this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature))).to.equal(signer);
  129. expect(await this.ecdsa.recover_r_vs(TEST_MESSAGE, ...split(to2098Format(signature)))).to.equal(signer);
  130. });
  131. it('rejects incorrect v value', async function () {
  132. const v = '1b'; // 27 = 1b.
  133. const signature = signatureWithoutV + v;
  134. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  135. expect(await this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature))).to.not.equal(signer);
  136. expect(await this.ecdsa.recover_r_vs(TEST_MESSAGE, ...split(to2098Format(signature)))).to.not.equal(signer);
  137. });
  138. it('reverts invalid v values', async function () {
  139. for (const v of ['00', '01']) {
  140. const signature = signatureWithoutV + v;
  141. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  142. await expectRevert(this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature)), 'ECDSA: invalid signature');
  143. }
  144. });
  145. it('rejects short EIP2098 format', async function () {
  146. const v = '1c'; // 27 = 1b.
  147. const signature = signatureWithoutV + v;
  148. await expectRevert(
  149. this.ecdsa.recover(TEST_MESSAGE, to2098Format(signature)),
  150. 'ECDSA: invalid signature length',
  151. );
  152. });
  153. });
  154. it('reverts with high-s value signature', async function () {
  155. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  156. // eslint-disable-next-line max-len
  157. const highSSignature = '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  158. await expectRevert(this.ecdsa.recover(message, highSSignature), 'ECDSA: invalid signature \'s\' value');
  159. await expectRevert(
  160. this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(highSSignature)),
  161. 'ECDSA: invalid signature \'s\' value',
  162. );
  163. expect(() => to2098Format(highSSignature)).to.throw('invalid signature \'s\' value');
  164. });
  165. });
  166. context('toEthSignedMessageHash', function () {
  167. it('prefixes bytes32 data correctly', async function () {
  168. expect(await this.ecdsa.methods['toEthSignedMessageHash(bytes32)'](TEST_MESSAGE))
  169. .to.equal(toEthSignedMessageHash(TEST_MESSAGE));
  170. });
  171. it('prefixes dynamic length data correctly', async function () {
  172. expect(await this.ecdsa.methods['toEthSignedMessageHash(bytes)'](NON_HASH_MESSAGE))
  173. .to.equal(toEthSignedMessageHash(NON_HASH_MESSAGE));
  174. });
  175. });
  176. });