resolveIdentity

tcmts.atproto.resolveIdentity · #zzvsiec3 · term · runs on the server

uses tcmts.atproto.pdsOf, which uses tcmts.atproto.localNetwork, which uses process

Who a handle or DID is. A handle counts only both ways: it names the DID, and the DID's document names it back; a DID's handle is left out when it doesn't.

import { didDocument, pdsOf, Refused, resolveHandle } from "#codebase/tcmts/atproto";
import type { Identity } from "#codebase/tcmts/atproto";

export async function resolveIdentity(who: string): Promise<Identity> {
  const id = who.trim().replace(/^@/, "");
  const did = id.startsWith("did:") ? id : await resolveHandle(id);
  const doc = await didDocument(did);
  const pds = pdsOf(doc, did);
  const claimed = doc.alsoKnownAs?.find((a) => a.startsWith("at://"))?.slice("at://".length);
  if (!id.startsWith("did:")) {
    if (claimed?.toLowerCase() !== id.toLowerCase())
      throw new Refused(
        `${id} names ${did}, but ${did} doesn't name ${id} back${claimed ? ` (it's ${claimed})` : ""}`,
      );
    return { did, handle: claimed, pds };
  }
  const handle =
    claimed && (await resolveHandle(claimed).catch(() => undefined)) === did ? claimed : undefined;
  return { did, ...(handle ? { handle } : {}), pds };
}

uses

used by