Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Nov 8, 2022
1 parent 55bc3e3 commit 7546478
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.iohk.atala.pollux.core.model

import java.util.UUID
import io.iohk.atala.mercury.protocol.issuecredential.OfferCredential
import io.iohk.atala.mercury.protocol.issuecredential.RequestCredential

final case class IssueCredentialRecord(
id: UUID,
Expand All @@ -9,7 +11,9 @@ final case class IssueCredentialRecord(
subjectId: String,
validityPeriod: Option[Double] = None,
claims: Map[String, String],
state: IssueCredentialRecord.State
state: IssueCredentialRecord.State,
offerData: Option[OfferCredential],
requestCredentialData: Option[RequestCredential]
)

object IssueCredentialRecord {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ trait CredentialService {
def receiveCredentialIssue(issue: IssueCredential): IO[IssueCredentialError, Option[IssueCredentialRecord]]

def markOfferSent(id: UUID): IO[IssueCredentialError, Option[IssueCredentialRecord]]

def markRequestSent(id: UUID): IO[IssueCredentialError, Option[IssueCredentialRecord]]

def markCredentialSent(id: UUID): IO[IssueCredentialError, Option[IssueCredentialRecord]]
Expand All @@ -101,7 +101,6 @@ object CredentialServiceImpl {
private class CredentialServiceImpl(irisClient: IrisServiceStub, credentialRepository: CredentialRepository[Task])
extends CredentialService {


override def markCredentialSent(id: UUID): IO[IssueCredentialError, Option[IssueCredentialRecord]] = ???

override def markCredentialPublicationQueued(id: UUID): IO[IssueCredentialError, Option[IssueCredentialRecord]] = ???
Expand Down Expand Up @@ -139,7 +138,9 @@ private class CredentialServiceImpl(irisClient: IrisServiceStub, credentialRepos
subjectId,
validityPeriod,
claims,
IssueCredentialRecord.State.OfferPending
IssueCredentialRecord.State.OfferPending,
offerData = None,
requestCredentialData = None
)
)
count <- credentialRepository
Expand Down Expand Up @@ -180,7 +181,9 @@ private class CredentialServiceImpl(irisClient: IrisServiceStub, credentialRepos
offer.to.value,
None,
Map.empty,
IssueCredentialRecord.State.OfferReceived
IssueCredentialRecord.State.OfferReceived,
offerData = Some(offer),
requestCredentialData = None
)
)
count <- credentialRepository
Expand All @@ -202,7 +205,7 @@ private class CredentialServiceImpl(irisClient: IrisServiceStub, credentialRepos

override def receiveCredentialRequest(
request: RequestCredential
): IO[IssueCredentialError, Option[IssueCredentialRecord]] = ???
): IO[IssueCredentialError, Option[IssueCredentialRecord]] = ??? // FIXME

override def issueCredential(id: UUID): IO[IssueCredentialError, Option[IssueCredentialRecord]] =
updateCredentialRecordState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,44 @@ object Modules {
_ <- ZIO.logInfo("Got ProposeCredential: " + msg)
credentialService <- ZIO.service[CredentialService]

// TODO
} yield ("OfferCredential Sent")

case s if s == OfferCredential.`type` => // Holder
for {
_ <- ZIO.logInfo("*" * 100)
_ <- ZIO.logInfo("As an Holder in issue-credential:")
_ <- ZIO.logInfo("Got OfferCredential: " + msg)
// store on BD TODO //pc = OfferCredential.readFromMessage(msg)
requestCredential = RequestCredential.makeRequestCredentialFromOffer(msg) // RequestCredential
didCommService <- ZIO.service[DidComm]
msg = requestCredential.makeMessage
_ <- sendMessage(msg)
} yield ("RequestCredential Sent")
credentialService <- ZIO.service[CredentialService]
offerFromIssuer = OfferCredential.readFromMessage(msg)
_ <- credentialService
.receiveCredentialOffer(offerFromIssuer)
.catchAll(s => ???) // FIXME

} yield ("Offer received")

case s if s == RequestCredential.`type` => // Issuer
for {
_ <- ZIO.logInfo("*" * 100)
_ <- ZIO.logInfo("As an Issuer in issue-credential:")
_ <- ZIO.logInfo("Got RequestCredential: " + msg)
requestCredential = RequestCredential.readFromMessage(msg)
_ <- ZIO.logInfo("Got RequestCredential: " + requestCredential)
credentialService <- ZIO.service[CredentialService]

issueCredential = IssueCredential.makeIssueCredentialFromRequestCredential(msg) // IssueCredential
didCommService <- ZIO.service[DidComm]
msg = issueCredential.makeMessage
_ <- sendMessage(msg)
} yield ("IssueCredential Sent")
todoTestOption <- credentialService
.receiveCredentialRequest(requestCredential)
.catchAll(s => ???) // FIXME
// TODO todoTestOption if none
} yield ("RequestCredential received")

case s if s == IssueCredential.`type` => // Holder
for {
_ <- ZIO.logInfo("*" * 100)
_ <- ZIO.logInfo("As an Holder in issue-credential:")
_ <- ZIO.logInfo("Got IssueCredential: " + msg)
issueCredential = IssueCredential.readFromMessage(msg)
_ <- ZIO.logInfo("Got IssueCredential: " + issueCredential)
// STORE
credentialService <- ZIO.service[CredentialService]
a = credentialService.receiveCredentialIssue(issueCredential)
} yield ("IssueCredential Received")

case _ => ZIO.succeed("Unknown Message Type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.iohk.atala.resolvers.UniversalDidResolver
import zhttp.service._
import zhttp.http._
import io.iohk.atala.mercury.MediaTypes
import io.iohk.atala.mercury.AgentCli._

object BackgroundJobs {

Expand All @@ -35,7 +36,7 @@ object BackgroundJobs {
_ <- Console.printLine(s"Running action with record => $record")

_ <- record match {
case IssueCredentialRecord(_, _, _, subjectId, _, claims, OfferPending) =>
case IssueCredentialRecord(id, _, _, subjectId, _, claims, OfferPending, _, _) =>
val attributes = claims.map { case (k, v) => Attribute(k, v) }
val credentialPreview = CredentialPreview(attributes = attributes.toSeq)
val body = OfferCredential.Body(goal_code = Some("Offer Credential"), credential_preview = credentialPreview)
Expand All @@ -54,14 +55,56 @@ object BackgroundJobs {
msg = offer.makeMessage

_ <- AgentCli.sendMessage(msg)
credentialService <- ZIO.service[CredentialService]
_ <- credentialService.markRequestSent(id)
_ <- ZIO.log(s"IssueCredentialRecord: OfferPending (END)")
// TODO UPDATE STATE
} yield ()

case IssueCredentialRecord(_, _, _, subjectId, _, _, RequestPending) => ???
case IssueCredentialRecord(_, _, _, subjectId, _, _, ProblemReportPending) => ???
case IssueCredentialRecord(_, _, _, subjectId, _, _, CredentialPending) => ???
case IssueCredentialRecord(_, _, _, _, _, _, _) => ZIO.unit
case IssueCredentialRecord(id, _, _, subjectId, _, _, RequestPending, Some(offerData), _) =>
for {
didCommService <- ZIO.service[DidComm]
requestCredential = RequestCredential(
body = RequestCredential.Body(
goal_code = offerData.body.goal_code,
comment = offerData.body.comment,
formats = offerData.body.formats
),
attachments = offerData.attachments,
thid = offerData.thid,
from = offerData.to,
to = offerData.from
)
msg = requestCredential.makeMessage
_ <- sendMessage(msg)
credentialService <- ZIO.service[CredentialService]
_ <- credentialService.markRequestSent(id)
_ <- ZIO.log(s"IssueCredentialRecord: RequestPending id='$id' (END)")
} yield ()

case IssueCredentialRecord(_, _, _, subjectId, _, _, ProblemReportPending, _, _) => ???
case IssueCredentialRecord(id, _, _, subjectId, _, _, CredentialPending, _, Some(requestCredentialData)) =>
val issueCredential = IssueCredential(
body = IssueCredential.Body(
goal_code = requestCredentialData.body.goal_code,
comment = requestCredentialData.body.comment,
replacement_id = None,
more_available = None,
formats = requestCredentialData.body.formats
),
attachments = requestCredentialData.attachments,
thid = requestCredentialData.thid,
from = requestCredentialData.to,
to = requestCredentialData.from
)

for {
didCommService <- ZIO.service[DidComm]
_ <- sendMessage(issueCredential.makeMessage)
credentialService <- ZIO.service[CredentialService]
_ <- credentialService.markCredentialSent(id)
_ <- ZIO.log(s"IssueCredentialRecord: RequestPending id='$id' (END)")
} yield ()
case IssueCredentialRecord(_, _, _, _, _, _, _, _, _) => ZIO.unit
}
} yield ()

Expand Down

0 comments on commit 7546478

Please sign in to comment.