From 16d5fdbadf20c1597bf42b4e366f71623804dfc4 Mon Sep 17 00:00:00 2001 From: yshyn-iohk Date: Tue, 29 Nov 2022 22:14:35 +0800 Subject: [PATCH] feat(pollux): cleanup the code of IssueCredentialApi --- .../io/iohk/atala/agent/server/Modules.scala | 13 +- .../atala/agent/server/http/HttpRoutes.scala | 10 +- .../IssueCredentialsApiMarshallerImpl.scala | 49 ----- .../server/http/marshaller/JsonSupport.scala | 10 - .../IssueCredentialsApiServiceImpl.scala | 206 ------------------ ...sueCredentialsProtocolApiServiceImpl.scala | 1 - 6 files changed, 6 insertions(+), 283 deletions(-) delete mode 100644 prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/IssueCredentialsApiMarshallerImpl.scala delete mode 100644 prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsApiServiceImpl.scala diff --git a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/Modules.scala b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/Modules.scala index f13d3f3d78..59661f6ffa 100644 --- a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/Modules.scala +++ b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/Modules.scala @@ -13,7 +13,6 @@ import io.iohk.atala.agent.server.http.marshaller.{ DIDAuthenticationApiMarshallerImpl, DIDOperationsApiMarshallerImpl, DIDRegistrarApiMarshallerImpl, - IssueCredentialsApiMarshallerImpl, ConnectionsManagementApiMarshallerImpl } import io.iohk.atala.agent.server.http.service.{ @@ -21,7 +20,6 @@ import io.iohk.atala.agent.server.http.service.{ DIDAuthenticationApiServiceImpl, DIDOperationsApiServiceImpl, DIDRegistrarApiServiceImpl, - IssueCredentialsApiServiceImpl, ConnectionsManagementApiServiceImpl } import io.iohk.atala.castor.core.repository.DIDOperationRepository @@ -30,7 +28,6 @@ import io.iohk.atala.agent.openapi.api.{ DIDAuthenticationApi, DIDOperationsApi, DIDRegistrarApi, - IssueCredentialsApi, ConnectionsManagementApi } import io.iohk.atala.castor.sql.repository.{JdbcDIDOperationRepository, TransactorLayer} @@ -378,13 +375,6 @@ object HttpModule { (apiServiceLayer ++ apiMarshallerLayer) >>> ZLayer.fromFunction(new DIDRegistrarApi(_, _)) } - val issueCredentialsApiLayer: RLayer[DidComm, IssueCredentialsApi] = { - val serviceLayer = AppModule.credentialServiceLayer - val apiServiceLayer = serviceLayer >>> IssueCredentialsApiServiceImpl.layer - val apiMarshallerLayer = IssueCredentialsApiMarshallerImpl.layer - (apiServiceLayer ++ apiMarshallerLayer) >>> ZLayer.fromFunction(new IssueCredentialsApi(_, _)) - } - val issueCredentialsProtocolApiLayer: RLayer[DidComm, IssueCredentialsProtocolApi] = { val serviceLayer = AppModule.credentialServiceLayer val apiServiceLayer = serviceLayer >>> IssueCredentialsProtocolApiServiceImpl.layer @@ -400,7 +390,8 @@ object HttpModule { } val layers = - didApiLayer ++ didOperationsApiLayer ++ didAuthenticationApiLayer ++ didRegistrarApiLayer ++ issueCredentialsApiLayer ++ issueCredentialsProtocolApiLayer ++ connectionsManagementApiLayer + didApiLayer ++ didOperationsApiLayer ++ didAuthenticationApiLayer ++ didRegistrarApiLayer ++ + issueCredentialsProtocolApiLayer ++ connectionsManagementApiLayer } object RepoModule { diff --git a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/HttpRoutes.scala b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/HttpRoutes.scala index e355797cca..dde4c952e1 100644 --- a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/HttpRoutes.scala +++ b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/HttpRoutes.scala @@ -7,18 +7,17 @@ import io.iohk.atala.agent.openapi.api.{ DIDAuthenticationApi, DIDOperationsApi, DIDRegistrarApi, - IssueCredentialsApi, + IssueCredentialsProtocolApi, ConnectionsManagementApi } import zio.* -import io.iohk.atala.agent.openapi.api.IssueCredentialsProtocolApi import akka.http.scaladsl.server.Route object HttpRoutes { def routes: URIO[ - DIDApi & DIDOperationsApi & DIDAuthenticationApi & DIDRegistrarApi & IssueCredentialsApi & - IssueCredentialsProtocolApi & ConnectionsManagementApi, + DIDApi & DIDOperationsApi & DIDAuthenticationApi & DIDRegistrarApi & IssueCredentialsProtocolApi & + ConnectionsManagementApi, Route ] = for { @@ -26,10 +25,9 @@ object HttpRoutes { didOperationsApi <- ZIO.service[DIDOperationsApi] didAuthApi <- ZIO.service[DIDAuthenticationApi] disRegistrarApi <- ZIO.service[DIDRegistrarApi] - issueCredentialApi <- ZIO.service[IssueCredentialsApi] issueCredentialsProtocolApi <- ZIO.service[IssueCredentialsProtocolApi] connectionsManagementApi <- ZIO.service[ConnectionsManagementApi] - } yield didApi.route ~ didOperationsApi.route ~ didAuthApi.route ~ disRegistrarApi.route ~ issueCredentialApi.route ~ issueCredentialsProtocolApi.route ~ connectionsManagementApi.route ~ additionalRoute + } yield didApi.route ~ didOperationsApi.route ~ didAuthApi.route ~ disRegistrarApi.route ~ issueCredentialsProtocolApi.route ~ connectionsManagementApi.route ~ additionalRoute private def additionalRoute: Route = { // swagger-ui expects this particular header when resolving relative $ref diff --git a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/IssueCredentialsApiMarshallerImpl.scala b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/IssueCredentialsApiMarshallerImpl.scala deleted file mode 100644 index 4c3ac94cbb..0000000000 --- a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/IssueCredentialsApiMarshallerImpl.scala +++ /dev/null @@ -1,49 +0,0 @@ -package io.iohk.atala.agent.server.http.marshaller - -import akka.http.scaladsl.marshalling.ToEntityMarshaller -import akka.http.scaladsl.unmarshalling.FromEntityUnmarshaller -import io.iohk.atala.agent.openapi.api.IssueCredentialsApiMarshaller -import io.iohk.atala.agent.openapi.model.{ - CreateCredentialsRequest, - W3CIssuanceBatchAction, - W3CCredentialInput, - W3CCredentialsPaginated, - W3CCredential, - W3CIssuanceBatchPaginated, - CreateCredentials201Response -} -import spray.json.RootJsonFormat -import zio.* - -object IssueCredentialsApiMarshallerImpl extends JsonSupport { - - val layer: ULayer[IssueCredentialsApiMarshaller] = ZLayer.succeed { - new IssueCredentialsApiMarshaller { - - implicit def fromEntityUnmarshallerCreateCredentialsRequest: FromEntityUnmarshaller[CreateCredentialsRequest] = - summon[RootJsonFormat[CreateCredentialsRequest]] - - implicit def fromEntityUnmarshallerW3CIssuanceBatchActionList - : FromEntityUnmarshaller[Seq[W3CIssuanceBatchAction]] = summon[RootJsonFormat[Seq[W3CIssuanceBatchAction]]] - - implicit def fromEntityUnmarshallerW3CCredentialInput: FromEntityUnmarshaller[W3CCredentialInput] = - summon[RootJsonFormat[W3CCredentialInput]] - - implicit def toEntityMarshallerW3CCredentialsPaginated: ToEntityMarshaller[W3CCredentialsPaginated] = - summon[RootJsonFormat[W3CCredentialsPaginated]] - - implicit def toEntityMarshallerW3CCredential: ToEntityMarshaller[W3CCredential] = - summon[RootJsonFormat[W3CCredential]] - - implicit def toEntityMarshallerW3CIssuanceBatchPaginated: ToEntityMarshaller[W3CIssuanceBatchPaginated] = - summon[RootJsonFormat[W3CIssuanceBatchPaginated]] - - implicit def toEntityMarshallerW3CIssuanceBatchActionarray: ToEntityMarshaller[Seq[W3CIssuanceBatchAction]] = - summon[RootJsonFormat[Seq[W3CIssuanceBatchAction]]] - - implicit def toEntityMarshallerCreateCredentials201Response: ToEntityMarshaller[CreateCredentials201Response] = - summon[RootJsonFormat[CreateCredentials201Response]] - } - } - -} diff --git a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/JsonSupport.scala b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/JsonSupport.scala index a78d2ebae5..78190d505b 100644 --- a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/JsonSupport.scala +++ b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/marshaller/JsonSupport.scala @@ -77,20 +77,10 @@ trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol { // // Pollux - given RootJsonFormat[CreateCredentials201Response] = jsonFormat3(CreateCredentials201Response.apply) - given RootJsonFormat[CreateCredentialsRequest] = jsonFormat1(CreateCredentialsRequest.apply) given RootJsonFormat[RevocationStatus] = jsonFormat2(RevocationStatus.apply) - given RootJsonFormat[W3CCredential] = jsonFormat6(W3CCredential.apply) -// given RootJsonFormat[W3CCredentialCredentialSubject] = jsonFormat1(W3CCredentialCredentialSubject.apply) - given RootJsonFormat[W3CCredentialInput] = jsonFormat4(W3CCredentialInput.apply) -// given RootJsonFormat[W3CCredentialInputClaims] = jsonFormat1(W3CCredentialInputClaims.apply) given RootJsonFormat[W3CCredentialRevocationRequest] = jsonFormat1(W3CCredentialRevocationRequest.apply) given RootJsonFormat[W3CCredentialRevocationResponse] = jsonFormat2(W3CCredentialRevocationResponse.apply) - given RootJsonFormat[W3CCredentialsPaginated] = jsonFormat4(W3CCredentialsPaginated.apply) given RootJsonFormat[W3CCredentialStatus] = jsonFormat2(W3CCredentialStatus.apply) - given RootJsonFormat[W3CIssuanceBatch] = jsonFormat3(W3CIssuanceBatch.apply) - given RootJsonFormat[W3CIssuanceBatchAction] = jsonFormat3(W3CIssuanceBatchAction.apply) - given RootJsonFormat[W3CIssuanceBatchPaginated] = jsonFormat4(W3CIssuanceBatchPaginated.apply) given RootJsonFormat[W3CPresentation] = jsonFormat1(W3CPresentation.apply) given RootJsonFormat[W3CPresentationInput] = jsonFormat1(W3CPresentationInput.apply) given RootJsonFormat[W3CPresentationPaginated] = jsonFormat4(W3CPresentationPaginated.apply) diff --git a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsApiServiceImpl.scala b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsApiServiceImpl.scala deleted file mode 100644 index 1fa8dd18fc..0000000000 --- a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsApiServiceImpl.scala +++ /dev/null @@ -1,206 +0,0 @@ -package io.iohk.atala.agent.server.http.service - -import akka.http.scaladsl.marshalling.ToEntityMarshaller -import akka.http.scaladsl.server.Route -import io.circe.* -import io.iohk.atala.agent.openapi.api.IssueCredentialsApiService -import io.iohk.atala.agent.openapi.model.* -import io.iohk.atala.agent.server.http.marshaller.IssueCredentialsApiMarshallerImpl -import io.iohk.atala.pollux.vc.jwt.* -import zio.* - -import java.security.spec.ECGenParameterSpec -import java.security.{KeyPairGenerator, SecureRandom} -import java.time.{Instant, OffsetDateTime, OffsetTime} -import java.util.{Base64, UUID} -import cats.implicits.* -import io.circe.* -import io.circe.generic.auto.* -import io.circe.syntax.* -import io.circe.{Decoder, Encoder, HCursor, Json} -import io.circe.parser.decode -import io.iohk.atala.pollux.core.service.CredentialService -import io.iohk.atala.pollux.core.model.EncodedJWTCredential - -// TODO: replace with actual implementation -class IssueCredentialsApiServiceImpl(service: CredentialService)(using runtime: Runtime[Any]) - extends IssueCredentialsApiService - with AkkaZioSupport { - - private val issuer = service.createIssuer - - case class Schema(context: String, `type`: String) - private val defaultSchemas = Set( - Schema("https://www.w3.org/2018/credentials/v1", "VerifiableCredential") - ) - private val mockSchemas = Map( - "06e126d1-fa44-4882-a243-1e326fbe21db" -> Schema( - "https://www.w3.org/2018/credentials/examples/v1", - "UniversityDegreeCredential" - ) - ) - - private val mockW3Credential = W3CCredential( - id = "fsdf234t523fdf3", - `type` = "University degree", - issuer = "University of applied science", - issuanceDate = "21/09/2022", - credentialSubject = Map("id" -> "University degree for smth"), - proof = Some( - W3CProof( - `type` = "proof", - created = "21/09/2022", - verificationMethod = "verificationMethod", - proofPurpose = "proofPurpose", - proofValue = "proofValue", - domain = Some("domain") - ) - ) - ) - - private val mockCredentialResponse = CreateCredentials201Response( - batchId = Some("1"), - count = Some(1), - credentials = Some( - Seq("") - ) - ) - - private val mockW3CCredentialsPaginated = W3CCredentialsPaginated( - data = Some(Seq(mockW3Credential)), - offset = Some(1), - limit = Some(1), - count = Some(1) - ) - - private val mockW3CIssuanceBatchAction = W3CIssuanceBatchAction( - action = Some("abc"), - id = Some("abc"), - status = Some("abc") - ) - - private val mockW3CIssuanceBatch = W3CIssuanceBatch( - id = Some("asdf23fsdf"), - count = Some(1), - actions = Some(Seq(mockW3CIssuanceBatchAction)) - ) - - private val mockW3CIssuanceBatchPaginated = W3CIssuanceBatchPaginated( - data = Some(Seq(mockW3CIssuanceBatch)), - offset = Some(1), - limit = Some(1), - count = Some(1) - ) - - private[this] def createPayload(input: W3CCredentialInput, issuer: Issuer): (UUID, W3cCredentialPayload) = { - val now = Instant.now() - val credentialId = UUID.randomUUID() - val claims = input.claims.map(kv => kv._1 -> Json.fromString(kv._2)) - val schemas = defaultSchemas ++ input.schemaId.flatMap(mockSchemas.get) - credentialId -> W3cCredentialPayload( - `@context` = schemas.map(_.context), - maybeId = Some(s"https://atala.io/prism/credentials/${credentialId.toString}"), - `type` = schemas.map(_.`type`), - issuer = issuer.did, - issuanceDate = now, - maybeExpirationDate = input.validityPeriod.map(sec => now.plusSeconds(sec.toLong)), - maybeCredentialSchema = None, - credentialSubject = claims.updated("id", Json.fromString(input.subjectId)).asJson, - maybeCredentialStatus = None, - maybeRefreshService = None, - maybeEvidence = None, - maybeTermsOfUse = None - ) - } - - /** Code: 201, Message: Array of created verifiable credentials objects, DataType: CreateCredentials201Response - */ - def createCredentials(createCredentialsRequest: CreateCredentialsRequest)(implicit - toEntityMarshallerCreateCredentials201Response: ToEntityMarshaller[CreateCredentials201Response] - ): Route = - onZioSuccess(ZIO.unit) { _ => - val credentials = createCredentialsRequest.credentials.map { input => - val (uuid, payload) = createPayload(input, issuer) - uuid.toString -> payload.toJwtCredentialPayload - } - val batchId = UUID.randomUUID().toString - // service.createCredentials( - // batchId, - // credentials.map { case (id, jwt) => EncodedJWTCredential(batchId, id, jwt) } - // ) - val resp = CreateCredentials201Response( - Some(batchId), - Some(credentials.size), - Some(credentials.map(c => JwtCredential.encodeJwt(c._2, issuer).value)) - ) - - createCredentials201(resp) - } - - /** Code: 204, Message: Credential was deleted - */ - def deleteCredentialById(id: String): Route = - onZioSuccess(ZIO.unit) { _ => deleteCredentialById204 } - - /** Code: 200, Message: Successful response, instance of Verifiable Credential is returned, DataType: W3CCredential - * Code: 404, Message: Schema is not found by id - */ - def getCredentialById(id: String)(implicit - toEntityMarshallerW3CCredential: ToEntityMarshaller[W3CCredential] - ): Route = - onZioSuccess(ZIO.unit) { _ => getCredentialById200(mockW3Credential) } - - /** Code: 200, Message: Paginated response of the verifiable credentials objects, DataType: W3CCredentialsPaginated - */ - def getCredentialsByBatchId(batchId: String, offset: Option[Int], limit: Option[Int])(implicit - toEntityMarshallerW3CCredentialsPaginated: ToEntityMarshaller[W3CCredentialsPaginated] - ): Route = - onZioSuccess(ZIO.unit) { _ => - getCredentialsByBatchId200(mockW3CCredentialsPaginated) - } - - /** Code: 200, Message: Returns the set of actions performed on the issuance-batch, DataType: - * Seq[W3CIssuanceBatchAction] - */ - def getIssuanceBatchActions(batchId: String)(implicit - toEntityMarshallerW3CIssuanceBatchActionarray: ToEntityMarshaller[Seq[W3CIssuanceBatchAction]] - ): Route = - onZioSuccess(ZIO.unit) { _ => getIssuanceBatchActions200(Seq(mockW3CIssuanceBatchAction)) } - - /** Code: 200, Message: Returns the paginated list of issuance-batch objects, DataType: W3CIssuanceBatchPaginated - */ - def getIssuanceBatches(limit: Option[Int], offset: Option[Int])(implicit - toEntityMarshallerW3CIssuanceBatchPaginated: ToEntityMarshaller[W3CIssuanceBatchPaginated] - ): Route = - onZioSuccess(ZIO.unit) { _ => - getIssuanceBatches200(mockW3CIssuanceBatchPaginated) - } - - /** Code: 200, Message: Returns the set of actions performed on the issuance-batch, DataType: - * Seq[W3CIssuanceBatchAction] - */ - def submitIssuanceBatchActions(batchId: String, w3CIssuanceBatchAction: Seq[W3CIssuanceBatchAction])(implicit - toEntityMarshallerW3CIssuanceBatchActionarray: ToEntityMarshaller[Seq[W3CIssuanceBatchAction]] - ): Route = - onZioSuccess(ZIO.unit) { _ => - submitIssuanceBatchActions200(Seq(mockW3CIssuanceBatchAction)) - } - - /** Code: 200, Message: Credential was updated successfully, DataType: W3CCredential - */ - def updateCredentialById(id: String, w3CCredentialInput: W3CCredentialInput)(implicit - toEntityMarshallerW3CCredential: ToEntityMarshaller[W3CCredential] - ): Route = - onZioSuccess(ZIO.unit) { _ => - updateCredentialById200(mockW3Credential) - } -} - -object IssueCredentialsApiServiceImpl { - val layer: URLayer[CredentialService, IssueCredentialsApiService] = ZLayer.fromZIO { - for { - rt <- ZIO.runtime[Any] - svc <- ZIO.service[CredentialService] - } yield IssueCredentialsApiServiceImpl(svc)(using rt) - } -} diff --git a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsProtocolApiServiceImpl.scala b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsProtocolApiServiceImpl.scala index 7d900587d2..8802689ad9 100644 --- a/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsProtocolApiServiceImpl.scala +++ b/prism-agent/service/server/src/main/scala/io/iohk/atala/agent/server/http/service/IssueCredentialsProtocolApiServiceImpl.scala @@ -3,7 +3,6 @@ package io.iohk.atala.agent.server.http.service import akka.http.scaladsl.server.Directives.* import akka.http.scaladsl.marshalling.ToEntityMarshaller import akka.http.scaladsl.server.Route -import io.iohk.atala.agent.openapi.api.IssueCredentialsApi import io.iohk.atala.agent.openapi.api.IssueCredentialsProtocolApiService import zio.* import io.iohk.atala.pollux.core.service.CredentialService