// Resolve a DID's #atproto signing pubkey (the verifyKey) from the PLC directory.
// MODULE NOTE (feedback_ask-should-this-be-a-module): the bridge resolves the same (peer-router
// resolvePlcVerifyKey). Kept favela-local for now; fold into an atproto-identity PLC helper when the
// bridge migrates. `fetch` is injectable (bare-fetch under Bare, global fetch under Node).
import { decodeDidKey } from 'atproto-identity/lib/did-key.js'
export async function resolveVerifyKey (did, { fetch: doFetch = fetch, plcUrl = 'https://plc.directory' } = {}) {
const res = await doFetch(`${plcUrl}/${did}`)
if (!res.ok) throw new Error(`PLC ${res.status} for ${did}`)
const doc = await res.json()
const vm = (doc.verificationMethod || []).find((m) => m.id && m.id.endsWith('#atproto')) || (doc.verificationMethod || [])[0]
if (!vm || !vm.publicKeyMultibase) throw new Error(`no #atproto verificationMethod for ${did}`)
return decodeDidKey(`did:key:${vm.publicKeyMultibase}`).pubkey
}