Skip to content

Commit

Permalink
feat: Create/Store presentation proof request (#774)
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 Feb 22, 2024
1 parent 7a7174e commit 4edcb0a
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ object PresentationError {
object MissingCredential extends PresentationError
object MissingCredentialFormat extends PresentationError
final case class UnsupportedCredentialFormat(vcFormat: String) extends PresentationError

final case class MissingAnoncredPresentationRequest(error: String) extends PresentationError
}
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 @@ -5,23 +5,31 @@ import io.iohk.atala.mercury.protocol.presentproof.{Presentation, ProofType, Pro
import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.model.presentation.Options
import io.iohk.atala.pollux.core.model.{DidCommID, PresentationRecord}
import io.iohk.atala.pollux.core.service.serdes.AnoncredPresentationRequestV1
import io.iohk.atala.pollux.vc.jwt.{Issuer, PresentationPayload, W3cCredentialPayload}
import io.iohk.atala.shared.models.WalletAccessContext
import zio.mock.{Mock, Proxy}
import zio.{IO, URLayer, ZIO, ZLayer, mock}

import java.time.Instant
import java.util.UUID
import io.iohk.atala.pollux.core.model.CredentialFormat

object MockPresentationService extends Mock[PresentationService] {

object CreatePresentationRecord
object CreateJwtPresentationRecord
extends Effect[
(DidId, DidId, DidCommID, Option[String], Seq[ProofType], Option[Options]),
PresentationError,
PresentationRecord
]

object CreateAnoncredPresentationRecord
extends Effect[
(DidId, DidId, DidCommID, Option[String], AnoncredPresentationRequestV1),
PresentationError,
PresentationRecord
]

object MarkRequestPresentationSent extends Effect[DidCommID, PresentationError, PresentationRecord]

object ReceivePresentation extends Effect[Presentation, PresentationError, PresentationRecord]
Expand Down Expand Up @@ -54,20 +62,32 @@ object MockPresentationService extends Mock[PresentationService] {
proxy <- ZIO.service[Proxy]
} yield new PresentationService {

override def createPresentationRecord(
override def createJwtPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
proofTypes: Seq[ProofType],
options: Option[Options],
format: CredentialFormat,
options: Option[Options]
): IO[PresentationError, PresentationRecord] =
proxy(
CreatePresentationRecord,
CreateJwtPresentationRecord,
(pairwiseVerifierDID, pairwiseProverDID, thid, connectionId, proofTypes, options)
)

override def createAnoncredPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
presentationRequest: AnoncredPresentationRequestV1
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] = {
proxy(
CreateAnoncredPresentationRecord,
(pairwiseVerifierDID, pairwiseProverDID, thid, connectionId, presentationRequest)
)
}

override def acceptRequestPresentation(
recordId: DidCommID,
credentialsToUse: Seq[String]
Expand Down Expand Up @@ -158,6 +178,7 @@ object MockPresentationService extends Mock[PresentationService] {
recordId: DidCommID,
failReason: Option[String]
): IO[PresentationError, Unit] = ???

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.iohk.atala.mercury.protocol.presentproof.*
import io.iohk.atala.pollux.core.model.*
import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.model.presentation.*
import io.iohk.atala.pollux.core.service.serdes.AnoncredPresentationRequestV1
import io.iohk.atala.pollux.vc.jwt.*
import io.iohk.atala.shared.models.WalletAccessContext
import zio.*
Expand All @@ -17,14 +18,21 @@ trait PresentationService {

def extractIdFromCredential(credential: W3cCredentialPayload): Option[UUID]

def createPresentationRecord(
def createJwtPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
proofTypes: Seq[ProofType],
options: Option[io.iohk.atala.pollux.core.model.presentation.Options],
format: CredentialFormat,
): ZIO[WalletAccessContext, PresentationError, PresentationRecord]

def createAnoncredPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
presentationRequest: AnoncredPresentationRequestV1
): ZIO[WalletAccessContext, PresentationError, PresentationRecord]

def getPresentationRecords(
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.AnoncredPresentationRequestV1
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 @@ -141,14 +142,13 @@ private class PresentationServiceImpl(
markPresentationRejected(recordId)
}

override def createPresentationRecord(
override def createJwtPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
proofTypes: Seq[ProofType],
maybeOptions: Option[io.iohk.atala.pollux.core.model.presentation.Options],
format: CredentialFormat,
maybeOptions: Option[io.iohk.atala.pollux.core.model.presentation.Options]
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] = {
for {
request <- ZIO.succeed(
Expand All @@ -157,13 +157,7 @@ private class PresentationServiceImpl(
thid,
pairwiseVerifierDID,
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
}
maybeOptions.map(options => Seq(toJWTAttachment(options))).getOrElse(Seq.empty)
)
)
record <- ZIO.succeed(
Expand All @@ -177,7 +171,57 @@ private class PresentationServiceImpl(
role = PresentationRecord.Role.Verifier,
subjectId = pairwiseProverDID,
protocolState = PresentationRecord.ProtocolState.RequestPending,
credentialFormat = format,
credentialFormat = CredentialFormat.JWT,
requestPresentationData = Some(request),
proposePresentationData = None,
presentationData = None,
credentialsToUse = None,
metaRetries = maxRetries,
metaNextRetry = Some(Instant.now()),
metaLastFailure = None,
)
)
count <- presentationRepository
.createPresentationRecord(record)
.flatMap {
case 1 => ZIO.succeed(())
case n => ZIO.fail(UnexpectedException(s"Invalid row count result: $n"))
}
.mapError(RepositoryError.apply) @@ CustomMetricsAspect.startRecordingTime(
s"${record.id}_present_proof_flow_verifier_req_pending_to_sent_ms_gauge"
)
} yield record
}

override def createAnoncredPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
presentationRequest: AnoncredPresentationRequestV1
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] = {
for {
request <- ZIO.succeed(
createDidCommRequestPresentation(
Seq.empty,
thid,
pairwiseVerifierDID,
pairwiseProverDID,
Seq(toAnoncredAttachment(presentationRequest))
)
)
record <- ZIO.succeed(
PresentationRecord(
id = DidCommID(),
createdAt = Instant.now,
updatedAt = None,
thid = thid,
connectionId = connectionId,
schemaId = None, // TODO REMOVE from DB
role = PresentationRecord.Role.Verifier,
subjectId = pairwiseProverDID,
protocolState = PresentationRecord.ProtocolState.RequestPending,
credentialFormat = CredentialFormat.AnonCreds,
requestPresentationData = Some(request),
proposePresentationData = None,
presentationData = None,
Expand Down Expand Up @@ -621,11 +665,13 @@ private class PresentationServiceImpl(
)
}

// TODO ATL-5945 Create Actual Anoncred Request
private[this] def toAnoncredAttachment(options: Options): AttachmentDescriptor = {
AttachmentDescriptor.buildJsonAttachment(
payload = PresentationAttachment.build(Some(options)),
format = Some(PresentCredentialRequestFormat.Anoncred.name)
private[this] def toAnoncredAttachment(
presentationRequest: AnoncredPresentationRequestV1
): AttachmentDescriptor = {
AttachmentDescriptor.buildBase64Attachment(
mediaType = Some("application/json"),
format = Some(PresentCredentialRequestFormat.Anoncred.name),
payload = AnoncredPresentationRequestV1.schemaSerDes.serialize(presentationRequest).getBytes()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import io.iohk.atala.mercury.protocol.presentproof.{Presentation, ProofType, Pro
import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.model.presentation.Options
import io.iohk.atala.pollux.core.model.{DidCommID, PresentationRecord}
import io.iohk.atala.pollux.core.service.serdes.AnoncredPresentationRequestV1
import io.iohk.atala.pollux.vc.jwt.{Issuer, PresentationPayload, W3cCredentialPayload}
import io.iohk.atala.shared.models.WalletAccessContext
import zio.{URLayer, ZIO, ZLayer, IO}
import zio.{IO, URLayer, ZIO, ZLayer}

import java.time.Instant
import java.util.UUID
import io.iohk.atala.pollux.core.model.CredentialFormat

class PresentationServiceNotifier(
svc: PresentationService,
Expand All @@ -21,24 +21,39 @@ class PresentationServiceNotifier(

private val presentationUpdatedEvent = "PresentationUpdated"

override def createPresentationRecord(
override def createJwtPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
proofTypes: Seq[ProofType],
options: Option[Options],
format: CredentialFormat,
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] =
notifyOnSuccess(
svc.createPresentationRecord(
svc.createJwtPresentationRecord(
pairwiseVerifierDID,
pairwiseProverDID,
thid,
connectionId,
proofTypes,
options,
format: CredentialFormat
options
)
)

def createAnoncredPresentationRecord(
pairwiseVerifierDID: DidId,
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
presentationRequest: AnoncredPresentationRequestV1
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] =
notifyOnSuccess(
svc.createAnoncredPresentationRecord(
pairwiseVerifierDID,
pairwiseProverDID,
thid,
connectionId,
presentationRequest
)
)

Expand Down
Loading

0 comments on commit 4edcb0a

Please sign in to comment.