import {z} from 'zod' export type AccountMetadataDto = {nickname?: string | undefined, purpose?: string | undefined, tags?: Array | undefined}; export type TransliteratedAddressDto = {addressLine1?: string | undefined, addressLine2?: string | undefined, city?: string | undefined, state?: string | undefined}; export type AddressDto = {addressLine1: string, addressLine2?: string | undefined, city: string, state?: string | undefined, postalCode: string, country: string, transliterated?: TransliteratedAddressDto | undefined}; export type CreateExternalWalletDto = {customerId: string, counterpartyId?: string | undefined, walletAddress: string, chains?: (Array<'ETHEREUM' | 'BASE' | 'POLYGON' | 'SOLANA' | 'TRON' | 'BITCOIN' | 'ARBITRUM' | 'ROBINHOOD'>) | undefined, currencies?: Array | undefined, externalReference?: string | undefined, isThirdParty?: boolean | undefined, custodyType?: ('SELF_HOSTED' | 'ANCHORAGE' | 'BINANCE' | 'BITGO' | 'CIRCLE' | 'COINBASE' | 'FIREBLOCKS' | 'GEMINI' | 'KRAKEN' | 'ROBINHOOD' | 'OTHER_EXCHANGE') | undefined, metadata?: AccountMetadataDto | undefined, beneficiaryType?: ('INDIVIDUAL' | 'BUSINESS') | undefined, beneficiaryName?: string | undefined, beneficiaryAddress?: AddressDto | undefined}; export type CreateApiV1ExternalWalletsArgs = {'X-API-Key'?: string | undefined, body: CreateExternalWalletDto}; export type CreateApiV1ExternalWalletsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const accountMetadataDto = z.object({nickname: z.string().max(256).optional(), purpose: z.string().max(512).optional(), tags: z.array(z.string()).optional()}); export const externalWalletResponseDto = z.object({id: z.string(), customerId: z.string(), counterpartyId: z.string().nullable().optional(), type: z.literal("EXTERNAL_WALLET"), status: z.enum(["IN_REVIEW", "ACTIVE", "CLOSED"]), metadata: accountMetadataDto.optional(), walletAddress: z.string(), addressType: z.enum(["EVM", "TRON", "BITCOIN", "SOLANA"]), chains: z.array(z.enum(["ETHEREUM", "BASE", "POLYGON", "SOLANA", "TRON", "BITCOIN", "ARBITRUM", "ROBINHOOD"])).optional(), currencies: z.array(z.array(z.string())), custodyType: z.enum(["SELF_HOSTED", "ANCHORAGE", "BINANCE", "BITGO", "CIRCLE", "COINBASE", "FIREBLOCKS", "GEMINI", "KRAKEN", "ROBINHOOD", "OTHER_EXCHANGE"]).optional(), createdAt: z.string(), updatedAt: z.string(), created: z.boolean().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 }; /** * Create an external wallet * * POST /v1/external-wallets */ export const createApiV1ExternalWallets = async (args: CreateApiV1ExternalWalletsArgs, options?: CreateApiV1ExternalWalletsOptions) => { const {baseUrl = "https://sandbox.api.infinite.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/external-wallets${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(externalWalletResponseDto, await res.json()) };