diff --git a/mercury/mercury-library/build.sbt b/mercury/mercury-library/build.sbt index 201ff33274..271945a26e 100644 --- a/mercury/mercury-library/build.sbt +++ b/mercury/mercury-library/build.sbt @@ -147,7 +147,7 @@ lazy val protocolLogin = project lazy val protocolReportProblem = project .in(file("protocol-report-problem")) - .settings(name := "protocol-report-problem") + .settings(name := "mercury-protocol-report-problem") .dependsOn(models) lazy val protocolRouting = project diff --git a/mercury/mercury-mediator/.scalafmt.conf b/mercury/mercury-mediator/.scalafmt.conf index 8cd9640023..357cc346c7 100644 --- a/mercury/mercury-mediator/.scalafmt.conf +++ b/mercury/mercury-mediator/.scalafmt.conf @@ -1,2 +1,5 @@ -version = "3.5.3" -runner.dialect = scala3 \ No newline at end of file +version = 3.5.8 +runner.dialect = scala3 + +maxColumn = 120 +trailingCommas = preserve \ No newline at end of file diff --git a/mercury/mercury-mediator/build.sbt b/mercury/mercury-mediator/build.sbt index 25897ae09b..7f7fe42942 100644 --- a/mercury/mercury-mediator/build.sbt +++ b/mercury/mercury-mediator/build.sbt @@ -15,10 +15,7 @@ inThisBuild( val apiBaseDirectory = settingKey[File]("The base directory for Castor API specifications") ThisBuild / apiBaseDirectory := baseDirectory.value / ".." / "api" -ThisBuild / resolvers += Resolver.githubPackages( - "input-output-hk", - "atala-prism-building-blocks" -) +ThisBuild / resolvers += Resolver.githubPackages("input-output-hk", "atala-prism-building-blocks") lazy val V = new { val munit = "1.0.0-M6" // "0.7.29" @@ -33,35 +30,24 @@ lazy val V = new { // https://mvnrepository.com/artifact/io.circe/circe-core val circe = "0.14.2" - val tapir = "1.0.3" - val mercury = "0.2.0" } /** Dependencies */ lazy val D = new { - val mercuryModels = - Def.setting("io.iohk.atala" %% "mercury-data-models" % V.mercury) - // val mercury10 = Def.setting("io.iohk.atala" %% "mercury-agent-didscala" % V.mercury) - // val mercury11 = Def.setting("io.iohk.atala" %% "mercury-agent-core" % V.mercury) - val mercuryAgent = - Def.setting("io.iohk.atala" %% "mercury-agent-didcommx" % V.mercury) - // val mercury2 = Def.setting("io.iohk.atala" %% "protocol-outofband-login" % V.mercury) //FIXME - val mercury4 = Def.setting("io.iohk.atala" %% "mercury-resolver" % V.mercury) - val mercury3 = - Def.setting("io.iohk.atala" %% "mercury-protocol-routing-2_0" % V.mercury) - val mercury5 = Def.setting( - "io.iohk.atala" %% "mercury-protocol-coordinate-mediation" % V.mercury - ) - val mercury6 = - Def.setting("io.iohk.atala" %% "mercury-protocol-invitation" % V.mercury) - val mercury7 = - Def.setting("io.iohk.atala" %% "mercury-protocol-did-exchange" % V.mercury) - val mercury8 = - Def.setting("io.iohk.atala" %% "mercury-protocol-mailbox" % V.mercury) - val mercury9 = - Def.setting("io.iohk.atala" %% "mercury-protocol-connection" % V.mercury) + val mercuryModels = Def.setting("io.iohk.atala" %% "mercury-data-models" % V.mercury) + val mercuryAgent = Def.setting("io.iohk.atala" %% "mercury-agent-didcommx" % V.mercury) + val mercuryResolver = Def.setting("io.iohk.atala" %% "mercury-resolver" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-agent-didscala" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-agent-core" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-protocol-outofband-login" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-protocol-routing-2_0" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-protocol-coordinate-mediation" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-protocol-invitation" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-protocol-did-exchange" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-protocol-mailbox" % V.mercury) + // Def.setting("io.iohk.atala" %% "mercury-protocol-connection" % V.mercury) val zio = Def.setting("dev.zio" %% "zio" % V.zio) val zioStreams = Def.setting("dev.zio" %% "zio-streams" % V.zio) @@ -70,9 +56,7 @@ lazy val D = new { val zioJson = Def.setting("dev.zio" %% "zio-json" % V.zioJson) // val zioHttp = Def.setting("dev.zio" %% "zio-http" % V.zioHttp) // FIXME USE THIS ONE - val zioHttp = Def.setting( - "io.d11" %% "zhttp" % V.zioHttp - ) // REMOVE (this is the old name) + val zioHttp = Def.setting("io.d11" %% "zhttp" % V.zioHttp) // REMOVE (this is the old name) val circeCore = Def.setting("io.circe" %% "circe-core" % V.circe) val circeGeneric = Def.setting("io.circe" %% "circe-generic" % V.circe) @@ -81,8 +65,7 @@ lazy val D = new { // For munit https://scalameta.org/munit/docs/getting-started.html#scalajs-setup val munit = Def.setting("org.scalameta" %% "munit" % V.munit % Test) // For munit zio https://github.com/poslegm/munit-zio - val munitZio = - Def.setting("com.github.poslegm" %% "munit-zio" % V.munitZio % Test) + val munitZio = Def.setting("com.github.poslegm" %% "munit-zio" % V.munitZio % Test) } @@ -97,47 +80,13 @@ lazy val mediator = project .settings(libraryDependencies += D.zio.value) .settings(libraryDependencies += D.munitZio.value) .settings( - libraryDependencies ++= Seq( - "org.http4s" %% "http4s-blaze-server" % "0.23.12", - "ch.qos.logback" % "logback-classic" % "1.2.11", - "com.softwaremill.sttp.tapir" %% "tapir-http4s-server-zio" % V.tapir, - "com.softwaremill.sttp.tapir" %% "tapir-http4s-server" % V.tapir, - "com.softwaremill.sttp.tapir" %% "tapir-json-circe" % V.tapir, - // "com.softwaremill.sttp.tapir" %% "tapir-sttp-stub-server" % V.tapir % Test, - // "com.softwaremill.sttp.client3" %% "circe" % "3.7.1" % Test, - "com.softwaremill.sttp.tapir" %% "tapir-openapi-docs" % V.tapir, - "com.softwaremill.sttp.tapir" %% "tapir-openapi-circe-yaml" % "1.0.0-M9", - "com.softwaremill.sttp.tapir" %% "tapir-swagger-ui" % V.tapir, - "com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-http4s" % "0.19.0-M4", - "com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % "1.0.3", // This helps with Arrow Functions. But swagger is just a pain! - "com.softwaremill.sttp.tapir" %% "tapir-redoc-http4s" % "0.19.0-M4", - "com.softwaremill.sttp.apispec" %% "openapi-circe-yaml" % "0.2.1", - D.mercuryModels.value, - D.mercuryAgent.value, - D.mercury3.value, - D.mercury4.value, - D.mercury5.value, - D.mercury6.value, - D.mercury7.value, - D.mercury8.value, - D.mercury9.value - ), + libraryDependencies ++= Seq(D.mercuryModels.value, D.mercuryAgent.value), Compile / unmanagedResourceDirectories += apiBaseDirectory.value, - testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")) - // Docker / maintainer := "atala-coredid@iohk.io", - // Docker / dockerRepository := Some("atala-prism.io"), - // dockerExposedPorts := Seq(8080), - // dockerBaseImage := "openjdk:11" + testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework")), + // ### Build Docker Image ### + Docker / maintainer := "atala-coredid@iohk.io", + Docker / dockerRepository := Some("atala-prism.io"), + dockerExposedPorts := Seq(8080), + dockerBaseImage := "openjdk:11" ) -// .enablePlugins(JavaAppPackaging, DockerPlugin) -// .dependsOn( -// agentDidcommx, -// resolver, -// protocolInvitation, -// protocolConnection, -// protocolDidExchange, -// protocolMercuryMailbox, -// protocolReportProblem, -// protocolRouting, -// protocolIssueCredential -// ) + .enablePlugins(JavaAppPackaging, DockerPlugin) diff --git a/mercury/mercury-mediator/project/plugins.sbt b/mercury/mercury-mediator/project/plugins.sbt index 027bba25c7..a1ced226cb 100644 --- a/mercury/mercury-mediator/project/plugins.sbt +++ b/mercury/mercury-mediator/project/plugins.sbt @@ -1,3 +1,3 @@ addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6") -// addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.11") +addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.9") //1.9.11 addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.3") diff --git a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Endpoints.scala b/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Endpoints.scala deleted file mode 100644 index d6988b1234..0000000000 --- a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Endpoints.scala +++ /dev/null @@ -1,106 +0,0 @@ -/* -package io.iohk.atala.mercury.mediator - -import io.circe.generic.auto.* -import sttp.apispec.openapi.circe.yaml.* -import sttp.tapir.docs.openapi.OpenAPIDocsInterpreter -import sttp.tapir.generic.auto.* -import sttp.tapir.json.circe.* -import sttp.tapir.ztapir.* -import sttp.tapir.{PublicEndpoint, endpoint, query} -import zio.* - -import java.io.IOException -import scala.collection.mutable -import io.iohk.atala.mercury.mediator.* -import io.iohk.atala.mercury.model.DidId -import io.iohk.atala.mercury.{Agent, AgentService, DidComm} -import io.iohk.atala.mercury.protocol.invitation.* -import io.iohk.atala.mercury.protocol.mailbox.Mailbox.`type` -import io.iohk.atala.mercury.protocol.routing.ForwardMessage - -import scala.jdk.CollectionConverters.* -import java.util.Base64 -// import io.iohk.atala.mercury.DidComm._ // For the dsl - -object Endpoints { - - // val messages = mutable.Map[ConnectionId, List[String]]() - - val messages = mutable.Map[DidId, List[String]]() - - val retrieveMessages: PublicEndpoint[String, Unit, List[String], Any] = endpoint.get - .tag("mediator") - .summary("Retrieve Messages") - .in("messages") - .in(header[String]("did")) - .description("did in the header") - .out(jsonBody[List[String]]) - - val retrieveMessagesServerEndpoint: ZServerEndpoint[Any, Any] = - retrieveMessages.serverLogicSuccess(id => ZIO.succeed(messages.getOrElse(DidId(id), List.empty[String]))) - - val registerMediator: PublicEndpoint[ConnectionId, ErrorInfo, Unit, Any] = - endpoint.post - .tag("mediator") - .summary("Registers the agent with the router.") - .in("register") - .in(jsonBody[ConnectionId]) - .errorOut(httpErrors) - val registerMediatorServerEndpoint: ZServerEndpoint[Any, Any] = - registerMediator.serverLogicSuccess(_ => ZIO.succeed(())) - - val sendMessageServerEndpoint: ZServerEndpoint[DidComm & MailStorage, Any] = { - val sendMessage: PublicEndpoint[String, ErrorInfo, String, Any] = - endpoint.post - .tag("mediator") - .summary("Mediator service endpoint for sending message") - .in("message") - .in(stringBody) - .out(jsonBody[String]) - .errorOut(httpErrors) - sendMessage.serverLogicSuccess { (base64EncodedString: String) => - MediatorProgram.program(base64EncodedString) - - } - } - - // val sendMessageServerEndpoint: ZServerEndpoint[DidComm, Any] = { - // sendMessage.serverLogicSuccess { (base64EncodedString: String) => - // for { - // _ <- Console.printLine(s"received message ...\n $base64EncodedString \n") - // msgInMediator <- DidComm.unpackBase64(base64EncodedString) - // _ <- Console.printLine("msgInMediator: ") - // _ <- Console.printLine(msgInMediator.getMessage) - // result <- ZIO.succeed(messageProcessing(msgInMediator.getMessage)) - - // } yield (result) - - // } - // } - - val createInvitation: PublicEndpoint[CreateInvitation, ErrorInfo, CreateInvitationResponse, Any] = { - endpoint.post - .tag("out-of-band") - .summary("Create a new DIDCommV2 out of band invitation.") - .in("invitations") - .in(jsonBody[CreateInvitation]) - .out(jsonBody[CreateInvitationResponse]) - .errorOut(httpErrors) - .description("Create Invitation Response") - } - - val createInvitationServerEndpoint: ZServerEndpoint[Any, Any] = - createInvitation.serverLogicSuccess(invitation => - ZIO.succeed(CreateInvitationResponse(invitation.goal, invitation.goal_code)) - ) - - val all: List[ZServerEndpoint[DidComm & MailStorage, Any]] = - List( - createInvitationServerEndpoint.widen[DidComm & MailStorage], - retrieveMessagesServerEndpoint.widen[DidComm & MailStorage], - registerMediatorServerEndpoint.widen[DidComm & MailStorage], - sendMessageServerEndpoint - ) -} - */ diff --git a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Mediator.scala b/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Mediator.scala deleted file mode 100644 index bf9202b404..0000000000 --- a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Mediator.scala +++ /dev/null @@ -1,67 +0,0 @@ -/* -package io.iohk.atala.mercury.mediator - -import cats.syntax.all._ -import org.http4s.blaze.server.BlazeServerBuilder -import org.http4s.server.Router -import sttp.tapir.server.http4s.ztapir.ZHttp4sServerInterpreter -import sttp.tapir.swagger.http4s.SwaggerHttp4s -import zio.interop.catz._ -import org.http4s._ - -import io.circe.generic.auto._ -import org.http4s._ -import org.http4s.blaze.server.BlazeServerBuilder -import org.http4s.server.Router -import sttp.tapir.PublicEndpoint -import sttp.tapir.generic.auto._ -import sttp.tapir.json.circe._ -import sttp.tapir.server.http4s.ztapir.ZHttp4sServerInterpreter -import sttp.tapir.swagger.bundle.SwaggerInterpreter -import sttp.tapir.ztapir._ -import zio.interop.catz._ -import zio.{ExitCode, Task, URIO, ZIO, ZIOAppDefault} - -import zio._ -import sttp.tapir.server.ServerEndpoint -import sttp.tapir.Endpoint - -import io.iohk.atala.mercury.DidComm -import io.iohk.atala.mercury.AgentService -import io.iohk.atala.mercury.resolvers.MediatorDidComm -import org.didcommx.didcomm.DIDComm - -type MyTask[+A] = // [_] =>> zio.RIO[io.iohk.atala.DidComm, _] - ZIO[DidComm & MailStorage, Throwable, A] // TODO improve this Throwable (is too much) - -object Mediator extends ZIOAppDefault { - - // API - val apiRoutes: org.http4s.HttpRoutes[MyTask] = ZHttp4sServerInterpreter() - .from(Endpoints.all) - .toRoutes - - // Documentation - val swaggerRoutes: HttpRoutes[MyTask] = { - val allEndpoints: List[Endpoint[_, _, _, _, _]] = Endpoints.all.map(_.endpoint) - ZHttp4sServerInterpreter() - .from(SwaggerInterpreter().fromEndpoints[MyTask](allEndpoints, "Atala Prism Mediator", "0.1.0")) - .toRoutes - } - - val serve: ZIO[DidComm & MailStorage, Throwable, Unit] = MediatorProgram.startLogo *> - ZIO.executor.flatMap(executor => - BlazeServerBuilder[MyTask] - .withExecutionContext(executor.asExecutionContext) - .bindHttp(MediatorProgram.port, "localhost") - .withHttpApp(Router("/" -> (apiRoutes <+> swaggerRoutes)).orNotFound) - .serve - .compile - .drain - ) - - override def run = serve - .provide(MediatorDidComm.mediator ++ MailStorage.layer) - .exitCode -} - */ diff --git a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/MediatorProgram.scala b/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/MediatorProgram.scala index 8ed652e8ac..51f3f646cf 100644 --- a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/MediatorProgram.scala +++ b/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/MediatorProgram.scala @@ -11,9 +11,16 @@ import io.iohk.atala.mercury.model.Message import io.circe.Json.* import io.circe.parser.* import io.circe.JsonObject -import io.iohk.atala.mercury.mediator.MediationState.{Denied, Granted, Requested} +import io.iohk.atala.mercury.mediator.MediationState.{ + Denied, + Granted, + Requested +} import io.iohk.atala.mercury.protocol.coordinatemediation.Keylist.Body -import io.iohk.atala.mercury.protocol.coordinatemediation.{MediateDeny, MediateGrant} +import io.iohk.atala.mercury.protocol.coordinatemediation.{ + MediateDeny, + MediateGrant +} import io.iohk.atala.mercury.Agent object MediatorProgram { val port = 8080 @@ -62,7 +69,7 @@ object MediatorProgram { ret <- // messageProcessing(mediatorMessage) { // val recipient = DidId(mediatorMessage.getTo.asScala.toList.head) // FIXME unsafe - mediatorMessage.`type` match { + mediatorMessage.piuri match { case "https://didcomm.org/routing/2.0/forward" => for { _ <- ZIO.logInfo("Mediator Forward Message: " + mediatorMessage.toString) @@ -123,22 +130,32 @@ object MediatorProgram { } } - def makeMsg(to: DidId, messageState: MediationState): ZIO[DidComm, Nothing, Message] = for { + def makeMsg( + to: DidId, + messageState: MediationState + ): ZIO[DidComm, Nothing, Message] = for { from <- ZIO.service[DidComm].map(_.myDid) message = messageState match case Granted => val body = MediateGrant.Body(routing_did = from.value) val mediateGrant = - MediateGrant(id = java.util.UUID.randomUUID().toString, `type` = MediateGrant.`type`, body = body) + MediateGrant( + id = java.util.UUID.randomUUID().toString, + `type` = MediateGrant.`type`, + body = body + ) Message( piuri = mediateGrant.`type`, from = Some(from), to = Some(to), - body = Map("routing_did" -> from.value), + body = Map("routing_did" -> from.value) ) case _ => val mediateDeny = - MediateDeny(id = java.util.UUID.randomUUID().toString, `type` = MediateDeny.`type`) + MediateDeny( + id = java.util.UUID.randomUUID().toString, + `type` = MediateDeny.`type` + ) Message( piuri = mediateDeny.`type`, from = Some(from), diff --git a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Models.scala b/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Models.scala index cda7d9205e..2f368cc48f 100644 --- a/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Models.scala +++ b/mercury/mercury-mediator/src/main/scala/io/iohk/atala/mercury/mediator/Models.scala @@ -1,23 +1,20 @@ package io.iohk.atala.mercury.mediator -import sttp.model.StatusCode -import sttp.tapir.EndpointOutput.OneOf -import sttp.tapir.generic.auto.* -import sttp.tapir.json.circe.jsonBody -import sttp.tapir.ztapir.{oneOf, oneOfVariant} -import io.circe.generic.auto._ - -import javax.xml.crypto.dsig.keyinfo.KeyInfo - -case class ConnectionId(connectionId: String) -case class Message(connectionId: String, msg: String) - -val httpErrors: OneOf[ErrorInfo, ErrorInfo] = oneOf[ErrorInfo]( - oneOfVariant(StatusCode.InternalServerError, jsonBody[InternalServerError]), - oneOfVariant(StatusCode.BadRequest, jsonBody[BadRequest]), - oneOfVariant(StatusCode.NotFound, jsonBody[NotFound]) +case class PublicKey( + id: String, + `type`: String, + controller: String, + publicKeyBase58: String +) +case class MediateRequest( + id: String, + `@type`: String, + invitationId: String, + publicKey: PublicKey +) +case class MediateResponse( + id: String, + `@type`: String, + endpoint: String, + routing_keys: Seq[String] ) - -case class PublicKey(id: String, `type`: String, controller: String, publicKeyBase58: String) -case class MediateRequest(id: String, `@type`: String, invitationId: String, publicKey: PublicKey) -case class MediateResponse(id: String, `@type`: String, endpoint: String, routing_keys: Seq[String])