Skip to content

Instantly share code, notes, and snippets.

@hugeblank
Last active December 6, 2024 23:18
Show Gist options
  • Save hugeblank/1758d6eba0e28f456fa6f0f39079279b to your computer and use it in GitHub Desktop.
Save hugeblank/1758d6eba0e28f456fa6f0f39079279b to your computer and use it in GitHub Desktop.
Somewhat futureproof helper function that resolves all blessed ATProto DID methods
const resolvers: Map<string, (structure: string) => string> = new Map([
["plc", (structure) => {
return `https://plc.directory/did:plc:${structure}`
}],
["web", (structure) => {
return `https://${structure}/.well-known/did.json`
}],
// Room for future DID methods
])
async function resolveDID(did: string) {
const [type, method, structure] = did.split(":") // Break up DID into components
if (type !== "did") throw new Error("Invalid DID"); // Drop anything that isn't a DID
if (!resolvers.has(method)) throw new Error(`Method ${method} is not blessed`); // Drop methods that aren't blessed
const url = resolvers.get(method)!(structure) // Get resolver and throw DID structure at it
const response = await fetch(url) // Fetch from the URL given
if (!response.ok) throw new Error(response.statusText) // Throw an error if the fetch fails
return JSON.parse(await response.text()) // Parse the document and return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment