ECDSA.test.js 4.8 KB

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