ECDSA.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const { constants, expectRevert } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { toEthSignedMessageHash, fixSignature } = require('../helpers/sign');
  4. const { expect } = require('chai');
  5. const ECDSAMock = artifacts.require('ECDSAMock');
  6. const TEST_MESSAGE = web3.utils.sha3('OpenZeppelin');
  7. const WRONG_MESSAGE = web3.utils.sha3('Nope');
  8. contract('ECDSA', function ([_, other]) {
  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('returns 0', async function () {
  20. const version = '00';
  21. const signature = signatureWithoutVersion + version;
  22. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(ZERO_ADDRESS);
  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. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.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. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(ZERO_ADDRESS);
  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('returns 0', async function () {
  48. const version = '01';
  49. const signature = signatureWithoutVersion + version;
  50. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(ZERO_ADDRESS);
  51. });
  52. });
  53. context('with 28 as version value', function () {
  54. it('works', async function () {
  55. const version = '1c'; // 28 = 1c.
  56. const signature = signatureWithoutVersion + version;
  57. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.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. expect(await this.ecdsa.recover(TEST_MESSAGE, signature)).to.equal(ZERO_ADDRESS);
  67. });
  68. });
  69. });
  70. context('with high-s value signature', function () {
  71. it('returns 0', async function () {
  72. const message = '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
  73. // eslint-disable-next-line max-len
  74. const highSSignature = '0xe742ff452d41413616a5bf43fe15dd88294e983d3d36206c2712f39083d638bde0a0fc89be718fbc1033e1d30d78be1c68081562ed2e97af876f286f3453231d1b';
  75. expect(await this.ecdsa.recover(message, highSSignature)).to.equal(ZERO_ADDRESS);
  76. });
  77. });
  78. context('using web3.eth.sign', function () {
  79. context('with correct signature', function () {
  80. it('returns signer address', async function () {
  81. // Create the signature
  82. const signature = fixSignature(await web3.eth.sign(TEST_MESSAGE, other));
  83. // Recover the signer address from the generated message and signature.
  84. expect(await this.ecdsa.recover(
  85. toEthSignedMessageHash(TEST_MESSAGE),
  86. signature
  87. )).to.equal(other);
  88. });
  89. });
  90. context('with wrong signature', function () {
  91. it('does not return signer address', async function () {
  92. // Create the signature
  93. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  94. // Recover the signer address from the generated message and wrong signature.
  95. expect(await this.ecdsa.recover(WRONG_MESSAGE, signature)).to.not.equal(other);
  96. });
  97. });
  98. });
  99. context('with small hash', function () {
  100. // @TODO - remove `skip` once we upgrade to solc^0.5
  101. it.skip('reverts', async function () {
  102. // Create the signature
  103. const signature = await web3.eth.sign(TEST_MESSAGE, other);
  104. await expectRevert(
  105. this.ecdsa.recover(TEST_MESSAGE.substring(2), signature),
  106. 'Failure message'
  107. );
  108. });
  109. });
  110. });
  111. context('toEthSignedMessage', function () {
  112. it('should prefix hashes correctly', async function () {
  113. (await this.ecdsa.toEthSignedMessageHash(TEST_MESSAGE)).should.equal(toEthSignedMessageHash(TEST_MESSAGE));
  114. expect(await this.ecdsa.toEthSignedMessageHash(TEST_MESSAGE)).to.equal(toEthSignedMessageHash(TEST_MESSAGE));
  115. });
  116. });
  117. });