ECDSA.test.js 9.5 KB

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