From 3d45d0b7b363c95f82dfe547d4d4982e96c71f69 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Thu, 1 Dec 2022 16:58:13 +0700 Subject: [PATCH] feat(prism-agent): refresh state from DLT before returning result --- .../model/error/ListManagedDIDError.scala | 10 +++++++ .../walletapi/service/ManagedDIDService.scala | 27 +++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/model/error/ListManagedDIDError.scala diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/model/error/ListManagedDIDError.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/model/error/ListManagedDIDError.scala new file mode 100644 index 0000000000..7149e1fd78 --- /dev/null +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/model/error/ListManagedDIDError.scala @@ -0,0 +1,10 @@ +package io.iohk.atala.agent.walletapi.model.error + +import io.iohk.atala.castor.core.model.error.DIDOperationError + +sealed trait ListManagedDIDError + +object ListManagedDIDError { + final case class WalletStorageError(cause: Throwable) extends ListManagedDIDError + final case class OperationError(cause: DIDOperationError) extends ListManagedDIDError +} diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDService.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDService.scala index bd1a53916a..50f55a5893 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDService.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDService.scala @@ -9,7 +9,7 @@ import io.iohk.atala.agent.walletapi.model.{ ManagedDIDTemplate } import io.iohk.atala.agent.walletapi.model.ECCoordinates.* -import io.iohk.atala.agent.walletapi.model.error.{CreateManagedDIDError, PublishManagedDIDError} +import io.iohk.atala.agent.walletapi.model.error.{CreateManagedDIDError, ListManagedDIDError, PublishManagedDIDError} import io.iohk.atala.agent.walletapi.service.ManagedDIDService.{CreateDIDSecret, DEFAULT_MASTER_KEY_ID} import io.iohk.atala.agent.walletapi.storage.{ DIDNonSecretStorage, @@ -56,10 +56,27 @@ final class ManagedDIDService private[walletapi] ( private val CURVE = EllipticCurve.SECP256K1 - def listManagedDID: Task[Seq[ManagedDIDDetail]] = nonSecretStorage.listManagedDID - .map(_.toSeq.map { case (did, state) => - ManagedDIDDetail(did = did.asCanonical, state = state) - }) + def listManagedDID: IO[ListManagedDIDError, Seq[ManagedDIDDetail]] = nonSecretStorage.listManagedDID + .mapBoth( + ListManagedDIDError.WalletStorageError.apply, + _.toSeq.map { case (did, state) => + ManagedDIDDetail(did = did.asCanonical, state = state) + } + ) + .flatMap { dids => + ZIO.foreach(dids) { didDetail => + // state in wallet maybe stale, update it from DLT + syncDIDStateFromDLT(didDetail.state) + .mapError(ListManagedDIDError.OperationError.apply) + .tap(state => + nonSecretStorage + .setManagedDIDState(didDetail.did, state) + .mapError(ListManagedDIDError.WalletStorageError.apply) + ) + .map(didDetail -> _) + } + } + .map(_.map { case (didDetail, newState) => didDetail.copy(state = newState) }) def publishStoredDID(did: CanonicalPrismDID): IO[PublishManagedDIDError, ScheduleDIDOperationOutcome] = { def syncDLTStateAndPersist =