Skip to content

Commit

Permalink
build(mercury): New module agent-cli-didcommx (#125)
Browse files Browse the repository at this point in the history
New module agent-cli-didcommx
Cleanup dependencies from agent-didcommx
  • Loading branch information
FabioPinheiro authored Nov 15, 2022
1 parent 3804e31 commit 7f2434d
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 59 deletions.
4 changes: 4 additions & 0 deletions mercury/mercury-library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ flowchart BT
alice
bob
mediator
agent-cli-didcommx
end
resolver --> models
Expand Down Expand Up @@ -75,6 +76,9 @@ flowchart BT
agent-didcommx --> agent
agent-didcommx -.-> didcommx
agent-cli-didcommx -.-> http
agent-cli-didcommx --> agent-didcommx
agent-didscala --> agent
agent-didscala -.-> did-scala
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import zio._
import zhttp.service.{ChannelFactory, Client, EventLoopGroup}
import zhttp.http.{Method, Headers}

import io.iohk.atala.mercury.Agent
import io.iohk.atala.mercury.AgentService
import io.iohk.atala.mercury.MediaTypes
import io.iohk.atala.mercury._
import io.iohk.atala.mercury.{given}
import io.circe.Printer
import io.circe.syntax._
Expand All @@ -21,8 +19,8 @@ import io.circe._, io.circe.parser._

@main def AgentClientAlice() = {

val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val app = AgentPrograms.pickupMessageProgram.provide(env, AgentService.alice)
val app = AgentPrograms.pickupMessageProgram
.provide(AgentService.alice, HttpClientZhttp.layer)

Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package io.iohk.atala

import zio._
import io.iohk.atala.mercury.Agent
import io.iohk.atala.mercury.AgentService
import io.iohk.atala.mercury.MediaTypes
import io.iohk.atala.mercury._
import io.iohk.atala.mercury.model.UnpackMessage
import io.iohk.atala.mercury.protocol.mailbox.Mailbox.ReadMessage
import org.didcommx.didcomm.message.Attachment.Data.Json
import zhttp.service._

@main def AgentClientBob() = {

val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val app = AgentPrograms.senderProgram.provide(env, AgentService.bob)
val app = AgentPrograms.senderProgram
.provide(AgentService.bob, HttpClientZhttp.layer)

Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ package io.iohk.atala
import zio._
import zhttp.service.{ChannelFactory, Client, EventLoopGroup}
import zhttp.http.{Headers, Method}
import io.iohk.atala.mercury.Agent
import io.iohk.atala.mercury.AgentService
import io.iohk.atala.mercury.MediaTypes
import io.iohk.atala.mercury._
import io.iohk.atala.mercury.model.UnpackMessage
import io.iohk.atala.mercury.protocol.mailbox.Mailbox.ReadMessage
import org.didcommx.didcomm.message.Attachment.Data.Json

import io.iohk.atala.mercury.protocol.coordinatemediation._
import io.iohk.atala.mercury.CoordinateMediationPrograms
import io.iohk.atala.mercury.InvitationPrograms
import org.didcommx.didcomm.message.Attachment.Data.Json

@main def AgentClientGetInvitation() = {
val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val app =
InvitationPrograms.getInvitationProgram("http://localhost:8000/oob_url").provide(env)
InvitationPrograms
.getInvitationProgram("http://localhost:8000/oob_url")
.provide(HttpClientZhttp.layer)

Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

Expand All @@ -28,7 +24,7 @@ import io.iohk.atala.mercury.InvitationPrograms
val mediatorURL = "http://localhost:8000"
val app = CoordinateMediationPrograms
.senderMediationRequestProgram(mediatorURL)
.provide(env, AgentService.charlie)
.provide(AgentService.charlie, HttpClientZhttp.layer)

Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

Expand All @@ -39,6 +35,6 @@ import io.iohk.atala.mercury.InvitationPrograms
val mediatorURL = "http://localhost:8080"
val app = CoordinateMediationPrograms
.senderMediationRequestProgram(mediatorURL)
.provide(env, AgentService.charlie)
.provide(AgentService.charlie, HttpClientZhttp.layer)
Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,15 @@ object AgentCli extends ZIOAppDefault {
} yield (peer)

didCommLayer = agentLayer(agentDID)
layers: ZLayer[Any, Nothing, AgentServiceAny & HttpClient] = didCommLayer ++ HttpClientZhttp.layer

_ <- options(
Seq(
"none" -> ZIO.unit,
"Show DID" -> Console.printLine(agentDID),
"Get DID Document" -> Console.printLine("DID Document:") *> Console.printLine(agentDID.getDIDDocument),
"Start WebServer endpoint" -> startEndpoint.provide(didCommLayer),
"Ask for Mediation Coordinate" -> askForMediation.provide(didCommLayer),
"Ask for Mediation Coordinate" -> askForMediation.provide(layers),
"Generate login invitation" -> generateLoginInvitation.provide(didCommLayer),
"Login with DID" -> loginInvitation.provide(didCommLayer),
"Propose Credential" -> proposeAndSendCredential.provide(didCommLayer),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.iohk.atala.mercury

import zio._
import zhttp.service._
import zhttp.http._
import io.iohk.atala.mercury._

object HttpClientZhttp {
val layer = ZLayer.succeed(new HttpClientZhttp())
}

class HttpClientZhttp extends HttpClient {

val env = ChannelFactory.auto ++ EventLoopGroup.auto()

override def get(url: String): Task[HttpResponseBody] =
Client
.request(url)
.provideSomeLayer(env)
.flatMap(_.body.asString)
.map(e => HttpResponseBody(e))

def postDIDComm(url: String, data: String): Task[HttpResponseBody] =
Client
.request(
url = url, // TODO make ERROR type
method = Method.POST,
headers = Headers("content-type" -> "application/didcomm-encrypted+json"),
// headers = Headers("content-type" -> MediaTypes.contentTypeEncrypted),
content = Body.fromChunk(Chunk.fromArray(data.getBytes)),
// ssl = ClientSSLOptions.DefaultSSL,
)
.provideSomeLayer(env)
.flatMap(_.body.asString)
.map(e => HttpResponseBody(e))
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.iohk.atala

import zio._
import zhttp.service.Client
import zhttp.http._

import io.circe._
import io.circe.Json._
Expand Down Expand Up @@ -54,16 +52,13 @@ object AgentPrograms {
_ <- Console.printLine("Sending bytes ...")
jsonString = encryptedMsg.string
// HTTP
res <- Client.request(
httpClient <- ZIO.service[HttpClient]
res <- httpClient.postDIDComm(
url = "http://localhost:8080",
method = Method.POST,
headers = Headers("content-type" -> MediaTypes.contentTypeEncrypted),
content = Body.fromChunk(Chunk.fromArray(jsonString.getBytes)),
// ssl = ClientSSLOptions.DefaultSSL,
data = jsonString
)
data <- res.body.asString
_ <- Console.printLine("Receiving the message ..." + data)
messageReceived <- bob.unpack(data)
_ <- Console.printLine("Receiving the message ..." + res.bodyAsString)
messageReceived <- bob.unpack(res.bodyAsString)
_ <- Console.printLine("Unpacking and decrypting the received message ...")
_ <- Console.printLine(
"\n*********************************************************************************************************************************\n"
Expand Down Expand Up @@ -101,15 +96,12 @@ object AgentPrograms {
// HTTP

alice <- ZIO.service[DidComm]
res <- Client.request(
httpClient <- ZIO.service[HttpClient]
res <- httpClient.postDIDComm(
url = "http://localhost:8080",
method = Method.POST,
headers = Headers("content-type" -> MediaTypes.contentTypeEncrypted),
content = Body.fromChunk(Chunk.fromArray(jsonString.getBytes)),
// ssl = ClientSSLOptions.DefaultSSL,
data = jsonString
)
data <- res.body.asString
_ <- Console.printLine(data)
_ <- Console.printLine(res.bodyAsString)
} yield ()

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package io.iohk.atala.mercury

import scala.util.chaining._
import zio._
import zhttp.service.Client
import zhttp.http._
import io.circe.Json._
import io.circe.syntax._
import io.circe.parser._
Expand Down Expand Up @@ -48,17 +46,11 @@ object CoordinateMediationPrograms {
jsonString = encryptedMessage.string
_ <- ZIO.log(jsonString)

res <- Client.request(
url = mediatorURL,
method = Method.POST,
headers = Headers("content-type" -> MediaTypes.contentTypeEncrypted),
content = Body.fromChunk(Chunk.fromArray(jsonString.getBytes)),
// ssl = ClientSSLOptions.DefaultSSL,
)
data <- res.body.asString
_ <- ZIO.log(data)
client <- ZIO.service[HttpClient]
res <- client.postDIDComm(mediatorURL, jsonString)
_ <- ZIO.log(res.bodyAsString)

messageReceived <- agentService.unpack(data)
messageReceived <- agentService.unpack(res.bodyAsString)
_ <- Console.printLine("Unpacking and decrypting the received message ...")
_ <- Console.printLine("*" * 100)
_ <- Console.printLine(toPrettyJson(messageReceived.getMessage.toString))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.iohk.atala.mercury

import zio._

case class HttpResponseBody(bodyAsString: String)

trait HttpClient {
def get(url: String): Task[HttpResponseBody]
def postDIDComm(url: String, data: String): Task[HttpResponseBody]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.iohk.atala.mercury

import zio.*
import zhttp.service.Client
import io.circe.Json.*
import io.circe.parser.*
import io.circe.JsonObject
Expand All @@ -20,9 +19,9 @@ object InvitationPrograms {

def getInvitationProgram(url: String) = for {
_ <- ZIO.log("#### Get Invitation ####")
res <- Client.request(url = url)
data <- res.body.asString
message = OutOfBand.parseInvitation(data)
client <- ZIO.service[HttpClient]
res <- client.get(url = url)
message = OutOfBand.parseInvitation(res.bodyAsString)
_ <- ZIO.log(s"*******OutOfBand********${message.toString}")
} yield (message)

Expand Down
15 changes: 11 additions & 4 deletions mercury/mercury-library/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ lazy val D = new {
val zioSLF4J = Def.setting("dev.zio" %% "zio-logging-slf4j" % V.zioLogging)
val zioJson = Def.setting("dev.zio" %% "zio-json" % V.zioJson)

// val zioHttp = Def.setting("dev.zio" %% "zio-http" % V.zioHttp) // FIXME USE THIS ONE
// TODO waiting for https://github.com/zio/zio-http/pull/1774 to use zio-http
// val zioHttp = Def.setting("dev.zio" %% "zio-http" % "0.0.1") // FIXME USE THIS ONE
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)
Expand Down Expand Up @@ -164,7 +165,6 @@ lazy val protocolIssueCredential = project
.settings(libraryDependencies += D.munitZio.value)
.dependsOn(models)


lazy val protocolPresentProof = project
.in(file("protocol-present-proof"))
.settings(name := "mercury-protocol-present-proof")
Expand Down Expand Up @@ -203,7 +203,7 @@ lazy val agent = project // maybe merge into models
.in(file("agent"))
.settings(name := "mercury-agent-core")
.settings(libraryDependencies += "com.google.zxing" % "core" % "3.5.0")
.settings(libraryDependencies ++= Seq(D.zioLog.value, D.zioHttp.value)) // , D.zioSLF4J.value))
.settings(libraryDependencies ++= Seq(D.zioLog.value)) // , D.zioSLF4J.value))
.dependsOn(
models,
resolver,
Expand All @@ -216,13 +216,20 @@ lazy val agent = project // maybe merge into models
protocolPresentProof,
)

/** Demos agents and services implementation with didcommx */
/** agents implementation with didcommx */
lazy val agentDidcommx = project
.in(file("agent-didcommx"))
.settings(name := "mercury-agent-didcommx")
.settings(libraryDependencies += D.didcommx.value)
.dependsOn(agent)

/** Demos agents and services implementation with didcommx */
lazy val agentCliDidcommx = project
.in(file("agent-cli-didcommx"))
.settings(name := "mercury-agent-cli-didcommx")
.settings(libraryDependencies += D.zioHttp.value)
.dependsOn(agentDidcommx)

///** TODO Demos agents and services implementation with did-scala */
lazy val agentDidScala =
project
Expand Down

0 comments on commit 7f2434d

Please sign in to comment.