Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptoKnightIOG committed Oct 25, 2023
1 parent f259aff commit eb9b8a2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private class PresentationServiceImpl(
pairwiseProverDID: DidId,
thid: DidCommID,
connectionId: Option[String],
proofTypes: Seq[ProofType],
proofType: ProofType,
maybeOptions: Option[io.iohk.atala.pollux.core.model.presentation.Options],
format: CredentialFormat,
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] = {
Expand Down Expand Up @@ -631,7 +631,7 @@ private class PresentationServiceImpl(
)
}

def createAnoncredPresentationRequest(proofType: ProofType, options: Options): String = {
private def createAnoncredPresentationRequest(proofType: ProofType, options: Options): String = {
val requestedAttributes = proofType.requiredFields.getOrElse(Seq.empty).map { field =>
field -> AnoncredRequestedAttribute(field, List(
AnoncredAttributeRestriction(Some(proofType.schema), None, None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.model.error.PresentationError.*
import io.iohk.atala.pollux.core.model.presentation.Options
import io.iohk.atala.pollux.core.repository.{CredentialRepository, PresentationRepository}
import io.iohk.atala.pollux.core.service.serdes.AnoncredPresentationRequestSchemaSerDesV1
import io.iohk.atala.pollux.vc.jwt.*
import io.iohk.atala.shared.models.{WalletAccessContext, WalletId}
import zio.*
import zio.test.*
import zio.test.Assertion.*

import java.time.Instant
import io.iohk.atala.shared.models.WalletId
import io.iohk.atala.shared.models.WalletAccessContext

object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSpecHelper {

Expand All @@ -28,7 +28,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp

private val singleWalletSpec =
suite("singleWalletSpec")(
test("createPresentationRecord creates a valid PresentationRecord") {
test("createPresentationRecord creates a valid JWT PresentationRecord") {
val didGen = for {
suffix <- Gen.stringN(10)(Gen.alphaNumericChar)
} yield DidId("did:peer:" + suffix)
Expand Down Expand Up @@ -100,12 +100,88 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
}
}
},
test("createPresentationRecord creates a valid Anoncred PresentationRecord") {
val didGen = for {
suffix <- Gen.stringN(10)(Gen.alphaNumericChar)
} yield DidId("did:peer:" + suffix)

val proofTypeGen = for {
schemaId <- Gen.stringN(10)(Gen.alphaChar)
requiredFields <- Gen.listOfBounded(1, 5)(Gen.stringN(10)(Gen.alphaChar)).map(Some(_))
trustIssuers <- Gen.listOfBounded(1, 5)(didGen).map(Some(_))
} yield ProofType(schemaId, requiredFields, trustIssuers)

val optionsGen = for {
challenge <- Gen.stringN(10)(Gen.alphaNumericChar)
domain <- Gen.stringN(10)(Gen.alphaNumericChar)
} yield Options(challenge, domain)

check(
Gen.uuid.map(e => DidCommID(e.toString())),
Gen.option(Gen.string),
Gen.listOfBounded(1, 5)(proofTypeGen),
Gen.option(optionsGen)
) { (thid, connectionId, proofTypes, options) =>
for {
svc <- ZIO.service[PresentationService]
pairwiseVerifierDid = DidId("did:peer:Verifier")
pairwiseProverDid = DidId("did:peer:Prover")
record <-
svc.createPresentationRecord(
pairwiseVerifierDid,
pairwiseProverDid,
thid,
connectionId,
proofTypes,
options,
format = CredentialFormat.AnonCreds,
)
} yield {
assertTrue(record.thid == thid) &&
assertTrue(record.updatedAt.isEmpty) &&
assertTrue(record.connectionId == connectionId) &&
assertTrue(record.role == PresentationRecord.Role.Verifier) &&
assertTrue(record.protocolState == PresentationRecord.ProtocolState.RequestPending) &&
assertTrue(record.requestPresentationData.isDefined) &&
assertTrue(record.requestPresentationData.get.to == pairwiseProverDid) &&
assertTrue(record.requestPresentationData.get.thid.contains(thid.toString)) &&
assertTrue(record.requestPresentationData.get.body.goal_code.contains("Request Proof Presentation")) &&
assertTrue(record.requestPresentationData.get.body.proof_types == proofTypes) &&
assertTrue(
if (record.requestPresentationData.get.attachments.length != 0) {
val presentations =
record.requestPresentationData.get.attachments.map(
attachment => {
decode[io.iohk.atala.mercury.model.JsonData](attachment.data.asJson.noSpaces)
.flatMap(data => AnoncredPresentationRequestSchemaSerDesV1.given_JsonDecoder_AnoncredPresentationRequestSchemaSerDesV1.decodeJson(data.toString))
}
)
val maybePresentationOptions =
record.requestPresentationData.get.attachments.headOption
.map(attachment =>
decode[io.iohk.atala.mercury.model.JsonData](attachment.data.asJson.noSpaces)
.flatMap(data => AnoncredPresentationRequestSchemaSerDesV1.schemaSerDes.deserialize(data.json.asJson))
)
.get
maybePresentationOptions
.map(
_ == options
)
.getOrElse(false)
} else true
) &&
assertTrue(record.proposePresentationData.isEmpty) &&
assertTrue(record.presentationData.isEmpty) &&
assertTrue(record.credentialsToUse.isEmpty)
}
}
},
test("getPresentationRecords returns created PresentationRecord") {
for {
svc <- ZIO.service[PresentationService]
pairwiseProverDid = DidId("did:peer:Prover")
record1 <- svc.createRecord()
record2 <- svc.createRecord()
record1 <- svc.createRecord(format = CredentialFormat.JWT)
record2 <- svc.createRecord(format = CredentialFormat.JWT)
records <- svc.getPresentationRecords(false)
} yield {
assertTrue(records.size == 2)
Expand All @@ -114,7 +190,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("getPresentationRecordsByStates returns the correct records") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
records <- svc.getPresentationRecordsByStates(
ignoreWithZeroRetries = true,
limit = 10,
Expand All @@ -132,16 +208,16 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("getPresentationRecord returns the correct record") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
bRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
bRecord <- svc.createRecord(format = CredentialFormat.JWT)
record <- svc.getPresentationRecord(bRecord.id)
} yield assertTrue(record.contains(bRecord))
},
test("getPresentationRecord returns nothing for an unknown 'recordId'") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
bRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
bRecord <- svc.createRecord(format = CredentialFormat.JWT)
record <- svc.getPresentationRecord(DidCommID())
} yield assertTrue(record.isEmpty)
},
Expand All @@ -159,7 +235,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
IssueCredentialRecord.ProtocolState.CredentialReceived
)
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
repo <- ZIO.service[PresentationRepository]
_ <- repo.updatePresentationWithCredentialsToUse(
aRecord.id,
Expand All @@ -176,7 +252,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
for {
svc <- ZIO.service[PresentationService]
pairwiseProverDid = DidId("did:peer:Prover")
record <- svc.createRecord()
record <- svc.createRecord(format = CredentialFormat.JWT)
record <- svc.markRequestPresentationSent(record.id)

} yield {
Expand All @@ -187,7 +263,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
for {
svc <- ZIO.service[PresentationService]
pairwiseProverDid = DidId("did:peer:Prover")
record <- svc.createRecord()
record <- svc.createRecord(format = CredentialFormat.JWT)
repo <- ZIO.service[PresentationRepository]
_ <- repo.updatePresentationRecordProtocolState(
record.id,
Expand Down Expand Up @@ -331,7 +407,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
for {
svc <- ZIO.service[PresentationService]
pairwiseProverDid = DidId("did:peer:Prover")
record <- svc.createRecord()
record <- svc.createRecord(format = CredentialFormat.JWT)
repo <- ZIO.service[PresentationRepository]
_ <- repo.updatePresentationRecordProtocolState(
record.id,
Expand All @@ -347,7 +423,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("receivePresentation updates the PresentatinRecord") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
p = presentation(aRecord.thid.value)
aRecordReceived <- svc.receivePresentation(p)

Expand All @@ -359,7 +435,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("acceptPresentation updates the PresentatinRecord") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
p = presentation(aRecord.thid.value)
aRecordReceived <- svc.receivePresentation(p)
repo <- ZIO.service[PresentationRepository]
Expand All @@ -377,7 +453,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("markPresentationRejected updates the PresentatinRecord") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
p = presentation(aRecord.thid.value)
_ <- svc.receivePresentation(p)
repo <- ZIO.service[PresentationRepository]
Expand All @@ -396,7 +472,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("rejectPresentation updates the PresentatinRecord") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
p = presentation(aRecord.thid.value)
aRecordReceived <- svc.receivePresentation(p)
repo <- ZIO.service[PresentationRepository]
Expand All @@ -416,7 +492,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
for {
svc <- ZIO.service[PresentationService]
pairwiseProverDid = DidId("did:peer:Prover")
record <- svc.createRecord()
record <- svc.createRecord(format = CredentialFormat.JWT)
p = presentation(record.thid.value)
repo <- ZIO.service[PresentationRepository]
_ <- repo.updatePresentationRecordProtocolState(
Expand All @@ -433,7 +509,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
for {
svc <- ZIO.service[PresentationService]
pairwiseProverDid = DidId("did:peer:Prover")
record <- svc.createRecord()
record <- svc.createRecord(format = CredentialFormat.JWT)
repo <- ZIO.service[PresentationRepository]
_ <- repo.updatePresentationRecordProtocolState(
record.id,
Expand All @@ -448,7 +524,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("receiveProposePresentation updates the PresentatinRecord") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
p = proposePresentation(aRecord.thid.value)
aRecordReceived <- svc.receiveProposePresentation(p)
} yield {
Expand All @@ -459,7 +535,7 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
test("acceptProposePresentation updates the PresentatinRecord") {
for {
svc <- ZIO.service[PresentationService]
aRecord <- svc.createRecord()
aRecord <- svc.createRecord(format = CredentialFormat.JWT)
p = proposePresentation(aRecord.thid.value)
aRecordReceived <- svc.receiveProposePresentation(p)
repo <- ZIO.service[PresentationRepository]
Expand All @@ -485,8 +561,8 @@ object PresentationServiceSpec extends ZIOSpecDefault with PresentationServiceSp
val wallet2 = ZLayer.succeed(WalletAccessContext(walletId2))
for {
svc <- ZIO.service[PresentationService]
record1 <- svc.createRecord().provide(wallet1)
record2 <- svc.createRecord().provide(wallet2)
record1 <- svc.createRecord(format = CredentialFormat.JWT).provide(wallet1)
record2 <- svc.createRecord(format = CredentialFormat.JWT).provide(wallet2)
ownRecord1 <- svc.getPresentationRecord(record1.id).provide(wallet1)
ownRecord2 <- svc.getPresentationRecord(record2.id).provide(wallet2)
crossRecord1 <- svc.getPresentationRecord(record1.id).provide(wallet2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import io.iohk.atala.mercury.model.{AttachmentDescriptor, DidId}
import io.iohk.atala.mercury.protocol.presentproof.*
import io.iohk.atala.mercury.{AgentPeerService, PeerDID}
import io.iohk.atala.pollux.core.model.*
import io.iohk.atala.pollux.core.repository.PresentationRepository
import io.iohk.atala.pollux.core.repository.{
CredentialRepository,
CredentialRepositoryInMemory,
PresentationRepositoryInMemory
}
import io.iohk.atala.pollux.core.model.error.PresentationError
import io.iohk.atala.pollux.core.repository.{CredentialRepository, CredentialRepositoryInMemory, PresentationRepository, PresentationRepositoryInMemory}
import io.iohk.atala.pollux.vc.jwt.*
import io.iohk.atala.shared.models.WalletAccessContext
import zio.*

import java.security.*
Expand Down Expand Up @@ -140,21 +137,22 @@ trait PresentationServiceSpecHelper {

extension (svc: PresentationService)
def createRecord(
pairwiseVerifierDID: DidId = DidId("did:prism:issuer"),
pairwiseProverDID: DidId = DidId("did:prism:prover-pairwise"),
thid: DidCommID = DidCommID(),
schemaId: String = "schemaId",
connectionId: Option[String] = None,
) = {
pairwiseVerifierDID: DidId = DidId("did:prism:issuer"),
pairwiseProverDID: DidId = DidId("did:prism:prover-pairwise"),
thid: DidCommID = DidCommID(),
schemaId: _root_.java.lang.String = "schemaId",
format: CredentialFormat = CredentialFormat.JWT,
options: Option[io.iohk.atala.pollux.core.model.presentation.Options] = None
): ZIO[WalletAccessContext, PresentationError, PresentationRecord] = {
val proofType = ProofType(schemaId, None, None)
svc.createPresentationRecord(
thid = thid,
pairwiseVerifierDID = pairwiseVerifierDID,
pairwiseProverDID = pairwiseProverDID,
connectionId = Some("connectionId"),
proofTypes = Seq(proofType),
options = None,
format = CredentialFormat.JWT,
options = options,
format = format
)
}

Expand Down

0 comments on commit eb9b8a2

Please sign in to comment.