import type { At } from '@atcute/client/lexicons'; import { CompositeDidDocumentResolver, PlcDidDocumentResolver, WebDidDocumentResolver, } from '@atcute/identity-resolver'; import { identityCache } from './sqlCache'; const identityResolve = async (did: At.Did) => { const resolver = new CompositeDidDocumentResolver({ methods: { plc: new PlcDidDocumentResolver(), web: new WebDidDocumentResolver(), }, }); if (did.startsWith('did:plc:') || did.startsWith('did:web:')) { const doc = await resolver.resolve( did as `did:plc:${string}` | `did:web:${string}`, ); return doc; } else { throw new Error(`Unsupported DID type: ${did}`); } }; export const blueskyHandleFromDid = async (did: At.Did) => { // Check cache first const cachedResult = identityCache.get(did); if (cachedResult) { return cachedResult.handle; } const doc = await identityResolve(did); if (doc.alsoKnownAs) { const handleAtUri = doc.alsoKnownAs.find((url) => url.startsWith('at://')); const handle = handleAtUri?.split('/')[2]; if (!handle) { return 'Handle not found'; } else { // Cache the result identityCache.set(did, { handle }); return handle; } } else { return 'Handle not found'; } };