import {z} from 'zod' export type V1Chain = string; export type V1Account = {account_id: string, account_creation_timestamp?: string | undefined, user_age?: number | undefined, user_country_code?: string | undefined, account_addresses?: Array | undefined}; export type V1Connection = {user_agent?: string | undefined, ip_address: unknown, origin?: string | undefined, walletconnect_name?: string | undefined, walletconnect_description?: string | undefined}; export type V1ScanRequestMetadata = {account?: V1Account | undefined, connection?: V1Connection | undefined}; export type V1ExchangeWithdrawalScanRequest = {format: 'EXCHANGE_WITHDRAWAL', chain: V1Chain, withdrawing_account: string, to: string, asset?: (string | null) | undefined, amount?: (string | null) | undefined, metadata?: V1ScanRequestMetadata | undefined}; export type V1ExchangeDepositScanRequest = {format: 'EXCHANGE_DEPOSIT', chain: V1Chain, depositor_address: string, to: string, asset?: (string | null) | undefined, amount?: (string | null) | undefined, metadata?: V1ScanRequestMetadata | undefined}; export type V1TransactionScanRequest = V1ExchangeWithdrawalScanRequest | V1ExchangeDepositScanRequest; export type CreateApiV1TransactionScanArgs = {body: V1TransactionScanRequest}; export type CreateApiV1TransactionScanOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const v1EntityType = z.enum(["TOKEN", "ADDRESS", "TRANSACTION", "SIGNATURE", "WEBSITE"]); export const v1Chain = z.string(); export const v1ExchangeWithdrawalDetails = z.object({format: z.literal("EXCHANGE_WITHDRAWAL"), chain: v1Chain, withdrawing_account: z.string(), to: z.string(), asset: z.string().nullable().optional(), amount: z.string().nullable().optional()}); export const v1ExchangeDepositDetails = z.object({format: z.literal("EXCHANGE_DEPOSIT"), chain: v1Chain, depositor_address: z.string(), to: z.string(), asset: z.string().nullable().optional(), amount: z.string().nullable().optional()}); export const v1TransactionMetadataDetails = z.discriminatedUnion("format", [v1ExchangeWithdrawalDetails, v1ExchangeDepositDetails]); export const v1TransactionMetadata = z.object({entity_type: v1EntityType, details: v1TransactionMetadataDetails}); export const v1RiskSeverityLevel = z.enum(["none", "low", "medium", "high", "critical"]); export const v1Severity = z.object({level: v1RiskSeverityLevel, score: z.number().int().gte(0).lte(100)}); export const v1Profile = z.object({id: z.string()}); export const v1Proximity = z.enum(["ownership", "counterparty", "indirect"]); export const v1Evidence = z.record(z.string(), z.unknown()); export const v1Signal = z.object({id: z.string(), name: z.string(), description: z.string(), evidence: v1Evidence}); export const v1NegativeCategory = z.object({category: z.string(), proximity: v1Proximity.optional(), name: z.string(), description: z.string(), signals: z.array(v1Signal)}); export const v1PositiveCategory = z.object({category: z.string(), name: z.string(), description: z.string(), signals: z.array(v1Signal)}); export const v1Risk = z.object({severity: v1Severity, profile: v1Profile, negative_categories: z.array(v1NegativeCategory), positive_categories: z.array(v1PositiveCategory)}); export const v1TransactionScanResponse = z.object({request_id: z.string(), metadata: v1TransactionMetadata, risk: v1Risk}); 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 a transaction * * POST /v1/transaction/scan * * Also accepts auth: ClientID */ export const createApiV1TransactionScan = async (args: CreateApiV1TransactionScanArgs, options?: CreateApiV1TransactionScanOptions) => { 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(/\/+$/, '')}/v1/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(v1TransactionScanResponse, await res.json()) };