ECDSA.test.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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(
  51. TEST_MESSAGE,
  52. '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
  53. ),
  54. 'ECDSA: invalid signature length',
  55. );
  56. });
  57. });
  58. context('recover with valid signature', function () {
  59. context('using web3.eth.sign', function () {
  60. it('returns signer address with correct signature', async function () {
  61. // Create the signature
  62. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  63. // Recover the signer address from the generated message and signature.
  64. expect(await this.ecdsa.$recover(toEthSignedMessageHash(TEST_MESSAGE), signature)).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(toEthSignedMessageHash(NON_HASH_MESSAGE), signature)).to.equal(other);
  71. });
  72. it('returns a different address', async function () {
  73. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  74. expect(await this.ecdsa.$recover(WRONG_MESSAGE, signature)).to.not.equal(other);
  75. });
  76. it('reverts with invalid signature', async function () {
  77. // eslint-disable-next-line max-len
  78. const signature =
  79. '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  80. await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  81. });
  82. });
  83. context('with v=27 signature', function () {
  84. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  85. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  86. // eslint-disable-next-line max-len
  87. const signatureWithoutV =
  88. '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  89. it('works with correct v value', async function () {
  90. const v = '1b'; // 27 = 1b.
  91. const signature = signatureWithoutV + v;
  92. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  93. expect(
  94. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  95. ).to.equal(signer);
  96. expect(
  97. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  98. TEST_MESSAGE,
  99. ...split(to2098Format(signature)),
  100. ),
  101. ).to.equal(signer);
  102. });
  103. it('rejects incorrect v value', async function () {
  104. const v = '1c'; // 28 = 1c.
  105. const signature = signatureWithoutV + v;
  106. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  107. expect(
  108. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  109. ).to.not.equal(signer);
  110. expect(
  111. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  112. TEST_MESSAGE,
  113. ...split(to2098Format(signature)),
  114. ),
  115. ).to.not.equal(signer);
  116. });
  117. it('reverts wrong v values', async function () {
  118. for (const v of ['00', '01']) {
  119. const signature = signatureWithoutV + v;
  120. await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  121. await expectRevert(
  122. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  123. 'ECDSA: invalid signature',
  124. );
  125. }
  126. });
  127. it('rejects short EIP2098 format', async function () {
  128. const v = '1b'; // 27 = 1b.
  129. const signature = signatureWithoutV + v;
  130. await expectRevert(
  131. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  132. 'ECDSA: invalid signature length',
  133. );
  134. });
  135. });
  136. context('with v=28 signature', function () {
  137. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  138. // eslint-disable-next-line max-len
  139. const signatureWithoutV =
  140. '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  141. it('works with correct v value', async function () {
  142. const v = '1c'; // 28 = 1c.
  143. const signature = signatureWithoutV + v;
  144. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  145. expect(
  146. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  147. ).to.equal(signer);
  148. expect(
  149. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  150. TEST_MESSAGE,
  151. ...split(to2098Format(signature)),
  152. ),
  153. ).to.equal(signer);
  154. });
  155. it('rejects incorrect v value', async function () {
  156. const v = '1b'; // 27 = 1b.
  157. const signature = signatureWithoutV + v;
  158. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  159. expect(
  160. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  161. ).to.not.equal(signer);
  162. expect(
  163. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  164. TEST_MESSAGE,
  165. ...split(to2098Format(signature)),
  166. ),
  167. ).to.not.equal(signer);
  168. });
  169. it('reverts invalid v values', async function () {
  170. for (const v of ['00', '01']) {
  171. const signature = signatureWithoutV + v;
  172. await expectRevert(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  173. await expectRevert(
  174. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  175. 'ECDSA: invalid signature',
  176. );
  177. }
  178. });
  179. it('rejects short EIP2098 format', async function () {
  180. const v = '1c'; // 27 = 1b.
  181. const signature = signatureWithoutV + v;
  182. await expectRevert(
  183. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  184. 'ECDSA: invalid signature length',
  185. );
  186. });
  187. });
  188. it('reverts with high-s value signature', async function () {
  189. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  190. // eslint-disable-next-line max-len
  191. const highSSignature =
  192. '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  193. await expectRevert(this.ecdsa.$recover(message, highSSignature), "ECDSA: invalid signature 's' value");
  194. await expectRevert(
  195. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(highSSignature)),
  196. "ECDSA: invalid signature 's' value",
  197. );
  198. expect(() => to2098Format(highSSignature)).to.throw("invalid signature 's' value");
  199. });
  200. });
  201. context('toEthSignedMessageHash', function () {
  202. it('prefixes bytes32 data correctly', async function () {
  203. expect(await this.ecdsa.methods['$toEthSignedMessageHash(bytes32)'](TEST_MESSAGE)).to.equal(
  204. toEthSignedMessageHash(TEST_MESSAGE),
  205. );
  206. });
  207. it('prefixes dynamic length data correctly', async function () {
  208. expect(await this.ecdsa.methods['$toEthSignedMessageHash(bytes)'](NON_HASH_MESSAGE)).to.equal(
  209. toEthSignedMessageHash(NON_HASH_MESSAGE),
  210. );
  211. });
  212. });
  213. });