ECRecovery.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. hashMessage,
  3. signMessage,
  4. } from '../helpers/sign';
  5. import expectThrow from '../helpers/expectThrow';
  6. const ECRecoveryMock = artifacts.require('ECRecoveryMock');
  7. require('chai')
  8. .use(require('chai-as-promised'))
  9. .should();
  10. contract('ECRecovery', function (accounts) {
  11. let ecrecovery;
  12. const TEST_MESSAGE = 'OpenZeppelin';
  13. before(async function () {
  14. ecrecovery = await ECRecoveryMock.new();
  15. });
  16. it('recover v0', async function () {
  17. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  18. let signer = '0x2cc1166f6212628a0deef2b33befb2187d35b86c';
  19. let message = web3.sha3(TEST_MESSAGE);
  20. // eslint-disable-next-line max-len
  21. let signature = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be89200';
  22. const addrRecovered = await ecrecovery.recover(message, signature);
  23. addrRecovered.should.eq(signer);
  24. });
  25. it('recover v1', async function () {
  26. // Signature generated outside ganache with method web3.eth.sign(signer, message)
  27. let signer = '0x1e318623ab09fe6de3c9b8672098464aeda9100e';
  28. let message = web3.sha3(TEST_MESSAGE);
  29. // eslint-disable-next-line max-len
  30. let signature = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e001';
  31. const addrRecovered = await ecrecovery.recover(message, signature);
  32. addrRecovered.should.eq(signer);
  33. });
  34. it('recover using web3.eth.sign()', async function () {
  35. // Create the signature using account[0]
  36. const signature = signMessage(accounts[0], TEST_MESSAGE);
  37. // Recover the signer address from the generated message and signature.
  38. const addrRecovered = await ecrecovery.recover(
  39. hashMessage(TEST_MESSAGE),
  40. signature
  41. );
  42. addrRecovered.should.eq(accounts[0]);
  43. });
  44. it('recover using web3.eth.sign() should return wrong signer', async function () {
  45. // Create the signature using account[0]
  46. const signature = signMessage(accounts[0], TEST_MESSAGE);
  47. // Recover the signer address from the generated message and wrong signature.
  48. const addrRecovered = await ecrecovery.recover(hashMessage('Nope'), signature);
  49. assert.notEqual(accounts[0], addrRecovered);
  50. });
  51. it('recover should revert when a small hash is sent', async function () {
  52. // Create the signature using account[0]
  53. let signature = signMessage(accounts[0], TEST_MESSAGE);
  54. try {
  55. await expectThrow(
  56. ecrecovery.recover(hashMessage(TEST_MESSAGE).substring(2), signature)
  57. );
  58. } catch (error) {
  59. // @TODO(shrugs) - remove this once we upgrade to solc^0.5
  60. }
  61. });
  62. context('toEthSignedMessage', () => {
  63. it('should prefix hashes correctly', async function () {
  64. const hashedMessage = web3.sha3(TEST_MESSAGE);
  65. const ethMessage = await ecrecovery.toEthSignedMessageHash(hashedMessage);
  66. ethMessage.should.eq(hashMessage(TEST_MESSAGE));
  67. });
  68. });
  69. });