message.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.verifyMessage = exports.hashMessage = void 0;
  4. const index_js_1 = require("../crypto/index.js");
  5. const index_js_2 = require("../constants/index.js");
  6. const index_js_3 = require("../transaction/index.js");
  7. const index_js_4 = require("../utils/index.js");
  8. /**
  9. * Computes the [[link-eip-191]] personal-sign message digest to sign.
  10. *
  11. * This prefixes the message with [[MessagePrefix]] and the decimal length
  12. * of %%message%% and computes the [[keccak256]] digest.
  13. *
  14. * If %%message%% is a string, it is converted to its UTF-8 bytes
  15. * first. To compute the digest of a [[DataHexString]], it must be converted
  16. * to [bytes](getBytes).
  17. *
  18. * @example:
  19. * hashMessage("Hello World")
  20. * //_result:
  21. *
  22. * // Hashes the SIX (6) string characters, i.e.
  23. * // [ "0", "x", "4", "2", "4", "3" ]
  24. * hashMessage("0x4243")
  25. * //_result:
  26. *
  27. * // Hashes the TWO (2) bytes [ 0x42, 0x43 ]...
  28. * hashMessage(getBytes("0x4243"))
  29. * //_result:
  30. *
  31. * // ...which is equal to using data
  32. * hashMessage(new Uint8Array([ 0x42, 0x43 ]))
  33. * //_result:
  34. *
  35. */
  36. function hashMessage(message) {
  37. if (typeof (message) === "string") {
  38. message = (0, index_js_4.toUtf8Bytes)(message);
  39. }
  40. return (0, index_js_1.keccak256)((0, index_js_4.concat)([
  41. (0, index_js_4.toUtf8Bytes)(index_js_2.MessagePrefix),
  42. (0, index_js_4.toUtf8Bytes)(String(message.length)),
  43. message
  44. ]));
  45. }
  46. exports.hashMessage = hashMessage;
  47. /**
  48. * Return the address of the private key that produced
  49. * the signature %%sig%% during signing for %%message%%.
  50. */
  51. function verifyMessage(message, sig) {
  52. const digest = hashMessage(message);
  53. return (0, index_js_3.recoverAddress)(digest, sig);
  54. }
  55. exports.verifyMessage = verifyMessage;
  56. //# sourceMappingURL=message.js.map