Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Preserve Presentation Format #1363

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,18 @@ object PresentBackgroundJobs extends BackgroundJobsHelper {
goal_code = requestPresentation.body.goal_code,
comment = requestPresentation.body.comment
),
attachments = Seq(
attachments = requestPresentation.attachments.map(attachment =>
AttachmentDescriptor
.buildBase64Attachment(
payload = signedJwtPresentation.value.getBytes(),
mediaType = Some(PresentCredentialFormat.JWT.name)
mediaType = attachment.media_type,
format = attachment.format.map {
case PresentCredentialRequestFormat.JWT.name => PresentCredentialFormat.JWT.name
case format =>
throw throw RuntimeException(
s"Unexpected PresentCredentialRequestFormat=$format. Expecting: ${PresentCredentialRequestFormat.JWT.name}"
)
}
)
),
thid = requestPresentation.thid.orElse(Some(requestPresentation.id)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.hyperledger.identus.api.http.model.PaginationInput
import org.hyperledger.identus.connect.core.model.error.ConnectionServiceError
import org.hyperledger.identus.connect.core.service.ConnectionService
import org.hyperledger.identus.mercury.model.DidId
import org.hyperledger.identus.mercury.protocol.presentproof.ProofType
import org.hyperledger.identus.mercury.protocol.presentproof.{PresentCredentialRequestFormat, ProofType}
import org.hyperledger.identus.pollux.core.model.{CredentialFormat, DidCommID, PresentationRecord}
import org.hyperledger.identus.pollux.core.model.error.PresentationError
import org.hyperledger.identus.pollux.core.model.presentation.Options
Expand Down Expand Up @@ -80,6 +80,7 @@ class PresentProofControllerImpl(
request.options.map(o => Options(o.challenge, o.domain)),
request.claims,
request.anoncredPresentationRequest,
request.presentationFormat,
request.goalCode,
request.goal,
expirationDuration
Expand All @@ -95,6 +96,7 @@ class PresentProofControllerImpl(
options: Option[Options],
claims: Option[zio.json.ast.Json.Obj],
anoncredPresentationRequest: Option[AnoncredPresentationRequestV1],
presentationFormat: Option[PresentCredentialRequestFormat],
goalCode: Option[String],
goal: Option[String],
expirationDuration: Option[Duration],
Expand All @@ -115,6 +117,7 @@ class PresentProofControllerImpl(
)
},
options = options,
presentationFormat = presentationFormat.getOrElse(PresentCredentialRequestFormat.JWT),
goalCode = goalCode,
goal = goal,
expirationDuration = expirationDuration,
Expand All @@ -136,6 +139,7 @@ class PresentProofControllerImpl(
},
claimsToDisclose = claimsToDisclose,
options = options,
presentationFormat = presentationFormat.getOrElse(PresentCredentialRequestFormat.SDJWT),
goalCode = goalCode,
goal = goal,
expirationDuration = expirationDuration,
Expand All @@ -156,6 +160,7 @@ class PresentProofControllerImpl(
thid = DidCommID(),
connectionId = connectionId,
presentationRequest = presentationRequest,
presentationFormat = presentationFormat.getOrElse(PresentCredentialRequestFormat.Anoncred),
goalCode = goalCode,
goal = goal,
expirationDuration = expirationDuration,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hyperledger.identus.presentproof.controller.http

import org.hyperledger.identus.api.http.Annotation
import org.hyperledger.identus.mercury.protocol.presentproof.PresentCredentialRequestFormat
import org.hyperledger.identus.pollux.core.service.serdes.*
import org.hyperledger.identus.presentproof.controller.http.RequestPresentationInput.annotations
import sttp.tapir.{Schema, Validator}
Expand Down Expand Up @@ -29,6 +30,9 @@ final case class RequestPresentationInput(
@description(annotations.anoncredPresentationRequest.description)
@encodedExample(annotations.anoncredPresentationRequest.example)
anoncredPresentationRequest: Option[AnoncredPresentationRequestV1],
@description(annotations.presentationFormat.description)
@encodedExample(annotations.presentationFormat.example)
presentationFormat: Option[PresentCredentialRequestFormat],
@description(annotations.claims.description)
@encodedExample(annotations.claims.example)
claims: Option[zio.json.ast.Json.Obj],
Expand Down Expand Up @@ -128,6 +132,20 @@ object RequestPresentationInput {
)
)
)

object presentationFormat
extends Annotation[Option[String]](
description =
"The presentation format to display in Didcomm messages (default to 'prism/jwt', vc+sd-jwt or anoncreds/[email protected])",
example = Some("prism/jwt"),
validator = Validator.enumeration(
List(
Some("prism/jwt"),
yshyn-iohk marked this conversation as resolved.
Show resolved Hide resolved
Some("vc+sd-jwt"),
Some("anoncreds/[email protected]")
)
)
)
object claims
extends Annotation[Option[zio.json.ast.Json.Obj]](
description = """
Expand Down Expand Up @@ -163,6 +181,8 @@ object RequestPresentationInput {

import AnoncredPresentationRequestV1.given

given Schema[PresentCredentialRequestFormat] = Schema.derivedEnumeration.defaultStringBased

given Schema[AnoncredPresentationRequestV1] = Schema.derived

given Schema[AnoncredRequestedAttributeV1] = Schema.derived
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.hyperledger.identus.mercury.protocol.presentproof

import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.*
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonDecoder, JsonEncoder}

/*
Present Credential Formats:
Expand Down Expand Up @@ -72,8 +73,6 @@ object PresentCredentialProposeFormat {
* - dif/presentation-exchange/[email protected]
*/
enum PresentCredentialRequestFormat(val name: String) {
case Unsupported(other: String) extends PresentCredentialRequestFormat(other)
// case JWT extends PresentCredentialRequestFormat("jwt/[email protected]") // TODO FOLLOW specs for JWT VC
case JWT extends PresentCredentialRequestFormat("prism/jwt") // TODO REMOVE
case SDJWT extends PresentCredentialRequestFormat("vc+sd-jwt")
case Anoncred extends PresentCredentialRequestFormat("anoncreds/[email protected]")
Expand All @@ -82,6 +81,13 @@ enum PresentCredentialRequestFormat(val name: String) {
object PresentCredentialRequestFormat {
given Encoder[PresentCredentialRequestFormat] = deriveEncoder[PresentCredentialRequestFormat]
given Decoder[PresentCredentialRequestFormat] = deriveDecoder[PresentCredentialRequestFormat]

given JsonEncoder[PresentCredentialRequestFormat] =
DeriveJsonEncoder.gen[PresentCredentialRequestFormat]

given JsonDecoder[PresentCredentialRequestFormat] =
DeriveJsonDecoder.gen[PresentCredentialRequestFormat]

}

/** Present Credential:
Expand All @@ -98,7 +104,6 @@ object PresentCredentialRequestFormat {
* - dif/presentation-exchange/[email protected]
*/
enum PresentCredentialFormat(val name: String) {
case Unsupported(other: String) extends PresentCredentialFormat(other)
// case JWT extends PresentCredentialFormat("jwt/[email protected]") // TODO FOLLOW specs for JWT VC
case JWT extends PresentCredentialFormat("prism/jwt") // TODO REMOVE
case SDJWT extends PresentCredentialFormat("vc+sd-jwt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ trait PresentationService {
connectionId: Option[String],
proofTypes: Seq[ProofType],
options: Option[org.hyperledger.identus.pollux.core.model.presentation.Options],
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String],
goal: Option[String],
expirationDuration: Option[Duration],
Expand All @@ -39,6 +40,7 @@ trait PresentationService {
proofTypes: Seq[ProofType],
claimsToDisclose: ast.Json.Obj,
options: Option[org.hyperledger.identus.pollux.core.model.presentation.Options],
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String],
goal: Option[String],
expirationDuration: Option[Duration],
Expand All @@ -50,6 +52,7 @@ trait PresentationService {
thid: DidCommID,
connectionId: Option[String],
presentationRequest: AnoncredPresentationRequestV1,
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String],
goal: Option[String],
expirationDuration: Option[Duration],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,18 @@ private class PresentationServiceImpl(
goal_code = requestPresentation.body.goal_code,
comment = requestPresentation.body.comment
),
attachments = Seq(
attachments = requestPresentation.attachments.map(attachment =>
AttachmentDescriptor
.buildBase64Attachment(
payload = presentationPayload.compact.getBytes,
mediaType = Some(PresentCredentialFormat.SDJWT.name)
payload = presentationPayload.compact.getBytes(),
mediaType = attachment.media_type,
format = attachment.format.map {
case PresentCredentialRequestFormat.SDJWT.name => PresentCredentialFormat.SDJWT.name
case format =>
throw throw RuntimeException(
s"Unexpected PresentCredentialRequestFormat=$format. Expecting: ${PresentCredentialRequestFormat.SDJWT.name}"
)
}
)
),
thid = requestPresentation.thid.orElse(Some(requestPresentation.id)),
Expand Down Expand Up @@ -259,12 +266,18 @@ private class PresentationServiceImpl(
goal_code = requestPresentation.body.goal_code,
comment = requestPresentation.body.comment
),
attachments = Seq(
attachments = requestPresentation.attachments.map(attachment =>
AttachmentDescriptor
.buildBase64Attachment(
payload = presentationPayload.data.getBytes(),
mediaType = Some(PresentCredentialFormat.Anoncred.name),
format = Some(PresentCredentialFormat.Anoncred.name),
mediaType = attachment.media_type,
format = attachment.format.map {
case PresentCredentialRequestFormat.Anoncred.name => PresentCredentialFormat.Anoncred.name
case format =>
throw throw RuntimeException(
s"Unexpected PresentCredentialRequestFormat=$format. Expecting: ${PresentCredentialRequestFormat.Anoncred.name}"
)
}
)
),
thid = requestPresentation.thid.orElse(Some(requestPresentation.id)),
Expand Down Expand Up @@ -310,6 +323,7 @@ private class PresentationServiceImpl(
connectionId: Option[String],
proofTypes: Seq[ProofType],
options: Option[org.hyperledger.identus.pollux.core.model.presentation.Options],
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String] = None,
goal: Option[String] = None,
expirationDuration: Option[Duration] = None,
Expand All @@ -321,7 +335,7 @@ private class PresentationServiceImpl(
connectionId,
CredentialFormat.JWT,
proofTypes,
options.map(o => Seq(toJWTAttachment(o))).getOrElse(Seq.empty),
options.map(o => Seq(toJWTAttachment(o, presentationFormat))).getOrElse(Seq.empty),
goalCode,
goal,
expirationDuration
Expand All @@ -336,6 +350,7 @@ private class PresentationServiceImpl(
proofTypes: Seq[ProofType],
claimsToDisclose: ast.Json.Obj,
options: Option[org.hyperledger.identus.pollux.core.model.presentation.Options],
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String] = None,
goal: Option[String] = None,
expirationDuration: Option[Duration] = None,
Expand All @@ -347,7 +362,7 @@ private class PresentationServiceImpl(
connectionId,
CredentialFormat.SDJWT,
proofTypes,
attachments = Seq(toSDJWTAttachment(options, claimsToDisclose)),
attachments = Seq(toSDJWTAttachment(options, claimsToDisclose, presentationFormat)),
goalCode,
goal,
expirationDuration
Expand All @@ -360,6 +375,7 @@ private class PresentationServiceImpl(
thid: DidCommID,
connectionId: Option[String],
presentationRequest: AnoncredPresentationRequestV1,
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String] = None,
goal: Option[String] = None,
expirationDuration: Option[Duration] = None,
Expand All @@ -371,7 +387,7 @@ private class PresentationServiceImpl(
connectionId,
CredentialFormat.AnonCreds,
Seq.empty,
Seq(toAnoncredAttachment(presentationRequest)),
Seq(toAnoncredAttachment(presentationRequest, presentationFormat)),
goalCode,
goal,
expirationDuration
Expand Down Expand Up @@ -1160,30 +1176,36 @@ private class PresentationServiceImpl(
} yield record
}

private def toJWTAttachment(options: Options): AttachmentDescriptor = {
private def toJWTAttachment(
options: Options,
presentationFormat: PresentCredentialRequestFormat
): AttachmentDescriptor = {
AttachmentDescriptor.buildJsonAttachment(
payload = PresentationAttachment.build(Some(options)),
format = Some(PresentCredentialRequestFormat.JWT.name)
format = Some(presentationFormat.name),
mediaType = Some("application/json")
)
}

private def toSDJWTAttachment(
options: Option[Options],
claimsToDsiclose: ast.Json.Obj
claimsToDsiclose: ast.Json.Obj,
presentationFormat: PresentCredentialRequestFormat
): AttachmentDescriptor = {
AttachmentDescriptor.buildBase64Attachment(
mediaType = Some("application/json"),
format = Some(PresentCredentialRequestFormat.SDJWT.name),
format = Some(presentationFormat.name),
payload = SDJwtPresentation(options, claimsToDsiclose).toJson.getBytes
)
}

private def toAnoncredAttachment(
presentationRequest: AnoncredPresentationRequestV1
presentationRequest: AnoncredPresentationRequestV1,
presentationFormat: PresentCredentialRequestFormat
): AttachmentDescriptor = {
AttachmentDescriptor.buildBase64Attachment(
mediaType = Some("application/json"),
format = Some(PresentCredentialRequestFormat.Anoncred.name),
format = Some(presentationFormat.name),
payload = AnoncredPresentationRequestV1.schemaSerDes.serializeToJsonString(presentationRequest).getBytes()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package org.hyperledger.identus.pollux.core.service

import org.hyperledger.identus.event.notification.{Event, EventNotificationService}
import org.hyperledger.identus.mercury.model.DidId
import org.hyperledger.identus.mercury.protocol.presentproof.{
Presentation,
ProofType,
ProposePresentation,
RequestPresentation
}
import org.hyperledger.identus.mercury.protocol.presentproof.*
import org.hyperledger.identus.pollux.anoncreds.AnoncredPresentation
import org.hyperledger.identus.pollux.core.model.{DidCommID, PresentationRecord}
import org.hyperledger.identus.pollux.core.model.error.PresentationError
Expand Down Expand Up @@ -38,6 +33,7 @@ class PresentationServiceNotifier(
connectionId: Option[String],
proofTypes: Seq[ProofType],
options: Option[Options],
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String],
goal: Option[String],
expirationTime: Option[Duration],
Expand All @@ -50,6 +46,7 @@ class PresentationServiceNotifier(
connectionId,
proofTypes,
options,
presentationFormat,
goalCode,
goal,
expirationTime
Expand All @@ -64,6 +61,7 @@ class PresentationServiceNotifier(
proofTypes: Seq[ProofType],
claimsToDisclose: ast.Json.Obj,
options: Option[org.hyperledger.identus.pollux.core.model.presentation.Options],
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String],
goal: Option[String],
expirationTime: Option[Duration],
Expand All @@ -77,6 +75,7 @@ class PresentationServiceNotifier(
proofTypes,
claimsToDisclose,
options,
presentationFormat,
goalCode,
goal,
expirationTime
Expand All @@ -89,6 +88,7 @@ class PresentationServiceNotifier(
thid: DidCommID,
connectionId: Option[String],
presentationRequest: AnoncredPresentationRequestV1,
presentationFormat: PresentCredentialRequestFormat,
goalCode: Option[String],
goal: Option[String],
expirationTime: Option[Duration],
Expand All @@ -100,6 +100,7 @@ class PresentationServiceNotifier(
thid,
connectionId,
presentationRequest,
presentationFormat,
goalCode,
goal,
expirationTime
Expand Down
Loading
Loading