Skip to content

Commit

Permalink
fix did dht resolve (#268)
Browse files Browse the repository at this point in the history
* fix did dht resolve

* spacing
  • Loading branch information
nitro-neal authored Mar 14, 2024
1 parent f3e8a88 commit ef57b72
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public object JwtUtil {
val verificationMethodId = jwt.header.keyID
val didUri = DidUri.Parser.parse(verificationMethodId)

val didResolutionResult = DidResolvers.resolve(didUri.url)
val didResolutionResult = DidResolvers.resolve(didUri.uri)

if (didResolutionResult.didResolutionMetadata.error != null) {
throw SignatureException(
"Signature verification failed: " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ class VerifiableCredentialTest {
VerifiableCredential.verify(vcJwt)
}

@Test
fun `verify does not throw an exception if vc signed with did dht is legit`() {
val keyManager = InMemoryKeyManager()
val issuerDid = DidDht.create(keyManager)
val holderDid = DidDht.create(keyManager)

val vc = VerifiableCredential.create(
type = "StreetCred",
issuer = issuerDid.uri,
subject = holderDid.uri,
data = StreetCredibility(localRespect = "high", legit = true)
)

val vcJwt = vc.sign(issuerDid)
VerifiableCredential.verify(vcJwt)
}

@Test
fun `verify handles DIDs without an assertionMethod`() {
val keyManager = InMemoryKeyManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import com.nimbusds.jwt.SignedJWT
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import web5.sdk.credentials.model.ConstraintsV2
import web5.sdk.credentials.model.FieldV2
import web5.sdk.credentials.model.InputDescriptorMapping
import web5.sdk.credentials.model.InputDescriptorV2
import web5.sdk.credentials.model.PresentationDefinitionV2
import web5.sdk.credentials.model.PresentationSubmission
import web5.sdk.crypto.AlgorithmId
import web5.sdk.crypto.InMemoryKeyManager
import web5.sdk.dids.didcore.Purpose
import web5.sdk.dids.methods.dht.CreateDidDhtOptions
import web5.sdk.dids.methods.dht.DidDht
import web5.sdk.dids.methods.jwk.DidJwk
import web5.sdk.dids.methods.key.DidKey
import java.security.SignatureException
import java.text.ParseException
Expand Down Expand Up @@ -254,4 +259,77 @@ class VerifiablePresentationTest {
"Verification Method with an Assertion verification relationship", exception.message
)
}

data class EmploymentStatus(val employmentStatus: String)
data class PIICredential(val name: String, val dateOfBirth: String)

@Test
fun `full flow with did dht`() {
val keyManager = InMemoryKeyManager()
val issuerDid = DidDht.create(keyManager)
val holderDid = DidDht.create(keyManager)

val vc = VerifiableCredential.create(
type = "EmploymentCredential",
issuer = issuerDid.uri,
subject = holderDid.uri,
data = EmploymentStatus(employmentStatus = "employed")
)

val vc2 = VerifiableCredential.create(
type = "PIICredential",
issuer = issuerDid.uri,
subject = holderDid.uri,
data = PIICredential(name = "Alice Smith", dateOfBirth = "2001-12-21T17:02:01Z")
)

val vcJwt1 = vc.sign(issuerDid)
val vcJwt2 = vc2.sign(issuerDid)

val presentationDefinition = PresentationDefinitionV2(
id = "presDefIdloanAppVerification123",
name = "Loan Application Employment Verification",
purpose = "To verify applicant’s employment, date of birth, and name",
inputDescriptors = listOf(
InputDescriptorV2(
id = "employmentVerification",
purpose = "Confirm current employment status",
constraints = ConstraintsV2(
fields = listOf(FieldV2(path = listOf("$.vc.credentialSubject.employmentStatus")))
)
),
InputDescriptorV2(
id = "dobVerification",
purpose = "Confirm the applicant’s date of birth",
constraints = ConstraintsV2(
fields = listOf(FieldV2(path = listOf("$.vc.credentialSubject.dateOfBirth")))
)
),
InputDescriptorV2(
id = "nameVerification",
purpose = "Confirm the applicant’s legal name",
constraints = ConstraintsV2(
fields = listOf(FieldV2(path = listOf("$.vc.credentialSubject.name")))
)
)
)
)

val presentationResult = PresentationExchange.createPresentationFromCredentials(
vcJwts= listOf(vcJwt1, vcJwt2),
presentationDefinition= presentationDefinition
)

val verifiablePresentation = VerifiablePresentation.create(
vcJwts = listOf(vcJwt1, vcJwt2),
holder = holderDid.uri,
additionalData = mapOf("presentation_submission" to presentationResult)
)

val vpJwt = verifiablePresentation.sign(holderDid)

assertDoesNotThrow {
VerifiablePresentation.verify(vpJwt)
}
}
}

0 comments on commit ef57b72

Please sign in to comment.