import {z} from 'zod' export type CreateSigningKeyDto = {signingDeviceKeyId: string, signedCertPem: string, agentUserId: string, proofOfOwnership?: {message: string, signature: string} | undefined}; export type CreateApiKeyLinkSigningKeysArgs = {'Idempotency-Key'?: string | undefined, body: CreateSigningKeyDto}; export type CreateApiKeyLinkSigningKeysOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const signingKeyDto = z.object({keyId: z.string().min(36).max(36), signingDeviceKeyId: z.string().max(256), publicKeyPem: z.string(), algorithm: z.string(), enabled: z.boolean(), vaultAccountId: z.number().nullable(), agentUserId: z.string().min(36).max(36), createdAt: z.number()}); 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 }; /** * Add a new signing key * * POST /key_link/signing_keys */ export const createApiKeyLinkSigningKeys = async (args: CreateApiKeyLinkSigningKeysArgs, options?: CreateApiKeyLinkSigningKeysOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/key_link/signing_keys${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(signingKeyDto, await res.json()) };