import {z} from 'zod' export type GetApiV2LiteserverGetBlockProofArgs = {known_block: string, target_block?: string | undefined, mode: number}; export type GetApiV2LiteserverGetBlockProofOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const blockRaw = z.object({file_hash: z.string(), root_hash: z.string(), seqno: z.number().int(), shard: z.string(), workchain: z.number().int()}); export const getApiV2LiteserverGetBlockProofResponse = z.object({complete: z.boolean(), from: blockRaw, steps: z.array(z.object({lite_server_block_link_back: z.object({dest_proof: z.string(), from: blockRaw, proof: z.string(), state_proof: z.string(), to: blockRaw, to_key_block: z.boolean()}), lite_server_block_link_forward: z.object({config_proof: z.string(), dest_proof: z.string(), from: blockRaw, signatures: z.object({catchain_seqno: z.number().int(), signatures: z.array(z.object({node_id_short: z.string(), signature: z.string()})), validator_set_hash: z.number().int()}), to: blockRaw, to_key_block: z.boolean()})})), to: blockRaw}); 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 raw block proof * * GET /v2/liteserver/get_block_proof */ export const getApiV2LiteserverGetBlockProof = async (args: GetApiV2LiteserverGetBlockProofArgs, options?: GetApiV2LiteserverGetBlockProofOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.known_block != null) query.append("known_block", String(args.known_block)) if (args.target_block != null) query.append("target_block", String(args.target_block)) if (args.mode != null) query.append("mode", String(args.mode)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/liteserver/get_block_proof${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(getApiV2LiteserverGetBlockProofResponse, await res.json()) };