import {z} from 'zod' export type WalletAddressFormat = 'BITCOIN_P2PKH' | 'BITCOIN_P2WPKH' | 'EVM' | 'TRON_BASE58' | 'BASE58' | 'TON_NON_BOUNCEABLE' | 'SUBSTRATE_58' | 'BITCOIN_P2TR'; export type V2WalletAddressType = 'MAIN' | 'DEPOSIT' | 'CHANGE'; export type BTCDetailsAddressFormat = 'P2PKH' | 'P2WPKH' | 'P2TR'; export type V2WalletAddressCreateParamsBTCDetails = {format: BTCDetailsAddressFormat}; export type V2WalletAddressCreateParams = {btcDetails?: V2WalletAddressCreateParamsBTCDetails | undefined}; export type V2WalletAddress = {name?: string | undefined, displayName?: string | undefined, network: string, address?: string | undefined, format?: WalletAddressFormat | undefined, key?: string | undefined, keyDerivationPath?: Array | undefined, type?: V2WalletAddressType | undefined, note?: string | undefined, createParams?: V2WalletAddressCreateParams | undefined}; export type CreateApiV2VaultsVaultIdWalletsWalletIdAddressesArgs = {vault_id: string, wallet_id: string, body: V2WalletAddress}; export type CreateApiV2VaultsVaultIdWalletsWalletIdAddressesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const walletAddressFormat = z.enum(["BITCOIN_P2PKH", "BITCOIN_P2WPKH", "EVM", "TRON_BASE58", "BASE58", "TON_NON_BOUNCEABLE", "SUBSTRATE_58", "BITCOIN_P2TR"]); export const v2WalletAddressType = z.enum(["MAIN", "DEPOSIT", "CHANGE"]); export const bTCDetailsAddressFormat = z.enum(["P2PKH", "P2WPKH", "P2TR"]); export const v2WalletAddressCreateParamsBTCDetails = z.object({format: bTCDetailsAddressFormat}); export const v2WalletAddressCreateParams = z.object({btcDetails: v2WalletAddressCreateParamsBTCDetails.optional()}); export const v2WalletAddress = z.object({name: z.string().optional(), displayName: z.string().optional(), network: z.string(), address: z.string().optional(), format: walletAddressFormat.optional(), key: z.string().optional(), keyDerivationPath: z.array(z.string()).optional(), type: v2WalletAddressType.optional(), note: z.string().optional(), createParams: v2WalletAddressCreateParams.optional()}); export const v2CreateWalletAddressResponse = z.object({walletAddress: v2WalletAddress.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 }; /** * CreateWalletAddress * * POST /v2/vaults/{vault_id}/wallets/{wallet_id}/addresses */ export const createApiV2VaultsVaultIdWalletsWalletIdAddresses = async (args: CreateApiV2VaultsVaultIdWalletsWalletIdAddressesArgs, options?: CreateApiV2VaultsVaultIdWalletsWalletIdAddressesOptions) => { const {baseUrl = "https://api.utila.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/vaults/${encodeURIComponent(String(args.vault_id))}/wallets/${encodeURIComponent(String(args.wallet_id))}/addresses${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(v2CreateWalletAddressResponse, await res.json()) };