ECDSA.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { signMessage, toEthSignedMessageHash } = require('../helpers/sign');
  2. const shouldFail = require('../helpers/shouldFail');
  3. const ECDSAMock = artifacts.require('ECDSAMock');
  4. require('../helpers/setup');
  5. const TEST_MESSAGE = web3.sha3('OpenZeppelin');
  6. const WRONG_MESSAGE = web3.sha3('Nope');
  7. contract('ECDSA', function ([_, anyone]) {
  8. beforeEach(async function () {
  9. this.ecdsa = await ECDSAMock.new();
  10. });
  11. context('recover with valid signature', function () {
  12. context('with v0 signature', function () {
  13. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  14. const signer = '0x2cc1166f6212628a0deef2b33befb2187d35b86c';
  15. // eslint-disable-next-line max-len
  16. const signatureWithoutVersion = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be892';
  17. context('with 00 as version value', function () {
  18. it('works', async function () {
  19. const version = '00';
  20. const signature = signatureWithoutVersion + version;
  21. (await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
  22. });
  23. });
  24. context('with 27 as version value', function () {
  25. it('works', async function () {
  26. const version = '1b'; // 27 = 1b.
  27. const signature = signatureWithoutVersion + version;
  28. (await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
  29. });
  30. });
  31. context('with wrong version', function () {
  32. it('returns 0', async function () {
  33. // The last two hex digits are the signature version.
  34. // The only valid values are 0, 1, 27 and 28.
  35. const version = '02';
  36. const signature = signatureWithoutVersion + version;
  37. (await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(
  38. '0x0000000000000000000000000000000000000000');
  39. });
  40. });
  41. });
  42. context('with v1 signature', function () {
  43. const signer = '0x1e318623ab09fe6de3c9b8672098464aeda9100e';
  44. // eslint-disable-next-line max-len
  45. const signatureWithoutVersion = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e0';
  46. context('with 01 as version value', function () {
  47. it('works', async function () {
  48. const version = '01';
  49. const signature = signatureWithoutVersion + version;
  50. (await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
  51. });
  52. });
  53. context('with 28 signature', function () {
  54. it('works', async function () {
  55. const version = '1c'; // 28 = 1c.
  56. const signature = signatureWithoutVersion + version;
  57. (await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(signer);
  58. });
  59. });
  60. context('with wrong version', function () {
  61. it('returns 0', async function () {
  62. // The last two hex digits are the signature version.
  63. // The only valid values are 0, 1, 27 and 28.
  64. const version = '02';
  65. const signature = signatureWithoutVersion + version;
  66. (await this.ecdsa.recover(TEST_MESSAGE, signature)).should.equal(
  67. '0x0000000000000000000000000000000000000000');
  68. });
  69. });
  70. });
  71. context('using web3.eth.sign', function () {
  72. context('with correct signature', function () {
  73. it('returns signer address', async function () {
  74. // Create the signature
  75. const signature = signMessage(anyone, TEST_MESSAGE);
  76. // Recover the signer address from the generated message and signature.
  77. (await this.ecdsa.recover(
  78. toEthSignedMessageHash(TEST_MESSAGE),
  79. signature
  80. )).should.equal(anyone);
  81. });
  82. });
  83. context('with wrong signature', function () {
  84. it('does not return signer address', async function () {
  85. // Create the signature
  86. const signature = signMessage(anyone, TEST_MESSAGE);
  87. // Recover the signer address from the generated message and wrong signature.
  88. (await this.ecdsa.recover(WRONG_MESSAGE, signature)).should.not.equal(anyone);
  89. });
  90. });
  91. });
  92. });
  93. context('with small hash', function () {
  94. // @TODO - remove `skip` once we upgrade to solc^0.5
  95. it.skip('reverts', async function () {
  96. // Create the signature
  97. const signature = signMessage(anyone, TEST_MESSAGE);
  98. await shouldFail.reverting(
  99. this.ecdsa.recover(TEST_MESSAGE.substring(2), signature)
  100. );
  101. });
  102. });
  103. context('toEthSignedMessage', function () {
  104. it('should prefix hashes correctly', async function () {
  105. (await this.ecdsa.toEthSignedMessageHash(TEST_MESSAGE)).should.equal(toEthSignedMessageHash(TEST_MESSAGE));
  106. });
  107. });
  108. });