ECDSA.test.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. require('@openzeppelin/test-helpers');
  2. const { expectRevertCustomError } = require('../../helpers/customError');
  3. const { toEthSignedMessageHash } = require('../../helpers/sign');
  4. const { expect } = require('chai');
  5. const ECDSA = artifacts.require('$ECDSA');
  6. const TEST_MESSAGE = web3.utils.sha3('OpenZeppelin');
  7. const WRONG_MESSAGE = web3.utils.sha3('Nope');
  8. const NON_HASH_MESSAGE = '0x' + Buffer.from('abcd').toString('hex');
  9. function to2098Format(signature) {
  10. const long = web3.utils.hexToBytes(signature);
  11. if (long.length !== 65) {
  12. throw new Error('invalid signature length (expected long format)');
  13. }
  14. if (long[32] >> 7 === 1) {
  15. throw new Error("invalid signature 's' value");
  16. }
  17. const short = long.slice(0, 64);
  18. short[32] |= long[64] % 27 << 7; // set the first bit of the 32nd byte to the v parity bit
  19. return web3.utils.bytesToHex(short);
  20. }
  21. function split(signature) {
  22. const raw = web3.utils.hexToBytes(signature);
  23. switch (raw.length) {
  24. case 64:
  25. return [
  26. web3.utils.bytesToHex(raw.slice(0, 32)), // r
  27. web3.utils.bytesToHex(raw.slice(32, 64)), // vs
  28. ];
  29. case 65:
  30. return [
  31. raw[64], // v
  32. web3.utils.bytesToHex(raw.slice(0, 32)), // r
  33. web3.utils.bytesToHex(raw.slice(32, 64)), // s
  34. ];
  35. default:
  36. expect.fail('Invalid signature length, cannot split');
  37. }
  38. }
  39. contract('ECDSA', function (accounts) {
  40. const [other] = accounts;
  41. beforeEach(async function () {
  42. this.ecdsa = await ECDSA.new();
  43. });
  44. context('recover with invalid signature', function () {
  45. it('with short signature', async function () {
  46. await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, '0x1234'), 'ECDSAInvalidSignatureLength', [2]);
  47. });
  48. it('with long signature', async function () {
  49. await expectRevertCustomError(
  50. // eslint-disable-next-line max-len
  51. this.ecdsa.$recover(
  52. TEST_MESSAGE,
  53. '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789',
  54. ),
  55. 'ECDSAInvalidSignatureLength',
  56. [85],
  57. );
  58. });
  59. });
  60. context('recover with valid signature', function () {
  61. context('using web3.eth.sign', function () {
  62. it('returns signer address with correct signature', async function () {
  63. // Create the signature
  64. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  65. // Recover the signer address from the generated message and signature.
  66. expect(await this.ecdsa.$recover(toEthSignedMessageHash(TEST_MESSAGE), signature)).to.equal(other);
  67. });
  68. it('returns signer address with correct signature for arbitrary length message', async function () {
  69. // Create the signature
  70. const signature = await web3.eth.sign(NON_HASH_MESSAGE, other);
  71. // Recover the signer address from the generated message and signature.
  72. expect(await this.ecdsa.$recover(toEthSignedMessageHash(NON_HASH_MESSAGE), signature)).to.equal(other);
  73. });
  74. it('returns a different address', async function () {
  75. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  76. expect(await this.ecdsa.$recover(WRONG_MESSAGE, signature)).to.not.equal(other);
  77. });
  78. it('reverts with invalid signature', async function () {
  79. // eslint-disable-next-line max-len
  80. const signature =
  81. '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  82. await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSAInvalidSignature', []);
  83. });
  84. });
  85. context('with v=27 signature', function () {
  86. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  87. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  88. // eslint-disable-next-line max-len
  89. const signatureWithoutV =
  90. '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  91. it('works with correct v value', async function () {
  92. const v = '1b'; // 27 = 1b.
  93. const signature = signatureWithoutV + v;
  94. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  95. expect(
  96. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  97. ).to.equal(signer);
  98. expect(
  99. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  100. TEST_MESSAGE,
  101. ...split(to2098Format(signature)),
  102. ),
  103. ).to.equal(signer);
  104. });
  105. it('rejects incorrect v value', async function () {
  106. const v = '1c'; // 28 = 1c.
  107. const signature = signatureWithoutV + v;
  108. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  109. expect(
  110. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  111. ).to.not.equal(signer);
  112. expect(
  113. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  114. TEST_MESSAGE,
  115. ...split(to2098Format(signature)),
  116. ),
  117. ).to.not.equal(signer);
  118. });
  119. it('reverts wrong v values', async function () {
  120. for (const v of ['00', '01']) {
  121. const signature = signatureWithoutV + v;
  122. await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSAInvalidSignature', []);
  123. await expectRevertCustomError(
  124. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  125. 'ECDSAInvalidSignature',
  126. [],
  127. );
  128. }
  129. });
  130. it('rejects short EIP2098 format', async function () {
  131. const v = '1b'; // 27 = 1b.
  132. const signature = signatureWithoutV + v;
  133. await expectRevertCustomError(
  134. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  135. 'ECDSAInvalidSignatureLength',
  136. [64],
  137. );
  138. });
  139. });
  140. context('with v=28 signature', function () {
  141. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  142. // eslint-disable-next-line max-len
  143. const signatureWithoutV =
  144. '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  145. it('works with correct v value', async function () {
  146. const v = '1c'; // 28 = 1c.
  147. const signature = signatureWithoutV + v;
  148. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.equal(signer);
  149. expect(
  150. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  151. ).to.equal(signer);
  152. expect(
  153. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  154. TEST_MESSAGE,
  155. ...split(to2098Format(signature)),
  156. ),
  157. ).to.equal(signer);
  158. });
  159. it('rejects incorrect v value', async function () {
  160. const v = '1b'; // 27 = 1b.
  161. const signature = signatureWithoutV + v;
  162. expect(await this.ecdsa.$recover(TEST_MESSAGE, signature)).to.not.equal(signer);
  163. expect(
  164. await this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  165. ).to.not.equal(signer);
  166. expect(
  167. await this.ecdsa.methods['$recover(bytes32,bytes32,bytes32)'](
  168. TEST_MESSAGE,
  169. ...split(to2098Format(signature)),
  170. ),
  171. ).to.not.equal(signer);
  172. });
  173. it('reverts invalid v values', async function () {
  174. for (const v of ['00', '01']) {
  175. const signature = signatureWithoutV + v;
  176. await expectRevertCustomError(this.ecdsa.$recover(TEST_MESSAGE, signature), 'ECDSAInvalidSignature', []);
  177. await expectRevertCustomError(
  178. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, ...split(signature)),
  179. 'ECDSAInvalidSignature',
  180. [],
  181. );
  182. }
  183. });
  184. it('rejects short EIP2098 format', async function () {
  185. const v = '1c'; // 27 = 1b.
  186. const signature = signatureWithoutV + v;
  187. await expectRevertCustomError(
  188. this.ecdsa.$recover(TEST_MESSAGE, to2098Format(signature)),
  189. 'ECDSAInvalidSignatureLength',
  190. [64],
  191. );
  192. });
  193. });
  194. it('reverts with high-s value signature', async function () {
  195. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  196. // eslint-disable-next-line max-len
  197. const highSSignature =
  198. '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  199. const [r, v, s] = split(highSSignature);
  200. await expectRevertCustomError(this.ecdsa.$recover(message, highSSignature), 'ECDSAInvalidSignatureS', [s]);
  201. await expectRevertCustomError(
  202. this.ecdsa.methods['$recover(bytes32,uint8,bytes32,bytes32)'](TEST_MESSAGE, r, v, s),
  203. 'ECDSAInvalidSignatureS',
  204. [s],
  205. );
  206. expect(() => to2098Format(highSSignature)).to.throw("invalid signature 's' value");
  207. });
  208. });
  209. });