···11# port for the lattice server to run on.
22# defaults to 7338.
33SERVER_PORT="7338"
44+55+# used for verifying inter-service jwts
66+# you *must* specify a did at which this shard may be found. may also include a service identifier.
77+# for more information on the service identifier, you may see https://atproto.com/specs/xrpc#inter-service-authentication-jwt
88+# usually a did:web, but if you're crazy you can put a did:plc, the verifier supports either anyway.
99+# defaults to did:web:localhost
1010+SERVICE_DID="did:web:localhost"
1111+1212+# to tell if you're in dev or prod. defaults to dev.
1313+# if running in prod, set to 'production'
1414+NODE_ENV="development"
···11+import { SERVICE_DID } from "@/lib/env";
22+import type { Did } from "@/lib/types/atproto";
33+import { didDocumentSchema, didWebSchema } from "@/lib/types/atproto";
44+import type { Route, RouteHandler } from "@/lib/types/routes";
55+import { didDoc as importedDidDoc } from "@/lib/utils/didDoc";
66+import { newErrorResponse } from "@/lib/utils/http/responses";
77+import { z } from "zod";
88+99+const routeHandlerFactory = (did: Did) => {
1010+ const serveDidPlc: RouteHandler = async () => {
1111+ const plcDirectoryReq = new Request(`https://plc.directory/${did}`);
1212+ const plcDirectoryRes = await fetch(plcDirectoryReq);
1313+ const {
1414+ success,
1515+ data: didDocument,
1616+ error,
1717+ } = didDocumentSchema.safeParse(await plcDirectoryRes.json());
1818+1919+ if (!success)
2020+ return newErrorResponse(500, {
2121+ message:
2222+ "Parsing the DID document from a public ledger failed. Either the Shard's did:plc is wrong, the did:plc was not registered with a public ledger, or there is something wrong with the public ledger.",
2323+ details: z.treeifyError(error),
2424+ });
2525+2626+ return Response.json(didDocument);
2727+ };
2828+2929+ const { success: isDidWeb } = didWebSchema.safeParse(did);
3030+ if (!isDidWeb) return serveDidPlc;
3131+3232+ const serveDidDoc: RouteHandler = () => {
3333+ const didDoc = importedDidDoc;
3434+ if (!didDoc) {
3535+ return newErrorResponse(500, {
3636+ message:
3737+ "Somehow tried to serve a did:web document when no did:web document was available. Specifically, somehow parsing the same SERVICE_DID environment variable resulted in both a did:web and a not did:web",
3838+ });
3939+ }
4040+ return Response.json(didDoc);
4141+ };
4242+4343+ return serveDidDoc;
4444+};
4545+4646+export const didWebDocRoute: Route = {
4747+ method: "GET",
4848+ handler: routeHandlerFactory(SERVICE_DID),
4949+};
+2
src/routes/index.ts
···11import type { Route, WsRoute } from "@/lib/types/routes";
22+import { didWebDocRoute } from "@/routes/dot-well-known/did-dot-json/route";
23import { indexRoute } from "@/routes/route";
3445export const routes: Record<string, Route | WsRoute> = {
56 "/": indexRoute,
77+ "/.well-known/did.json": didWebDocRoute,
68};