ECRecovery.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var ECRecovery = artifacts.require("../contracts/ECRecovery.sol");
  2. var utils = require('ethereumjs-util');
  3. contract('ECRecovery', function(accounts) {
  4. let ecrecovery;
  5. before(async function() {
  6. ecrecovery = await ECRecovery.new();
  7. });
  8. it("recover v0", async function() {
  9. // Signature generated outside testrpc with method web3.eth.sign(signer, message)
  10. let signer = '0x2cc1166f6212628a0deef2b33befb2187d35b86c';
  11. let message = '0x7dbaf558b0a1a5dc7a67202117ab143c1d8605a983e4a743bc06fcc03162dc0d'; // web3.sha3('OpenZeppelin')
  12. let signature = '0x5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be89200';
  13. assert.equal(signer, await ecrecovery.recover(message, signature));
  14. });
  15. it("recover v1", async function() {
  16. // Signature generated outside testrpc with method web3.eth.sign(signer, message)
  17. let signer = '0x1e318623ab09fe6de3c9b8672098464aeda9100e';
  18. let message = '0x7dbaf558b0a1a5dc7a67202117ab143c1d8605a983e4a743bc06fcc03162dc0d'; // web3.sha3('OpenZeppelin')
  19. let signature = '0x331fe75a821c982f9127538858900d87d3ec1f9f737338ad67cad133fa48feff48e6fa0c18abc62e42820f05943e47af3e9fbe306ce74d64094bdf1691ee53e001';
  20. assert.equal(signer, await ecrecovery.recover(message, signature));
  21. });
  22. it("recover using web3.eth.sign()", async function() {
  23. // Create the signature using account[0]
  24. const signature = web3.eth.sign(web3.eth.accounts[0], web3.sha3('OpenZeppelin'));
  25. // Testrpc add a prefix to the signed message, we generate the hash of
  26. // 'OpenZeppelin' string repeating testrpc steps
  27. const message = new Buffer(web3.sha3('OpenZeppelin').substring(2), 'hex');
  28. const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString());
  29. const prefixedHash = utils.bufferToHex( utils.sha3(Buffer.concat([prefix, message])) );
  30. // Recover the signer address form the generated message and signature.
  31. assert.equal(web3.eth.accounts[0], await ecrecovery.recover(prefixedHash, signature));
  32. });
  33. it("recover using web3.eth.sign() should return wrong signer", async function() {
  34. // Create the signature using account[0]
  35. const signature = web3.eth.sign(web3.eth.accounts[0], web3.sha3('OpenZeppelin'));
  36. // Testrpc add a prefix to the signed message, we generate the hash of
  37. // 'Test' string repeating testrpc steps
  38. const message = new Buffer(web3.sha3('Test').substring(2), 'hex');
  39. const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString());
  40. const prefixedHash = utils.bufferToHex( utils.sha3(Buffer.concat([prefix, message])) );
  41. // Recover the signer address form the generated message and wrong signature.
  42. assert.notEqual(web3.eth.accounts[0], await ecrecovery.recover(prefixedHash, signature));
  43. });
  44. it("recover should fail when a wrong hash is sent", async function() {
  45. // Create the signature using account[0]
  46. let signature = web3.eth.sign(web3.eth.accounts[0], web3.sha3('OpenZeppelin'));
  47. // Testrpc add a prefix to the signed message, we generate the hash of
  48. // 'OpenZeppelin' string repeating testrpc steps
  49. let message = new Buffer(web3.sha3('OpenZeppelin'), 'hex');
  50. let prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString());
  51. let prefixedHash = utils.sha3(Buffer.concat([prefix, message]));
  52. // Recover the signer address form the generated message and wrong signature.
  53. assert.equal('0x0000000000000000000000000000000000000000', await ecrecovery.recover(prefixedHash, signature));
  54. });
  55. });