import {z} from 'zod' export type CreateIdentityRequest = {didMetadata: {method: string, blockchain: string, network: string, type: 'BJJ' | 'ETH'}, credentialStatusType?: ('Iden3commRevocationStatusV1.0' | 'Iden3ReverseSparseMerkleTreeProof' | 'Iden3OnchainSparseMerkleTreeProof2023') | undefined, displayName?: string | undefined}; export type CreateApiV2IdentitiesArgs = {body: CreateIdentityRequest}; export type CreateApiV2IdentitiesOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const timeUTC = z.string(); export const identityState = z.object({stateID: z.number().int(), identifier: z.string(), state: z.string().optional(), rootOfRoots: z.string().optional(), claimsTreeRoot: z.string().optional(), revocationTreeRoot: z.string().optional(), blockTimestamp: z.number().int().optional(), blockNumber: z.number().int().optional(), txID: z.string().optional(), previousState: z.string().optional(), status: z.string(), modifiedAt: timeUTC, createdAt: timeUTC}); export const createIdentityResponse = z.object({identifier: z.string().optional(), displayName: z.string().optional(), state: identityState.optional(), address: z.string().optional(), balance: z.string().optional(), keyType: z.string(), credentialStatusType: z.enum(["Iden3commRevocationStatusV1.0", "Iden3ReverseSparseMerkleTreeProof", "Iden3OnchainSparseMerkleTreeProof2023"])}); 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 }; /** * Create Identity * * POST /v2/identities */ export const createApiV2Identities = async (args: CreateApiV2IdentitiesArgs, options: CreateApiV2IdentitiesOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/identities${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(createIdentityResponse, await res.json()) };