import { keccak_256 } from "@noble/hashes/sha3"; import { type Hex, hexToUint8Array, isHex, uint8ArrayToHex, } from "../encoding/hex.js"; type To = "hex" | "bytes"; type Keccak256Hash = | (TTo extends "bytes" ? Uint8Array : never) | (TTo extends "hex" ? Hex : never); /** * Calculates the Keccak-256 hash of the given value. * @param value - The value to hash, either as a hexadecimal string or a Uint8Array. * @param to - The desired output format of the hash (optional). Defaults to 'hex'. * @returns The Keccak-256 hash of the value in the specified format. * @example * ```ts * import { keccak256 } from "thirdweb/utils"; * const hash = keccak256("0x1234"); * ``` * @utils */ export function keccak256( value: Hex | Uint8Array, to?: TTo, ): Keccak256Hash { const bytes = keccak_256( isHex(value, { strict: false }) ? hexToUint8Array(value) : value, ); if (to === "bytes") { return bytes as Keccak256Hash; } // default fall through to hex return uint8ArrayToHex(bytes) as Keccak256Hash; }