import {z} from 'zod' export type BlockchainNetwork = 'TRON' | 'ETHEREUM' | 'LITECOIN' | 'BITCOIN' | 'RIPPLE' | 'DOGECOIN' | 'SOLANA' | 'ALGORAND' | 'CARDANO' | 'BINANCE' | 'POLYGON' | 'BITCOIN_CASH' | 'ARC' | 'ARBITRUM' | 'BASE'; export type LedgerWalletV2CreateRequest = {customerId?: string | undefined, currency: string, name: string, profileId?: string | undefined, network?: BlockchainNetwork | undefined}; export type CreateApiLedgerV2WalletsArgs = {'Idempotency-Key': string, body: LedgerWalletV2CreateRequest}; export type CreateApiLedgerV2WalletsOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const ledgerWalletV2Customer = z.object({id: z.string().optional(), name: z.string().optional()}); export const ledgerWalletV2BankAddress = z.object({addressLine1: z.string().optional(), addressLine2: z.string().optional(), city: z.string().optional(), country: z.string().optional(), stateCode: z.string().optional(), postCode: z.string().optional(), fullAddress: z.string().optional()}); export const ledgerWalletV2BankNid = z.object({value: z.string(), type: z.enum(["ROUTING_NUMBER", "SORT_CODE", "OTHER"]).optional()}); export const ledgerWalletV2Bank = z.object({name: z.string(), address: ledgerWalletV2BankAddress.optional(), bic: z.string(), nid: ledgerWalletV2BankNid.optional()}); export const ledgerWalletV2FiatLedger = z.object({type: z.literal("FIAT"), accountHolderName: z.string(), accountHolderAddress: ledgerWalletV2BankAddress.optional(), accountNumber: z.string(), remittanceInformationPrefix: z.string().optional(), bankDetails: ledgerWalletV2Bank.optional()}); export const blockchainNetwork = z.enum(["TRON", "ETHEREUM", "LITECOIN", "BITCOIN", "RIPPLE", "DOGECOIN", "SOLANA", "ALGORAND", "CARDANO", "BINANCE", "POLYGON", "BITCOIN_CASH", "ARC", "ARBITRUM", "BASE"]); export const ledgerWalletV2CryptoLedger = z.object({address: z.string(), network: blockchainNetwork, tag: z.string().nullable().optional()}); export const ledgerWalletV2Response = z.object({id: z.string(), name: z.string(), customer: ledgerWalletV2Customer.optional(), status: z.enum(["ACTIVE", "INACTIVE", "TERMINATED"]), balance: z.object({amount: z.number(), currency: z.string()}).optional(), paymentInstruments: z.array(z.union([ledgerWalletV2FiatLedger, ledgerWalletV2CryptoLedger])).optional(), createdAt: z.string().optional(), updatedAt: z.string().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 wallet * * POST /ledger/v2/wallets */ export const createApiLedgerV2Wallets = async (args: CreateApiLedgerV2WalletsArgs, options?: CreateApiLedgerV2WalletsOptions) => { 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['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/ledger/v2/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(ledgerWalletV2Response, await res.json()) };