import {z} from 'zod' export type GetApiApiV1KmsKmsIdInfoArgs = {kms_id: string}; export type GetApiApiV1KmsKmsIdInfoOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bootstrapInfoOut = z.object({k256_pubkey: z.string(), ca_pubkey: z.string(), quote_length: z.number().int(), eventlog_length: z.number().int()}); export const kmsRpcInfo = z.object({version: z.string(), is_dev: z.boolean(), chain_id: z.number().int().nullable().optional(), kms_contract_address: z.string().nullable().optional(), gateway_app_id: z.string().nullable().optional(), bootstrap_info: bootstrapInfoOut, version_synced: z.boolean()}); export const guestAgentInfo = z.object({device_id: z.string(), mr_aggregated: z.string().nullable().optional(), os_image_hash: z.string().nullable().optional(), instance_id: z.string().nullable().optional(), app_id: z.string().nullable().optional(), mr_key_provider: z.string().nullable().optional(), compose_hash: z.string().nullable().optional(), app_compose: z.string().nullable().optional()}); export const teepodImageInfo = z.object({name: z.string(), version: z.string(), is_dev: z.boolean()}); export const teepodInfo = z.object({id: z.number().int(), name: z.string(), teepod_version_db: z.string().nullable().optional(), teepod_version_real: z.string().nullable().optional(), kms_url: z.string().nullable().optional(), kms_urls: z.array(z.string()).optional(), available_os_images: z.array(teepodImageInfo).optional(), error: z.string().nullable().optional()}); export const onChainKmsInfo = z.object({k256_pubkey: z.string(), ca_pubkey: z.string(), quote_length: z.number().int(), eventlog_length: z.number().int()}); export const onChainVerification = z.object({k256_pubkey_match: z.boolean().nullable().optional(), ca_pubkey_match: z.boolean().nullable().optional(), gateway_app_id_match: z.boolean().nullable().optional(), device_id_allowed: z.boolean().nullable().optional(), os_image_allowed: z.boolean().nullable().optional(), mr_aggregated_allowed: z.boolean().nullable().optional()}); export const onChainInfo = z.object({kms_info: onChainKmsInfo.nullable().optional(), gateway_app_id: z.string().nullable().optional(), owner: z.string().nullable().optional(), verification: onChainVerification.optional(), error: z.string().nullable().optional()}); export const kmsInspectResponse = z.object({id: z.union([z.number().int(), z.string()]), slug: z.string().nullable(), endpoint: z.string(), kms_type: z.string(), db_version: z.string(), rpc: kmsRpcInfo.nullable().optional(), rpc_error: z.string().nullable().optional(), guest_agent: guestAgentInfo.nullable().optional(), guest_agent_error: z.string().nullable().optional(), teepods: z.array(teepodInfo).optional(), on_chain: onChainInfo.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 }; /** * Inspect KMS instance * * GET /api/v1/kms/{kms_id}/info */ export const getApiApiV1KmsKmsIdInfo = async (args: GetApiApiV1KmsKmsIdInfoArgs, options?: GetApiApiV1KmsKmsIdInfoOptions) => { 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/${encodeURIComponent(String(args.kms_id))}/info${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(kmsInspectResponse, await res.json()) };