From 5d258bb1efb180bfa5b0f2e937b3c7be0267795b Mon Sep 17 00:00:00 2001 From: Fabio Pinheiro Date: Mon, 19 Feb 2024 19:40:13 +0000 Subject: [PATCH] fix: Support alias (DID) in Live mode (#235) Fix #230 Signed-off-by: Fabio Pinheiro Signed-off-by: Shailesh Patil --- .../mediator/MediatorTransportManager.scala | 4 ++-- .../atala/mediator/db/UserAccountRepo.scala | 20 ++++++++++++++++++- .../protocols/ForwardMessageExecuter.scala | 19 +++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/MediatorTransportManager.scala b/mediator/src/main/scala/io/iohk/atala/mediator/MediatorTransportManager.scala index 4e664ad3..f4ca703e 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/MediatorTransportManager.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/MediatorTransportManager.scala @@ -71,11 +71,11 @@ case class MediatorTransportManager( liveMode.get(subject).toSeq.flatMap(transportId => transports.filter(t => transportId.contains(t.id))) def sendForLiveMode( - next: TO, + next: FROMTO, msg: /*lazy*/ => SignedMessage | EncryptedMessage ): ZIO[Any, DidFail, Unit] = for { - transportIDs <- ZIO.succeed(this.liveMode.getOrElse(next.asFROMTO, Set.empty)) + transportIDs <- ZIO.succeed(this.liveMode.getOrElse(next, Set.empty)) myChannels <- ZIO.succeed(transportIDs.flatMap(id => this.transports.find(_.id == id))) _ <- ZIO.foreach(myChannels) { _.send(msg) } } yield () 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 c2a239ad..9f2cedbf 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 @@ -75,6 +75,25 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon } yield result } + def getDidAccountFromAlias(alias: DIDSubject): IO[StorageError, Option[DidAccount]] = { + def selector: BSONDocument = BSONDocument("alias" -> alias.did) // BSONDocument("$in" -> BSONArray(alias.did))) + def projection: Option[BSONDocument] = None + + for { + _ <- ZIO.logInfo("getDidAccountFromAlias") + coll <- collection + result <- ZIO + .fromFuture(implicit ec => + coll + .find(selector, projection) + .cursor[DidAccount]() + .collect[Seq](1, Cursor.FailOnError[Seq[DidAccount]]()) // Just one + ) + .tapError(err => ZIO.logError(s"getDidAccount : ${err.getMessage}")) + .mapError(ex => StorageThrowable(ex)) + } yield result.headOption + } + def getDidAccount(did: DIDSubject): IO[StorageError, Option[DidAccount]] = { def selector: BSONDocument = BSONDocument("did" -> did) def projection: Option[BSONDocument] = None @@ -92,7 +111,6 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon .tapError(err => ZIO.logError(s"getDidAccount : ${err.getMessage}")) .mapError(ex => StorageThrowable(ex)) } yield result.headOption - } def addAlias(owner: DIDSubject, newAlias: DIDSubject): ZIO[Any, StorageError, Either[String, Int]] = { diff --git a/mediator/src/main/scala/io/iohk/atala/mediator/protocols/ForwardMessageExecuter.scala b/mediator/src/main/scala/io/iohk/atala/mediator/protocols/ForwardMessageExecuter.scala index f8c94937..9eea285e 100644 --- a/mediator/src/main/scala/io/iohk/atala/mediator/protocols/ForwardMessageExecuter.scala +++ b/mediator/src/main/scala/io/iohk/atala/mediator/protocols/ForwardMessageExecuter.scala @@ -54,9 +54,22 @@ object ForwardMessageExecuter eMsgDelivery <- Operations .authEncrypt(messageDelivery) .mapError(didFail => MediatorDidError(didFail)) - _ <- mediatorTransportManager - .sendForLiveMode(m.next.asTO, eMsgDelivery) - .mapError(didFail => MediatorDidError(didFail)) + _ <- for { + maybeDidAccount <- repoDidAccount + .getDidAccountFromAlias(m.next) + .tapErrorCause(errorCause => + ZIO.logErrorCause("Error when retrieving account for live mode forward message", errorCause) + ) + .catchAll(ex => ZIO.none) // ignoring error + ret <- maybeDidAccount match { + case None => ZIO.unit // nothing to do + case Some(didAccount) => + val accountOwner = didAccount.did + mediatorTransportManager + .sendForLiveMode(accountOwner.asFROMTO, eMsgDelivery) + .mapError(didFail => MediatorDidError(didFail)) + } + } yield ret } yield NoReply } else { for {