1import {
2 CompositeHandleResolver,
3 DohJsonHandleResolver,
4 WellKnownHandleResolver,
5} from "@atcute/identity-resolver";
6import { query } from "@solidjs/router";
7import { slingshotUrl } from "./microcosm";
8import type { DID } from "./types";
9
10const useSlingshot: boolean = true;
11
12const didMap = new Map<string, DID>();
13
14const handleResolver = new CompositeHandleResolver({
15 strategy: "race",
16 methods: {
17 dns: new DohJsonHandleResolver({
18 dohUrl: "https://mozilla.cloudflare-dns.com/dns-query",
19 }),
20 http: new WellKnownHandleResolver(),
21 },
22});
23
24export const figureOutDid = query(async (user: string): Promise<DID> => {
25 const isDid = user.startsWith("did:");
26 if (!isDid && user.includes(".")) {
27 let did = didMap.get(user);
28 if (!did && useSlingshot) {
29 const res = await fetch(
30 `${slingshotUrl}/xrpc/com.atproto.identity.resolveHandle?handle=${user}`,
31 );
32 if (res.ok) {
33 const json = await res.json();
34 if ("did" in json && typeof json.did === "string") did = json.did;
35 }
36 }
37 if (!did) did = await handleResolver.resolve(user as `${string}.${string}`);
38 didMap.set(user, did);
39 return did;
40 }
41 return user as DID;
42}, "handles");