ECDSA.test.js 8.8 KB

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