ECDSA.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { toEthSignedMessageHash, fixSignature } = require('../../helpers/sign');
  3. const { expect } = require('chai');
  4. const ECDSAMock = artifacts.require('ECDSAMock');
  5. const TEST_MESSAGE = web3.utils.sha3('OpenZeppelin');
  6. const WRONG_MESSAGE = web3.utils.sha3('Nope');
  7. function to2098Format (signature) {
  8. const long = web3.utils.hexToBytes(signature);
  9. expect(long.length).to.be.equal(65);
  10. const short = long.slice(0, 64);
  11. short[32] |= (long[64] % 27) << 7; // set the first bit of the 32nd byte to the v parity bit
  12. return web3.utils.bytesToHex(short);
  13. }
  14. function from2098Format (signature) {
  15. const short = web3.utils.hexToBytes(signature);
  16. expect(short.length).to.be.equal(64);
  17. short.push((short[32] >> 7) + 27);
  18. short[32] &= (1 << 7) - 1; // zero out the first bit of 1 the 32nd byte
  19. return web3.utils.bytesToHex(short);
  20. }
  21. contract('ECDSA', function (accounts) {
  22. const [ other ] = accounts;
  23. beforeEach(async function () {
  24. this.ecdsa = await ECDSAMock.new();
  25. });
  26. context('recover with invalid signature', function () {
  27. it('with short signature', async function () {
  28. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, '0x1234'), 'ECDSA: invalid signature length');
  29. });
  30. it('with long signature', async function () {
  31. await expectRevert(
  32. // eslint-disable-next-line max-len
  33. this.ecdsa.recover(TEST_MESSAGE, '0x01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'),
  34. 'ECDSA: invalid signature length',
  35. );
  36. });
  37. });
  38. context('recover with valid signature', function () {
  39. context('with v0 signature', function () {
  40. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  41. const signer = '0x2cc1166f6212628A0deEf2B33BEFB2187D35b86c';
  42. // eslint-disable-next-line max-len
  43. const signatureWithoutVersion = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  44. it('reverts with 00 as version value', async function () {
  45. const version = '00';
  46. const signature = signatureWithoutVersion + version;
  47. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  48. });
  49. it('works with 27 as version value', async function () {
  50. const version = '1b'; // 27 = 1b.
  51. const signature = signatureWithoutVersion + version;
  52. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  53. });
  54. it('reverts with wrong version', async function () {
  55. // The last two hex digits are the signature version.
  56. // The only valid values are 0, 1, 27 and 28.
  57. const version = '02';
  58. const signature = signatureWithoutVersion + version;
  59. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  60. });
  61. it('works with short EIP2098 format', async function () {
  62. const version = '1b'; // 27 = 1b.
  63. const signature = signatureWithoutVersion + version;
  64. expect(await this.ecdsa.recover(TEST_MESSAGE, to2098Format(signature))).to.equal(signer);
  65. expect(await this.ecdsa.recover(TEST_MESSAGE, from2098Format(to2098Format(signature)))).to.equal(signer);
  66. });
  67. });
  68. context('with v1 signature', function () {
  69. const signer = '0x1E318623aB09Fe6de3C9b8672098464Aeda9100E';
  70. // eslint-disable-next-line max-len
  71. const signatureWithoutVersion = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  72. it('reverts with 01 as version value', async function () {
  73. const version = '01';
  74. const signature = signatureWithoutVersion + version;
  75. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  76. });
  77. it('works with 28 as version value', async function () {
  78. const version = '1c'; // 28 = 1c.
  79. const signature = signatureWithoutVersion + version;
  80. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(signer);
  81. });
  82. it('reverts with wrong version', async function () {
  83. // The last two hex digits are the signature version.
  84. // The only valid values are 0, 1, 27 and 28.
  85. const version = '02';
  86. const signature = signatureWithoutVersion + version;
  87. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature \'v\' value');
  88. });
  89. it('works with short EIP2098 format', async function () {
  90. const version = '1c'; // 27 = 1b.
  91. const signature = signatureWithoutVersion + version;
  92. expect(await this.ecdsa.recover(TEST_MESSAGE, to2098Format(signature))).to.equal(signer);
  93. expect(await this.ecdsa.recover(TEST_MESSAGE, from2098Format(to2098Format(signature)))).to.equal(signer);
  94. });
  95. });
  96. it('reverts with high-s value signature', async function () {
  97. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  98. // eslint-disable-next-line max-len
  99. const highSSignature = '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  100. await expectRevert(this.ecdsa.recover(message, highSSignature), 'ECDSA: invalid signature \'s\' value');
  101. });
  102. context('using web3.eth.sign', function () {
  103. it('returns signer address with correct signature', async function () {
  104. // Create the signature
  105. const signature = fixSignature(await web3.eth.sign(TEST_MESSAGE, other));
  106. // Recover the signer address from the generated message and signature.
  107. expect(await this.ecdsa.recover(
  108. toEthSignedMessageHash(TEST_MESSAGE),
  109. signature,
  110. )).to.equal(other);
  111. });
  112. it('returns a different address', async function () {
  113. const signature = fixSignature(await web3.eth.sign(TEST_MESSAGE, other));
  114. expect(await this.ecdsa.recover(WRONG_MESSAGE, signature)).to.not.equal(other);
  115. });
  116. it('reverts with invalid signature', async function () {
  117. // eslint-disable-next-line max-len
  118. const signature = '0x332ce75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e01c';
  119. await expectRevert(this.ecdsa.recover(TEST_MESSAGE, signature), 'ECDSA: invalid signature');
  120. });
  121. });
  122. });
  123. context('toEthSignedMessage', function () {
  124. it('prefixes hashes correctly', async function () {
  125. expect(await this.ecdsa.toEthSignedMessageHash(TEST_MESSAGE)).to.equal(toEthSignedMessageHash(TEST_MESSAGE));
  126. });
  127. });
  128. });