ECRecovery.test.js 3.0 KB

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