import {z} from 'zod' export type RoutersChainAgnosticModelsRequestAccount = {account_id: string, account_creation_timestamp?: string | undefined, user_age?: number | undefined, user_country_code?: string | undefined, account_addresses?: Array | undefined}; export type RoutersChainAgnosticModelsRequestConnection = {user_agent?: string | undefined, ip_address: unknown, origin?: string | undefined, walletconnect_name?: string | undefined, walletconnect_description?: string | undefined}; export type ScanRequestMetadata = {domain?: string | undefined, non_dapp?: boolean | undefined, account?: RoutersChainAgnosticModelsRequestAccount | undefined, connection?: RoutersChainAgnosticModelsRequestConnection | undefined}; export type SiteScanRequest = {url: string, metadata?: ScanRequestMetadata | undefined}; export type CreateApiV0SiteScanArgs = {body: SiteScanRequest}; export type CreateApiV0SiteScanOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const attackEntry = z.object({score: z.number(), threshold: z.number()}); export const contractOperations = z.object({contract_addresses: z.array(z.string()), functions: z.record(z.string(), z.array(z.string()))}); export const siteScanHitResponse = z.object({status: z.literal("hit"), url: z.string(), scan_start_time: z.string(), scan_end_time: z.string(), malicious_score: z.number(), is_reachable: z.boolean(), is_web3_site: z.boolean(), is_malicious: z.boolean(), attack_types: z.record(z.string(), attackEntry), network_operations: z.array(z.string()), json_rpc_operations: z.array(z.string()), contract_write: contractOperations, contract_read: contractOperations}); export const siteScanMissResponse = z.object({status: z.literal("miss")}); export const createApiV0SiteScanResponse = z.union([siteScanHitResponse, siteScanMissResponse]); 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 }; /** * Scan Site * * POST /v0/site/scan * * Also accepts auth: ClientID */ export const createApiV0SiteScan = async (args: CreateApiV0SiteScanArgs, options?: CreateApiV0SiteScanOptions) => { 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/site/scan${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(createApiV0SiteScanResponse, await res.json()) };