import {z} from 'zod' export type GetApiProofV2CardanoTransactionArgs = {transaction_hashes: Array}; export type GetApiProofV2CardanoTransactionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const cardanoTransaction = z.object({transaction_hash: z.string(), block_hash: z.string(), block_number: z.number().int(), slot_number: z.number().int()}); export const cardanoTransactionProofV2Message = z.object({certificate_hash: z.string(), certified_transactions: z.union([z.unknown(), z.object({items: z.array(cardanoTransaction), proof: z.string()})]), non_certified_transactions: z.array(z.string()), latest_block_number: z.number().int(), security_parameter: z.number().int()}); 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 }; /** * (Unstable) Get the proofs of a Cardano transaction list from Cardano Blocks and Transactions snapshots * * GET /proof/v2/cardano-transaction */ export const getApiProofV2CardanoTransaction = async (args: GetApiProofV2CardanoTransactionArgs, options?: GetApiProofV2CardanoTransactionOptions) => { const {baseUrl = "https://aggregator.release-preprod.api.mithril.network/aggregator", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.transaction_hashes != null) query.append("transaction_hashes", args.transaction_hashes.join(',')) const url = `${baseUrl.replace(/\/+$/, '')}/proof/v2/cardano-transaction${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(cardanoTransactionProofV2Message, await res.json()) };