Skip to content

Commit

Permalink
feat: adopt CredentialDefinition to MT (merge the main into mt branch)
Browse files Browse the repository at this point in the history
  • Loading branch information
yshyn-iohk committed Sep 7, 2023
1 parent 018fd11 commit ad4463e
Show file tree
Hide file tree
Showing 31 changed files with 341 additions and 650 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ trait ConnectionService {

def markConnectionResponseSent(recordId: UUID): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord]

def markConnectionInvitationExpired(recordId: UUID): IO[ConnectionServiceError, ConnectionRecord]
def markConnectionInvitationExpired(
recordId: UUID
): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord]

def receiveConnectionResponse(
response: ConnectionResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ private class ConnectionServiceImpl(
case Some(value) => ZIO.succeed(value)
}

override def markConnectionInvitationExpired(recordId: UUID): IO[ConnectionServiceError, ConnectionRecord] =
override def markConnectionInvitationExpired(
recordId: UUID
): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord] =
updateConnectionProtocolState(
recordId,
ProtocolState.InvitationGenerated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class ConnectionServiceNotifier(
): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord] =
notifyOnSuccess(svc.markConnectionResponseSent(recordId))

override def markConnectionInvitationExpired(recordId: UUID): IO[ConnectionServiceError, ConnectionRecord] =
// FIXME: should it be context aware????
override def markConnectionInvitationExpired(
recordId: UUID
): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord] =
notifyOnSuccess(svc.markConnectionInvitationExpired(recordId))

