Skip to content

Commit

Permalink
feat: presentation-exchange model and json schema refactoring (#1304)
Browse files Browse the repository at this point in the history
Signed-off-by: Pat Losoponkul <[email protected]>
Signed-off-by: Hyperledger Bot <[email protected]>
Co-authored-by: Hyperledger Bot <[email protected]>
  • Loading branch information
patlo-iog and hyperledger-bot authored Aug 29, 2024
1 parent 2668baa commit 75b2736
Show file tree
Hide file tree
Showing 48 changed files with 917 additions and 306 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
pull-requests: write
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SBT_OPTS: -Xmx2G
container:
image: ghcr.io/hyperledger-labs/ci-debian-jdk-22:0.1.0
volumes:
Expand Down
39 changes: 34 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ lazy val D = new {
val circeGeneric: ModuleID = "io.circe" %% "circe-generic" % V.circe
val circeParser: ModuleID = "io.circe" %% "circe-parser" % V.circe

val networkntJsonSchemaValidator = "com.networknt" % "json-schema-validator" % V.jsonSchemaValidator
val jwtCirce = "com.github.jwt-scala" %% "jwt-circe" % V.jwtCirceVersion
val jsonCanonicalization: ModuleID = "io.github.erdtman" % "java-json-canonicalization" % "1.1"
val titaniumJsonLd: ModuleID = "com.apicatalog" % "titanium-json-ld" % "1.4.0"
Expand Down Expand Up @@ -179,16 +180,28 @@ lazy val D_Shared = new {
D.zio,
D.zioHttp,
D.scalaUri,
D.zioPrelude,
// FIXME: split shared DB stuff as subproject?
D.doobieHikari,
D.doobiePostgres,
D.zioCatsInterop,
D.zioPrelude,
)
}

lazy val D_SharedJson = new {
lazy val dependencies: Seq[ModuleID] =
Seq(
D.zio,
D.zioJson,
D.circeCore,
D.circeGeneric,
D.circeParser,
D.jsonCanonicalization,
D.titaniumJsonLd,
D.jakartaJson,
D.ironVC,
D.scodecBits,
D.networkntJsonSchemaValidator
)
}

Expand Down Expand Up @@ -306,16 +319,14 @@ lazy val D_Pollux_VC_JWT = new {
val zio = "dev.zio" %% "zio" % V.zio
val zioPrelude = "dev.zio" %% "zio-prelude" % V.zioPreludeVersion

val networkntJsonSchemaValidator = "com.networknt" % "json-schema-validator" % V.jsonSchemaValidator

val zioTest = "dev.zio" %% "zio-test" % V.zio % Test
val zioTestSbt = "dev.zio" %% "zio-test-sbt" % V.zio % Test
val zioTestMagnolia = "dev.zio" %% "zio-test-magnolia" % V.zio % Test

// Dependency Modules
val zioDependencies: Seq[ModuleID] = Seq(zio, zioPrelude, zioTest, zioTestSbt, zioTestMagnolia)
val baseDependencies: Seq[ModuleID] =
zioDependencies :+ D.jwtCirce :+ networkntJsonSchemaValidator :+ D.nimbusJwt :+ D.scalaTest
zioDependencies :+ D.jwtCirce :+ D.networkntJsonSchemaValidator :+ D.nimbusJwt :+ D.scalaTest

// Project Dependencies
lazy val polluxVcJwtDependencies: Seq[ModuleID] = baseDependencies
Expand Down Expand Up @@ -456,6 +467,15 @@ lazy val shared = (project in file("shared/core"))
libraryDependencies ++= D_Shared.dependencies
)

lazy val sharedJson = (project in file("shared/json"))
.settings(commonSetttings)
.settings(
name := "shared-json",
crossPaths := false,
libraryDependencies ++= D_SharedJson.dependencies
)
.dependsOn(shared)

lazy val sharedCrypto = (project in file("shared/crypto"))
.settings(commonSetttings)
.settings(
Expand Down Expand Up @@ -714,7 +734,7 @@ lazy val polluxVcJWT = project
name := "pollux-vc-jwt",
libraryDependencies ++= D_Pollux_VC_JWT.polluxVcJwtDependencies
)
.dependsOn(castorCore)
.dependsOn(castorCore, sharedJson)

lazy val polluxCore = project
.in(file("pollux/core"))
Expand All @@ -734,6 +754,7 @@ lazy val polluxCore = project
polluxAnoncreds,
polluxVcJWT,
polluxSDJWT,
polluxPreX
)

lazy val polluxDoobie = project
Expand All @@ -747,6 +768,12 @@ lazy val polluxDoobie = project
.dependsOn(shared)
.dependsOn(sharedTest % "test->test")

lazy val polluxPreX = project
.in(file("pollux/prex"))
.settings(commonSetttings)
.settings(name := "pollux-prex")
.dependsOn(shared, sharedJson)

