-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add serdeser for anoncred presentation request (#768)
Signed-off-by: Bassam Riman <[email protected]>
- Loading branch information
1 parent
15ff710
commit bbeb742
Showing
3 changed files
with
263 additions
and
9 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
.../io/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesV1.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
...o/iohk/atala/pollux/core/service/serdes/AnoncredPresentationRequestSchemaSerDesSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
) | ||
} |
13 changes: 4 additions & 9 deletions
13
...ublicCredentialDefinitionSerDesSpec.scala → ...redentialDefinitionSchemaSerDesSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters