@@ -4,19 +4,18 @@ import io.ktor.client.HttpClient
4
4
import io.ktor.client.call.body
5
5
import io.ktor.client.request.get
6
6
7
- public class DIDDocumentResolver public constructor(
8
- private val httpClient : HttpClient
9
- ) {
7
+ public interface DIDDocumentResolver {
10
8
11
- public suspend fun resolve (did : DID ): DIDDocument =
12
- when (val method = did.method) {
13
- " web" -> resolveDidWeb(did)
14
- " plc" -> resolveDidPlc(did)
15
- // TODO: Possibly support other well known DID methods.
16
- else -> error(" Unsupported DID method '$method '" )
17
- }
9
+ public suspend fun resolve (did : DID ): DIDDocument
10
+
11
+ public companion object
12
+ }
18
13
19
- private suspend fun resolveDidWeb (did : DID ): DIDDocument {
14
+ public class DIDWebDocumentResolver public constructor(
15
+ private val httpClient : HttpClient
16
+ ) : DIDDocumentResolver {
17
+
18
+ override suspend fun resolve (did : DID ): DIDDocument {
20
19
val identifier = did.id ? : error(" DID id part was required but was missing." )
21
20
22
21
// Convert identifier to URL (e.g., chris.keenan -> https://chris.keenan/.well-known/did.json)
@@ -40,7 +39,18 @@ public class DIDDocumentResolver public constructor(
40
39
}
41
40
}
42
41
43
- private suspend fun resolveDidPlc (did : DID ): DIDDocument {
42
+ private fun DIDDocument.validateDidDocument (did : DID ) {
43
+ if (this .id != did.value) {
44
+ error(" DID document id '${this .id} ' does not match expected DID '${did.value} '." )
45
+ }
46
+ }
47
+ }
48
+
49
+ public class DIDPlcDocumentResolver public constructor(
50
+ private val httpClient : HttpClient
51
+ ) : DIDDocumentResolver {
52
+
53
+ override suspend fun resolve (did : DID ): DIDDocument {
44
54
// Query PLC directory (e.g., https://plc.directory/did:plc:abc123)
45
55
val url = " https://plc.directory/${did.value} "
46
56
@@ -57,3 +67,19 @@ public class DIDDocumentResolver public constructor(
57
67
}
58
68
}
59
69
}
70
+
71
+ public class DefaultDIDDocumentResolver public constructor(
72
+ httpClient : HttpClient
73
+ ) : DIDDocumentResolver {
74
+
75
+ private val didWebDocumentResolver = DIDWebDocumentResolver (httpClient = httpClient)
76
+ private val didPlcDocumentResolver = DIDPlcDocumentResolver (httpClient = httpClient)
77
+
78
+ override suspend fun resolve (did : DID ): DIDDocument =
79
+ when (val method = did.method) {
80
+ " web" -> didWebDocumentResolver.resolve(did)
81
+ " plc" -> didPlcDocumentResolver.resolve(did)
82
+ // TODO: Possibly support other well known DID methods.
83
+ else -> error(" Unsupported DID method '$method '" )
84
+ }
85
+ }
0 commit comments