import {z} from 'zod' export type LocalSignerRequestConfig = {key: string}; export type AwsKmsSignerRequestConfig = {key_id: string, region: string}; export type AzureKeyVaultAuthType = 'client_secret' | 'managed_identity' | 'workload_identity'; export type AzureKeyVaultSignerRequestConfig = {auth_type?: (AzureKeyVaultAuthType | null) | undefined, client_id?: (string | null) | undefined, client_secret?: (string | null) | undefined, federated_token_file?: (string | null) | undefined, key_name: string, key_version?: string | undefined, tenant_id?: (string | null) | undefined, vault_url: string}; export type VaultSignerRequestConfig = {address: string, key_name: string, mount_point?: string | undefined, namespace?: string | undefined, role_id: string, secret_id: string}; export type VaultTransitSignerRequestConfig = {address: string, key_name: string, mount_point?: string | undefined, namespace?: string | undefined, pubkey: string, role_id: string, secret_id: string}; export type TurnkeySignerRequestConfig = {api_private_key: string, api_public_key: string, organization_id: string, private_key_id: string, public_key: string}; export type CdpSignerRequestConfig = {account_address: string, api_key_id: string, api_key_secret: string, wallet_secret: string}; export type GoogleCloudKmsSignerKeyRequestConfig = {key_id: string, key_ring_id: string, key_version: number, location: string}; export type GoogleCloudKmsSignerServiceAccountRequestConfig = {auth_provider_x509_cert_url: string, auth_uri: string, client_email: string, client_id: string, client_x509_cert_url: string, private_key: string, private_key_id: string, project_id: string, token_uri: string, universe_domain: string}; export type GoogleCloudKmsSignerRequestConfig = {key: GoogleCloudKmsSignerKeyRequestConfig, service_account: GoogleCloudKmsSignerServiceAccountRequestConfig}; export type SignerConfigRequest = LocalSignerRequestConfig | AwsKmsSignerRequestConfig | AzureKeyVaultSignerRequestConfig | VaultSignerRequestConfig | VaultTransitSignerRequestConfig | TurnkeySignerRequestConfig | CdpSignerRequestConfig | GoogleCloudKmsSignerRequestConfig; export type SignerTypeRequest = 'plain' | 'aws_kms' | 'azure_key_vault' | 'vault' | 'vault_transit' | 'turnkey' | 'cdp' | 'google_cloud_kms'; export type SignerCreateRequest = {config: SignerConfigRequest, id?: string | undefined, type: SignerTypeRequest}; export type CreateApiApiV1SignersArgs = {body: SignerCreateRequest}; export type CreateApiApiV1SignersOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const azureKeyVaultAuthType = z.enum(["client_secret", "managed_identity", "workload_identity"]); export const googleCloudKmsSignerKeyResponseConfig = z.object({key_id: z.string(), key_ring_id: z.string(), key_version: z.number().int().gte(0), location: z.string()}); export const googleCloudKmsSignerServiceAccountResponseConfig = z.object({auth_provider_x509_cert_url: z.string(), auth_uri: z.string(), client_id: z.string(), client_x509_cert_url: z.string(), project_id: z.string(), token_uri: z.string(), universe_domain: z.string()}); export const signerConfigResponse = z.union([z.object({address: z.string(), key_name: z.string(), mount_point: z.string().nullable().optional(), namespace: z.string().nullable().optional()}), z.object({address: z.string(), key_name: z.string(), mount_point: z.string().nullable().optional(), namespace: z.string().nullable().optional(), pubkey: z.string()}), z.object({key_id: z.string(), region: z.string().nullable().optional()}), z.object({auth_type: azureKeyVaultAuthType, client_id: z.string().nullable().optional(), key_name: z.string(), key_version: z.string().nullable().optional(), tenant_id: z.string().nullable().optional(), vault_url: z.string()}), z.object({api_public_key: z.string(), organization_id: z.string(), private_key_id: z.string(), public_key: z.string()}), z.object({account_address: z.string(), api_key_id: z.string()}), z.object({key: googleCloudKmsSignerKeyResponseConfig, service_account: googleCloudKmsSignerServiceAccountResponseConfig}), z.object({})]); export const signerType = z.enum(["local", "aws_kms", "azure_key_vault", "google_cloud_kms", "vault", "vault_transit", "turnkey", "cdp"]); export const logLevel = z.enum(["log", "info", "error", "warn", "debug", "result"]); export const logEntry = z.object({level: logLevel, message: z.string()}); export const pluginMetadata = z.object({logs: z.array(logEntry).nullable().optional(), traces: z.array(z.unknown()).nullable().optional()}); export const paginationMeta = z.object({current_page: z.number().int().gte(0), per_page: z.number().int().gte(0), total_items: z.number().int().gte(0)}); export const apiResponseSignerResponse = z.object({data: z.object({config: signerConfigResponse, id: z.string(), type: signerType}).optional(), error: z.string().optional(), metadata: pluginMetadata.optional(), pagination: paginationMeta.optional(), success: z.boolean()}); 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 }; /** * Creates a new signer. * * POST /api/v1/signers */ export const createApiApiV1Signers = async (args: CreateApiApiV1SignersArgs, options: CreateApiApiV1SignersOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/signers${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(apiResponseSignerResponse, await res.json()) };