ECDSA.test.js 5.3 KB

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