Skip to content

Commit

Permalink
feat: ATL-5966 Implemented Anoncred Presentation
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <[email protected]>
  • Loading branch information
CryptoKnightIOG committed Oct 25, 2023
1 parent 413ce93 commit f259aff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package io.iohk.atala.pollux.core.model.schema.validator

import com.networknt.schema.JsonSchema
import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError.*
import zio.IO
import zio.ZIO
import zio.json.*
import zio.json.JsonDecoder
import zio.json.ast.Json
import zio.json.ast.Json.*
import zio.{IO, ZIO}

class SchemaSerDes[S](jsonSchemaSchemaStr: String) {

def initialiseJsonSchema: IO[JsonSchemaError, JsonSchema] =
JsonSchemaUtils.jsonSchema(jsonSchemaSchemaStr)

def serialize(instance: S)(using encoder: JsonEncoder[S]): String = {
instance.toJson
}

def deserialize(
schema: zio.json.ast.Json
)(using decoder: JsonDecoder[S]): IO[JsonSchemaError, S] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.model.error.PresentationError.*
import io.iohk.atala.pollux.core.model.presentation.*
import io.iohk.atala.pollux.core.repository.{CredentialRepository, PresentationRepository}
import io.iohk.atala.pollux.core.service.serdes.{AnoncredAttributeRestriction, AnoncredPresentationRequestSchemaSerDesV1, AnoncredRequestedAttribute, AnoncredRequestedPredicate}
import io.iohk.atala.pollux.vc.jwt.*
import io.iohk.atala.shared.models.WalletAccessContext
import io.iohk.atala.shared.utils.aspects.CustomMetricsAspect
Expand Down Expand Up @@ -157,10 +158,13 @@ private class PresentationServiceImpl(
thid,
pairwiseVerifierDID,
pairwiseProverDID,
format match {
case CredentialFormat.JWT => maybeOptions.map(options => Seq(toJWTAttachment(options))).getOrElse(Seq.empty)
case CredentialFormat.AnonCreds => Seq(toAnoncredAttachment(proofTypes)) // TODO ATL-5945 Create Actual Anoncred Request
}
maybeOptions.map(
options =>
format match {
case CredentialFormat.JWT => Seq(toJWTAttachment(options))
case CredentialFormat.AnonCreds => proofTypes.map(protoType => toAnoncredAttachment(protoType, options))
}
).getOrElse(Seq.empty)
)
)
record <- ZIO.succeed(
Expand Down Expand Up @@ -618,41 +622,35 @@ private class PresentationServiceImpl(
)
}

/*
def createAnoncredPresentationRequest(proofType: ProofType): String = {
// Generate a random nonce
val nonce = Random.nextLong().toString
private[this] def toAnoncredAttachment(proofType: ProofType, options: Options): AttachmentDescriptor = {
val presentationRequest = createAnoncredPresentationRequest(proofType, options)
AttachmentDescriptor.buildBase64Attachment(
mediaType = Some("application/json"),
format = Some(PresentCredentialRequestFormat.Anoncred.name),
payload = presentationRequest.getBytes()
)
}

// Create the requested_attributes and requested_predicates maps
def createAnoncredPresentationRequest(proofType: ProofType, options: Options): String = {
val requestedAttributes = proofType.requiredFields.getOrElse(Seq.empty).map { field =>
field -> Map("name" -> field, "restrictions" -> Map("schema_id" -> proofType.schema))
field -> AnoncredRequestedAttribute(field, List(
AnoncredAttributeRestriction(Some(proofType.schema), None, None)
))
}.toMap

val requestedPredicates = Map.empty[String, Map[String, Any]] // You can add predicates if needed
val requestedPredicates = Map.empty[String, AnoncredRequestedPredicate] // 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
val presentationRequest =
AnoncredPresentationRequestSchemaSerDesV1(
requested_attributes = requestedAttributes,
requested_predicates = requestedPredicates,
name = options.domain,
nonce = options.challenge,
version = "0.1",
non_revoked = None
)

// 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(proofTypes: Seq[ProofType]): AttachmentDescriptor = {
//anoncreds.CredentialRequest(requestCredentialAttachmentData)
AttachmentDescriptor.buildJsonAttachment(
payload = PresentationAttachment.build(None),
format = Some(PresentCredentialRequestFormat.Anoncred.name)
)
AnoncredPresentationRequestSchemaSerDesV1.schemaSerDes.serialize(presentationRequest)
}

private[this] def createDidCommRequestPresentation(
Expand Down

0 comments on commit f259aff

Please sign in to comment.