checkAttestation

tcmts.registry.checkAttestation · #ikatg5rx · term · runs on the server

uses tcmts.registry.attestations, which uses tcmts.server.table, which uses tcmts.server.Table, which uses #tcm/host (database)

Check an attestation against its writer's signed repository (a job, queued as it arrives): only then is it shown as theirs, saying what the signed record says (not what the event did). One that doesn't check out is failed; a PDS that doesn't answer is retried, as a job is.

import { NotTheirs, resolveIdentity, signedRecord } from "#codebase/tcmts/atproto";
import { parseAttestation } from "#codebase/tcmts/records";
import { attestationFields, attestations, people } from "#codebase/tcmts/registry";
import type { RegistryAttestation } from "#codebase/tcmts/registry";

export async function checkAttestation(uri: string): Promise<void> {
  const found = await attestations.findOne({ uri });
  if (!found || found.value.state !== "pending") return;
  const [, , did, collection, rkey] = uri.split("/") as [string, string, string, string, string];
  try {
    const got = await signedRecord(did, collection, rkey);
    const a = parseAttestation(collection, got.record);
    const ok = a !== undefined;
    // (who wrote it, for showing: a handle that names the DID back, as a release's author's is found)
    const handle = await resolveIdentity(did).then(
      (i) => i.handle,
      () => undefined,
    );
    const person = await people.findOne({ did });
    if (person && handle) await people.put(person.id, { ...person.value, handle });
    await attestations.update(found.id, (v) => {
      const next: RegistryAttestation = {
        ...v,
        ...(a ? attestationFields(a) : {}),
        state: ok ? "verified" : "failed",
        ...(ok ? {} : { reason: "the signed record says something else" }),
      };
      if (ok) delete next.reason;
      return next;
    });
  } catch (e) {
    if (!(e instanceof NotTheirs)) throw e;
    await attestations.update(found.id, (v) => ({ ...v, state: "failed", reason: e.message }));
  }
}

uses

used by