// ########################
// ### Pollux Anoncreds ###
// ########################
Expand Down Expand Up @@ -891,6 +918,7 @@ releaseProcess := Seq[ReleaseStep](

lazy val aggregatedProjects: Seq[ProjectReference] = Seq(
shared,
sharedJson,
sharedCrypto,
sharedTest,
models,
Expand All @@ -917,6 +945,7 @@ lazy val aggregatedProjects: Seq[ProjectReference] = Seq(
polluxAnoncreds,
polluxAnoncredsTest,
polluxSDJWT,
polluxPreX,
connectCore,
connectDoobie,
agentWalletAPI,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.Router
import org.hyperledger.identus.api.http.ErrorResponse
import org.hyperledger.identus.shared.crypto.Sha256Hash
import org.hyperledger.identus.shared.utils.Json
import org.hyperledger.identus.shared.json.Json
import org.hyperledger.identus.system.controller.SystemEndpoints
import sttp.tapir.*
import sttp.tapir.model.ServerRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
package org.hyperledger.identus.api.http.codec

import io.circe.Json as CirceJson
import org.hyperledger.identus.shared.json.JsonInterop
import sttp.tapir.json.zio.*
import sttp.tapir.Schema
import zio.json.*
import zio.json.ast.Json as ZioJson

object CirceJsonInterop {

private def toZioJsonAst(circeJson: CirceJson): ZioJson = {
val encoded = circeJson.noSpaces
encoded.fromJson[ZioJson] match {
case Left(failure) =>
throw Exception(s"Circe and Zio Json interop fail. Unable to convert from Circe to Zio AST. $failure")
case Right(value) => value
}
}

private def toCirceJsonAst(zioJson: ZioJson): CirceJson = {
val encoded = zioJson.toJson
io.circe.parser.parse(encoded).left.map(_.toString) match {
case Left(failure) =>
throw Exception(s"Circe and Zio Json interop fail. Unable to convert from Zio to Circe AST. $failure")
case Right(value) => value
}
}

given encodeJson: JsonEncoder[CirceJson] = JsonEncoder[ZioJson].contramap(toZioJsonAst)

given decodeJson: JsonDecoder[CirceJson] = JsonDecoder[ZioJson].map(toCirceJsonAst)

given encodeJson: JsonEncoder[CirceJson] = JsonEncoder[ZioJson].contramap(JsonInterop.toZioJsonAst)
given decodeJson: JsonDecoder[CirceJson] = JsonDecoder[ZioJson].map(JsonInterop.toCirceJsonAst)
given schemaJson: Schema[CirceJson] =
Schema.derived[ZioJson].map[CirceJson](js => Some(toCirceJsonAst(js)))(toZioJsonAst)

Schema.derived[ZioJson].map[CirceJson](js => Some(JsonInterop.toCirceJsonAst(js)))(JsonInterop.toZioJsonAst)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package org.hyperledger.identus.connect.core.model
import org.hyperledger.identus.connect.core.model.ConnectionRecord.{ProtocolState, Role}
import org.hyperledger.identus.mercury.protocol.connection.{ConnectionRequest, ConnectionResponse}
import org.hyperledger.identus.mercury.protocol.invitation.v2.Invitation
import org.hyperledger.identus.shared.models.Failure
import org.hyperledger.identus.shared.models.WalletAccessContext
import org.hyperledger.identus.shared.models.WalletId
import zio.ZIO
import org.hyperledger.identus.shared.models.{Failure, WalletId}

import java.time.temporal.ChronoUnit
import java.time.Instant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package org.hyperledger.identus.pollux.core.model

import io.circe.*
import io.circe.generic.semiauto.*
import org.hyperledger.identus.pollux.core.model.presentation.{Options, PresentationDefinition}
import org.hyperledger.identus.pollux.core.model.presentation.Options
import org.hyperledger.identus.pollux.prex.PresentationDefinition

final case class CredentialOfferAttachment(options: Options, presentation_definition: PresentationDefinition)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.hyperledger.identus.mercury.model.DidId
import org.hyperledger.identus.mercury.protocol.invitation.v2.Invitation
import org.hyperledger.identus.mercury.protocol.presentproof.{Presentation, ProposePresentation, RequestPresentation}
import org.hyperledger.identus.shared.models.{Failure, WalletAccessContext, WalletId}
import zio.{UIO, URIO, ZIO}
import zio.{URIO, ZIO}

import java.time.temporal.ChronoUnit
import java.time.Instant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.hyperledger.identus.pollux.core.model.error

import org.hyperledger.identus.pollux.core.model.schema.validator.JsonSchemaError
import org.hyperledger.identus.shared.json.JsonSchemaError
import org.hyperledger.identus.shared.models.{Failure, StatusCode}

sealed trait CredentialSchemaError(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.hyperledger.identus.pollux.core.model.error

import org.hyperledger.identus.pollux.core.model.schema.validator.JsonSchemaError
import org.hyperledger.identus.pollux.core.model.DidCommID
import org.hyperledger.identus.pollux.core.service.URIDereferencerError
import org.hyperledger.identus.shared.json.JsonSchemaError
import org.hyperledger.identus.shared.models.{Failure, StatusCode}

sealed trait PresentationError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,7 @@ package org.hyperledger.identus.pollux.core.model.presentation

import io.circe.*
import io.circe.generic.semiauto.*

case class Field(
id: Option[String] = None,
path: Seq[String] = Seq.empty,
name: Option[String] = None,
purpose: Option[String] = None
)
object Field {
given Encoder[Field] = deriveEncoder[Field]
given Decoder[Field] = deriveDecoder[Field]
}

case class Jwt(alg: Seq[String], proof_type: Seq[String])
object Jwt {
given Encoder[Jwt] = deriveEncoder[Jwt]
given Decoder[Jwt] = deriveDecoder[Jwt]
}
case class Ldp(proof_type: Seq[String])
object Ldp {
given Encoder[Ldp] = deriveEncoder[Ldp]
given Decoder[Ldp] = deriveDecoder[Ldp]
}
case class ClaimFormat(jwt: Option[Jwt] = None, ldp: Option[Ldp] = None)
object ClaimFormat {
given Encoder[ClaimFormat] = deriveEncoder[ClaimFormat]
given Decoder[ClaimFormat] = deriveDecoder[ClaimFormat]
}
case class Constraints(fields: Option[Seq[Field]])
object Constraints {
given Encoder[Constraints] = deriveEncoder[Constraints]
given Decoder[Constraints] = deriveDecoder[Constraints]
}

/** Refer to <a href="https://identity.foundation/presentation-exchange/#input-descriptor">Input Descriptors</a>
*/
case class InputDescriptor(
id: String = java.util.UUID.randomUUID.toString(),
name: Option[String] = None,
purpose: Option[String] = None,
format: Option[ClaimFormat] = None,
constraints: Constraints
)
object InputDescriptor {
given Encoder[InputDescriptor] = deriveEncoder[InputDescriptor]
given Decoder[InputDescriptor] = deriveDecoder[InputDescriptor]
}

/** Refer to <a href="https://identity.foundation/presentation-exchange/#presentation-definition">Presentation
* Definition</a>
*/
case class PresentationDefinition(
id: String = java.util.UUID.randomUUID.toString(), // UUID
input_descriptors: Seq[InputDescriptor] = Seq.empty,
name: Option[String] = None,
purpose: Option[String] = None,
format: Option[ClaimFormat] = None
)
object PresentationDefinition {
given Encoder[PresentationDefinition] = deriveEncoder[PresentationDefinition]
given Decoder[PresentationDefinition] = deriveDecoder[PresentationDefinition]
}
import org.hyperledger.identus.pollux.prex.PresentationDefinition

case class Options(challenge: String, domain: String)
object Options {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import org.hyperledger.identus.pollux.core.model.schema.`type`.{
CredentialSchemaType
}
import org.hyperledger.identus.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
import org.hyperledger.identus.pollux.core.model.schema.validator.{JsonSchemaValidator, JsonSchemaValidatorImpl}
import org.hyperledger.identus.pollux.core.service.URIDereferencer
import org.hyperledger.identus.shared.json.{JsonSchemaValidator, JsonSchemaValidatorImpl}
import zio.*
import zio.json.*
import zio.json.ast.Json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ package org.hyperledger.identus.pollux.core.model.schema.`type`
import com.networknt.schema.*
import org.hyperledger.identus.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
import org.hyperledger.identus.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1.*
import org.hyperledger.identus.pollux.core.model.schema.validator.{
JsonSchemaError,
JsonSchemaUtils,
JsonSchemaValidatorImpl,
SchemaSerDes
}
import org.hyperledger.identus.pollux.core.model.schema.Schema
import org.hyperledger.identus.shared.json.{JsonSchemaError, JsonSchemaUtils, JsonSchemaValidatorImpl, SchemaSerDes}
import zio.*
import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.hyperledger.identus.pollux.core.model.schema.`type`

import org.hyperledger.identus.pollux.core.model.schema.validator.SchemaSerDes
import org.hyperledger.identus.shared.json.SchemaSerDes
import zio.*
import zio.json.*
import zio.json.ast.Json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.hyperledger.identus.pollux.core.model.schema.`type`

import org.hyperledger.identus.pollux.core.model.schema.validator.{JsonSchemaError, JsonSchemaValidatorImpl}
import org.hyperledger.identus.pollux.core.model.schema.Schema
import org.hyperledger.identus.shared.json.{JsonSchemaError, JsonSchemaValidatorImpl}
import zio.*
import zio.json.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.hyperledger.identus.pollux.core.model.schema.`type`

import org.hyperledger.identus.pollux.core.model.schema.validator.JsonSchemaError
import org.hyperledger.identus.pollux.core.model.schema.Schema
import org.hyperledger.identus.shared.json.JsonSchemaError
import zio.IO

trait CredentialSchemaType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.hyperledger.identus.pollux.core.model.schema.`type`.anoncred

import org.hyperledger.identus.pollux.core.model.schema.validator.SchemaSerDes
import org.hyperledger.identus.shared.json.SchemaSerDes
import zio.*
import zio.json.*

Expand Down

This file was deleted.

Loading

0 comments on commit 75b2736

Please sign in to comment.