override def receiveConnectionResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ object ConnectionRepositorySpecSuite {
},
test("updateConnectionProtocolState updates the record to InvitationExpired") {
for {
repo <- ZIO.service[ConnectionRepository[Task]]
repo <- ZIO.service[ConnectionRepository]
aRecord = connectionRecord
_ <- repo.createConnectionRecord(aRecord)
record <- repo.getConnectionRecord(aRecord.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ package io.iohk.atala.pollux.core.repository

import io.iohk.atala.pollux.core.model.schema.CredentialDefinition
import io.iohk.atala.pollux.core.repository.Repository.SearchCapability
import io.iohk.atala.shared.models.WalletAccessContext
import zio.{RIO, Task}

import java.util.UUID

trait CredentialDefinitionRepository[F[_]]
extends Repository[F, CredentialDefinition]
with SearchCapability[F, CredentialDefinition.Filter, CredentialDefinition] {
def create(cs: CredentialDefinition): F[CredentialDefinition]
trait CredentialDefinitionRepository
extends Repository[WalletTask, CredentialDefinition]
with SearchCapability[WalletTask, CredentialDefinition.Filter, CredentialDefinition] {
def create(cs: CredentialDefinition): RIO[WalletAccessContext, CredentialDefinition]

def getByGuid(guid: UUID): F[Option[CredentialDefinition]]
def getByGuid(guid: UUID): Task[Option[CredentialDefinition]]

def update(cs: CredentialDefinition): F[Option[CredentialDefinition]]
def update(cs: CredentialDefinition): RIO[WalletAccessContext, Option[CredentialDefinition]]

def getAllVersions(id: UUID, author: String): F[Seq[String]]
def getAllVersions(id: UUID, author: String): RIO[WalletAccessContext, Seq[String]]

def delete(guid: UUID): F[Option[CredentialDefinition]]
def delete(guid: UUID): RIO[WalletAccessContext, Option[CredentialDefinition]]

def deleteAll(): F[Long]
def deleteAll(): RIO[WalletAccessContext, Long]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package io.iohk.atala.pollux.core.repository
import io.iohk.atala.pollux.core.model.*
import io.iohk.atala.pollux.core.model.error.CredentialRepositoryError.*
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition
import io.iohk.atala.shared.models.WalletAccessContext
import zio.*

import java.util.UUID

class CredentialDefinitionRepositoryInMemory(
storeRef: Ref[Map[UUID, CredentialDefinition]]
) extends CredentialDefinitionRepository[Task] {
override def create(record: CredentialDefinition): Task[CredentialDefinition] = {
) extends CredentialDefinitionRepository {
override def create(record: CredentialDefinition): RIO[WalletAccessContext, CredentialDefinition] = {
for {
_ <- for {
store <- storeRef.get
Expand All @@ -30,7 +31,7 @@ class CredentialDefinitionRepositoryInMemory(
} yield record
}

override def update(cs: CredentialDefinition): Task[Option[CredentialDefinition]] = {
override def update(cs: CredentialDefinition): RIO[WalletAccessContext, Option[CredentialDefinition]] = {
for {
store <- storeRef.get
maybeExisting = store.get(cs.id)
Expand All @@ -43,7 +44,7 @@ class CredentialDefinitionRepositoryInMemory(
} yield maybeExisting
}

override def getAllVersions(id: UUID, author: String): Task[Seq[String]] = {
override def getAllVersions(id: UUID, author: String): RIO[WalletAccessContext, Seq[String]] = {
storeRef.get.map { store =>
store.values
.filter(credDef => credDef.id == id && credDef.author == author)
Expand All @@ -52,7 +53,7 @@ class CredentialDefinitionRepositoryInMemory(
}
}

override def delete(guid: UUID): Task[Option[CredentialDefinition]] = {
override def delete(guid: UUID): RIO[WalletAccessContext, Option[CredentialDefinition]] = {
for {
store <- storeRef.get
maybeRecord = store.get(guid)
Expand All @@ -63,7 +64,7 @@ class CredentialDefinitionRepositoryInMemory(
} yield maybeRecord
}

override def deleteAll(): Task[Long] = {
override def deleteAll(): RIO[WalletAccessContext, Long] = {
for {
store <- storeRef.get
deleted = store.size
Expand All @@ -73,7 +74,7 @@ class CredentialDefinitionRepositoryInMemory(

override def search(
query: Repository.SearchQuery[CredentialDefinition.Filter]
): Task[Repository.SearchResult[CredentialDefinition]] = {
): RIO[WalletAccessContext, Repository.SearchResult[CredentialDefinition]] = {
storeRef.get.map { store =>
val filtered = store.values.filter { credDef =>
query.filter.author.forall(_ == credDef.author) &&
Expand All @@ -88,7 +89,7 @@ class CredentialDefinitionRepositoryInMemory(
}

object CredentialDefinitionRepositoryInMemory {
val layer: ULayer[CredentialDefinitionRepository[Task]] = ZLayer.fromZIO(
val layer: ULayer[CredentialDefinitionRepository] = ZLayer.fromZIO(
Ref
.make(Map.empty[UUID, CredentialDefinition])
.map(CredentialDefinitionRepositoryInMemory(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import zio.{RIO, Task}

import java.util.UUID

type WalletTask[T] = RIO[WalletAccessContext, T]

trait CredentialSchemaRepository
extends Repository[WalletTask, CredentialSchema]
with SearchCapability[WalletTask, CredentialSchema.Filter, CredentialSchema] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.iohk.atala.pollux.core.repository

import io.iohk.atala.shared.models.WalletAccessContext
import zio.RIO

trait Repository[F[_], T]

type WalletTask[T] = RIO[WalletAccessContext, T]

object Repository {
case class SearchQuery[Filter](filter: Filter, skip: Int, limit: Int)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package io.iohk.atala.pollux.core.service
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition.*
import zio.IO
import io.iohk.atala.shared.models.WalletAccessContext
import zio.{IO, ZIO}

import java.util.UUID

trait CredentialDefinitionService {
type Result[T] = IO[CredentialDefinitionService.Error, T]
type Result[T] = ZIO[WalletAccessContext, CredentialDefinitionService.Error, T]

/** @param in
* CredentialDefinition form for creating the instance
Expand All @@ -22,7 +23,7 @@ trait CredentialDefinitionService {
* @return
* The instance of the credential definition or credential service error
*/
def getByGUID(guid: UUID): Result[CredentialDefinition]
def getByGUID(guid: UUID): IO[CredentialDefinitionService.Error, CredentialDefinition]

def delete(guid: UUID): Result[CredentialDefinition]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
package io.iohk.atala.pollux.core.service

import io.iohk.atala.agent.walletapi.storage
import io.iohk.atala.agent.walletapi.storage.DIDSecret
import io.iohk.atala.agent.walletapi.storage.DIDSecretStorage
import io.iohk.atala.agent.walletapi.storage.{DIDSecret, DIDSecretStorage}
import io.iohk.atala.mercury.model.DidId
import io.iohk.atala.pollux.anoncreds.AnoncredLib
import io.iohk.atala.pollux.anoncreds.SchemaDef
import io.iohk.atala.pollux.anoncreds.{AnoncredLib, SchemaDef}
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError
import io.iohk.atala.pollux.core.model.error.CredentialSchemaError.URISyntaxError
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition.Filter
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition.FilteredEntries
import io.iohk.atala.pollux.core.model.schema.CredentialSchema
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition.{Filter, FilteredEntries}
import io.iohk.atala.pollux.core.model.schema.CredentialSchema.parseCredentialSchema
import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
import io.iohk.atala.pollux.core.model.schema.validator.JsonSchemaError
import io.iohk.atala.pollux.core.model.schema.{CredentialDefinition, CredentialSchema}
import io.iohk.atala.pollux.core.repository.CredentialDefinitionRepository
import io.iohk.atala.pollux.core.repository.Repository.SearchQuery
import io.iohk.atala.pollux.core.service.CredentialDefinitionService.Error.*
import io.iohk.atala.pollux.core.service.serdes.PrivateCredentialDefinitionSchemaSerDesV1
import io.iohk.atala.pollux.core.service.serdes.ProofKeyCredentialDefinitionSchemaSerDesV1
import io.iohk.atala.pollux.core.service.serdes.PublicCredentialDefinitionSerDesV1
import zio.Task
import zio.URLayer
import zio.ZIO
import io.iohk.atala.pollux.core.service.serdes.{
PrivateCredentialDefinitionSchemaSerDesV1,
ProofKeyCredentialDefinitionSchemaSerDesV1,
PublicCredentialDefinitionSerDesV1
}
import zio.ZIO.getOrFailWith
import zio.ZLayer
import zio.{IO, URLayer, ZIO, ZLayer}

import java.net.URI
import java.util.UUID
import scala.util.Try

class CredentialDefinitionServiceImpl(
didSecretStorage: DIDSecretStorage,
credentialDefinitionRepository: CredentialDefinitionRepository[Task],
credentialDefinitionRepository: CredentialDefinitionRepository,
uriDereferencer: URIDereferencer
) extends CredentialDefinitionService {
private val KEY_ID = "anoncred-credential-definition-private-key"
Expand Down Expand Up @@ -116,7 +111,7 @@ class CredentialDefinitionServiceImpl(
.map(sr => FilteredEntries(sr.entries, sr.count.toInt, sr.totalCount.toInt))
}

override def getByGUID(guid: UUID): Result[CredentialDefinition] = {
override def getByGUID(guid: UUID): IO[CredentialDefinitionService.Error, CredentialDefinition] = {
credentialDefinitionRepository
.getByGuid(guid)
.mapError[CredentialDefinitionService.Error](t => RepositoryError(t))
Expand All @@ -128,7 +123,7 @@ class CredentialDefinitionServiceImpl(

object CredentialDefinitionServiceImpl {
val layer: URLayer[
DIDSecretStorage & CredentialDefinitionRepository[Task] & URIDereferencer,
DIDSecretStorage & CredentialDefinitionRepository & URIDereferencer,
CredentialDefinitionService
] =
ZLayer.fromFunction(CredentialDefinitionServiceImpl(_, _, _))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import io.iohk.atala.agent.walletapi.memory.DIDSecretStorageInMemory
import io.iohk.atala.pollux.core.model.*
import io.iohk.atala.pollux.core.model.schema.CredentialDefinition
import io.iohk.atala.pollux.core.repository.CredentialDefinitionRepositoryInMemory
import io.iohk.atala.shared.models.WalletId.*
import io.iohk.atala.shared.models.{WalletAccessContext, WalletId}
import zio.*

import java.time.OffsetDateTime

trait CredentialDefinitionServiceSpecHelper {

protected val defaultWalletLayer = ZLayer.succeed(WalletAccessContext(WalletId.default))

protected val credentialDefinitionServiceLayer =
DIDSecretStorageInMemory.layer ++ CredentialDefinitionRepositoryInMemory.layer ++ ResourceURIDereferencerImpl.layer >>> CredentialDefinitionServiceImpl.layer
DIDSecretStorageInMemory.layer ++ CredentialDefinitionRepositoryInMemory.layer ++ ResourceURIDereferencerImpl.layer >>>
CredentialDefinitionServiceImpl.layer ++ defaultWalletLayer

val defaultDefinition =
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ CREATE TABLE public.credential_definition
schema_id VARCHAR(255) NOT NULL,
signature_type VARCHAR(64) NOT NULL,
support_revocation BOOLEAN NOT NULL,
UNIQUE (name, version, author)
wallet_id UUID NOT NULL,
UNIQUE (wallet_id, name, version, author)
);

CREATE INDEX credential_definition_name_index ON public.credential_definition (name);
Expand All @@ -25,5 +26,9 @@ CREATE INDEX credential_definition_tags_index ON public.credential_definition (t
CREATE INDEX credential_definition_author_index ON public.credential_definition (author);
CREATE INDEX credential_definition_authored_index ON public.credential_definition (authored);

ALTER TABLE public.credential_definition ENABLE ROW LEVEL SECURITY;

CREATE POLICY credential_definition_wallet_isolation
ON public.credential_definition
USING (wallet_id = current_setting('app.current_wallet_id')::UUID);

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import io.getquill.*
import io.getquill.context.json.PostgresJsonExtensions
import io.getquill.doobie.DoobieContext
import io.getquill.idiom.*
import io.iohk.atala.pollux.core.model.schema.CorrectnessProof
import io.iohk.atala.pollux.core.model.schema.Definition
import io.iohk.atala.pollux.core.model.schema.{CorrectnessProof, Definition}
import io.iohk.atala.shared.models.WalletId

import java.time.OffsetDateTime
import java.util.UUID
Expand All @@ -25,14 +25,16 @@ case class CredentialDefinition(
keyCorrectnessProofJsonSchemaId: String,
keyCorrectnessProof: JsonValue[CorrectnessProof],
signatureType: String,
supportRevocation: Boolean
supportRevocation: Boolean,
walletId: WalletId
) {
lazy val uniqueConstraintKey = author + name + version
}

object CredentialDefinition {
def fromModel(
m: io.iohk.atala.pollux.core.model.schema.CredentialDefinition
m: io.iohk.atala.pollux.core.model.schema.CredentialDefinition,
walletId: WalletId
): CredentialDefinition =
CredentialDefinition(
guid = m.guid,
Expand All @@ -49,7 +51,8 @@ object CredentialDefinition {
keyCorrectnessProof = JsonValue(m.keyCorrectnessProof),
schemaId = m.schemaId,
signatureType = m.signatureType,
supportRevocation = m.supportRevocation
supportRevocation = m.supportRevocation,
walletId = walletId
)

def toModel(
Expand Down
Loading

0 comments on commit ad4463e

Please sign in to comment.