message.ts 503 B

123456789101112131415
  1. import { Bytes, concat } from "@ethersproject/bytes";
  2. import { keccak256 } from "@ethersproject/keccak256";
  3. import { toUtf8Bytes } from "@ethersproject/strings";
  4. export const messagePrefix = "\x19Ethereum Signed Message:\n";
  5. export function hashMessage(message: Bytes | string): string {
  6. if (typeof(message) === "string") { message = toUtf8Bytes(message); }
  7. return keccak256(concat([
  8. toUtf8Bytes(messagePrefix),
  9. toUtf8Bytes(String(message.length)),
  10. message
  11. ]));
  12. }