import type { ErrorType } from '../../errors/utils.js' import type { ByteArray, Hex, SignableMessage } from '../../types/misc.js' import { type ConcatErrorType, concat } from '../data/concat.js' import { type StringToBytesErrorType, type ToBytesErrorType, stringToBytes, toBytes, } from '../encoding/toBytes.js' import { type Keccak256ErrorType, keccak256 } from '../hash/keccak256.js' type To = 'hex' | 'bytes' export type HashMessage = | (TTo extends 'bytes' ? ByteArray : never) | (TTo extends 'hex' ? Hex : never) export type HashMessageErrorType = | ConcatErrorType | Keccak256ErrorType | StringToBytesErrorType | ToBytesErrorType | ErrorType export function hashMessage( message: SignableMessage, to_?: TTo, ): HashMessage { const messageBytes = (() => { if (typeof message === 'string') return stringToBytes(message) if (message.raw instanceof Uint8Array) return message.raw return toBytes(message.raw) })() const prefixBytes = stringToBytes( `\x19Ethereum Signed Message:\n${messageBytes.length}`, ) return keccak256(concat([prefixBytes, messageBytes]), to_) }