From c1bae9fc080609534fde0128a558d670b5ccd6ed Mon Sep 17 00:00:00 2001 From: Bassam Riman Date: Wed, 25 Oct 2023 14:50:35 -0400 Subject: [PATCH] feat: ATL-5966 Added Serdeser For Anoncred Presentation Request Signed-off-by: Bassam Riman --- ...redPresentationRequestSchemaSerDesV1.scala | 158 ++++++++++++++++++ ...dPresentationRequestSchemaSerDesSpec.scala | 101 +++++++++++ ...redentialDefinitionSchemaSerDesSpec.scala} | 13 +- 3 files changed, 263 insertions(+), 9 deletions(-) create mode 100644 pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesV1.scala create mode 100644 pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesSpec.scala rename pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/{helper/PublicCredentialDefinitionSerDesSpec.scala => serdes/PublicCredentialDefinitionSchemaSerDesSpec.scala} (86%) diff --git a/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesV1.scala b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesV1.scala new file mode 100644 index 0000000000..e788e52b7d --- /dev/null +++ b/pollux/lib/core/src/main/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesV1.scala @@ -0,0 +1,158 @@ +package io.iohk.atala.pollux.core.service.serdes + +import io.iohk.atala.pollux.core.model.schema.validator.SchemaSerDes +import zio.* +import zio.json.* + +case class AnoncredPresentationRequestSchemaSerDesV1( + requested_attributes: Map[String, AnoncredRequestedAttribute], + requested_predicates: Map[String, AnoncredRequestedPredicate], + name: String, + nonce: String, + version: String, + non_revoked: Option[AnoncredNonRevokedInterval] +) + +case class AnoncredRequestedAttribute(name: String, restrictions: List[AnoncredAttributeRestriction]) + +case class AnoncredRequestedPredicate( + name: String, + p_type: String, + p_value: Int, + restrictions: List[AnoncredPredicateRestriction] +) + +case class AnoncredAttributeRestriction( + schema_id: Option[String], + cred_def_id: Option[String], + non_revoked: Option[AnoncredNonRevokedInterval] +) + +case class AnoncredPredicateRestriction( + schema_id: Option[String], + cred_def_id: Option[String], + non_revoked: Option[AnoncredNonRevokedInterval] +) + +case class AnoncredNonRevokedInterval(from: Option[Int], to: Option[Int]) + +object AnoncredPresentationRequestSchemaSerDesV1 { + val version: String = "PresentationRequestV1" + + private val schema: String = + """ + |{ + | "$schema": "http://json-schema.org/draft-07/schema#", + | "type": "object", + | "properties": { + | "requested_attributes": { + | "type": "object", + | "additionalProperties": { + | "type": "object", + | "properties": { + | "name": { "type": "string" }, + | "restrictions": { + | "type": "array", + | "items": { + | "type": "object", + | "properties": { + | "schema_id": { "type": "string" }, + | "cred_def_id": { "type": "string" }, + | "non_revoked": { + | "type": "object", + | "properties": { + | "from": { "type": "integer" }, + | "to": { "type": "integer" } + | } + | } + | } + | } + | } + | }, + | "required": ["name", "restrictions"] + | } + | }, + | "requested_predicates": { + | "type": "object", + | "additionalProperties": { + | "type": "object", + | "properties": { + | "name": { "type": "string" }, + | "p_type": { "type": "string" }, + | "p_value": { "type": "integer" }, + | "restrictions": { + | "type": "array", + | "items": { + | "type": "object", + | "properties": { + | "schema_id": { "type": "string" }, + | "cred_def_id": { "type": "string" }, + | "non_revoked": { + | "type": "object", + | "properties": { + | "from": { "type": "integer" }, + | "to": { "type": "integer" } + | } + | } + | } + | } + | } + | }, + | "required": ["name", "p_type", "p_value", "restrictions"] + | } + | }, + | "name": { "type": "string" }, + | "nonce": { "type": "string" }, + | "version": { "type": "string" }, + | "non_revoked": { + | "type": "object", + | "properties": { + | "from": { "type": "integer" }, + | "to": { "type": "integer" } + | } + | } + | }, + | "required": ["requested_attributes", "requested_predicates", "name", "nonce", "version" ] + |} + | + |""".stripMargin + + val schemaSerDes: SchemaSerDes[AnoncredPresentationRequestSchemaSerDesV1] = SchemaSerDes(schema) + + given JsonDecoder[AnoncredRequestedAttribute] = + DeriveJsonDecoder.gen[AnoncredRequestedAttribute] + + given JsonEncoder[AnoncredRequestedAttribute] = + DeriveJsonEncoder.gen[AnoncredRequestedAttribute] + + given JsonDecoder[AnoncredRequestedPredicate] = + DeriveJsonDecoder.gen[AnoncredRequestedPredicate] + + given JsonEncoder[AnoncredRequestedPredicate] = + DeriveJsonEncoder.gen[AnoncredRequestedPredicate] + + given JsonDecoder[AnoncredAttributeRestriction] = + DeriveJsonDecoder.gen[AnoncredAttributeRestriction] + + given JsonEncoder[AnoncredNonRevokedInterval] = + DeriveJsonEncoder.gen[AnoncredNonRevokedInterval] + + given JsonDecoder[AnoncredNonRevokedInterval] = + DeriveJsonDecoder.gen[AnoncredNonRevokedInterval] + + given JsonEncoder[AnoncredAttributeRestriction] = + DeriveJsonEncoder.gen[AnoncredAttributeRestriction] + + given JsonDecoder[AnoncredPredicateRestriction] = + DeriveJsonDecoder.gen[AnoncredPredicateRestriction] + + given JsonEncoder[AnoncredPredicateRestriction] = + DeriveJsonEncoder.gen[AnoncredPredicateRestriction] + + given JsonDecoder[AnoncredPresentationRequestSchemaSerDesV1] = + DeriveJsonDecoder.gen[AnoncredPresentationRequestSchemaSerDesV1] + + given JsonEncoder[AnoncredPresentationRequestSchemaSerDesV1] = + DeriveJsonEncoder.gen[AnoncredPresentationRequestSchemaSerDesV1] + +} diff --git a/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesSpec.scala b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesSpec.scala new file mode 100644 index 0000000000..5545985f22 --- /dev/null +++ b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesSpec.scala @@ -0,0 +1,101 @@ +package io.iohk.atala.pollux.core.service.serdes + +import zio.* +import zio.test.* +import zio.test.Assertion.* + +object AnoncredPresentationRequestSchemaSerDesSpec extends ZIOSpecDefault { + val json = + """ + |{ + | "requested_attributes": { + | "attribute1": { + | "name": "Attribute 1", + | "restrictions": [ + | { + | "cred_def_id": "credential_definition_id_of_attribute1", + | "non_revoked": { + | "from": 1635734400, + | "to": 1735734400 + | } + | } + | ] + | } + | }, + | "requested_predicates": { + | "predicate1": { + | "name": "Predicate 1", + | "p_type": ">=", + | "p_value": 18, + | "restrictions": [ + | { + | "schema_id": "schema_id_of_predicate1", + | "non_revoked": { + | "from": 1635734400 + | } + | } + | ] + | } + | }, + | "name": "Example Presentation Request", + | "nonce": "1234567890", + | "version": "1.0" + |} + |""".stripMargin + + override def spec: Spec[TestEnvironment with Scope, Any] = suite("AnoncredPresentationRequestSerDes")( + test("should validate a correct schema") { + assertZIO(AnoncredPresentationRequestSchemaSerDesV1.schemaSerDes.validate(json))(isTrue) + }, + test("should deserialize correctly") { + val expectedPresentationRequest = + AnoncredPresentationRequestSchemaSerDesV1( + requested_attributes = Map( + "attribute1" -> AnoncredRequestedAttribute( + "Attribute 1", + List( + AnoncredAttributeRestriction( + None, + Some("credential_definition_id_of_attribute1"), + Some( + AnoncredNonRevokedInterval( + Some(1635734400), + Some(1735734400) + ) + ) + ) + ) + ) + ), + requested_predicates = Map( + "predicate1" -> + AnoncredRequestedPredicate( + "Predicate 1", + ">=", + 18, + List( + AnoncredPredicateRestriction( + Some("schema_id_of_predicate1"), + None, + Some( + AnoncredNonRevokedInterval( + Some(1635734400), + None + ) + ) + ) + ) + ) + ), + name = "Example Presentation Request", + nonce = "1234567890", + version = "1.0", + non_revoked = None + ) + + assertZIO(AnoncredPresentationRequestSchemaSerDesV1.schemaSerDes.deserialize(json))( + Assertion.equalTo(expectedPresentationRequest) + ) + } + ) +} diff --git a/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/helper/PublicCredentialDefinitionSerDesSpec.scala b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/serdes/PublicCredentialDefinitionSchemaSerDesSpec.scala similarity index 86% rename from pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/helper/PublicCredentialDefinitionSerDesSpec.scala rename to pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/serdes/PublicCredentialDefinitionSchemaSerDesSpec.scala index 4a05b09fd5..aebb68bea8 100644 --- a/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/helper/PublicCredentialDefinitionSerDesSpec.scala +++ b/pollux/lib/core/src/test/scala/io/iohk/atala/pollux/core/service/serdes/PublicCredentialDefinitionSchemaSerDesSpec.scala @@ -1,16 +1,11 @@ -package io.iohk.atala.pollux.core.service.helper +package io.iohk.atala.pollux.core.service.serdes -import io.iohk.atala.pollux.core.service.serdes.PublicCredentialDefinitionSerDesV1 -import io.iohk.atala.pollux.core.service.serdes.PublicCredentialPrimaryPublicKeyV1 -import io.iohk.atala.pollux.core.service.serdes.PublicCredentialRevocationKeyV1 -import io.iohk.atala.pollux.core.service.serdes.PublicCredentialValueV1 import zio.* -import zio.test.* import zio.test.Assertion.* -import zio.test.assertZIO +import zio.test.* -object PublicCredentialDefinitionSerDesSpec extends ZIOSpecDefault { - val json = +object PublicCredentialDefinitionSchemaSerDesSpec extends ZIOSpecDefault { + val json: String = """ |{ | "schemaId": "resource:///anoncred-schema-example.json",