import {z} from 'zod' export type Options = 'validation'; export type Chains = 'arbitrum' | 'avalanche' | 'base' | 'base-sepolia' | 'bsc' | 'ethereum' | 'ethereum-sepolia' | 'optimism' | 'polygon' | 'zksync' | 'zksync-sepolia' | 'zora' | 'linea' | 'blast' | 'scroll' | 'avalanche-fuji' | 'degen' | 'gnosis' | 'soneium-minato' | 'ronin' | 'apechain' | 'berachain' | 'ink-sepolia' | 'ink' | 'abstract' | 'abstract-testnet' | 'metacade' | 'metacade-testnet' | 'soneium' | 'unichain' | 'lordchain-testnet' | 'lordchain' | 'sei' | 'flow-evm' | 'hyperevm' | 'megaeth' | 'katana' | 'plume' | 'solana' | 'bitcoin' | 'xlayer' | 'monad' | 'monad-testnet' | 'tron' | 'kaia' | 'plasma' | 'mantle' | 'robinhood' | 'arc' | 'flare'; export type AssetAddress = {address: string}; export type AssetSymbol = {symbol: string}; export type Data = {chain: Chains, transaction_action?: ('deposit' | 'withdrawal') | undefined, from?: string | undefined, to: string, amount: number, asset: AssetAddress | AssetSymbol}; 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 TransactionScanRequest = {options: Array, data: Data, metadata: ScanRequestMetadata}; export type CreateApiV0TransactionScanArgs = {body: TransactionScanRequest}; export type CreateApiV0TransactionScanOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionScanStatus = z.enum(["Success", "Error"]); export const routersChainAgnosticModelsResponseValidationResultType = z.enum(["Malicious", "Warning", "Benign", "High-Risk"]); export const routersChainAgnosticModelsResponseFeatureType = z.enum(["Malicious", "Warning", "Benign", "High-Risk", "Info"]); export const addressFeature = z.object({entity: z.literal("address").optional(), type: routersChainAgnosticModelsResponseFeatureType, feature_id: z.string(), description: z.string(), address: z.string(), associated_urls: z.array(z.string())}); export const urlFeature = z.object({entity: z.literal("url").optional(), type: routersChainAgnosticModelsResponseFeatureType, feature_id: z.string(), description: z.string(), url: z.string()}); export const transactionFeature = z.object({entity: z.literal("transaction").optional(), type: routersChainAgnosticModelsResponseFeatureType, feature_id: z.string(), description: z.string()}); export const validationResult = z.object({status: transactionScanStatus, result_type: routersChainAgnosticModelsResponseValidationResultType, classification: z.string(), description: z.string(), features: z.array(z.union([addressFeature, urlFeature, transactionFeature])).optional()}); export const scanResult = z.object({validation: validationResult, platform_url: z.string().nullable().optional()}); 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 Transaction * * POST /v0/transaction/scan * * Also accepts auth: ClientID */ export const createApiV0TransactionScan = async (args: CreateApiV0TransactionScanArgs, options?: CreateApiV0TransactionScanOptions) => { 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/transaction/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(scanResult, await res.json()) };