Skip to content

Commit

Permalink
Update did:dht resolution on invalid uri (#237)
Browse files Browse the repository at this point in the history
* fix jsonwebkey issue

* typo

* update dht resolution

* merge and update

* new unit test
  • Loading branch information
nitro-neal authored Mar 21, 2024
1 parent fef88b9 commit 7d58412
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ class Web5TestVectorsCredentials {
val testVectors = mapper.readValue(File("../web5-spec/test-vectors/credentials/create.json"), typeRef)

testVectors.vectors.filterNot { it.errors ?: false }.forEach { vector ->
println(vector.description)
val vc = VerifiableCredential.fromJson(mapper.writeValueAsString(vector.input.credential))

val keyManager = InMemoryKeyManager()
Expand All @@ -288,7 +287,6 @@ class Web5TestVectorsCredentials {
val testVectors = mapper.readValue(File("../web5-spec/test-vectors/credentials/verify.json"), typeRef)

testVectors.vectors.filterNot { it.errors ?: false }.forEach { vector ->
println(vector.description)
assertDoesNotThrow {
VerifiableCredential.verify(vector.input.vcJwt)
}
Expand Down
1 change: 1 addition & 0 deletions dids/src/main/kotlin/web5/sdk/dids/ResolutionError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum class ResolutionError(public val value: String) {
METHOD_NOT_SUPPORTED("methodNotSupported"),
NOT_FOUND("notFound"),
INVALID_DID("invalidDid"),
INVALID_PUBLIC_KEY("invalidPublicKey"),
INTERNAL_ERROR("internalError"),
}
5 changes: 4 additions & 1 deletion dids/src/main/kotlin/web5/sdk/dids/methods/dht/DidDht.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import web5.sdk.dids.didcore.VerificationMethod
import web5.sdk.dids.exceptions.InvalidIdentifierException
import web5.sdk.dids.exceptions.InvalidIdentifierSizeException
import web5.sdk.dids.exceptions.InvalidMethodNameException
import web5.sdk.dids.exceptions.ParserException
import web5.sdk.dids.exceptions.PkarrRecordNotFoundException
import web5.sdk.dids.exceptions.PublicKeyJwkMissingException
import web5.sdk.dids.validateKeyMaterialInsideKeyManager
Expand Down Expand Up @@ -225,12 +226,14 @@ public sealed class DidDhtApi(configuration: DidDhtConfiguration) : DidMethod<Di
private fun resolveInternal(did: String): DidResolutionResult {
try {
validate(did)
} catch (_: ParserException) {
return DidResolutionResult.fromResolutionError(ResolutionError.INVALID_DID)
} catch (_: InvalidMethodNameException) {
return DidResolutionResult.fromResolutionError(ResolutionError.METHOD_NOT_SUPPORTED)
} catch (_: InvalidIdentifierSizeException) {
return DidResolutionResult.fromResolutionError(ResolutionError.INVALID_DID)
} catch (_: InvalidIdentifierException) {
return DidResolutionResult.fromResolutionError(ResolutionError.INVALID_DID)
return DidResolutionResult.fromResolutionError(ResolutionError.INVALID_PUBLIC_KEY)
}
val getId = DidDht.suffix(did)
val bep44Message = try {
Expand Down
6 changes: 6 additions & 0 deletions dids/src/test/kotlin/web5/sdk/dids/methods/dht/DidDhtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ class Web5TestVectorsDidDht {
assertEquals("invalidDid", result.didResolutionMetadata.error)
}

@Test
fun `resolve fails with an invalid did`() {
val result = DidDht.resolve("this-is-an-invalid-did")
assertEquals("invalidDid", result.didResolutionMetadata.error)
}

@Test
fun resolve() {
val typeRef = object : TypeReference<TestVectors<ResolveTestInput, DidResolutionResult>>() {}
Expand Down

0 comments on commit 7d58412

Please sign in to comment.