Something went wrong. Try again.
fork of ozone with nix stuff
Something went wrong. Try again.
TypeScript
at customized
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104import { globalAgent } from './client'import { PLC_DIRECTORY_URL } from './constants'import { chunkArray } from './util'
export const getDidFromHandle = async ( handle: string,): Promise<string | null> => { try { const { data } = await globalAgent.resolveHandle({ handle }) return data.did } catch (err) { return null }}
export const getDidFromHandleInBatch = async (handles: string[]) => { const handleToDid: Record<string, string | null> = {}
for (const handleChunk of chunkArray(handles, 50)) { await Promise.all( handleChunk.map(async (handle) => { const did = await getDidFromHandle(handle) handleToDid[handle] = did }), ) }
return handleToDid}
export const resolveDidDocData = async function ( did: string, signal?: AbortSignal,): Promise<DidDocData | null> { if (did.startsWith('did:plc:')) { const url = new URL(`/${did}/data`, PLC_DIRECTORY_URL) const res = await fetch(url, { signal }) if (res.status !== 200) return null const doc = await res.json() return doc } if (did.startsWith('did:web:')) { const hostname = did.slice('did:web:'.length) const url = new URL(`https://${hostname}/.well-known/did.json`) const res = await fetch(url, { signal }) if (res.status !== 200) return null const doc = await res.json().catch(() => null) if (!doc || typeof doc !== 'object' || doc['id'] !== did) return null return didDocToData(doc) } return null}
export function didDocToData(doc: { id: string [key: string]: unknown}): DidDocData { return { did: doc.id, alsoKnownAs: Array.isArray(doc['alsoKnownAs']) ? doc['alsoKnownAs'] : [], verificationMethods: Array.isArray(doc['verificationMethod']) ? doc['verificationMethod'].reduce((acc, vm) => { if ( vm && typeof vm['id'] === 'string' && vm['type'] === 'Multikey' && typeof vm['publicKeyMultibase'] === 'string' ) { const [, id] = vm['id'].split('#') acc[id] = `did:key:${vm['publicKeyMultibase']}` } return acc }, {}) : {}, services: Array.isArray(doc['service']) ? doc['service'].reduce((acc, s) => { if ( s && typeof s['id'] === 'string' && typeof s['type'] === 'string' && typeof s['serviceEndpoint'] === 'string' ) { const [, id] = s['id'].split('#') acc[id] = { type: s['type'], serviceEndpoint: s['serviceEndpoint'], } } return acc }, {}) : {}, }}
export type DidDocData = { did: string alsoKnownAs: string[] verificationMethods: Record<string, string> services: Record< string, { type: string; serviceEndpoint?: string; endpoint?: string } >}