Skip to content

Commit

Permalink
feat(mercury): Bump Mercury version in Mediator (#110)
Browse files Browse the repository at this point in the history
Bump Mercury version to 0.4.0 in Mediator
Fix Mediator for the new Mercury version
Add debug endpoint to generate peer did
Add debug endpoint to generate the peer did document
  • Loading branch information
FabioPinheiro authored Nov 10, 2022
1 parent 4158f8c commit 3fd370a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion mercury/mercury-mediator/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ lazy val V = new {
// https://mvnrepository.com/artifact/io.circe/circe-core
val circe = "0.14.2"

val mercury = "0.2.0"
val mercury = "0.4.0"
}

/** Dependencies */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@ 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.circe.syntax._
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
import io.circe.JsonNumber
object MediatorProgram {
val port = 8080

Expand Down Expand Up @@ -81,10 +76,10 @@ object MediatorProgram {
msg = mediatorMessage.attachments.map(_.data.toString).head // FIXME Head
// msgxx = mediatorMessage.getAttachments().get(0).getData().toJSONObject().get("json").toString() //FIXME REMOVE
nextRecipient = DidId(
mediatorMessage.body // REMOVE mediatorMessage.getBody.asScala
.get("next")
.map(e => e.asInstanceOf[String])
.get
mediatorMessage
.body("next")
.flatMap(e => e.asString)
.get // TODO remove get
)
_ <- ZIO.log(s"Store Massage for ${nextRecipient}: " + mediatorMessage.to)
// db <- ZIO.service[ZState[MyDB]]
Expand Down Expand Up @@ -148,7 +143,7 @@ object MediatorProgram {
piuri = mediateGrant.`type`,
from = Some(from),
to = Some(to),
body = Map("routing_did" -> from.value)
body = JsonObject("routing_did" -> from.value.asJson)
)
case _ =>
val mediateDeny =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.iohk.atala.mercury.model.DidId
import io.iohk.atala.mercury.{Agent, DidComm, InvitationPrograms, MediaTypes}
import io.iohk.atala.mercury.resolvers.MediatorDidComm
import scala.io.Source
import java.util.Base64

/** sbt "mediator/runMain io.iohk.atala.mercury.mediator.ZhttpMediator" */
object ZhttpMediator extends ZIOAppDefault {
Expand All @@ -18,7 +19,7 @@ object ZhttpMediator extends ZIOAppDefault {
val app: HttpApp[DidComm & MailStorage & ConnectionStorage, Throwable] = Http.collectZIO[Request] {
case req @ Method.POST -> !!
if req.headersAsList.exists(h => h._1.equalsIgnoreCase(header._1) && h._2.equalsIgnoreCase(header._2)) =>
req.bodyAsString
req.body.asString
.flatMap(data => MediatorProgram.program(data))
.map(str => Response.text(str))
case Method.GET -> !! / "api" / "openapi-spec.yaml" =>
Expand All @@ -31,6 +32,61 @@ object ZhttpMediator extends ZIOAppDefault {
val serverUrl = s"http://locahost:${MediatorProgram.port}?_oob="
InvitationPrograms.createInvitationV2().map(oob => Response.text(serverUrl + oob))

case req @ Method.GET -> !! / "peer" / keyAgreementBase64 / keyAuthenticationBase64 / endpointBase64 => // REMOVE For debug purpose
import io.iohk.atala.mercury.PeerDID
import com.nimbusds.jose.jwk.Curve
import com.nimbusds.jose.jwk.OctetKeyPair

// REMOVE Example for debug purpose
// val keyAgreement = OctetKeyPair.parse("""{"kty":"OKP","d":"_4e_7Xn6TWV9ic_Fo187hGgsDi566sTbfI3yj167Gqc","crv":"X25519","x":"KQ-H8O6zyAtakmN1yc-Dztjy9RLhXNHHbyjoscsh_lQ"}""")
// "eyJrdHkiOiJPS1AiLCJkIjoiXzRlXzdYbjZUV1Y5aWNfRm8xODdoR2dzRGk1NjZzVGJmSTN5ajE2N0dxYyIsImNydiI6IlgyNTUxOSIsIngiOiJLUS1IOE82enlBdGFrbU4xeWMtRHp0ank5UkxoWE5ISGJ5am9zY3NoX2xRIn0="
// val keyAuthentication = OctetKeyPair.parse("""{"kty":"OKP","d":"ctDfZA_duF92Ypx3xsZgv2yCUdusd-3oy-9pBkp5Rgk","crv":"Ed25519","x":"Zjkryaft13epwbriH7TrJpgl3Y1vB1Gibnv2WqDskZk"}""")
// "eyJrdHkiOiJPS1AiLCJkIjoiY3REZlpBX2R1RjkyWXB4M3hzWmd2MnlDVWR1c2QtM295LTlwQmtwNVJnayIsImNydiI6IkVkMjU1MTkiLCJ4IjoiWmprcnlhZnQxM2Vwd2JyaUg3VHJKcGdsM1kxdkIxR2libnYyV3FEc2taayJ9"
// val endpoint = "http://localhost:9999"
// aHR0cDovL2xvY2FsaG9zdDo5OTk5

val keyAgreement = OctetKeyPair.parse(
String(
Base64
.getUrlDecoder()
.decode(keyAgreementBase64)
)
)
val keyAuthentication = OctetKeyPair.parse(
String(
Base64
.getUrlDecoder()
.decode(keyAuthenticationBase64)
)
)

val agentDID = for {
peer <- ZIO.succeed(
PeerDID.makePeerDid(
jwkForKeyAgreement = keyAgreement,
jwkForKeyAuthentication = keyAuthentication,
serviceEndpoint = Some(
String(
Base64
.getUrlDecoder()
.decode(endpointBase64)
)
)
)
)
_ <- Console.printLine(s"New PEER DID: ${peer.did}") *>
Console.printLine(s"JWK for KeyAgreement: ${peer.jwkForKeyAgreement.toJSONString}") *>
Console.printLine(s"JWK for KeyAuthentication: ${peer.jwkForKeyAuthentication.toJSONString}")
ret <- ZIO.succeed(Response.text(peer.did.value.toString))
} yield (ret)
agentDID

case req @ Method.GET -> !! / "resolve" / did => // REMOVE For debug purpose
import org.didcommx.peerdid.VerificationMaterialFormatPeerDID
def getDIDDocument = org.didcommx.peerdid.PeerDIDResolver
.resolvePeerDID(did, VerificationMaterialFormatPeerDID.JWK)
ZIO.succeed(Response.text(getDIDDocument))

case req =>
ZIO.succeed(
Response.text(
Expand Down

0 comments on commit 3fd370a

Please sign in to comment.