Skip to content

Commit

Permalink
feat: add serdeser for anoncred presentation request (#768)
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <[email protected]>
  • Loading branch information
CryptoKnightIOG committed Feb 9, 2024
1 parent 28a6d41 commit c50c41a
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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]

}
Original file line number Diff line number Diff line change
@@ -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)
)
}
)
}
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit c50c41a

Please sign in to comment.