diff --git a/mercury/prism-mediator/build.sbt b/mercury/prism-mediator/build.sbt index c6af2b9b21..c4160ae6c0 100644 --- a/mercury/prism-mediator/build.sbt +++ b/mercury/prism-mediator/build.sbt @@ -39,6 +39,7 @@ lazy val D = new { // Test DID comm val didcommx = Def.setting("org.didcommx" % "didcomm" % "0.3.1") + val peerDidcommx = Def.setting("org.didcommx" % "peerdid" % "0.3.0") val didScala = Def.setting("app.fmgp" %% "did" % "0.0.0+74-691ada28+20220902-0934-SNAPSHOT") // For munit https://scalameta.org/munit/docs/getting-started.html#scalajs-setup @@ -124,9 +125,13 @@ lazy val resolver = project // maybe merge into models .settings( libraryDependencies ++= Seq( D.didcommx.value, + D.peerDidcommx.value, + D.munit.value, + D.munitZio.value, "org.jetbrains.kotlin" % "kotlin-runtime" % "1.2.71", "org.jetbrains.kotlin" % "kotlin-stdlib" % "1.7.10", - ) + ), + testFrameworks += new TestFramework("munit.Framework") ) .dependsOn(models) diff --git a/mercury/prism-mediator/resolver/src/main/scala/io/iohk/atala/resolvers/PeerDidResolver.scala b/mercury/prism-mediator/resolver/src/main/scala/io/iohk/atala/resolvers/PeerDidResolver.scala new file mode 100644 index 0000000000..cf95447675 --- /dev/null +++ b/mercury/prism-mediator/resolver/src/main/scala/io/iohk/atala/resolvers/PeerDidResolver.scala @@ -0,0 +1,46 @@ +package io.iohk.atala.resolvers + +import io.iohk.atala.resolvers.UniversalDidResolver.diddocs +import org.didcommx.didcomm.diddoc.{DIDDoc, DIDDocResolver, DIDDocResolverInMemory} +import org.didcommx.peerdid.PeerDIDResolver.resolvePeerDID +import org.didcommx.peerdid.VerificationMaterialFormatPeerDID +import io.circe.Json +import io.circe.generic.auto._ +import io.circe.parser._ +import zio._ +import zio.{Console, Task, UIO, URLayer, ZIO} + +trait PeerDidResolver { + def resolve(did: String): UIO[String] + def resolveDidAsJson(did: String): UIO[Option[Json]] +} + +case class PeerDidResolverImpl() extends PeerDidResolver { + + def resolve(did: String): UIO[String] = { + ZIO.succeed { resolvePeerDID(did, VerificationMaterialFormatPeerDID.MULTIBASE) } + } + + def resolveDidAsJson(did: String): UIO[Option[Json]] = { + ZIO.succeed { + parse(resolvePeerDID(did, VerificationMaterialFormatPeerDID.MULTIBASE)).toOption + } + } +} + +object PeerDidResolver { + + val layer: ULayer[PeerDidResolver] = { + ZLayer.succeedEnvironment( + ZEnvironment(PeerDidResolverImpl()) + ) + } + + def resolve(did: String): URIO[PeerDidResolver, String] = { + ZIO.serviceWithZIO(_.resolve(did)) + } + + def resolveDidAsJson(did: String): URIO[PeerDidResolver, Option[Json]] = { + ZIO.serviceWithZIO(_.resolveDidAsJson(did)) + } +} diff --git a/mercury/prism-mediator/resolver/src/test/scala/io/iohk/atala/resolvers/PeerDidResolverSpec.scala b/mercury/prism-mediator/resolver/src/test/scala/io/iohk/atala/resolvers/PeerDidResolverSpec.scala new file mode 100644 index 0000000000..3f0d72c033 --- /dev/null +++ b/mercury/prism-mediator/resolver/src/test/scala/io/iohk/atala/resolvers/PeerDidResolverSpec.scala @@ -0,0 +1,55 @@ +package io.iohk.atala.resolvers +import zio.* +import munit.* +import io.circe.parser.* + +class PeerDidResolverSpec extends ZSuite { + + testZ("peer did") { + val a = + "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0" + val expectedDidDocJson = parse("""{ + | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", + | "authentication": [ + | { + | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V", + | "type": "Ed25519VerificationKey2020", + | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", + | "publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V" + | }, + | { + | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0#6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg", + | "type": "Ed25519VerificationKey2020", + | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", + | "publicKeyMultibase": "z6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg" + | } + | ], + | "keyAgreement": [ + | { + | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0#6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc", + | "type": "X25519KeyAgreementKey2020", + | "controller": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0", + | "publicKeyMultibase": "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc" + | } + | ], + | "service": [ + | { + | "id": "did:peer:2.Ez6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc.Vz6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V.Vz6MkgoLTnTypo3tDRwCkZXSccTPHRLhF4ZnjhueYAFpEX6vg.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9lbmRwb2ludCIsInIiOlsiZGlkOmV4YW1wbGU6c29tZW1lZGlhdG9yI3NvbWVrZXkiXSwiYSI6WyJkaWRjb21tL3YyIiwiZGlkY29tbS9haXAyO2Vudj1yZmM1ODciXX0#didcommmessaging-0", + | "type": "DIDCommMessaging", + | "serviceEndpoint": "https://example.com/endpoint", + | "routingKeys": [ + | "did:example:somemediator#somekey" + | ], + | "accept": [ + | "didcomm/v2", "didcomm/aip2;env=rfc587" + | ] + | } + | ] + |}""".stripMargin).toOption + val peerDidResolver = PeerDidResolverImpl() + val didDocJson = peerDidResolver.resolveDidAsJson(a) + didDocJson.map(assertEquals(_, expectedDidDocJson)) + // assertEqualsZ(didDocJson,expectedDidDocJson) // this fails need find why + } + +}