This repository has no description
1// src/utils/didUtils.js
2
3export async function resolveDIDToHandle(did) {
4 try {
5 // Only process if it's a DID
6 if (!did.startsWith('did:')) {
7 return null;
8 }
9
10 // Fetch the DID document from PLC directory
11 const response = await fetch(`https://plc.directory/${encodeURIComponent(did)}`);
12 if (!response.ok) {
13 throw new Error('Failed to fetch DID document');
14 }
15
16 const data = await response.json();
17
18 // Look for alsoKnownAs array
19 if (!data.alsoKnownAs || !Array.isArray(data.alsoKnownAs) || data.alsoKnownAs.length === 0) {
20 throw new Error('No aliases found for this DID');
21 }
22
23 // Find the first at:// handle
24 const handle = data.alsoKnownAs
25 .find(alias => alias.startsWith('at://'))
26 ?.replace('at://', '');
27
28 if (!handle) {
29 throw new Error('No valid handle found for this DID');
30 }
31
32 return handle;
33 } catch (error) {
34 console.error('Error resolving DID:', error);
35 throw error;
36 }
37 }
38
39 export function isDID(input) {
40 return input.startsWith('did:plc:') || input.startsWith('did:web:');
41 }