ECDSA.test.js 10 KB

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