import type { EncryptionKeypair } from '../../domain/keypair.js' import type { Result } from '../../framework/types/result.js' import { BaseError } from '../../framework/error/mod.js' import { makeError, makeSuccess } from '../../framework/types/result.js' import { generateKeypair } from './internal/generate-keypair.js' import { buildSerde } from './internal/serde.js' export class CreateEncryptionKeypairError extends BaseError { public readonly _tag = 'CreateEncryptionKeypairError' constructor(public readonly cause: unknown) { super(`failed to create encryption keypair: ${cause}`) } } export function buildCreateKeypair() { return async function createKeypair(): Promise< Result > { try { const keypair = await generateKeypair() const serde = buildSerde() const { privateKey, publicKey } = await serde.serialize(keypair) return makeSuccess({ privateKey, publicKey } as const) } catch (cause: unknown) { return makeError(new CreateEncryptionKeypairError(cause)) } } }