-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prism-agent): implement JDBC did nonsecret storage (#284)
* Implement JdbcDIDNonSecretStorage * feat(prism-agent): rename did nonsecret storage columns * Refactor ManagedDIDService syncing DID state * Add sync DID state backgroundJob * PR cleanup
- Loading branch information
Showing
11 changed files
with
286 additions
and
73 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
prism-agent/service/server/src/main/resources/sql/agent/V2__did_nonsecret_storage.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TYPE public.did_publication_status AS ENUM( | ||
'CREATED', | ||
'PUBLICATION_PENDING', | ||
'PUBLISHED' | ||
); | ||
|
||
CREATE TABLE public.did_publication_state( | ||
"did" TEXT NOT NULL PRIMARY KEY, | ||
"publication_status" did_publication_status NOT NULL, | ||
"atala_operation_content" BYTEA NOT NULL, | ||
"publish_operation_id" BYTEA | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...service/wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/model/error/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.iohk.atala.agent.walletapi.model | ||
|
||
import io.iohk.atala.castor.core.model.did.PrismDID | ||
import io.iohk.atala.castor.core.model.error.DIDOperationError | ||
|
||
package object error { | ||
final case class CommonWalletStorageError(cause: Throwable) | ||
|
||
given Conversion[CommonWalletStorageError, PublishManagedDIDError] = e => | ||
PublishManagedDIDError.WalletStorageError(e.cause) | ||
given Conversion[DIDOperationError, PublishManagedDIDError] = PublishManagedDIDError.OperationError(_) | ||
|
||
given Conversion[CommonWalletStorageError, GetManagedDIDError] = e => GetManagedDIDError.WalletStorageError(e.cause) | ||
given Conversion[DIDOperationError, GetManagedDIDError] = GetManagedDIDError.OperationError(_) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...wallet-api/src/main/scala/io/iohk/atala/agent/walletapi/sql/JdbcDIDNonSecretStorage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.iohk.atala.agent.walletapi.sql | ||
|
||
import doobie.* | ||
import doobie.implicits.* | ||
import doobie.postgres.implicits.* | ||
|
||
import io.iohk.atala.agent.walletapi.model.ManagedDIDState | ||
import io.iohk.atala.agent.walletapi.storage.DIDNonSecretStorage | ||
import io.iohk.atala.castor.core.model.did.PrismDID | ||
import zio.* | ||
import zio.interop.catz.* | ||
|
||
class JdbcDIDNonSecretStorage(xa: Transactor[Task]) extends DIDNonSecretStorage { | ||
|
||
override def getManagedDIDState(did: PrismDID): Task[Option[ManagedDIDState]] = { | ||
val cxnIO = | ||
sql""" | ||
| SELECT | ||
| did, | ||
| publication_status, | ||
| atala_operation_content, | ||
| publish_operation_id | ||
| FROM public.did_publication_state | ||
| WHERE did = $did | ||
""".stripMargin | ||
.query[DIDPublicationStateRow] | ||
.option | ||
|
||
cxnIO | ||
.transact(xa) | ||
.flatMap(_.map(_.toDomain).fold(ZIO.none)(t => ZIO.fromTry(t).asSome)) | ||
} | ||
|
||
override def setManagedDIDState(did: PrismDID, state: ManagedDIDState): Task[Unit] = { | ||
val row = DIDPublicationStateRow.from(did, state) | ||
val cxnIO = sql""" | ||
| INSERT INTO public.did_publication_state( | ||
| did, | ||
| publication_status, | ||
| atala_operation_content, | ||
| publish_operation_id | ||
| ) | ||
| VALUES ( | ||
| ${row.did}, | ||
| ${row.publicationStatus}, | ||
| ${row.atalaOperationContent}, | ||
| ${row.publishOperationId} | ||
| ) | ||
| ON CONFLICT (did) DO UPDATE SET | ||
| publication_status = EXCLUDED.publication_status, | ||
| atala_operation_content = EXCLUDED.atala_operation_content, | ||
| publish_operation_id = EXCLUDED.publish_operation_id | ||
""".stripMargin.update | ||
|
||
cxnIO.run.transact(xa).unit | ||
} | ||
|
||
override def listManagedDID: Task[Map[PrismDID, ManagedDIDState]] = { | ||
val cxnIO = | ||
sql""" | ||
| SELECT | ||
| did, | ||
| publication_status, | ||
| atala_operation_content, | ||
| publish_operation_id | ||
| FROM public.did_publication_state | ||
""".stripMargin | ||
.query[DIDPublicationStateRow] | ||
.to[List] | ||
|
||
cxnIO | ||
.transact(xa) | ||
.map(_.map(row => row.toDomain.map(row.did -> _))) | ||
.flatMap(ls => ZIO.foreach(ls)(ZIO.fromTry[(PrismDID, ManagedDIDState)](_))) | ||
.map(_.toMap) | ||
} | ||
|
||
} | ||
|
||
object JdbcDIDNonSecretStorage { | ||
val layer: URLayer[Transactor[Task], DIDNonSecretStorage] = ZLayer.fromFunction(new JdbcDIDNonSecretStorage(_)) | ||
} |
Oops, something went wrong.