import {z} from 'zod' export type EvaluateTransactionRequest = {account_address: string, chain: 'arbitrum' | 'base' | 'bsc' | 'gnosis' | 'ethereum' | 'optimism' | 'polygon' | 'sepolia' | 'monad' | 'katana', data: {data: string, from: string, to: string, type?: 'transaction' | undefined, value: string} | {eip712_executor?: (string | null) | undefined, type: 'eip712', typed_data: string}, evaluation_type: 'simulate', inline_policies?: (Array<{action: string, name: string, policy: Record} | {[key: string]: unknown}> | null) | undefined, metadata?: Record | undefined, options?: Array | undefined} | {chain: 'arbitrum' | 'base' | 'bsc' | 'gnosis' | 'ethereum' | 'optimism' | 'polygon' | 'sepolia' | 'monad' | 'katana', evaluation_type: 'tx_hash', inline_policies?: (Array<{action: string, name: string, policy: Record} | {[key: string]: unknown}> | null) | undefined, show_transaction_detail_for?: (Array<'approved' | 'rejected' | 'manual_approved'> | null) | undefined, tx_hash: string} | {chain: 'arbitrum' | 'base' | 'bsc' | 'gnosis' | 'ethereum' | 'optimism' | 'polygon' | 'sepolia' | 'monad' | 'katana', evaluation_type: 'time_period', show_transaction_detail_for?: (Array<'approved' | 'rejected' | 'manual_approved'> | null) | undefined, time_period: {from: string, to: string}}; export type CreateApiV0PlatformCosignersIdEvmEvaluateArgs = {id: string, body: EvaluateTransactionRequest}; export type CreateApiV0PlatformCosignersIdEvmEvaluateOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const matchedPolicyInfoExternal = z.object({name: z.string(), action: z.string(), type: z.string()}); export const policyTraceEntryExternal = z.object({name: z.string(), action: z.string(), type: z.string(), result: z.enum(["matched", "not_matched", "skipped"])}); export const policiesExternal = z.object({decision: z.enum(["approve", "reject", "error", "pending"]), reason: z.string().nullable().optional(), matched_policy: matchedPolicyInfoExternal.nullable().optional(), trace: z.array(policyTraceEntryExternal), should_handle_pending_tx: z.boolean().nullable().optional()}); export const evaluateTransactionResponse = z.object({policies: policiesExternal, scan_id: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const evaluationSummary = z.object({total_transactions: z.number().int(), approved: z.number().int(), rejected: z.number().int(), manual_approved: z.number().int()}); export const policyEvaluationSummary = z.object({policy_id: z.string(), policy_name: z.string(), total_evaluated: z.number().int(), approved: z.number().int(), rejected: z.number().int(), manual_approved: z.number().int(), approved_transactions: z.array(z.string()).nullable().optional(), rejected_transactions: z.array(z.string()).nullable().optional(), manual_approved_transactions: z.array(z.string()).nullable().optional(), policy_type: z.string().nullable().optional()}); export const evaluateHistoricalDataResponse = z.object({cosigner_id: z.string(), chain: z.string(), summary: evaluationSummary, policies: z.array(policyEvaluationSummary), scan: z.object({}).nullable().optional(), scan_id: z.string().nullable().optional(), scan_result: z.string().nullable().optional(), scan_timestamp: z.string().nullable().optional(), transaction_timestamp: z.string().nullable().optional()}); export const createApiV0PlatformCosignersIdEvmEvaluateResponse = z.union([evaluateTransactionResponse, evaluateHistoricalDataResponse]); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Evaluate EVM Transaction * * POST /v0/platform/cosigners/{id}/evm/evaluate * * Also accepts auth: ClientID, JWT */ export const createApiV0PlatformCosignersIdEvmEvaluate = async (args: CreateApiV0PlatformCosignersIdEvmEvaluateArgs, options?: CreateApiV0PlatformCosignersIdEvmEvaluateOptions) => { const {baseUrl = "https://api.blockaid.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v0/platform/cosigners/${encodeURIComponent(String(args.id))}/evm/evaluate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV0PlatformCosignersIdEvmEvaluateResponse, await res.json()) };