From b8635044c93eb6ad65a2cd439c949e9fb8f57f66 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Mon, 26 Jun 2023 17:48:56 +0700 Subject: [PATCH 1/6] chore: fix test typo --- .../agent/walletapi/benchmark/KeyDerivation.scala | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/benchmark/KeyDerivation.scala b/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/benchmark/KeyDerivation.scala index 998913f579..c037093fb5 100644 --- a/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/benchmark/KeyDerivation.scala +++ b/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/benchmark/KeyDerivation.scala @@ -17,16 +17,16 @@ object KeyDerivation extends ZIOSpecDefault, VaultTestContainerSupport { private val seedHex = "00" * 64 private val seed = HexString.fromStringUnsafe(seedHex).toByteArray - override def spec = suite("Key derivation benchamrk")( + override def spec = suite("Key derivation benchmark")( deriveKeyBenchmark.provide(Apollo.prism14Layer), queryKeyBenchmark.provide(vaultKvClientLayer, Apollo.prism14Layer) ) @@ TestAspect.sequential @@ TestAspect.timed @@ TestAspect.tag("benchmark") @@ TestAspect.ignore private val deriveKeyBenchmark = suite("Key derivation benchmark")( - benchamrkKeyDerivation(1), - benchamrkKeyDerivation(8), - benchamrkKeyDerivation(16), - benchamrkKeyDerivation(32), + benchmarkKeyDerivation(1), + benchmarkKeyDerivation(8), + benchmarkKeyDerivation(16), + benchmarkKeyDerivation(32), ) @@ TestAspect.before(deriveKeyWarmUp()) private val queryKeyBenchmark = suite("Query key benchmark - vault storage")( @@ -36,7 +36,7 @@ object KeyDerivation extends ZIOSpecDefault, VaultTestContainerSupport { benchmarkVaultQuery(32), ) @@ TestAspect.before(vaultWarmUp()) - private def benchamrkKeyDerivation(parallelism: Int) = { + private def benchmarkKeyDerivation(parallelism: Int) = { test(s"derive 50000 keys - $parallelism parallelism") { for { apollo <- ZIO.service[Apollo] From 45512827381a6ef73236bf21455b27c52f17c677 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Mon, 26 Jun 2023 19:10:50 +0700 Subject: [PATCH 2/6] chore: refactor create did handler --- .../service/ManagedDIDServiceImpl.scala | 50 +++++++++------- .../service/handler/DIDCreateHandler.scala | 58 +++++++++++++++++++ .../service/handler/DIDUpdateHandler.scala | 9 ++- .../service/ManagedDIDServiceSpec.scala | 39 ++++++++----- 4 files changed, 115 insertions(+), 41 deletions(-) create mode 100644 prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala index 23e59c7abf..1d7d782ea2 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala @@ -18,6 +18,7 @@ import scala.language.implicitConversions import java.security.{PrivateKey as JavaPrivateKey, PublicKey as JavaPublicKey} import scala.collection.immutable.ArraySeq +import io.iohk.atala.agent.walletapi.service.handler.DIDCreateHandler /** A wrapper around Castor's DIDService providing key-management capability. Analogous to the secretAPI in * indy-wallet-sdk. @@ -38,10 +39,8 @@ final class ManagedDIDServiceImpl private[walletapi] ( private val keyResolver = KeyResolver(apollo, nonSecretStorage, secretStorage)(seed) private val publicationHandler = PublicationHandler(didService, keyResolver)(DEFAULT_MASTER_KEY_ID) - private val didUpdateHandler = DIDUpdateHandler(apollo, nonSecretStorage, secretStorage, publicationHandler)(seed) - - private val generateCreateOperationHdKey = - OperationFactory(apollo).makeCreateOperationHdKey(DEFAULT_MASTER_KEY_ID, seed) + private val didCreateHandler = DIDCreateHandler(apollo, nonSecretStorage)(seed) + private val didUpdateHandler = DIDUpdateHandler(apollo, nonSecretStorage, publicationHandler)(seed) def syncManagedDIDState: IO[GetManagedDIDError, Unit] = nonSecretStorage .listManagedDID(offset = None, limit = None) @@ -123,30 +122,39 @@ final class ManagedDIDServiceImpl private[walletapi] ( } yield outcome } - // TODO: update this method to use the same handler as updateManagedDID def createAndStoreDID(didTemplate: ManagedDIDTemplate): IO[CreateManagedDIDError, LongFormPrismDID] = { + // for { + // _ <- ZIO + // .fromEither(ManagedDIDTemplateValidator.validate(didTemplate)) + // .mapError(CreateManagedDIDError.InvalidArgument.apply) + // didIndex <- nonSecretStorage + // .getMaxDIDIndex() + // .mapBoth( + // CreateManagedDIDError.WalletStorageError.apply, + // maybeIdx => maybeIdx.map(_ + 1).getOrElse(0) + // ) + // generated <- generateCreateOperationHdKey(didIndex, didTemplate) + // (createOperation, hdKey) = generated + // longFormDID = PrismDID.buildLongFormFromOperation(createOperation) + // did = longFormDID.asCanonical + // _ <- ZIO + // .fromEither(didOpValidator.validate(createOperation)) + // .mapError(CreateManagedDIDError.InvalidOperation.apply) + // state = ManagedDIDState(createOperation, didIndex, PublicationState.Created()) + // _ <- nonSecretStorage + // .insertManagedDID(did, state, hdKey.keyPaths ++ hdKey.internalKeyPaths) + // .mapError(CreateManagedDIDError.WalletStorageError.apply) + // } yield longFormDID for { _ <- ZIO .fromEither(ManagedDIDTemplateValidator.validate(didTemplate)) .mapError(CreateManagedDIDError.InvalidArgument.apply) - didIndex <- nonSecretStorage - .getMaxDIDIndex() - .mapBoth( - CreateManagedDIDError.WalletStorageError.apply, - maybeIdx => maybeIdx.map(_ + 1).getOrElse(0) - ) - generated <- generateCreateOperationHdKey(didIndex, didTemplate) - (createOperation, hdKey) = generated - longFormDID = PrismDID.buildLongFormFromOperation(createOperation) - did = longFormDID.asCanonical + material <- didCreateHandler.materialize(DEFAULT_MASTER_KEY_ID, didTemplate) _ <- ZIO - .fromEither(didOpValidator.validate(createOperation)) + .fromEither(didOpValidator.validate(material.operation)) .mapError(CreateManagedDIDError.InvalidOperation.apply) - state = ManagedDIDState(createOperation, didIndex, PublicationState.Created()) - _ <- nonSecretStorage - .insertManagedDID(did, state, hdKey.keyPaths ++ hdKey.internalKeyPaths) - .mapError(CreateManagedDIDError.WalletStorageError.apply) - } yield longFormDID + _ <- material.persist.mapError(CreateManagedDIDError.WalletStorageError.apply) + } yield PrismDID.buildLongFormFromOperation(material.operation) } def updateManagedDID( diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala new file mode 100644 index 0000000000..6b0b8530a1 --- /dev/null +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala @@ -0,0 +1,58 @@ +package io.iohk.atala.agent.walletapi.service.handler + +import io.iohk.atala.agent.walletapi.crypto.Apollo +import io.iohk.atala.agent.walletapi.model.CreateDIDHdKey +import io.iohk.atala.agent.walletapi.model.ManagedDIDState +import io.iohk.atala.agent.walletapi.model.ManagedDIDTemplate +import io.iohk.atala.agent.walletapi.model.error.CreateManagedDIDError +import io.iohk.atala.agent.walletapi.storage.DIDNonSecretStorage +import io.iohk.atala.castor.core.model.did.PrismDIDOperation +import zio.* +import io.iohk.atala.agent.walletapi.util.OperationFactory +import io.iohk.atala.agent.walletapi.model.PublicationState + +private[walletapi] class DIDCreateHandler( + apollo: Apollo, + nonSecretStorage: DIDNonSecretStorage +)( + seed: Array[Byte] +) { + def materialize( + masterKeyId: String, + didTemplate: ManagedDIDTemplate + ): IO[CreateManagedDIDError, DIDCreateMaterial] = { + val operationFactory = OperationFactory(apollo) + for { + didIndex <- nonSecretStorage + .getMaxDIDIndex() // TODO: fix this + .mapBoth( + CreateManagedDIDError.WalletStorageError.apply, + maybeIdx => maybeIdx.map(_ + 1).getOrElse(0) + ) + generated <- operationFactory.makeCreateOperationHdKey(masterKeyId, seed)(didIndex, didTemplate) + (createOperation, hdKey) = generated + state = ManagedDIDState(createOperation, didIndex, PublicationState.Created()) + } yield DIDCreateMaterialImpl(nonSecretStorage)(createOperation, state, hdKey) + } +} + +private[walletapi] trait DIDCreateMaterial { + def operation: PrismDIDOperation.Create + def state: ManagedDIDState + def persist: Task[Unit] +} + +private[walletapi] class DIDCreateMaterialImpl(nonSecretStorage: DIDNonSecretStorage)( + val operation: PrismDIDOperation.Create, + val state: ManagedDIDState, + hdKey: CreateDIDHdKey +) extends DIDCreateMaterial { + def persist: Task[Unit] = { + val did = operation.did + for { + _ <- nonSecretStorage + .insertManagedDID(did, state, hdKey.keyPaths ++ hdKey.internalKeyPaths) + .mapError(CreateManagedDIDError.WalletStorageError.apply) + } yield () + } +} diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDUpdateHandler.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDUpdateHandler.scala index bebb3cd60f..0f051471f0 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDUpdateHandler.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDUpdateHandler.scala @@ -17,10 +17,9 @@ import io.iohk.atala.castor.core.model.did.ScheduledDIDOperationStatus import io.iohk.atala.castor.core.model.did.SignedPrismDIDOperation import scala.collection.immutable.ArraySeq -class DIDUpdateHandler( +private[walletapi] class DIDUpdateHandler( apollo: Apollo, nonSecretStorage: DIDNonSecretStorage, - secretStorage: DIDSecretStorage, publicationHandler: PublicationHandler )( seed: Array[Byte] @@ -44,12 +43,12 @@ class DIDUpdateHandler( result <- operationFactory.makeUpdateOperationHdKey(seed)(did, previousOperationHash, actions, keyCounter) (operation, hdKey) = result signedOperation <- publicationHandler.signOperationWithMasterKey[UpdateManagedDIDError](state, operation) - } yield HdKeyUpdateMaterial(secretStorage, nonSecretStorage)(operation, signedOperation, state, hdKey) + } yield HdKeyUpdateMaterial(nonSecretStorage)(operation, signedOperation, state, hdKey) } } } -trait DIDUpdateMaterial { +private[walletapi] trait DIDUpdateMaterial { def operation: PrismDIDOperation.Update @@ -78,7 +77,7 @@ trait DIDUpdateMaterial { } -class HdKeyUpdateMaterial(secretStorage: DIDSecretStorage, nonSecretStorage: DIDNonSecretStorage)( +private class HdKeyUpdateMaterial(nonSecretStorage: DIDNonSecretStorage)( val operation: PrismDIDOperation.Update, val signedOperation: SignedPrismDIDOperation, val state: ManagedDIDState, diff --git a/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala b/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala index aa2230a449..1c6001a986 100644 --- a/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala +++ b/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala @@ -1,7 +1,16 @@ package io.iohk.atala.agent.walletapi.service +import io.iohk.atala.agent.walletapi.crypto.Apollo +import io.iohk.atala.agent.walletapi.crypto.ApolloSpecHelper +import io.iohk.atala.agent.walletapi.model.UpdateManagedDIDAction +import io.iohk.atala.agent.walletapi.model.error.UpdateManagedDIDError import io.iohk.atala.agent.walletapi.model.error.{CreateManagedDIDError, PublishManagedDIDError} import io.iohk.atala.agent.walletapi.model.{DIDPublicKeyTemplate, ManagedDIDState, ManagedDIDTemplate, PublicationState} +import io.iohk.atala.agent.walletapi.sql.JdbcDIDNonSecretStorage +import io.iohk.atala.agent.walletapi.sql.JdbcDIDSecretStorage +import io.iohk.atala.agent.walletapi.util.SeedResolver +import io.iohk.atala.agent.walletapi.vault.VaultDIDSecretStorage +import io.iohk.atala.castor.core.model.did.InternalKeyPurpose import io.iohk.atala.castor.core.model.did.{ DIDData, DIDMetadata, @@ -19,23 +28,13 @@ import io.iohk.atala.castor.core.model.did.{ import io.iohk.atala.castor.core.model.error import io.iohk.atala.castor.core.service.DIDService import io.iohk.atala.castor.core.util.DIDOperationValidator +import io.iohk.atala.test.container.DBTestUtils +import io.iohk.atala.test.container.PostgresTestContainerSupport +import io.iohk.atala.test.container.VaultTestContainerSupport +import scala.collection.immutable.ArraySeq import zio.* import zio.test.* import zio.test.Assertion.* - -import scala.collection.immutable.ArraySeq -import io.iohk.atala.test.container.PostgresTestContainerSupport -import io.iohk.atala.test.container.VaultTestContainerSupport -import io.iohk.atala.agent.walletapi.crypto.ApolloSpecHelper -import io.iohk.atala.agent.walletapi.sql.JdbcDIDSecretStorage -import io.iohk.atala.agent.walletapi.sql.JdbcDIDNonSecretStorage -import io.iohk.atala.test.container.DBTestUtils -import io.iohk.atala.castor.core.model.did.InternalKeyPurpose -import io.iohk.atala.agent.walletapi.model.error.UpdateManagedDIDError -import io.iohk.atala.agent.walletapi.model.UpdateManagedDIDAction -import io.iohk.atala.agent.walletapi.crypto.Apollo -import io.iohk.atala.agent.walletapi.util.SeedResolver -import io.iohk.atala.agent.walletapi.vault.VaultDIDSecretStorage import zio.test.TestAspect.sequential object ManagedDIDServiceSpec @@ -270,7 +269,17 @@ object ManagedDIDServiceSpec ) val result = ZIO.serviceWithZIO[ManagedDIDService](_.createAndStoreDID(template)) assertZIO(result.exit)(fails(isSubtype[CreateManagedDIDError.InvalidArgument](anything))) - } + }, + test("concurrent DID creation successfully create DID using different did-index") { + for { + svc <- ZIO.service[ManagedDIDService] + dids <- ZIO + .foreachPar(1 to 4)(_ => svc.createAndStoreDID(generateDIDTemplate()).map(_.asCanonical)) + .withParallelism(4) + .map(_.toList) + .debug("dids") + } yield assertCompletes // TODO: implement + } @@ TestAspect.tag("dev") @@ TestAspect.ignore ) private val updateManagedDIDSpec = From 565d0f02ca6acbdca9f7bee88d225f25955fb648 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Tue, 27 Jun 2023 12:00:43 +0700 Subject: [PATCH 3/6] fix: createDID with only 1 permit semaphore --- .../service/ManagedDIDServiceImpl.scala | 46 +++++++++---------- .../service/handler/DIDCreateHandler.scala | 2 +- .../service/ManagedDIDServiceSpec.scala | 13 ++++-- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala index 1d7d782ea2..7f1ebd39a9 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala @@ -29,7 +29,8 @@ final class ManagedDIDServiceImpl private[walletapi] ( private[walletapi] val secretStorage: DIDSecretStorage, override private[walletapi] val nonSecretStorage: DIDNonSecretStorage, apollo: Apollo, - seed: Array[Byte] + seed: Array[Byte], + createDIDSem: Semaphore ) extends ManagedDIDService { private val CURVE = EllipticCurve.SECP256K1 @@ -123,29 +124,7 @@ final class ManagedDIDServiceImpl private[walletapi] ( } def createAndStoreDID(didTemplate: ManagedDIDTemplate): IO[CreateManagedDIDError, LongFormPrismDID] = { - // for { - // _ <- ZIO - // .fromEither(ManagedDIDTemplateValidator.validate(didTemplate)) - // .mapError(CreateManagedDIDError.InvalidArgument.apply) - // didIndex <- nonSecretStorage - // .getMaxDIDIndex() - // .mapBoth( - // CreateManagedDIDError.WalletStorageError.apply, - // maybeIdx => maybeIdx.map(_ + 1).getOrElse(0) - // ) - // generated <- generateCreateOperationHdKey(didIndex, didTemplate) - // (createOperation, hdKey) = generated - // longFormDID = PrismDID.buildLongFormFromOperation(createOperation) - // did = longFormDID.asCanonical - // _ <- ZIO - // .fromEither(didOpValidator.validate(createOperation)) - // .mapError(CreateManagedDIDError.InvalidOperation.apply) - // state = ManagedDIDState(createOperation, didIndex, PublicationState.Created()) - // _ <- nonSecretStorage - // .insertManagedDID(did, state, hdKey.keyPaths ++ hdKey.internalKeyPaths) - // .mapError(CreateManagedDIDError.WalletStorageError.apply) - // } yield longFormDID - for { + val effect = for { _ <- ZIO .fromEither(ManagedDIDTemplateValidator.validate(didTemplate)) .mapError(CreateManagedDIDError.InvalidArgument.apply) @@ -155,6 +134,14 @@ final class ManagedDIDServiceImpl private[walletapi] ( .mapError(CreateManagedDIDError.InvalidOperation.apply) _ <- material.persist.mapError(CreateManagedDIDError.WalletStorageError.apply) } yield PrismDID.buildLongFormFromOperation(material.operation) + + // This synchronize createDID effect to only allow 1 execution at a time + // to avoid concurrent didIndex update. Long-term solution should be + // solved at the DB level. + // + // Concurrency performance may be improved by not synchronizing the whole operation, + // but only the counter increment part allowing multiple in-flight create operations. + createDIDSem.withPermit(effect) } def updateManagedDID( @@ -374,7 +361,16 @@ object ManagedDIDServiceImpl { nonSecretStorage <- ZIO.service[DIDNonSecretStorage] apollo <- ZIO.service[Apollo] seed <- ZIO.serviceWithZIO[SeedResolver](_.resolve) - } yield ManagedDIDServiceImpl(didService, didOpValidator, secretStorage, nonSecretStorage, apollo, seed) + creatDIDSem <- Semaphore.make(1) + } yield ManagedDIDServiceImpl( + didService, + didOpValidator, + secretStorage, + nonSecretStorage, + apollo, + seed, + creatDIDSem + ) } } diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala index 6b0b8530a1..22dac3a9ec 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala @@ -24,7 +24,7 @@ private[walletapi] class DIDCreateHandler( val operationFactory = OperationFactory(apollo) for { didIndex <- nonSecretStorage - .getMaxDIDIndex() // TODO: fix this + .getMaxDIDIndex() .mapBoth( CreateManagedDIDError.WalletStorageError.apply, maybeIdx => maybeIdx.map(_ + 1).getOrElse(0) diff --git a/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala b/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala index 1c6001a986..eeeb8b24c0 100644 --- a/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala +++ b/prism-agent/service/wallet-api/src/test/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceSpec.scala @@ -274,12 +274,15 @@ object ManagedDIDServiceSpec for { svc <- ZIO.service[ManagedDIDService] dids <- ZIO - .foreachPar(1 to 4)(_ => svc.createAndStoreDID(generateDIDTemplate()).map(_.asCanonical)) - .withParallelism(4) + .foreachPar(1 to 50)(_ => svc.createAndStoreDID(generateDIDTemplate()).map(_.asCanonical)) + .withParallelism(8) .map(_.toList) - .debug("dids") - } yield assertCompletes // TODO: implement - } @@ TestAspect.tag("dev") @@ TestAspect.ignore + states <- ZIO + .foreach(dids)(did => svc.nonSecretStorage.getManagedDIDState(did)) + .map(_.toList.flatten) + } yield assert(dids)(hasSize(equalTo(50))) && + assert(states.map(_.didIndex))(hasSameElementsDistinct(0 until 50)) + } ) private val updateManagedDIDSpec = From 5a8954cd5986a841de6bf525487664c190dc0eb5 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Tue, 27 Jun 2023 12:08:20 +0700 Subject: [PATCH 4/6] chore: pr cleanup --- .../walletapi/service/ManagedDIDServiceImpl.scala | 11 ++++++----- .../walletapi/service/handler/DIDCreateHandler.scala | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala index 7f1ebd39a9..671abb4bf8 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala @@ -40,7 +40,7 @@ final class ManagedDIDServiceImpl private[walletapi] ( private val keyResolver = KeyResolver(apollo, nonSecretStorage, secretStorage)(seed) private val publicationHandler = PublicationHandler(didService, keyResolver)(DEFAULT_MASTER_KEY_ID) - private val didCreateHandler = DIDCreateHandler(apollo, nonSecretStorage)(seed) + private val didCreateHandler = DIDCreateHandler(apollo, nonSecretStorage)(seed, DEFAULT_MASTER_KEY_ID) private val didUpdateHandler = DIDUpdateHandler(apollo, nonSecretStorage, publicationHandler)(seed) def syncManagedDIDState: IO[GetManagedDIDError, Unit] = nonSecretStorage @@ -128,19 +128,20 @@ final class ManagedDIDServiceImpl private[walletapi] ( _ <- ZIO .fromEither(ManagedDIDTemplateValidator.validate(didTemplate)) .mapError(CreateManagedDIDError.InvalidArgument.apply) - material <- didCreateHandler.materialize(DEFAULT_MASTER_KEY_ID, didTemplate) + material <- didCreateHandler.materialize(didTemplate) _ <- ZIO .fromEither(didOpValidator.validate(material.operation)) .mapError(CreateManagedDIDError.InvalidOperation.apply) _ <- material.persist.mapError(CreateManagedDIDError.WalletStorageError.apply) } yield PrismDID.buildLongFormFromOperation(material.operation) - // This synchronize createDID effect to only allow 1 execution at a time + // This synchronizes createDID effect to only allow 1 execution at a time // to avoid concurrent didIndex update. Long-term solution should be // solved at the DB level. // - // Concurrency performance may be improved by not synchronizing the whole operation, - // but only the counter increment part allowing multiple in-flight create operations. + // Performance may be improved by not synchronizing the whole operation, + // but only the counter increment part allowing multiple in-flight create operations + // once didIndex is acquired. createDIDSem.withPermit(effect) } diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala index 22dac3a9ec..040ce0ff92 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/handler/DIDCreateHandler.scala @@ -15,10 +15,10 @@ private[walletapi] class DIDCreateHandler( apollo: Apollo, nonSecretStorage: DIDNonSecretStorage )( - seed: Array[Byte] + seed: Array[Byte], + masterKeyId: String ) { def materialize( - masterKeyId: String, didTemplate: ManagedDIDTemplate ): IO[CreateManagedDIDError, DIDCreateMaterial] = { val operationFactory = OperationFactory(apollo) From 099955ea2161f7338f88cde5aba30c73417a20c9 Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Tue, 27 Jun 2023 12:48:23 +0700 Subject: [PATCH 5/6] ci: use undeprecated vault image --- infrastructure/shared/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infrastructure/shared/docker-compose.yml b/infrastructure/shared/docker-compose.yml index 1ec9dc8710..d3e3abcf02 100644 --- a/infrastructure/shared/docker-compose.yml +++ b/infrastructure/shared/docker-compose.yml @@ -52,7 +52,7 @@ services: condition: service_healthy vault-server: - image: vault:latest + image: hashicorp/vault:latest # ports: # - "8200:8200" environment: From b922796bc911673020d8d7e9259e63df4ed7692a Mon Sep 17 00:00:00 2001 From: Pat Losoponkul Date: Tue, 27 Jun 2023 13:41:57 +0700 Subject: [PATCH 6/6] chore: pr cleanup --- .../atala/agent/walletapi/service/ManagedDIDServiceImpl.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala index 671abb4bf8..f97cc3deb3 100644 --- a/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala +++ b/prism-agent/service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/service/ManagedDIDServiceImpl.scala @@ -362,7 +362,7 @@ object ManagedDIDServiceImpl { nonSecretStorage <- ZIO.service[DIDNonSecretStorage] apollo <- ZIO.service[Apollo] seed <- ZIO.serviceWithZIO[SeedResolver](_.resolve) - creatDIDSem <- Semaphore.make(1) + createDIDSem <- Semaphore.make(1) } yield ManagedDIDServiceImpl( didService, didOpValidator, @@ -370,7 +370,7 @@ object ManagedDIDServiceImpl { nonSecretStorage, apollo, seed, - creatDIDSem + createDIDSem ) } }