import type { CryptoKey } from 'jose' import type { SigningKeypair, PrivateKey, PublicKey, SigningPublicKey, } from '../../../domain/keypair.js' import { exportJWK, importJWK } from 'jose' import { SIGNING_KEYPAIR_ALGORITHM } from './algorithm.js' export function buildSerde() { return { serialize: async function serialize(keypair: { privateKey: CryptoKey publicKey: CryptoKey }) { const [privateKey, publicKey] = await Promise.all([ exportJWK(keypair.privateKey), exportJWK(keypair.publicKey), ]) const signingKeypair: SigningKeypair = { privateKey: privateKey as PrivateKey, publicKey: publicKey as SigningPublicKey, } return signingKeypair }, deserialize: async function deserialize(keypair: SigningKeypair) { const [privateKey, publicKey] = await Promise.all([ importJWK(keypair.privateKey, SIGNING_KEYPAIR_ALGORITHM), importJWK(keypair.publicKey, SIGNING_KEYPAIR_ALGORITHM), ]) return { privateKey, publicKey } as const }, deserializeSingleKey: async function deserializeSingleKey( key: PrivateKey | PublicKey, ) { return await importJWK(key, SIGNING_KEYPAIR_ALGORITHM) }, } as const }