import {z} from 'zod' export type Instruction = {walletProfile?: string | undefined, type: 'FIAT' | 'CRYPTO', virtualAccountRequired: boolean}; export type CreateWalletRequest = {currencyCode: 'USD' | 'EUR' | 'GBP' | 'USDT' | 'POL' | 'PYUSD' | 'BTC' | 'SOL' | 'BNB' | 'XRP' | 'ETH' | 'LTC' | 'USDC' | 'USDG' | 'TRX', name: string, customerReference: string, instruction: Instruction}; export type CreateApiLedgerV1WalletsArgs = {'X-Idempotency-Key': string, body: CreateWalletRequest}; export type CreateApiLedgerV1WalletsOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const balance = z.object({value: z.number(), currencyCode: z.enum(["USD", "EUR", "GBP", "USDT", "POL", "PYUSD", "BTC", "SOL", "BNB", "XRP", "ETH", "LTC", "USDC", "USDG", "TRX"])}); export const address = z.object({addressLine1: z.string().max(35).optional(), addressLine2: z.string().max(35).optional(), region: z.string().optional(), city: z.string().optional(), country: z.string(), postCode: z.string().optional()}); export const fiatLedger = z.object({type: z.literal("FIAT"), accountHolderName: z.string().optional(), accountNumber: z.string(), code: z.string(), accountNumberFormat: z.string().optional(), paymentReference: z.string().optional(), bank: z.object({identificationCode: z.string().optional(), name: z.string().optional(), address: address.optional()}).optional()}); export const cryptoLedger = z.object({type: z.literal("CRYPTO"), address: z.string(), network: z.string(), tag: z.string().optional()}); export const ledger = z.discriminatedUnion("type", [fiatLedger, cryptoLedger]); export const walletResponse = z.object({id: z.string(), accountReference: z.string(), customerReference: z.string(), name: z.string(), status: z.enum(["ACTIVE", "INACTIVE", "TERMINATED"]), balance: balance, ledgers: z.array(ledger), capabilities: z.literal("SAFEGUARDED").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 fiat wallet * * POST /ledger/v1/wallets */ export const createApiLedgerV1Wallets = async (args: CreateApiLedgerV1WalletsArgs, options?: CreateApiLedgerV1WalletsOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Idempotency-Key'] != null) headers.set("X-Idempotency-Key", String(args['X-Idempotency-Key'])) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/v1/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(walletResponse, await res.json()) };