import {z} from 'zod' export type GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeAddressesUnusedArgs = {cryptoCode: string, derivationScheme: string, feature?: ('Deposit' | 'Change' | 'Direct' | 'Custom') | undefined, skip?: number | undefined, reserve?: boolean | undefined, autoTrack?: boolean | undefined}; export type GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeAddressesUnusedOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const keyInformation = z.object({trackedSource: z.string().nullable().optional(), feature: z.string().optional(), derivationStrategy: z.string().optional(), keyPath: z.string().nullable().optional(), index: z.number().int().nullable().optional(), scriptPubKey: z.string().optional(), address: z.string().optional(), redeem: z.string().nullable().optional(), blindingKey: z.string().nullable().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 }; /** * Get a new unused address * * GET /v1/cryptos/{cryptoCode}/derivations/{derivationScheme}/addresses/unused */ export const getApiV1CryptosCryptoCodeDerivationsDerivationSchemeAddressesUnused = async (args: GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeAddressesUnusedArgs, options: GetApiV1CryptosCryptoCodeDerivationsDerivationSchemeAddressesUnusedOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.feature != null) query.append("feature", String(args.feature)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.reserve != null) query.append("reserve", String(args.reserve)) if (args.autoTrack != null) query.append("autoTrack", String(args.autoTrack)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/cryptos/${encodeURIComponent(String(args.cryptoCode))}/derivations/${encodeURIComponent(String(args.derivationScheme))}/addresses/unused${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(keyInformation, await res.json()) };