Skip to content

Commit

Permalink
fix: Id/hash of the message must be deterministic (#53)
Browse files Browse the repository at this point in the history
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 <fabiomgpinheiro@gmail.com>

Signed-off-by: Shailesh Patil <shailesh.patil@iohk.io>
  • Loading branch information
FabioPinheiro authored and mineme0110 committed May 1, 2024
1 parent b80b633 commit 281f6a1
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 13 deletions.
9 changes: 7 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ case class MediatorAgent(
Option[EncryptedMessage]
] =
ZIO
.logAnnotate("msgHash", msg.hashCode.toString) {
.logAnnotate("msgHash", msg.sha1) {
for {
_ <- ZIO.log("receivedMessage")
maybeSyncReplyMsg <-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
)
Expand All @@ -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))
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand All @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
)
)
Expand All @@ -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)
Expand Down

0 comments on commit 281f6a1

Please sign in to comment.