A decentralized music tracking and discovery platform built on AT Protocol 馃幍
at 2b044aaed2c433aca9688cbd767408dc80f1fc4f 52 lines 1.5 kB view raw
1import { IdResolver } from "@atproto/identity"; 2import type { Storage } from "unstorage"; 3import { StorageCache } from "./didUnstorageCache"; 4 5const HOUR = 60e3 * 60; 6const DAY = HOUR * 24; 7const WEEK = HOUR * 7; 8 9export function createIdResolver(kv: Storage) { 10 return new IdResolver({ 11 didCache: new StorageCache({ 12 store: kv, 13 prefix: "didCache:", 14 staleTTL: DAY, 15 maxTTL: WEEK, 16 }), 17 }); 18} 19 20export interface BidirectionalResolver { 21 resolveDidToHandle(did: string): Promise<string>; 22 resolveDidsToHandles(dids: string[]): Promise<Record<string, string>>; 23} 24 25export function createBidirectionalResolver(resolver: IdResolver) { 26 return { 27 async resolveDidToHandle(did: string): Promise<string> { 28 const didDoc = await resolver.did.resolveAtprotoData(did); 29 30 // asynchronously double check that the handle resolves back 31 resolver.handle.resolve(didDoc.handle).then((resolvedHandle) => { 32 if (resolvedHandle !== did) { 33 resolver.did.ensureResolve(did, true); 34 } 35 }); 36 return didDoc?.handle ?? did; 37 }, 38 39 async resolveDidsToHandles( 40 dids: string[], 41 ): Promise<Record<string, string>> { 42 const didHandleMap: Record<string, string> = {}; 43 const resolves = await Promise.all( 44 dids.map((did) => this.resolveDidToHandle(did).catch((_) => did)), 45 ); 46 for (let i = 0; i < dids.length; i++) { 47 didHandleMap[dids[i]] = resolves[i]; 48 } 49 return didHandleMap; 50 }, 51 }; 52}