Skip to content

Commit

Permalink
feat: ATL-5966 Added Serdeser For Anoncred Presentation Request
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <bassam.riman@iohk.io>
  • Loading branch information
CryptoKnightIOG committed Oct 25, 2023
1 parent 5558a9e commit 413ce93
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ private class PresentationServiceImpl(
pairwiseProverDID,
format match {
case CredentialFormat.JWT => maybeOptions.map(options => Seq(toJWTAttachment(options))).getOrElse(Seq.empty)
case CredentialFormat.AnonCreds =>
maybeOptions
.map(options => Seq(toAnoncredAttachment(options)))
.getOrElse(Seq.empty) // TODO ATL-5945 Create Actual Anoncred Request
case CredentialFormat.AnonCreds => Seq(toAnoncredAttachment(proofTypes)) // TODO ATL-5945 Create Actual Anoncred Request
}
)
)
Expand Down Expand Up @@ -621,10 +618,39 @@ private class PresentationServiceImpl(
)
}

/*
def createAnoncredPresentationRequest(proofType: ProofType): String = {
// Generate a random nonce
val nonce = Random.nextLong().toString
// Create the requested_attributes and requested_predicates maps
val requestedAttributes = proofType.requiredFields.getOrElse(Seq.empty).map { field =>
field -> Map("name" -> field, "restrictions" -> Map("schema_id" -> proofType.schema))
}.toMap
val requestedPredicates = Map.empty[String, Map[String, Any]] // You can add predicates if needed
// Construct the Anoncred presentation request JSON
val presentationRequest = Map(
"nonce" -> nonce,
"name" -> "proof_req_1",
"version" -> "0.1",
"requested_attributes" -> requestedAttributes,
"requested_predicates" -> requestedPredicates
)
// Convert the map to JSON string
val json = scala.util.parsing.json.JSONObject(presentationRequest).toString()
json
}
*/
// TODO ATL-5945 Create Actual Anoncred Request
private[this] def toAnoncredAttachment(options: Options): AttachmentDescriptor = {
private[this] def toAnoncredAttachment(proofTypes: Seq[ProofType]): AttachmentDescriptor = {
//anoncreds.CredentialRequest(requestCredentialAttachmentData)
AttachmentDescriptor.buildJsonAttachment(
payload = PresentationAttachment.build(Some(options)),
payload = PresentationAttachment.build(None),
format = Some(PresentCredentialRequestFormat.Anoncred.name)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@

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,86 @@
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 413ce93

Please sign in to comment.