import {z} from 'zod' export type CoinNonEmptyString = string; export type WalletIdNonEmptyString = string; export type GetApiApiV2CoinWalletWalletIdShieldedunspentsArgs = {limit?: unknown | undefined, prevId?: string | undefined, spendState?: ('UNSPENT' | 'PENDING_SPEND' | 'SPENT') | undefined, minValue?: string | undefined, coin: CoinNonEmptyString, walletId: WalletIdNonEmptyString}; export type GetApiApiV2CoinWalletWalletIdShieldedunspentsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const shieldedSpendState = z.enum(["UNSPENT", "PENDING_SPEND", "SPENT"]); export const shieldedNoteDirection = z.enum(["incoming", "outgoing"]); export const walletShieldedUnspentsResponse = z.object({notes: z.array(z.object({id: z.string(), address: z.string(), valueString: z.string(), blockHeight: z.number(), date: z.string(), spendState: shieldedSpendState, hasLeafPosition: z.boolean(), txid: z.string(), direction: shieldedNoteDirection})), nextBatchPrevId: 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 }; /** * List a wallet's shielded notes * * GET /api/v2/{coin}/wallet/{walletId}/shieldedunspents */ export const getApiApiV2CoinWalletWalletIdShieldedunspents = async (args: GetApiApiV2CoinWalletWalletIdShieldedunspentsArgs, options: GetApiApiV2CoinWalletWalletIdShieldedunspentsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.prevId != null) query.append("prevId", String(args.prevId)) if (args.spendState != null) query.append("spendState", String(args.spendState)) if (args.minValue != null) query.append("minValue", String(args.minValue)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.coin))}/wallet/${encodeURIComponent(String(args.walletId))}/shieldedunspents${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(walletShieldedUnspentsResponse, await res.json()) };