import {z} from 'zod' export type StarknetAppealTypes = 'should_be_malicious' | 'should_be_benign' | 'wrong_simulation_result'; export type StarknetAppealRequestId = {type?: 'request_id' | undefined, id: string}; export type StarknetChain = 'mainnet'; export type StarknetOptions = 'validation' | 'simulation'; export type StarknetInvokeV1TransactionSchema = {version: 1, nonce: string, sender_address: string, calldata?: Array | undefined, max_fee: string}; export type StarknetInvokeV3TransactionSchema = {version: 3, nonce: string, sender_address: string, calldata: Array, chain_id: string, nonce_data_availability_mode?: 0 | undefined, paymaster_data?: Array | undefined, account_deployment_data?: Array | undefined}; export type StarknetDeployAccountV1TransactionSchema = {version: 1, nonce: string, class_hash: string, contract_address_salt: string, constructor_calldata: Array, max_fee: string}; export type StarknetDeployAccountV3TransactionSchema = {version: 3, nonce: string, class_hash: string, contract_address_salt: string, constructor_calldata: Array, max_fee: string}; export type StarknetOutsideCallSchema = {to: string, selector: string, calldata?: Array | undefined}; export type StarknetOutsideExecutionSchema = {caller: string, nonce: string, execute_after: string, execute_before: string, calls: Array}; export type StarknetWalletRequestMetadata = {type: 'wallet', url: string}; export type StarknetInAppRequestMetadata = {type?: 'in_app' | undefined}; export type StarknetTransactionScanRequest = {chain: StarknetChain | string, options?: Array | undefined, account_address: string, transaction: StarknetInvokeV1TransactionSchema | StarknetInvokeV3TransactionSchema | StarknetDeployAccountV1TransactionSchema | StarknetDeployAccountV3TransactionSchema | StarknetOutsideExecutionSchema, metadata: StarknetWalletRequestMetadata | StarknetInAppRequestMetadata, block_number?: (string | null) | undefined}; export type StarknetAppealTransactionDataReport = {type?: 'params' | undefined, params: StarknetTransactionScanRequest}; export type StarknetReportTransactionRequest = {event: StarknetAppealTypes, report: StarknetAppealRequestId | StarknetAppealTransactionDataReport, details: string}; export type CreateApiV0StarknetTransactionReportArgs = {body: StarknetReportTransactionRequest}; export type CreateApiV0StarknetTransactionReportOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV0StarknetTransactionReportResponse = z.number().int(); 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 }; /** * Report Transaction * * POST /v0/starknet/transaction/report * * Also accepts auth: ClientID */ export const createApiV0StarknetTransactionReport = async (args: CreateApiV0StarknetTransactionReportArgs, options?: CreateApiV0StarknetTransactionReportOptions) => { 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/starknet/transaction/report${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(createApiV0StarknetTransactionReportResponse, await res.json()) };