import {z} from 'zod' export type GetApiV1PublicRacesRaceIdSeedCommitmentArgs = {race_id: string}; export type GetApiV1PublicRacesRaceIdSeedCommitmentOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const worldAttemptPublic = z.object({attempt: z.number().int(), reason: z.string().nullable().optional(), archive_sha256: z.string().nullable().optional()}); export const raceSeedCommitmentResponse = z.object({race_id: z.string(), scheme: z.string(), secret_sha256: z.string(), drand_chain_hash: z.string(), drand_offset_seconds: z.number().int(), drand_round: z.number().int(), drand_round_time: z.string(), committed_at: z.string(), drand_signature: z.string().nullable().optional(), drand_randomness: z.string().nullable().optional(), revealed: z.boolean(), secret: z.string().nullable().optional(), world_seed: z.string().nullable().optional(), world_pack_sha256: z.string().nullable().optional(), rejected_attempts: z.array(worldAttemptPublic).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 }; /** * A race's committed world seed and its drand beacon (ORO-2756) * * GET /v1/public/races/{race_id}/seed-commitment */ export const getApiV1PublicRacesRaceIdSeedCommitment = async (args: GetApiV1PublicRacesRaceIdSeedCommitmentArgs, options: GetApiV1PublicRacesRaceIdSeedCommitmentOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/public/races/${encodeURIComponent(String(args.race_id))}/seed-commitment${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(raceSeedCommitmentResponse, await res.json()) };