ECDSA.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. expect(await this.mock.$recoverCalldata(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. expect(await this.mock.$recoverCalldata(ethers.hashMessage(NON_HASH_MESSAGE), signature)).to.equal(this.signer);
  48. });
  49. it('returns a different address', async function () {
  50. const signature = await this.signer.signMessage(TEST_MESSAGE);
  51. expect(await this.mock.$recover(WRONG_MESSAGE, signature)).to.not.be.equal(this.signer);
  52. expect(await this.mock.$recoverCalldata(WRONG_MESSAGE, signature)).to.not.be.equal(this.signer);
  53. });
  54. it('reverts with invalid signature', async function () {
  55. const signature =
  56. '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  57. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  58. this.mock,
  59. 'ECDSAInvalidSignature',
  60. );
  61. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  62. this.mock,
  63. 'ECDSAInvalidSignature',
  64. );
  65. });
  66. });
  67. describe('with v=27 signature', function () {
  68. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  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. expect(await this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.equal(signer);
  76. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  77. expect(await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s)).to.equal(
  78. signer,
  79. );
  80. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.equal(signer);
  81. });
  82. it('rejects incorrect v value', async function () {
  83. const v = '0x1c'; // 28 = 1c.
  84. const signature = ethers.concat([signatureWithoutV, v]);
  85. expect(await this.mock.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  86. expect(await this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.not.equal(signer);
  87. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  88. expect(
  89. await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  90. ).to.not.equal(signer);
  91. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.not.equal(
  92. signer,
  93. );
  94. });
  95. it('reverts wrong v values', async function () {
  96. for (const v of ['0x00', '0x01']) {
  97. const signature = ethers.concat([signatureWithoutV, v]);
  98. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  99. this.mock,
  100. 'ECDSAInvalidSignature',
  101. );
  102. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  103. this.mock,
  104. 'ECDSAInvalidSignature',
  105. );
  106. const { r, s } = ethers.Signature.from(signature);
  107. await expect(
  108. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  109. ).to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignature');
  110. }
  111. });
  112. it('rejects short EIP2098 format', async function () {
  113. const v = '0x1b'; // 27 = 1b.
  114. const signature = ethers.concat([signatureWithoutV, v]);
  115. const { compactSerialized } = ethers.Signature.from(signature);
  116. await expect(this.mock.$recover(TEST_MESSAGE, compactSerialized))
  117. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  118. .withArgs(64);
  119. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, compactSerialized))
  120. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  121. .withArgs(64);
  122. });
  123. });
  124. describe('with v=28 signature', function () {
  125. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  126. const signatureWithoutV =
  127. '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  128. it('works with correct v value', async function () {
  129. const v = '0x1c'; // 28 = 1c.
  130. const signature = ethers.concat([signatureWithoutV, v]);
  131. expect(await this.mock.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  132. expect(await this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.equal(signer);
  133. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  134. expect(await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s)).to.equal(
  135. signer,
  136. );
  137. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.equal(signer);
  138. });
  139. it('rejects incorrect v value', async function () {
  140. const v = '0x1b'; // 27 = 1b.
  141. const signature = ethers.concat([signatureWithoutV, v]);
  142. expect(await this.mock.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  143. expect(await this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.not.equal(signer);
  144. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  145. expect(
  146. await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  147. ).to.not.equal(signer);
  148. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.not.equal(
  149. signer,
  150. );
  151. });
  152. it('reverts invalid v values', async function () {
  153. for (const v of ['0x00', '0x01']) {
  154. const signature = ethers.concat([signatureWithoutV, v]);
  155. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  156. this.mock,
  157. 'ECDSAInvalidSignature',
  158. );
  159. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  160. this.mock,
  161. 'ECDSAInvalidSignature',
  162. );
  163. const { r, s } = ethers.Signature.from(signature);
  164. await expect(
  165. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  166. ).to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignature');
  167. }
  168. });
  169. it('rejects short EIP2098 format', async function () {
  170. const v = '0x1b'; // 28 = 1b.
  171. const signature = ethers.concat([signatureWithoutV, v]);
  172. const { compactSerialized } = ethers.Signature.from(signature);
  173. await expect(this.mock.$recover(TEST_MESSAGE, compactSerialized))
  174. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  175. .withArgs(64);
  176. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, compactSerialized))
  177. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  178. .withArgs(64);
  179. });
  180. });
  181. it('reverts with high-s value signature', async function () {
  182. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  183. const highSSignature =
  184. '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  185. const r = ethers.dataSlice(highSSignature, 0, 32);
  186. const s = ethers.dataSlice(highSSignature, 32, 64);
  187. const v = ethers.dataSlice(highSSignature, 64, 65);
  188. await expect(this.mock.$recover(message, highSSignature))
  189. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  190. .withArgs(s);
  191. await expect(this.mock.$recoverCalldata(message, highSSignature))
  192. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  193. .withArgs(s);
  194. await expect(this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s))
  195. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  196. .withArgs(s);
  197. expect(() => ethers.Signature.from(highSSignature)).to.throw('non-canonical s');
  198. });
  199. });
  200. });