ECDSA.test.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { toEthSignedMessageHash } = require('../../helpers/sign');
  3. const { expect } = require('chai');
  4. const ECDSA = artifacts.require('$ECDSA');
  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 ECDSA.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(
  94. TEST_MESSAGE,
  95. signature,
  96. )).to.equal(signer);
  97. expect(await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](
  98. TEST_MESSAGE,
  99. ...split(signature),
  100. )).to.equal(signer);
  101. expect(await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  102. TEST_MESSAGE,
  103. ...split(to2098Format(signature)),
  104. )).to.equal(signer);
  105. });
  106. it('rejects incorrect v value', async function () {
  107. const v = '1c'; // 28 = 1c.
  108. const signature = signatureWithoutV + v;
  109. expect(await this.ecdsa.$recover(
  110. TEST_MESSAGE,
  111. signature,
  112. )).to.not.equal(signer);
  113. expect(await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](
  114. TEST_MESSAGE,
  115. ...split(signature),
  116. )).to.not.equal(signer);
  117. expect(await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  118. TEST_MESSAGE,
  119. ...split(to2098Format(signature)),
  120. )).to.not.equal(signer);
  121. });
  122. it('reverts wrong v values', async function () {
  123. for (const v of ['00', '01']) {
  124. const signature = signatureWithoutV + v;
  125. await expectRevert(
  126. this.ecdsa.$recover(TEST_MESSAGE, signature),
  127. 'ECDSA: invalid signature',
  128. );
  129. await expectRevert(
  130. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  131. 'ECDSA: invalid signature',
  132. );
  133. }
  134. });
  135. it('rejects short EIP2098 format', async function () {
  136. const v = '1b'; // 27 = 1b.
  137. const signature = signatureWithoutV + v;
  138. await expectRevert(
  139. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  140. 'ECDSA: invalid signature length',
  141. );
  142. });
  143. });
  144. context('with v=28 signature', function () {
  145. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  146. // eslint-disable-next-line max-len
  147. const signatureWithoutV = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  148. it('works with correct v value', async function () {
  149. const v = '1c'; // 28 = 1c.
  150. const signature = signatureWithoutV + v;
  151. expect(await this.ecdsa.$recover(
  152. TEST_MESSAGE,
  153. signature,
  154. )).to.equal(signer);
  155. expect(await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](
  156. TEST_MESSAGE,
  157. ...split(signature),
  158. )).to.equal(signer);
  159. expect(await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  160. TEST_MESSAGE,
  161. ...split(to2098Format(signature)),
  162. )).to.equal(signer);
  163. });
  164. it('rejects incorrect v value', async function () {
  165. const v = '1b'; // 27 = 1b.
  166. const signature = signatureWithoutV + v;
  167. expect(await this.ecdsa.$recover(
  168. TEST_MESSAGE,
  169. signature,
  170. )).to.not.equal(signer);
  171. expect(await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](
  172. TEST_MESSAGE,
  173. ...split(signature),
  174. )).to.not.equal(signer);
  175. expect(await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  176. TEST_MESSAGE,
  177. ...split(to2098Format(signature)),
  178. )).to.not.equal(signer);
  179. });
  180. it('reverts invalid v values', async function () {
  181. for (const v of ['00', '01']) {
  182. const signature = signatureWithoutV + v;
  183. await expectRevert(
  184. this.ecdsa.$recover(TEST_MESSAGE, signature),
  185. 'ECDSA: invalid signature',
  186. );
  187. await expectRevert(
  188. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  189. 'ECDSA: invalid signature',
  190. );
  191. }
  192. });
  193. it('rejects short EIP2098 format', async function () {
  194. const v = '1c'; // 27 = 1b.
  195. const signature = signatureWithoutV + v;
  196. await expectRevert(
  197. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  198. 'ECDSA: invalid signature length',
  199. );
  200. });
  201. });
  202. it('reverts with high-s value signature', async function () {
  203. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  204. // eslint-disable-next-line max-len
  205. const highSSignature = '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  206. await expectRevert(this.ecdsa.$recover(message, highSSignature), 'ECDSA: invalid signature \'s\' value');
  207. await expectRevert(
  208. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(highSSignature)),
  209. 'ECDSA: invalid signature \'s\' value',
  210. );
  211. expect(() => to2098Format(highSSignature)).to.throw('invalid signature \'s\' value');
  212. });
  213. });
  214. context('toEthSignedMessageHash', function () {
  215. it('prefixes bytes32 data correctly', async function () {
  216. expect(await this.ecdsa.methods['$toEthSignedMessageHash(bytes32)'](TEST_MESSAGE))
  217. .to.equal(toEthSignedMessageHash(TEST_MESSAGE));
  218. });
  219. it('prefixes dynamic length data correctly', async function () {
  220. expect(await this.ecdsa.methods['$toEthSignedMessageHash(bytes)'](NON_HASH_MESSAGE))
  221. .to.equal(toEthSignedMessageHash(NON_HASH_MESSAGE));
  222. });
  223. });
  224. });