ECDSA.test.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { toEthSignedMessageHash, toDataWithIntendedValidatorHash } = 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. const RANDOM_ADDRESS = web3.utils.toChecksumAddress(web3.utils.randomHex(20));
  9. function to2098Format(signature) {
  10. const long = web3.utils.hexToBytes(signature);
  11. if (long.length !== 65) {
  12. throw new Error('invalid signature length (expected long format)');
  13. }
  14. if (long[32] >> 7 === 1) {
  15. throw new Error("invalid signature 's' value");
  16. }
  17. const short = long.slice(0, 64);
  18. short[32] |= long[64] % 27 << 7; // set the first bit of the 32nd byte to the v parity bit
  19. return web3.utils.bytesToHex(short);
  20. }
  21. function split(signature) {
  22. const raw = web3.utils.hexToBytes(signature);
  23. switch (raw.length) {
  24. case 64:
  25. return [
  26. web3.utils.bytesToHex(raw.slice(0, 32)), // r
  27. web3.utils.bytesToHex(raw.slice(32, 64)), // vs
  28. ];
  29. case 65:
  30. return [
  31. raw[64], // v
  32. web3.utils.bytesToHex(raw.slice(0, 32)), // r
  33. web3.utils.bytesToHex(raw.slice(32, 64)), // s
  34. ];
  35. default:
  36. expect.fail('Invalid signature length, cannot split');
  37. }
  38. }
  39. contract('ECDSA', function (accounts) {
  40. const [other] = accounts;
  41. beforeEach(async function () {
  42. this.ecdsa = await ECDSA.new();
  43. });
  44. context('recover with invalid signature', function () {
  45. it('with short signature', async function () {
  46. await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, '0x1234'), 'ECDSA: invalid signature length');
  47. });
  48. it('with long signature', async function () {
  49. await expectRevert(
  50. // eslint-disable-next-line max-len
  51. this.ecdsa.$recover(
  52. TEST_MESSAGE,
  53. '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
  54. ),
  55. 'ECDSA: invalid signature length',
  56. );
  57. });
  58. });
  59. context('recover with valid signature', function () {
  60. context('using web3.eth.sign', function () {
  61. it('returns signer address with correct signature', async function () {
  62. // Create the signature
  63. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  64. // Recover the signer address from the generated message and signature.
  65. expect(await this.ecdsa.$recover(toEthSignedMessageHash(TEST_MESSAGE), signature)).to.equal(other);
  66. });
  67. it('returns signer address with correct signature for arbitrary length message', async function () {
  68. // Create the signature
  69. const signature = await web3.eth.sign(NON_HASH_MESSAGE, other);
  70. // Recover the signer address from the generated message and signature.
  71. expect(await this.ecdsa.$recover(toEthSignedMessageHash(NON_HASH_MESSAGE), signature)).to.equal(other);
  72. });
  73. it('returns a different address', async function () {
  74. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  75. expect(await this.ecdsa.$recover(WRONG_MESSAGE, signature)).to.not.equal(other);
  76. });
  77. it('reverts with invalid signature', async function () {
  78. // eslint-disable-next-line max-len
  79. const signature =
  80. '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  81. await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  82. });
  83. });
  84. context('with v=27 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 signatureWithoutV =
  89. '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(
  95. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  96. ).to.equal(signer);
  97. expect(
  98. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  99. TEST_MESSAGE,
  100. ...split(to2098Format(signature)),
  101. ),
  102. ).to.equal(signer);
  103. });
  104. it('rejects incorrect v value', async function () {
  105. const v = '1c'; // 28 = 1c.
  106. const signature = signatureWithoutV + v;
  107. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  108. expect(
  109. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  110. ).to.not.equal(signer);
  111. expect(
  112. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  113. TEST_MESSAGE,
  114. ...split(to2098Format(signature)),
  115. ),
  116. ).to.not.equal(signer);
  117. });
  118. it('reverts wrong v values', async function () {
  119. for (const v of ['00', '01']) {
  120. const signature = signatureWithoutV + v;
  121. await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  122. await expectRevert(
  123. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  124. 'ECDSA: invalid signature',
  125. );
  126. }
  127. });
  128. it('rejects short EIP2098 format', async function () {
  129. const v = '1b'; // 27 = 1b.
  130. const signature = signatureWithoutV + v;
  131. await expectRevert(
  132. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  133. 'ECDSA: invalid signature length',
  134. );
  135. });
  136. });
  137. context('with v=28 signature', function () {
  138. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  139. // eslint-disable-next-line max-len
  140. const signatureWithoutV =
  141. '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  142. it('works with correct v value', async function () {
  143. const v = '1c'; // 28 = 1c.
  144. const signature = signatureWithoutV + v;
  145. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  146. expect(
  147. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  148. ).to.equal(signer);
  149. expect(
  150. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  151. TEST_MESSAGE,
  152. ...split(to2098Format(signature)),
  153. ),
  154. ).to.equal(signer);
  155. });
  156. it('rejects incorrect v value', async function () {
  157. const v = '1b'; // 27 = 1b.
  158. const signature = signatureWithoutV + v;
  159. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  160. expect(
  161. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  162. ).to.not.equal(signer);
  163. expect(
  164. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  165. TEST_MESSAGE,
  166. ...split(to2098Format(signature)),
  167. ),
  168. ).to.not.equal(signer);
  169. });
  170. it('reverts invalid v values', async function () {
  171. for (const v of ['00', '01']) {
  172. const signature = signatureWithoutV + v;
  173. await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  174. await expectRevert(
  175. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  176. 'ECDSA: invalid signature',
  177. );
  178. }
  179. });
  180. it('rejects short EIP2098 format', async function () {
  181. const v = '1c'; // 27 = 1b.
  182. const signature = signatureWithoutV + v;
  183. await expectRevert(
  184. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  185. 'ECDSA: invalid signature length',
  186. );
  187. });
  188. });
  189. it('reverts with high-s value signature', async function () {
  190. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  191. // eslint-disable-next-line max-len
  192. const highSSignature =
  193. '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  194. await expectRevert(this.ecdsa.$recover(message, highSSignature), "ECDSA: invalid signature 's' value");
  195. await expectRevert(
  196. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(highSSignature)),
  197. "ECDSA: invalid signature 's' value",
  198. );
  199. expect(() => to2098Format(highSSignature)).to.throw("invalid signature 's' value");
  200. });
  201. });
  202. context('toEthSignedMessageHash', function () {
  203. it('prefixes bytes32 data correctly', async function () {
  204. expect(await this.ecdsa.methods['$toEthSignedMessageHash(bytes32)'](TEST_MESSAGE)).to.equal(
  205. toEthSignedMessageHash(TEST_MESSAGE),
  206. );
  207. });
  208. it('prefixes dynamic length data correctly', async function () {
  209. expect(await this.ecdsa.methods['$toEthSignedMessageHash(bytes)'](NON_HASH_MESSAGE)).to.equal(
  210. toEthSignedMessageHash(NON_HASH_MESSAGE),
  211. );
  212. });
  213. });
  214. context('toDataWithIntendedValidatorHash', function () {
  215. it('returns the hash correctly', async function () {
  216. expect(
  217. await this.ecdsa.methods['$toDataWithIntendedValidatorHash(address,bytes)'](RANDOM_ADDRESS, NON_HASH_MESSAGE),
  218. ).to.equal(toDataWithIntendedValidatorHash(RANDOM_ADDRESS, NON_HASH_MESSAGE));
  219. });
  220. });
  221. });