import {z} from 'zod' export type GetApiApiV1KmsOnChainChainArgs = {chain: string}; export type GetApiApiV1KmsOnChainChainOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const onChainDeviceOut = z.object({device_id: z.string(), node_name: z.string().nullable().optional(), on_chain_allowed: z.boolean().nullable().optional()}); export const onChainOsImageOut = z.object({name: z.string(), version: z.string(), os_image_hash: z.string().nullable().optional(), on_chain_allowed: z.boolean().nullable().optional()}); export const onChainKmsContractOut = z.object({contract_address: z.string(), chain_id: z.number().int(), chain_name: z.string(), k256_pubkey: z.string().nullable().optional(), ca_pubkey: z.string().nullable().optional(), gateway_contract_address: z.string().nullable().optional(), devices: z.array(onChainDeviceOut), os_images: z.array(onChainOsImageOut)}); export const onChainKmsResponse = z.object({chain_name: z.string(), chain_id: z.number().int(), contracts: z.array(onChainKmsContractOut)}); 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 on-chain KMS details by chain * * GET /api/v1/kms/on-chain/{chain} */ export const getApiApiV1KmsOnChainChain = async (args: GetApiApiV1KmsOnChainChainArgs, options?: GetApiApiV1KmsOnChainChainOptions) => { const {baseUrl = "https://cloud-api.phala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/kms/on-chain/${encodeURIComponent(String(args.chain))}${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(onChainKmsResponse, await res.json()) };