forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {type Did} from '@atproto/api'
2
3export function getDidDocumentUrl(
4 did: Did,
5 plcDirectory: string,
6): string | undefined {
7 if (did.startsWith('did:plc:')) {
8 return `${plcDirectory}/${did}`
9 }
10
11 if (!did.startsWith('did:web:')) {
12 return undefined
13 }
14
15 const msid = did.slice('did:web:'.length)
16 if (!msid) {
17 return undefined
18 }
19
20 const [hostEnc, ...pathSegments] = msid.split(':')
21 if (!hostEnc) {
22 return undefined
23 }
24
25 const host = hostEnc.replace(/%3A/gi, ':')
26 const protocol =
27 host.startsWith('localhost') &&
28 (host.length === 'localhost'.length ||
29 host.charAt('localhost'.length) === ':')
30 ? 'http'
31 : 'https'
32 const path =
33 pathSegments.length > 0
34 ? `/${pathSegments.join('/')}/did.json`
35 : '/.well-known/did.json'
36
37 return `${protocol}://${host}${path}`
38}