ECDSA.test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 v0 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 signatureWithoutVersion = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  90. it('reverts with 00 as version value', async function () {
  91. const version = '00';
  92. const signature = signatureWithoutVersion + version;
  93. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  94. await expectRevert(
  95. this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature)),
  96. 'ECDSA: invalid signature \'v\' value',
  97. );
  98. });
  99. it('works with 27 as version value', async function () {
  100. const version = '1b'; // 27 = 1b.
  101. const signature = signatureWithoutVersion + version;
  102. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  103. expect(await this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature))).to.equal(signer);
  104. expect(await this.ecdsa.recover_r_vs(TEST_MESSAGE, ...split(to2098Format(signature)))).to.equal(signer);
  105. });
  106. it('reverts with wrong version', async function () {
  107. // The last two hex digits are the signature version.
  108. // The only valid values are 0, 1, 27 and 28.
  109. const version = '02';
  110. const signature = signatureWithoutVersion + version;
  111. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  112. await expectRevert(
  113. this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature)),
  114. 'ECDSA: invalid signature \'v\' value',
  115. );
  116. });
  117. it('rejects short EIP2098 format', async function () {
  118. const version = '1b'; // 27 = 1b.
  119. const signature = signatureWithoutVersion + version;
  120. await expectRevert(
  121. this.ecdsa.recover(TEST_MESSAGE, to2098Format(signature)),
  122. 'ECDSA: invalid signature length',
  123. );
  124. });
  125. });
  126. context('with v1 signature', function () {
  127. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  128. // eslint-disable-next-line max-len
  129. const signatureWithoutVersion = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  130. it('reverts with 01 as version value', async function () {
  131. const version = '01';
  132. const signature = signatureWithoutVersion + version;
  133. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  134. await expectRevert(
  135. this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature)),
  136. 'ECDSA: invalid signature \'v\' value',
  137. );
  138. });
  139. it('works with 28 as version value', async function () {
  140. const version = '1c'; // 28 = 1c.
  141. const signature = signatureWithoutVersion + version;
  142. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  143. expect(await this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature))).to.equal(signer);
  144. expect(await this.ecdsa.recover_r_vs(TEST_MESSAGE, ...split(to2098Format(signature)))).to.equal(signer);
  145. });
  146. it('reverts with wrong version', async function () {
  147. // The last two hex digits are the signature version.
  148. // The only valid values are 0, 1, 27 and 28.
  149. const version = '02';
  150. const signature = signatureWithoutVersion + version;
  151. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  152. await expectRevert(
  153. this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(signature)),
  154. 'ECDSA: invalid signature \'v\' value',
  155. );
  156. });
  157. it('rejects short EIP2098 format', async function () {
  158. const version = '1c'; // 27 = 1b.
  159. const signature = signatureWithoutVersion + version;
  160. await expectRevert(
  161. this.ecdsa.recover(TEST_MESSAGE, to2098Format(signature)),
  162. 'ECDSA: invalid signature length',
  163. );
  164. });
  165. });
  166. it('reverts with high-s value signature', async function () {
  167. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  168. // eslint-disable-next-line max-len
  169. const highSSignature = '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  170. await expectRevert(this.ecdsa.recover(message, highSSignature), 'ECDSA: invalid signature \'s\' value');
  171. await expectRevert(
  172. this.ecdsa.recover_v_r_s(TEST_MESSAGE, ...split(highSSignature)),
  173. 'ECDSA: invalid signature \'s\' value',
  174. );
  175. expect(() => to2098Format(highSSignature)).to.throw('invalid signature \'s\' value');
  176. });
  177. });
  178. context('toEthSignedMessageHash', function () {
  179. it('prefixes bytes32 data correctly', async function () {
  180. expect(await this.ecdsa.methods['toEthSignedMessageHash(bytes32)'](TEST_MESSAGE))
  181. .to.equal(toEthSignedMessageHash(TEST_MESSAGE));
  182. });
  183. it('prefixes dynamic length data correctly', async function () {
  184. expect(await this.ecdsa.methods['toEthSignedMessageHash(bytes)'](NON_HASH_MESSAGE))
  185. .to.equal(toEthSignedMessageHash(NON_HASH_MESSAGE));
  186. });
  187. });
  188. });