import { type ConcatHexErrorType, concatHex, } from '../../../core/internal/data/concat.js' import { type HexToBytesErrorType, hexToBytes, } from '../../../core/internal/encoding/toBytes.js' import { type ToHexErrorType, toHex, } from '../../../core/internal/encoding/toHex.js' import { type ToRlpErrorType, toRlp, } from '../../../core/internal/encoding/toRlp.js' import type { ErrorType } from '../../../core/internal/errors/utils.js' import { type Keccak256ErrorType, keccak256, } from '../../../core/internal/hash/keccak256.js' import type { BigIntegerish, Bytes, Hex, Integerish, } from '../../../core/internal/types/data.js' import type { Authorization } from '../types/authorization.js' type To = 'hex' | 'bytes' export type HashAuthorizationParameters = Authorization< BigIntegerish, Integerish > & { /** Output format. @default "hex" */ to?: to | To | undefined } export type HashAuthorizationReturnType = | (to extends 'bytes' ? Bytes : never) | (to extends 'hex' ? Hex : never) export type HashAuthorizationErrorType = | Keccak256ErrorType | ConcatHexErrorType | ToRlpErrorType | ToHexErrorType | HexToBytesErrorType | ErrorType /** * Computes an Authorization hash in [EIP-7702 format](https://eips.ethereum.org/EIPS/eip-7702): `keccak256('0x05' || rlp([chain_id, address, nonce]))`. */ export function hashAuthorization( parameters: HashAuthorizationParameters, ): HashAuthorizationReturnType { const { chainId, contractAddress, nonce, to } = parameters const hash = keccak256( concatHex([ '0x05', toRlp([toHex(chainId), contractAddress, [nonce ? toHex(nonce) : '0x']]), ]), ) if (to === 'bytes') return hexToBytes(hash) as HashAuthorizationReturnType return hash as HashAuthorizationReturnType }