Skip to content

Commit

Permalink
fix: mediator rename package and refactoring (#41)
Browse files Browse the repository at this point in the history
* Refactoring the package

* TrustPingExecutor moved to seprated file and package

* Moved all the protocols executor to protocol package

Signed-off-by: Shailesh Patil <53746241+mineme0110@users.noreply.github.com>
  • Loading branch information
mineme0110 committed May 1, 2024
1 parent 9f34f4e commit b7c8979
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 161 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ lazy val webapp = project
stShortModuleNames := true,
webpackBundlingMode := BundlingMode.LibraryAndApplication(), // BundlingMode.Application,
Compile / scalaJSModuleInitializers += {
org.scalajs.linker.interface.ModuleInitializer.mainMethod("fmgp.webapp.App", "main")
org.scalajs.linker.interface.ModuleInitializer.mainMethod("io.iohk.atala.mediator.App", "main")
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import fmgp.did.comm.protocol.trustping2.*
import io.iohk.atala.mediator.*
import io.iohk.atala.mediator.comm.*
import io.iohk.atala.mediator.db.*
import io.iohk.atala.mediator.protocols.NullProtocolExecuter
import zio.*
import zio.json.*
//TODO pick a better name // maybe "Protocol" only
Expand Down Expand Up @@ -38,14 +39,14 @@ case class ProtocolExecuterCollection[-R](executers: ProtocolExecuter[R]*) exten
plaintextMessage: PlaintextMessage,
): ZIO[R1, MediatorError, Option[EncryptedMessage]] =
selectExecutersFor(plaintextMessage.`type`) match
case None => NullProtocolExecute.execute(plaintextMessage)
case None => NullProtocolExecuter.execute(plaintextMessage)
case Some(px) => px.execute(plaintextMessage)

override def program[R1 <: R](
plaintextMessage: PlaintextMessage,
): ZIO[R1, MediatorError, Action] =
selectExecutersFor(plaintextMessage.`type`) match
case None => NullProtocolExecute.program(plaintextMessage)
case None => NullProtocolExecuter.program(plaintextMessage)
case Some(px) => px.program(plaintextMessage)
}

Expand Down Expand Up @@ -133,52 +134,3 @@ trait ProtocolExecuterWithServices[-R <: ProtocolExecuter.Services] extends Prot
// context: Context
): ZIO[R1, MediatorError, Action]
}

object NullProtocolExecute extends ProtocolExecuter[Any] {

override def suportedPIURI = Seq()
override def program[R1 <: Any](plaintextMessage: PlaintextMessage) =
ZIO.fail(MissingProtocolError(plaintextMessage.`type`))
}

object BasicMessageExecuter extends ProtocolExecuter[Any] {

override def suportedPIURI: Seq[PIURI] = Seq(BasicMessage.piuri)
override def program[R1 <: Any](plaintextMessage: PlaintextMessage) = for {
job <- BasicMessage.fromPlaintextMessage(plaintextMessage) match
case Left(error) => ZIO.fail(MediatorDidError(FailToParse(error)))
case Right(bm) => Console.printLine(bm.toString).mapError(ex => MediatorThrowable(ex))
} yield NoReply
}

