Skip to content

Commit

Permalink
feat(agent): integrate publishStoredDID in HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Losoponkul committed Oct 21, 2022
1 parent 7424fda commit c94ab5a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ sealed trait PrismDID {

}

object PrismDID {
// TODO: implement a proper DID parser
// For now, just make it work with a simple case of Prism DID V1
def parse(didRef: String): Either[String, PrismDID] = {
if (didRef.startsWith("did:prism:1:")) {
val suffix = didRef.drop("did:prism:1:".length)
suffix.split(':').toList match {
case network :: suffix :: encodedState :: Nil =>
Right(
LongFormPrismDIDV1(
network,
HexString.fromStringUnsafe(suffix),
Base64UrlString.fromStringUnsafe(encodedState)
)
)
case network :: suffix :: Nil => Right(PrismDIDV1(network, HexString.fromStringUnsafe(suffix)))
case _ => Left("Invalid DID syntax")
}
} else {
Left("DID parsing only supports Prism DID with did:prism:1 prefix")
}
}
}

final case class PrismDIDV1 private[did] (network: String, suffix: HexString) extends PrismDID {

override val version: PrismDIDVersion = PrismDIDVersion.V1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.iohk.atala.agent.server.http.model

import akka.http.scaladsl.server.StandardRoute
import io.iohk.atala.agent.openapi.model.ErrorResponse
import io.iohk.atala.agent.walletapi.model.error.PublishManagedDIDError
import io.iohk.atala.castor.core.model.error.DIDOperationError

trait ToErrorResponse[E] {
Expand All @@ -27,12 +28,24 @@ trait OASErrorModelHelper {
}

given ToErrorResponse[DIDOperationError] with {
def toErrorResponse(error: DIDOperationError): ErrorResponse = {
override def toErrorResponse(e: DIDOperationError): ErrorResponse = {
ErrorResponse(
`type` = "error-type",
title = "error-title",
status = 500,
detail = Some(error.toString),
detail = Some(e.toString),
instance = "error-instance"
)
}
}

given ToErrorResponse[PublishManagedDIDError] with {
override def toErrorResponse(e: PublishManagedDIDError): ErrorResponse = {
ErrorResponse(
`type` = "error-type",
title = "error-title",
status = 500,
detail = Some(e.toString),
instance = "error-instance"
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.iohk.atala.agent.server.http.service

import akka.http.scaladsl.marshalling.ToEntityMarshaller
import akka.http.scaladsl.server.Directives.*
import akka.http.scaladsl.server.Route
import io.iohk.atala.agent.walletapi.service.ManagedDIDService
import zio.*
import io.iohk.atala.agent.openapi.api.DIDRegistrarApiService
import io.iohk.atala.agent.openapi.model.*
import io.iohk.atala.agent.server.http.model.{OASDomainModelHelper, OASErrorModelHelper}
import io.iohk.atala.agent.server.http.model.{HttpServiceError, OASDomainModelHelper, OASErrorModelHelper}
import io.iohk.atala.agent.walletapi.model.error.PublishManagedDIDError
import io.iohk.atala.castor.core.model.did.PrismDID

class DIDRegistrarApiServiceImpl(service: ManagedDIDService)(using runtime: Runtime[Any])
extends DIDRegistrarApiService,
Expand Down Expand Up @@ -61,11 +64,20 @@ class DIDRegistrarApiServiceImpl(service: ManagedDIDService)(using runtime: Runt
}
}

// TODO: implement
override def publishManagedDid(didRef: String)(implicit
toEntityMarshallerDIDOperationResponse: ToEntityMarshaller[DIDOperationResponse],
toEntityMarshallerErrorResponse: ToEntityMarshaller[ErrorResponse]
): Route = ???
): Route = {
val result = for {
prismDID <- ZIO.fromEither(PrismDID.parse(didRef)).mapError(HttpServiceError.InvalidPayload.apply)
outcome <- service.publishStoredDID(prismDID).mapError(HttpServiceError.DomainError[PublishManagedDIDError].apply)
} yield outcome

onZioSuccess(result.mapBoth(_.toOAS, _.toOAS).either) {
case Left(error) => complete(error.status -> error)
case Right(result) => publishManagedDid202(result)
}
}

}

Expand Down

0 comments on commit c94ab5a

Please sign in to comment.