From 8cf6145255999452916f0ba0c5d3fa9cce23bf8c Mon Sep 17 00:00:00 2001 From: Lijo George <81345608+lijogeorgep@users.noreply.github.com> Date: Tue, 5 Nov 2024 19:22:43 +0530 Subject: [PATCH] Fix: #2 - Support for optional parameter inside fields --- .../PresentationExchange.kt | 4 ++- .../PresentationExchangeTest.kt | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/src/main/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchange.kt b/lib/src/main/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchange.kt index 5e9134f..9b0424f 100644 --- a/lib/src/main/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchange.kt +++ b/lib/src/main/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchange.kt @@ -111,7 +111,9 @@ class PresentationExchange() : IPresentationExchange { if (!fieldMatched) { // If any one field didn't match then move to next credential - credentialMatched = false + if (field.optional != true) { + credentialMatched = false + } break@fieldLoop } } diff --git a/lib/src/test/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchangeTest.kt b/lib/src/test/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchangeTest.kt index 5b92c0e..6b6bb92 100644 --- a/lib/src/test/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchangeTest.kt +++ b/lib/src/test/kotlin/com/github/decentraliseddataexchange/presentationexchangesdk/PresentationExchangeTest.kt @@ -69,4 +69,40 @@ class PresentationExchangeTest { assertEquals(1, matches.size) } + + @Test + fun `find matched Credentials of field vct which is not present in credential and field optional value is true`(){ + val pex = PresentationExchange() + val inputDescriptor = + """{"id":"abd4acb1-1dcb-41ad-8596-ceb1401a69c7","format":{"vc+sd-jwt":{"alg":["ES256","ES384"]}},"constraints":{"fields":[{"path":["${'$'}.given_name"]},{"path":["${'$'}.last_name"]},{"path":["${'$'}.vct"],"filter":{"type":"string","const":"VerifiablePortableDocumentA1"},"optional":true}]},"limit_disclosure":"required"}""" + val credentialsList = listOf( + """{"iss":"https://dss.aegean.gr/rfc-issuer","iat":1712657569263,"given_name":"John","last_name":"Doe"}""", + ) + val matches: List = pex.matchCredentials(inputDescriptor, credentialsList) + assertEquals(1, matches.size) + } + + @Test + fun `find matched Credentials of field vct which is not present in credential and field optional value is false`(){ + val pex = PresentationExchange() + val inputDescriptor = + """{"id":"abd4acb1-1dcb-41ad-8596-ceb1401a69c7","format":{"vc+sd-jwt":{"alg":["ES256","ES384"]}},"constraints":{"fields":[{"path":["${'$'}.given_name"]},{"path":["${'$'}.last_name"]},{"path":["${'$'}.vct"],"filter":{"type":"string","const":"VerifiablePortableDocumentA1"},"optional":false}]},"limit_disclosure":"required"}""" + val credentialsList = listOf( + """{"iss":"https://dss.aegean.gr/rfc-issuer","iat":1712657569263,"given_name":"John","last_name":"Doe"}""", + ) + val matches: List = pex.matchCredentials(inputDescriptor, credentialsList) + assertEquals(0, matches.size) + } + + @Test + fun `find matched Credentials of field vct which is not present in credential and field optional is not present`(){ + val pex = PresentationExchange() + val inputDescriptor = + """{"id":"abd4acb1-1dcb-41ad-8596-ceb1401a69c7","format":{"vc+sd-jwt":{"alg":["ES256","ES384"]}},"constraints":{"fields":[{"path":["${'$'}.given_name"]},{"path":["${'$'}.last_name"]},{"path":["${'$'}.vct"],"filter":{"type":"string","const":"VerifiablePortableDocumentA1"}}]},"limit_disclosure":"required"}""" + val credentialsList = listOf( + """{"iss":"https://dss.aegean.gr/rfc-issuer","iat":1712657569263,"given_name":"John","last_name":"Doe"}""", + ) + val matches: List = pex.matchCredentials(inputDescriptor, credentialsList) + assertEquals(0, matches.size) + } } \ No newline at end of file