From dfaf6e17c20b3a108a7a8fbe857be5610dd68dbe Mon Sep 17 00:00:00 2001 From: Fabio Pinheiro Date: Thu, 13 Jul 2023 10:34:19 +0100 Subject: [PATCH] fix: Id/hash of the message must be deterministic (#53) Id/hash of the message must be deterministic Use SHA1 for hashing message Fix for ATL-5213 Other minor bug fixes Signed-off-by: Fabio Pinheiro Signed-off-by: Shailesh Patil --- build.sbt | 9 +++++++-- .../scala/io/iohk/atala/mediator/app/MediatorAgent.scala | 2 +- .../scala/io/iohk/atala/mediator/db/DataModels.scala | 6 ++++-- .../io/iohk/atala/mediator/db/UserAccountRepo.scala | 4 ++-- .../iohk/atala/mediator/protocols/PickupExecuter.scala | 4 ++-- .../io/iohk/atala/mediator/db/MessageItemRepoSpec.scala | 4 ++-- .../io/iohk/atala/mediator/db/UserAccountRepoSpec.scala | 4 ++-- 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/build.sbt b/build.sbt index e1d8bde1..0aca5f7d 100644 --- a/build.sbt +++ b/build.sbt @@ -9,7 +9,7 @@ inThisBuild( /** Versions */ lazy val V = new { - val scalaDID = "0.1.0-M4" + val scalaDID = "0.1.0-M5" // val scalajsJavaSecureRandom = "1.0.0" // FIXME another bug in the test framework https://github.com/scalameta/munit/issues/554 @@ -98,6 +98,9 @@ lazy val NPM = new { val qrcode = Seq("qrcode-generator" -> "1.4.4") val materialDesign = Seq("material-components-web" -> V.materialComponents) + + val sha1 = Seq("js-sha1" -> "0.6.0", "@types/js-sha1" -> "0.6.0") + val sha256 = Seq("js-sha256" -> "0.9.0") } inThisBuild( @@ -183,6 +186,8 @@ lazy val httpUtils = crossProject(JSPlatform, JVMPlatform) // project .settings( libraryDependencies += D.scalaDID.value, ) + .jsConfigure(scalaJSBundlerConfigure) + .jsSettings(Compile / npmDependencies ++= NPM.sha1 ++ NPM.sha256) .jvmSettings( libraryDependencies += D.zioHttp.value, ) @@ -264,7 +269,7 @@ lazy val webapp = project libraryDependencies ++= Seq(D.laminar.value, D.waypoint.value, D.upickle.value), libraryDependencies ++= Seq(D.zio.value, D.zioJson.value), libraryDependencies ++= Seq(D.scalaDID.value, D.scalaDID_peer.value), - Compile / npmDependencies ++= NPM.qrcode ++ NPM.materialDesign + Compile / npmDependencies ++= NPM.qrcode ++ NPM.materialDesign ++ NPM.sha1 ++ NPM.sha256, ) .settings( stShortModuleNames := true, diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala index 5280b08d..125b2483 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/app/MediatorAgent.scala @@ -109,7 +109,7 @@ case class MediatorAgent( Option[EncryptedMessage] ] = ZIO - .logAnnotate("msgHash", msg.hashCode.toString) { + .logAnnotate("msgHash", msg.sha1) { for { _ <- ZIO.log("receivedMessage") maybeSyncReplyMsg <- diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/db/DataModels.scala b/mediator/src/main/scala/io/iohk/atala/mediator/db/DataModels.scala index f7b43938..993257b0 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/db/DataModels.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/db/DataModels.scala @@ -5,12 +5,14 @@ import fmgp.did.comm.* import reactivemongo.api.bson.* import java.time.Instant -type HASH = Int +type HASH = String // messages case class MessageItem(_id: HASH, msg: EncryptedMessage, headers: ProtectedHeader) object MessageItem { - def apply(msg: EncryptedMessage): MessageItem = new MessageItem(msg.hashCode(), msg, msg.`protected`.obj) + def apply(msg: EncryptedMessage): MessageItem = { + new MessageItem(msg.sha1, msg, msg.`protected`.obj) + } given BSONDocumentWriter[MessageItem] = Macros.writer[MessageItem] given BSONDocumentReader[MessageItem] = Macros.reader[MessageItem] } diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/db/UserAccountRepo.scala b/mediator/src/main/scala/io/iohk/atala/mediator/db/UserAccountRepo.scala index 94cbc422..d78b3d1b 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/db/UserAccountRepo.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/db/UserAccountRepo.scala @@ -124,7 +124,7 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon BSONDocument( "$elemMatch" -> BSONDocument( - "hash" -> msg.hashCode, + "hash" -> msg.sha1, "recipient" -> BSONDocument("$in" -> recipients.map(_.did)) ) ) @@ -135,7 +135,7 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon "$push" -> BSONDocument( "messagesRef" -> BSONDocument( "$each" -> - recipients.map(recipient => MessageMetaData(msg.hashCode, recipient)) + recipients.map(recipient => MessageMetaData(msg.sha1, recipient)) ) ) ) diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/protocols/PickupExecuter.scala b/mediator/src/main/scala/io/iohk/atala/mediator/protocols/PickupExecuter.scala index a14f12bd..42511739 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/protocols/PickupExecuter.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/protocols/PickupExecuter.scala @@ -90,7 +90,7 @@ object PickupExecuter from = m.to.asFROM, to = m.from.asTO, recipient_did = m.recipient_did, - attachments = messagesToReturn.map(m => (m.hashCode.toString, m.msg)).toMap, + attachments = messagesToReturn.map(m => (m._id, m.msg)).toMap, ) } yield SyncReplyOnly(deliveryRequest.toPlaintextMessage) case m: MessageDelivery => @@ -112,7 +112,7 @@ object PickupExecuter didRequestingMessages = m.from.asFROMTO mDidAccount <- repoDidAccount.markAsDelivered( didRequestingMessages.toDID, - m.message_id_list.map(e => e.toInt) // TODO have it safe 'toInt' + m.message_id_list ) } yield NoReply case m: LiveModeChange => ZIO.logWarning("LiveModeChange not implemented") *> ZIO.succeed(NoReply) // TODO diff --git a/mediator/src/test/scala/io/iohk/atala/mediator/db/MessageItemRepoSpec.scala b/mediator/src/test/scala/io/iohk/atala/mediator/db/MessageItemRepoSpec.scala index cc8bd171..2fe51f4e 100644 --- a/mediator/src/test/scala/io/iohk/atala/mediator/db/MessageItemRepoSpec.scala +++ b/mediator/src/test/scala/io/iohk/atala/mediator/db/MessageItemRepoSpec.scala @@ -28,7 +28,7 @@ object MessageItemRepoSpec extends ZIOSpecDefault with AccountStubSetup { for { messageItem <- ZIO.service[MessageItemRepo] msg <- ZIO.fromEither(encryptedMessageAlice) - result <- messageItem.findById(msg.hashCode()) + result <- messageItem.findById(msg.sha1) } yield { assertTrue(result.contains(MessageItem(msg))) } @@ -39,7 +39,7 @@ object MessageItemRepoSpec extends ZIOSpecDefault with AccountStubSetup { msg <- ZIO.fromEither(encryptedMessageAlice) msg2 <- ZIO.fromEither(encryptedMessageBob) msg2Added <- messageItem.insert(MessageItem(msg2)) - result <- messageItem.findByIds(Seq(msg.hashCode(), msg2.hashCode())) + result <- messageItem.findByIds(Seq(msg.sha1, msg2.sha1)) } yield { assertTrue(result.contains(MessageItem(msg))) assertTrue(result.contains(MessageItem(msg2))) diff --git a/mediator/src/test/scala/io/iohk/atala/mediator/db/UserAccountRepoSpec.scala b/mediator/src/test/scala/io/iohk/atala/mediator/db/UserAccountRepoSpec.scala index 038e7362..8854d0c9 100644 --- a/mediator/src/test/scala/io/iohk/atala/mediator/db/UserAccountRepoSpec.scala +++ b/mediator/src/test/scala/io/iohk/atala/mediator/db/UserAccountRepoSpec.scala @@ -131,7 +131,7 @@ object UserAccountRepoSpec extends ZIOSpecDefault with AccountStubSetup { val messageMetaData: Seq[MessageMetaData] = didAccount.map(_.messagesRef).getOrElse(Seq.empty) assert(messageMetaData)( forall( - hasField("hash", (m: MessageMetaData) => m.hash, equalTo(msg.hashCode())) + hasField("hash", (m: MessageMetaData) => m.hash, equalTo(msg.sha1)) && hasField("recipient", (m: MessageMetaData) => m.recipient, equalTo(DIDSubject(bob))) ) ) @@ -141,7 +141,7 @@ object UserAccountRepoSpec extends ZIOSpecDefault with AccountStubSetup { for { userAccount <- ZIO.service[UserAccountRepo] msg <- ZIO.fromEither(encryptedMessageAlice) - markedDelivered <- userAccount.markAsDelivered(DIDSubject(alice), Seq(msg.hashCode())) + markedDelivered <- userAccount.markAsDelivered(DIDSubject(alice), Seq(msg.sha1)) didAccount <- userAccount.getDidAccount(DIDSubject(alice)) } yield { assertTrue(markedDelivered == 1)