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