ECDSA.test.js 9.0 KB

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