ECDSA.test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const TEST_MESSAGE = ethers.id('OpenZeppelin');
  5. const WRONG_MESSAGE = ethers.id('Nope');
  6. const NON_HASH_MESSAGE = '0xabcd';
  7. function toSignature(signature) {
  8. return ethers.Signature.from(signature);
  9. }
  10. async function fixture() {
  11. const [signer] = await ethers.getSigners();
  12. const mock = await ethers.deployContract('$ECDSA');
  13. return { signer, mock };
  14. }
  15. describe('ECDSA', function () {
  16. beforeEach(async function () {
  17. Object.assign(this, await loadFixture(fixture));
  18. });
  19. describe('recover with invalid signature', function () {
  20. it('with short signature', async function () {
  21. await expect(this.mock.$recover(TEST_MESSAGE, '0x1234'))
  22. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  23. .withArgs(2);
  24. });
  25. it('with long signature', async function () {
  26. await expect(
  27. // eslint-disable-next-line max-len
  28. this.mock.$recover(
  29. TEST_MESSAGE,
  30. '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
  31. ),
  32. )
  33. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  34. .withArgs(85);
  35. });
  36. });
  37. describe('recover with valid signature', function () {
  38. describe('using <signer>.sign', function () {
  39. it('returns signer address with correct signature', async function () {
  40. // Create the signature
  41. const signature = await this.signer.signMessage(TEST_MESSAGE);
  42. // Recover the signer address from the generated message and signature.
  43. expect(await this.mock.$recover(ethers.hashMessage(TEST_MESSAGE), signature)).to.equal(this.signer.address);
  44. });
  45. it('returns signer address with correct signature for arbitrary length message', async function () {
  46. // Create the signature
  47. const signature = await this.signer.signMessage(NON_HASH_MESSAGE);
  48. // Recover the signer address from the generated message and signature.
  49. expect(await this.mock.$recover(ethers.hashMessage(NON_HASH_MESSAGE), signature)).to.equal(this.signer.address);
  50. });
  51. it('returns a different address', async function () {
  52. const signature = await this.signer.signMessage(TEST_MESSAGE);
  53. expect(await this.mock.$recover(WRONG_MESSAGE, signature)).to.not.be.equal(this.signer.address);
  54. });
  55. it('reverts with invalid signature', async function () {
  56. // eslint-disable-next-line max-len
  57. const signature =
  58. '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  59. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  60. this.mock,
  61. 'ECDSAInvalidSignature',
  62. );
  63. });
  64. });
  65. describe('with v=27 signature', function () {
  66. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  67. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  68. // eslint-disable-next-line max-len
  69. const signatureWithoutV =
  70. '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  71. it('works with correct v value', async function () {
  72. const v = '0x1b'; // 27 = 1b.
  73. const signature = ethers.concat([signatureWithoutV, v]);
  74. expect(await this.mock.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  75. const { r, s, yParityAndS: vs } = toSignature(signature);
  76. expect(await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s)).to.equal(
  77. signer,
  78. );
  79. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.equal(signer);
  80. });
  81. it('rejects incorrect v value', async function () {
  82. const v = '0x1c'; // 28 = 1c.
  83. const signature = ethers.concat([signatureWithoutV, v]);
  84. expect(await this.mock.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  85. const { r, s, yParityAndS: vs } = toSignature(signature);
  86. expect(
  87. await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  88. ).to.not.equal(signer);
  89. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.not.equal(
  90. signer,
  91. );
  92. });
  93. it('reverts wrong v values', async function () {
  94. for (const v of ['0x00', '0x01']) {
  95. const signature = ethers.concat([signatureWithoutV, v]);
  96. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  97. this.mock,
  98. 'ECDSAInvalidSignature',
  99. );
  100. const { r, s } = toSignature(signature);
  101. await expect(
  102. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  103. ).to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignature');
  104. }
  105. });
  106. it('rejects short EIP2098 format', async function () {
  107. const v = '0x1b'; // 27 = 1b.
  108. const signature = ethers.concat([signatureWithoutV, v]);
  109. await expect(this.mock.$recover(TEST_MESSAGE, toSignature(signature).compactSerialized))
  110. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  111. .withArgs(64);
  112. });
  113. });
  114. describe('with v=28 signature', function () {
  115. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  116. // eslint-disable-next-line max-len
  117. const signatureWithoutV =
  118. '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  119. it('works with correct v value', async function () {
  120. const v = '0x1c'; // 28 = 1c.
  121. const signature = ethers.concat([signatureWithoutV, v]);
  122. expect(await this.mock.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  123. const { r, s, yParityAndS: vs } = toSignature(signature);
  124. expect(await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s)).to.equal(
  125. signer,
  126. );
  127. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.equal(signer);
  128. });
  129. it('rejects incorrect v value', async function () {
  130. const v = '0x1b'; // 27 = 1b.
  131. const signature = ethers.concat([signatureWithoutV, v]);
  132. expect(await this.mock.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  133. const { r, s, yParityAndS: vs } = toSignature(signature);
  134. expect(
  135. await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  136. ).to.not.equal(signer);
  137. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.not.equal(
  138. signer,
  139. );
  140. });
  141. it('reverts invalid v values', async function () {
  142. for (const v of ['0x00', '0x01']) {
  143. const signature = ethers.concat([signatureWithoutV, v]);
  144. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  145. this.mock,
  146. 'ECDSAInvalidSignature',
  147. );
  148. const { r, s } = toSignature(signature);
  149. await expect(
  150. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  151. ).to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignature');
  152. }
  153. });
  154. it('rejects short EIP2098 format', async function () {
  155. const v = '0x1c'; // 27 = 1b.
  156. const signature = ethers.concat([signatureWithoutV, v]);
  157. await expect(this.mock.$recover(TEST_MESSAGE, toSignature(signature).compactSerialized))
  158. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  159. .withArgs(64);
  160. });
  161. });
  162. it('reverts with high-s value signature', async function () {
  163. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  164. // eslint-disable-next-line max-len
  165. const highSSignature =
  166. '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  167. const r = ethers.dataSlice(highSSignature, 0, 32);
  168. const s = ethers.dataSlice(highSSignature, 32, 64);
  169. const v = ethers.dataSlice(highSSignature, 64, 65);
  170. await expect(this.mock.$recover(message, highSSignature))
  171. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  172. .withArgs(s);
  173. await expect(this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s))
  174. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  175. .withArgs(s);
  176. expect(() => toSignature(highSSignature)).to.throw('non-canonical s');
  177. });
  178. });
  179. });