-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d097ca0
commit f2a0aec
Showing
12 changed files
with
206 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
waltid-did/src/commonMain/kotlin/id/walt/did/dids/resolver/local/DidCheqdResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package id.walt.did.dids.resolver.local | ||
|
||
import id.walt.crypto.keys.Key | ||
import id.walt.did.dids.document.DidCheqdDocument | ||
import id.walt.did.dids.document.DidDocument | ||
import io.ktor.client.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.jsonObject | ||
import kotlinx.serialization.json.jsonPrimitive | ||
|
||
class DidCheqdResolver : LocalResolverMethod("cheqd") { | ||
private val httpClient = HttpClient() //TODO: inject | ||
|
||
override suspend fun resolve(did: String): Result<DidDocument> = runCatching { | ||
resolveDid(did) | ||
} | ||
|
||
override suspend fun resolveToKey(did: String): Result<Key> { | ||
TODO("Not yet implemented") | ||
// response verificationMethod contains only publicKeyMultibase | ||
// required: convert it to publicKeyJwk | ||
// (no functionality provided by crypto, only multibase58btc available) | ||
} | ||
|
||
private val json = Json() { ignoreUnknownKeys = true } | ||
|
||
private fun resolveDid(did: String): DidDocument { | ||
val response = runBlocking { | ||
httpClient.get("https://resolver.cheqd.net/1.0/identifiers/${did}") { | ||
headers { | ||
append("contentType", "application/did+ld+json") | ||
} | ||
}.bodyAsText() | ||
} | ||
val resolution = Json.decodeFromString<JsonObject>(response) | ||
|
||
val didDocument = resolution.jsonObject["didResolutionMetadata"]?.jsonObject?.get("error")?.let { | ||
throw IllegalArgumentException("Could not resolve did:cheqd, resolver responded: ${it.jsonPrimitive.content}") | ||
} ?: let { | ||
resolution.jsonObject["didDocument"]?.jsonObject | ||
?: throw IllegalArgumentException("Response for did:cheqd did not contain a DID document!") | ||
}.let { | ||
json.decodeFromString<id.walt.did.dids.registrar.local.cheqd.models.job.didstates.finished.DidDocument>( | ||
it.toString() | ||
) | ||
} | ||
return DidDocument(DidCheqdDocument(didDocument).toMap()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
waltid-did/src/jvmTest/kotlin/resolvers/DidCheqdResolverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package resolvers | ||
|
||
import id.walt.crypto.keys.Key | ||
import id.walt.did.dids.document.DidDocument | ||
import id.walt.did.dids.resolver.local.DidCheqdResolver | ||
import id.walt.did.dids.resolver.local.LocalResolverMethod | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
import org.junit.jupiter.api.Disabled | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.Arguments.arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import java.util.stream.Stream | ||
|
||
class DidCheqdResolverTest : DidResolverTestBase() { | ||
override val sut: LocalResolverMethod = DidCheqdResolver() | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
override fun `given a did String, when calling resolve, then the result is a valid did document`( | ||
did: String, | ||
key: JsonObject, | ||
assert: resolverAssertion<DidDocument> | ||
) { | ||
super.`given a did String, when calling resolve, then the result is a valid did document`(did, key, assert) | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
@Disabled // not implemented | ||
override fun `given a did String, when calling resolveToKey, then the result is valid key`( | ||
did: String, | ||
key: JsonObject, | ||
assert: resolverAssertion<Key> | ||
) { | ||
super.`given a did String, when calling resolveToKey, then the result is valid key`(did, key, assert) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun `given a did String, when calling resolve, then the result is a valid did document`(): Stream<Arguments> = | ||
Stream.of( | ||
arguments( | ||
"did:cheqd:testnet:W5a3426DZ1f4qBkYC9ZT6s", | ||
Json.decodeFromString<JsonObject>("{\"kty\":\"OKP\",\"use\":\"sig\",\"crv\":\"Ed25519\",\"kid\":\"151df6ec01714883b812f26f2d63e584\",\"x\":\"qBDsYw3k62mUT8UmEx99Xz3yckiSRmTsL6aa21ZcAVM\",\"alg\":\"EdDSA\"}"), | ||
ed25519DidAssertions | ||
) | ||
) | ||
|
||
@JvmStatic | ||
fun `given a did String, when calling resolveToKey, then the result is valid key`(): Stream<Arguments> = | ||
Stream.of( | ||
arguments( | ||
"did:cheqd:testnet:38088d21-a5f8-4277-bd35-b36918d81c14", | ||
Json.decodeFromString<JsonObject>("{\"kty\":\"OKP\",\"d\":\"24WxHxiKpnd1_BitZBU57ex8EKaNukiyC4punO4Lh-s\",\"crv\":\"Ed25519\",\"kid\":\"GaQD9pzL5wJyATB1UA2J71ygXgkykT1QOnL7uIBgcpo\",\"x\":\"bz2K3xX-D_R2_Pu7al6UCRXGSl1pzBRfEoD3bj94s_w\"}"), | ||
ed25519KeyAssertions | ||
) | ||
) | ||
} | ||
} |
Oops, something went wrong.