Skip to content

Commit

Permalink
feat(prism-agent): refresh state from DLT before returning result
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Losoponkul committed Dec 2, 2022
1 parent d2c8684 commit 3d45d0b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 3d45d0b

Please sign in to comment.