ECDSA.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. const signature = '0x1234';
  19. await expect(this.mock.$recover(TEST_MESSAGE, signature))
  20. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  21. .withArgs(2);
  22. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature))
  23. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  24. .withArgs(2);
  25. });
  26. it('with long signature', async function () {
  27. const signature =
  28. '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
  29. await expect(this.mock.$recover(TEST_MESSAGE, signature))
  30. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  31. .withArgs(85);
  32. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature))
  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. await expect(this.mock.$recover(ethers.hashMessage(TEST_MESSAGE), signature)).to.eventually.equal(this.signer);
  44. await expect(this.mock.$recoverCalldata(ethers.hashMessage(TEST_MESSAGE), signature)).to.eventually.equal(
  45. this.signer,
  46. );
  47. });
  48. it('returns signer address with correct signature for arbitrary length message', async function () {
  49. // Create the signature
  50. const signature = await this.signer.signMessage(NON_HASH_MESSAGE);
  51. // Recover the signer address from the generated message and signature.
  52. await expect(this.mock.$recover(ethers.hashMessage(NON_HASH_MESSAGE), signature)).to.eventually.equal(
  53. this.signer,
  54. );
  55. await expect(this.mock.$recoverCalldata(ethers.hashMessage(NON_HASH_MESSAGE), signature)).to.eventually.equal(
  56. this.signer,
  57. );
  58. });
  59. it('returns a different address', async function () {
  60. const signature = await this.signer.signMessage(TEST_MESSAGE);
  61. await expect(this.mock.$recover(WRONG_MESSAGE, signature)).to.eventually.not.equal(this.signer);
  62. await expect(this.mock.$recoverCalldata(WRONG_MESSAGE, signature)).to.eventually.not.equal(this.signer);
  63. });
  64. it('reverts with invalid signature', async function () {
  65. const signature =
  66. '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  67. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  68. this.mock,
  69. 'ECDSAInvalidSignature',
  70. );
  71. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  72. this.mock,
  73. 'ECDSAInvalidSignature',
  74. );
  75. });
  76. });
  77. describe('with v=27 signature', function () {
  78. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  79. const signatureWithoutV =
  80. '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  81. it('works with correct v value', async function () {
  82. const v = '0x1b'; // 27 = 1b.
  83. const signature = ethers.concat([signatureWithoutV, v]);
  84. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.eventually.equal(signer);
  85. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.eventually.equal(signer);
  86. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  87. await expect(
  88. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  89. ).to.eventually.equal(signer);
  90. await expect(
  91. this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs),
  92. ).to.eventually.equal(signer);
  93. });
  94. it('rejects incorrect v value', async function () {
  95. const v = '0x1c'; // 28 = 1c.
  96. const signature = ethers.concat([signatureWithoutV, v]);
  97. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.eventually.not.equal(signer);
  98. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.eventually.not.equal(signer);
  99. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  100. expect(
  101. await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  102. ).to.not.equal(signer);
  103. expect(await this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.not.equal(
  104. signer,
  105. );
  106. });
  107. it('reverts wrong v values', async function () {
  108. for (const v of ['0x00', '0x01']) {
  109. const signature = ethers.concat([signatureWithoutV, v]);
  110. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  111. this.mock,
  112. 'ECDSAInvalidSignature',
  113. );
  114. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  115. this.mock,
  116. 'ECDSAInvalidSignature',
  117. );
  118. const { r, s } = ethers.Signature.from(signature);
  119. await expect(
  120. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  121. ).to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignature');
  122. }
  123. });
  124. it('rejects short EIP2098 format', async function () {
  125. const v = '0x1b'; // 27 = 1b.
  126. const signature = ethers.concat([signatureWithoutV, v]);
  127. const { compactSerialized } = ethers.Signature.from(signature);
  128. await expect(this.mock.$recover(TEST_MESSAGE, compactSerialized))
  129. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  130. .withArgs(64);
  131. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, compactSerialized))
  132. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  133. .withArgs(64);
  134. });
  135. });
  136. describe('with v=28 signature', function () {
  137. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  138. const signatureWithoutV =
  139. '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  140. it('works with correct v value', async function () {
  141. const v = '0x1c'; // 28 = 1c.
  142. const signature = ethers.concat([signatureWithoutV, v]);
  143. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.eventually.equal(signer);
  144. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.eventually.equal(signer);
  145. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  146. await expect(
  147. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  148. ).to.eventually.equal(signer);
  149. await expect(
  150. this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs),
  151. ).to.eventually.equal(signer);
  152. });
  153. it('rejects incorrect v value', async function () {
  154. const v = '0x1b'; // 27 = 1b.
  155. const signature = ethers.concat([signatureWithoutV, v]);
  156. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  157. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.not.equal(signer);
  158. const { r, s, yParityAndS: vs } = ethers.Signature.from(signature);
  159. expect(
  160. await this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  161. ).to.not.equal(signer);
  162. await expect(this.mock.getFunction('$recover(bytes32,bytes32,bytes32)')(TEST_MESSAGE, r, vs)).to.not.equal(
  163. signer,
  164. );
  165. });
  166. it('reverts invalid v values', async function () {
  167. for (const v of ['0x00', '0x01']) {
  168. const signature = ethers.concat([signatureWithoutV, v]);
  169. await expect(this.mock.$recover(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  170. this.mock,
  171. 'ECDSAInvalidSignature',
  172. );
  173. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature)).to.be.revertedWithCustomError(
  174. this.mock,
  175. 'ECDSAInvalidSignature',
  176. );
  177. const { r, s } = ethers.Signature.from(signature);
  178. await expect(
  179. this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s),
  180. ).to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignature');
  181. }
  182. });
  183. it('rejects short EIP2098 format', async function () {
  184. const v = '0x1b'; // 28 = 1b.
  185. const signature = ethers.concat([signatureWithoutV, v]);
  186. const { compactSerialized } = ethers.Signature.from(signature);
  187. await expect(this.mock.$recover(TEST_MESSAGE, compactSerialized))
  188. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  189. .withArgs(64);
  190. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, compactSerialized))
  191. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  192. .withArgs(64);
  193. });
  194. });
  195. it('reverts with high-s value signature', async function () {
  196. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  197. const highSSignature =
  198. '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  199. const r = ethers.dataSlice(highSSignature, 0, 32);
  200. const s = ethers.dataSlice(highSSignature, 32, 64);
  201. const v = ethers.dataSlice(highSSignature, 64, 65);
  202. await expect(this.mock.$recover(message, highSSignature))
  203. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  204. .withArgs(s);
  205. await expect(this.mock.$recoverCalldata(message, highSSignature))
  206. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  207. .withArgs(s);
  208. await expect(this.mock.getFunction('$recover(bytes32,uint8,bytes32,bytes32)')(TEST_MESSAGE, v, r, s))
  209. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureS')
  210. .withArgs(s);
  211. expect(() => ethers.Signature.from(highSSignature)).to.throw('non-canonical s');
  212. });
  213. });
  214. describe('parse signature', function () {
  215. it('65 and 64 bytes signatures', async function () {
  216. // Create the signature
  217. const signature = await this.signer.signMessage(TEST_MESSAGE).then(ethers.Signature.from);
  218. await expect(this.mock.$parse(signature.serialized)).to.eventually.deep.equal([
  219. signature.v,
  220. signature.r,
  221. signature.s,
  222. ]);
  223. await expect(this.mock.$parse(signature.compactSerialized)).to.eventually.deep.equal([
  224. signature.v,
  225. signature.r,
  226. signature.s,
  227. ]);
  228. await expect(this.mock.$parseCalldata(signature.serialized)).to.eventually.deep.equal([
  229. signature.v,
  230. signature.r,
  231. signature.s,
  232. ]);
  233. await expect(this.mock.$parseCalldata(signature.compactSerialized)).to.eventually.deep.equal([
  234. signature.v,
  235. signature.r,
  236. signature.s,
  237. ]);
  238. });
  239. it('with short signature', async function () {
  240. const signature = '0x1234';
  241. await expect(this.mock.$parse(signature)).to.eventually.deep.equal([0n, ethers.ZeroHash, ethers.ZeroHash]);
  242. await expect(this.mock.$parseCalldata(signature)).to.eventually.deep.equal([
  243. 0n,
  244. ethers.ZeroHash,
  245. ethers.ZeroHash,
  246. ]);
  247. });
  248. it('with long signature', async function () {
  249. const signature =
  250. '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
  251. await expect(this.mock.$recover(TEST_MESSAGE, signature))
  252. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  253. .withArgs(85);
  254. await expect(this.mock.$recoverCalldata(TEST_MESSAGE, signature))
  255. .to.be.revertedWithCustomError(this.mock, 'ECDSAInvalidSignatureLength')
  256. .withArgs(85);
  257. await expect(this.mock.$parse(signature)).to.eventually.deep.equal([0n, ethers.ZeroHash, ethers.ZeroHash]);
  258. await expect(this.mock.$parseCalldata(signature)).to.eventually.deep.equal([
  259. 0n,
  260. ethers.ZeroHash,
  261. ethers.ZeroHash,
  262. ]);
  263. });
  264. });
  265. });