ECRecovery.test.js 3.0 KB

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