message.ts 1.6 KB

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