class TrustPingExecuter extends ProtocolExecuterWithServices[ProtocolExecuter.Services] {

override def suportedPIURI: Seq[PIURI] = Seq(TrustPing.piuri, TrustPingResponse.piuri)

override def program[R1 <: Agent](
plaintextMessage: PlaintextMessage
): ZIO[R1, MediatorError, Action] = {
// the val is from the match to be definitely stable
val piuriTrustPing = TrustPing.piuri
val piuriTrustPingResponse = TrustPingResponse.piuri

plaintextMessage.`type` match
case `piuriTrustPing` =>
TrustPing.fromPlaintextMessage(plaintextMessage) match
case Left(error) => ZIO.fail(MediatorDidError(FailToParse(error)))
case Right(ping: TrustPingWithOutRequestedResponse) => ZIO.logInfo(ping.toString()) *> ZIO.succeed(NoReply)
case Right(ping: TrustPingWithRequestedResponse) =>
for {
_ <- ZIO.logInfo(ping.toString())
agent <- ZIO.service[Agent]
ret = ping.makeRespond
} yield Reply(ret.toPlaintextMessage)
case `piuriTrustPingResponse` =>
for {
job <- TrustPingResponse.fromPlaintextMessage(plaintextMessage) match
case Left(error) => ZIO.fail(MediatorDidError(FailToParse(error)))
case Right(ping) => ZIO.logInfo(ping.toString())
} yield NoReply
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.iohk.atala.mediator.protocols

import fmgp.crypto.error.FailToParse
import fmgp.did.comm.{PIURI, PlaintextMessage}
import fmgp.did.comm.protocol.basicmessage2.BasicMessage
import io.iohk.atala.mediator.{MediatorDidError, MediatorThrowable}
import io.iohk.atala.mediator.actions.{NoReply, ProtocolExecuter}
import zio.{Console, ZIO}

object BasicMessageExecuter extends ProtocolExecuter[Any] {

override def suportedPIURI: Seq[PIURI] = Seq(BasicMessage.piuri)
override def program[R1 <: Any](plaintextMessage: PlaintextMessage) = for {
job <- BasicMessage.fromPlaintextMessage(plaintextMessage) match
case Left(error) => ZIO.fail(MediatorDidError(FailToParse(error)))
case Right(bm) => Console.printLine(bm.toString).mapError(ex => MediatorThrowable(ex))
} yield NoReply
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.iohk.atala.mediator.protocols

import fmgp.did.comm.PlaintextMessage
import io.iohk.atala.mediator.MissingProtocolError
import io.iohk.atala.mediator.actions.ProtocolExecuter
import zio.ZIO

object NullProtocolExecuter extends ProtocolExecuter[Any] {

override def suportedPIURI = Seq()
override def program[R1 <: Any](plaintextMessage: PlaintextMessage) =
ZIO.fail(MissingProtocolError(plaintextMessage.`type`))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.iohk.atala.mediator.protocols

import fmgp.crypto.error.FailToParse
import fmgp.did.Agent
import fmgp.did.comm.{PIURI, PlaintextMessage}
import fmgp.did.comm.protocol.trustping2.{
TrustPing,
TrustPingResponse,
TrustPingWithOutRequestedResponse,
TrustPingWithRequestedResponse
}
import io.iohk.atala.mediator.{MediatorDidError, MediatorError}
import io.iohk.atala.mediator.actions.{Action, NoReply, ProtocolExecuter, ProtocolExecuterWithServices, Reply}
import zio.ZIO

class TrustPingExecuter extends ProtocolExecuterWithServices[ProtocolExecuter.Services] {

override def suportedPIURI: Seq[PIURI] = Seq(TrustPing.piuri, TrustPingResponse.piuri)

override def program[R1 <: Agent](
plaintextMessage: PlaintextMessage
): ZIO[R1, MediatorError, Action] = {
// the val is from the match to be definitely stable
val piuriTrustPing = TrustPing.piuri
val piuriTrustPingResponse = TrustPingResponse.piuri

plaintextMessage.`type` match
case `piuriTrustPing` =>
TrustPing.fromPlaintextMessage(plaintextMessage) match
case Left(error) => ZIO.fail(MediatorDidError(FailToParse(error)))
case Right(ping: TrustPingWithOutRequestedResponse) => ZIO.logInfo(ping.toString()) *> ZIO.succeed(NoReply)
case Right(ping: TrustPingWithRequestedResponse) =>
for {
_ <- ZIO.logInfo(ping.toString())
agent <- ZIO.service[Agent]
ret = ping.makeRespond
} yield Reply(ret.toPlaintextMessage)
case `piuriTrustPingResponse` =>
for {
job <- TrustPingResponse.fromPlaintextMessage(plaintextMessage) match
case Left(error) => ZIO.fail(MediatorDidError(FailToParse(error)))
case Right(ping) => ZIO.logInfo(ping.toString())
} yield NoReply
}

}
104 changes: 0 additions & 104 deletions webapp/src/main/scala/fmgp/webapp/MyRouter.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fmgp.webapp
package io.iohk.atala.mediator

import scala.scalajs.js.annotation._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fmgp.webapp
package io.iohk.atala.mediator

import scala.scalajs.js.annotation.JSExportTopLevel
import scala.scalajs.js.annotation.JSExport
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fmgp.webapp
package io.iohk.atala.mediator

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExport
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fmgp.webapp
package io.iohk.atala.mediator

import org.scalajs.dom
import com.raquo.laminar.api.L._
Expand All @@ -17,7 +17,7 @@ object MediatorInfo {
goal = Some("RequestMediate"),
accept = Some(Seq("didcomm/v2")),
)
val qrCodeData = OutOfBandPlaintext.from(invitation.toPlaintextMessage).makeURI("https://did.fmgp.app/#/")
val qrCodeData = OutOfBandPlaintext.from(invitation.toPlaintextMessage).makeURI("#/")

val divQRCode = div()
{
Expand Down
56 changes: 56 additions & 0 deletions webapp/src/main/scala/io/iohk/atala/mediator/MyRouter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.iohk.atala.mediator

import com.raquo.laminar.api.L.{_, given}
import com.raquo.waypoint._
import org.scalajs.dom
import upickle.default._

object MyRouter {
sealed abstract class Page(
val title: String,
val icon: String // https://fonts.google.com/icons?selected=Material+Icons+Outlined
)

case object MediatorPage extends Page("Mediator", "diversity_3")

given mediatorPageRW: ReadWriter[MediatorPage.type] = macroRW

given rw: ReadWriter[Page] = macroRW

private val routes = List(
// // http://localhost:8080/?_oob=eyJ0eXBlIjoiaHR0cHM6Ly9kaWRjb21tLm9yZy9vdXQtb2YtYmFuZC8yLjAvaW52aXRhdGlvbiIsImlkIjoiNTk5ZjM2MzgtYjU2My00OTM3LTk0ODctZGZlNTUwOTlkOTAwIiwiZnJvbSI6ImRpZDpleGFtcGxlOnZlcmlmaWVyIiwiYm9keSI6eyJnb2FsX2NvZGUiOiJzdHJlYW1saW5lZC12cCIsImFjY2VwdCI6WyJkaWRjb21tL3YyIl19fQ
Route.static(MediatorPage, root / endOfSegments, Router.localFragmentBasePath),
)

val router = new Router[Page](
routes = routes,
getPageTitle = _.title, // displayed in the browser tab next to favicon
serializePage = page => write(page)(rw), // serialize page data for storage in History API log
deserializePage = pageStr => read(pageStr)(rw), // deserialize the above
// routeFallback = { (_: String) => HomePage },
routeFallback = { (_: String) => MediatorPage },
)(
popStateEvents = windowEvents(_.onPopState), // this is how Waypoint avoids an explicit dependency on Laminar
owner = unsafeWindowOwner // this router will live as long as the window
)

// Note: for fragment ('#') URLs this isn't actually needed.
// See https://github.com/raquo/Waypoint docs for why this modifier is useful in general.
def navigateTo(page: Page): Binder[HtmlElement] = Binder { el =>

val isLinkElement = el.ref.isInstanceOf[dom.html.Anchor]

if (isLinkElement) {
el.amend(href(router.absoluteUrlForPage(page)))
}

// If element is a link and user is holding a modifier while clicking:
// - Do nothing, browser will open the URL in new tab / window / etc. depending on the modifier key
// Otherwise:
// - Perform regular pushState transition
(onClick
.filter(ev => !(isLinkElement && (ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.altKey)))
.preventDefault
--> (_ => router.pushState(page))).bind(el)
}
}

0 comments on commit b7c8979

Please sign in to comment.