import {
Address,
encodeAbiParameters,
Hash,
keccak256,
PublicClient,
} from "viem";
const CallAbi = {
type: "tuple",
components: [
{ name: "to", type: "address" },
{ name: "value", type: "uint" },
{ name: "data", type: "bytes" },
],
} as const;
const ChainAuthorizationSignatureComponentsAbi = [
{ name: "chainId", type: "uint256" },
{ name: "calls", ...CallAbi, type: "tuple[]" },
{ name: "recentBlock", type: "uint256" },
] as const;
export function hashIntents(intent: Intent): ChainAuthorization[] {
return intent.map(({ chainId, calls, recentBlock }) => {
chainId = BigInt(chainId);
recentBlock = BigInt(recentBlock);
const hash = keccak256(
encodeAbiParameters(ChainAuthorizationSignatureComponentsAbi, [
chainId,
calls,
recentBlock,
]),
);
return {
hash,
chainId,
calls,
recentBlock,
};
});
}
export function getAuthorizationHash(
chainAuthorizations: ChainAuthorization[],
): Hash {
const chainAuthorizationHashes = chainAuthorizations.map(({ hash }) => hash);
return keccak256(
encodeAbiParameters([{ type: "bytes32[]" }], [chainAuthorizationHashes]),
);
}
export function encodeAuthorization(authorization: Authorization): Hash {
return encodeAbiParameters([AuthorizationAbi], [authorization]);
}