ECDSA.test.js 4.8 KB

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