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

feat(mercury): forward messaging to mediator #264

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package io.iohk.atala.mercury
import scala.jdk.CollectionConverters.*
import zio._

import io.circe._
import io.circe.Json._
import io.circe.parser._
import io.circe.JsonObject
import io.iohk.atala.mercury.model._
import io.iohk.atala.mercury.model.error._
import io.iohk.atala.mercury.protocol.routing._
import io.iohk.atala.resolvers.DIDResolver
import org.didcommx.didcomm.common.VerificationMethodType
import org.didcommx.didcomm.common.VerificationMaterialFormat
Expand All @@ -14,6 +19,29 @@ case class ServiceEndpoint(uri: HttpOrDID, accept: Option[Seq[String]], routingK

object MessagingService {

def isForwardMessage[Service <: DidComm, Resolver <: DIDResolver](
didCommService: Service,
resolver: Resolver,
didCommServiceEndpoint: ServiceEndpoint,
message: Message,
encrypted: EncryptedMessage): ZIO[Any, Throwable, EncryptedMessage] = {
if (didCommServiceEndpoint.uri.startsWith("did:")) {
Console.printLine("RoutingDID:" + DidId(didCommServiceEndpoint.uri))
didCommService.packEncrypted(
ForwardMessage(
from = message.from.get,
to = DidId(didCommServiceEndpoint.uri),
expires_time = None,
body = ForwardBody(next = message.to.head), // TODO check msg header
attachments = Seq(AttachmentDescriptor.buildJsonAttachment(payload = encrypted.asJson)),
).asMessage,
to = DidId(didCommServiceEndpoint.uri)
)
FabioPinheiro marked this conversation as resolved.
Show resolved Hide resolved
} else {
ZIO.succeed(encrypted)
}
}

/** Encrypt and send a Message via HTTP
*
* TODO Move this method to another model
Expand All @@ -30,11 +58,10 @@ object MessagingService {
case head +: tail => // TODO support for multiple destinations
ZIO.fail(new RuntimeException("TODO multiple destinations"))
}
_ <- Console.printLine("Encrypted Message")
encryptedMessage <- didCommService.packEncrypted(msg, to = msg.to.head) // TODO head

encryptedForwardMessage <- didCommService.packEncryptedForward(msg, to = msg.to.head) // TODO head
jsonString = encryptedForwardMessage.string

didCommService <- resolver
didCommServiceUrl <- resolver
.didCommServices(sendToDID) /* Seq[DIDCommService] */
.flatMap {
case Seq() =>
Expand All @@ -57,14 +84,18 @@ object MessagingService {
)
)
}
serviceEndpoint <-
if (didCommService.uri.startsWith("did:"))
resolver
.didCommServices(DidId(didCommService.uri))
.map(_.toSeq.head.getServiceEndpoint()) // TODO this is not safe and also need to be recursive
else ZIO.succeed(didCommService.uri)

_ <- Console.printLine("Sending to " + serviceEndpoint)
_ <- Console.printLine("Forward message")
sendMsg = isForwardMessage(didCommService, resolver, didCommServiceUrl, msg, encryptedMessage)
jsonString <- sendMsg.map(_.string)

serviceEndpoint <- if (didCommServiceUrl.uri.startsWith("did:"))
resolver
.didCommServices(DidId(didCommServiceUrl.uri))
.map(_.toSeq.head.getServiceEndpoint()) // TODO this is not safe and also need to be recursive
else ZIO.succeed(didCommServiceUrl.uri)

_ <- Console.printLine("Sending to" + serviceEndpoint)

res <- HttpClient.postDIDComm(serviceEndpoint, jsonString)
} yield (res)
}.catchAll { case ex =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ object AttachmentDescriptor {
AttachmentDescriptor(id, mediaType, Base64(encoded)) // use JsonData or Base64 by default?
}

def buildJsonAttachment[A](
id: String = java.util.UUID.randomUUID.toString,
payload: A,
mediaType: Option[String] = Some("application/json")
)(using Encoder[A]): AttachmentDescriptor = {
val jsonObject = payload.asJson.asObject.getOrElse(JsonObject.empty)
AttachmentDescriptor(id, mediaType, JsonData(jsonObject)) // use JsonData or Base64 by default?
}

given attachmentDescriptorEncoderV1: Encoder[AttachmentDescriptor] = (a: AttachmentDescriptor) => {
Json.obj(
"@id" -> a.id.asJson,
Expand Down