Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mercury) : Added challenge domain and presentation definition #322

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import io.iohk.atala.resolvers.UniversalDidResolver
import io.iohk.atala.mercury.protocol.connection.*
import io.iohk.atala.mercury.protocol.invitation.v2.Invitation
import io.iohk.atala.resolvers.DIDResolver
import io.circe.Json
import io.circe.parser.*
import io.circe.syntax.*

/** AgentCli
* {{{
Expand Down Expand Up @@ -187,12 +190,14 @@ object AgentCli extends ZIOAppDefault {
}

// Make a Request
body = RequestPresentation.Body(goal_code = Some("Propose Presentation"))
attachmentDescriptor = AttachmentDescriptor(
"1",
Some("application/json"),
LinkData(links = Seq("http://test"), hash = "1234")
body = RequestPresentation.Body(goal_code = Some("Presentation Request"))
presentationAttachment = PresentationAttachment.build(
Some(Options(challenge = "somechallenge", domain = "somedomain"))
)
// attachmentDescriptor = AttachmentDescriptor.buildBase64Attachment(payload =
// presentationAttachment.asJson.noSpaces.getBytes()
// )
attachmentDescriptor = AttachmentDescriptor.buildJsonAttachment(payload = presentationAttachment)
requestPresentation = RequestPresentation(
body = body,
attachments = Seq(attachmentDescriptor),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ def json2Map(json: Json): Any = json match {
case _ => null // Impossible case but Json cases are private in circe ...
}

def mapValueToJson(obj: java.lang.Object): Json = {
obj match {
case null => Json.Null
case b: java.lang.Boolean => Json.fromBoolean(b)
case i: java.lang.Integer => Json.fromInt(i)
case d: java.lang.Double =>
Json.fromDouble(d).getOrElse(Json.fromDouble(0d).get)
case l: java.lang.Long => Json.fromLong(l)
case s: java.lang.String => Json.fromString(String.valueOf(s))
case array: com.nimbusds.jose.shaded.json.JSONArray => {
Json.fromValues(array.iterator().asScala.map(mapValueToJson).toList)
}
case joseObject: com.nimbusds.jose.shaded.json.JSONObject =>
Json.fromJsonObject {
JsonObject.fromMap(
joseObject
.asInstanceOf[java.util.Map[String, Object]]
.asScala
.toMap
.view
.mapValues(mapValueToJson)
.toMap
)
}
case any => {
println("*****NotImplemented***" + any.getClass().getCanonicalName() + "**********") // FIXME
???
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this method returns a ZIO
We have to change a lot of code, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it will be more refactoring required

}
}
}

given Conversion[AttachmentDescriptor, XAttachment] with {
def apply(attachment: AttachmentDescriptor): XAttachment = {
val id = attachment.id
Expand All @@ -82,8 +113,8 @@ given Conversion[XAttachment, AttachmentDescriptor] with {
val data: AttachmentData = attachment.getData().toJSONObject.asScala.toMap match {
case e if e contains ("json") =>
val aux = e("json")
println(aux.getClass().getCanonicalName()) // TODO
???
val x = aux.asInstanceOf[java.util.Map[String, Object]].asScala.toMap.view.mapValues(mapValueToJson)
JsonData(JsonObject.fromMap(x.toMap))
case e if e contains ("base64") =>
val tmp = e("base64").asInstanceOf[String] // ...
Base64(tmp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object LinkData {

}

final case class JsonData(data: JsonObject) extends AttachmentData
final case class JsonData(json: JsonObject) extends AttachmentData
object JsonData {
given Encoder[JsonData] = deriveEncoder[JsonData]
given Decoder[JsonData] = deriveDecoder[JsonData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private[this] trait ReadAttachmentsUtils {
case obj: JsonData =>
java.util.Base64
.getUrlEncoder()
.encode(obj.data.asJson.noSpaces.getBytes())
.encode(obj.json.asJson.noSpaces.getBytes())
})
maybeAttachament.map(formatName -> _)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.iohk.atala.mercury.protocol.presentproof

import io.circe._
import io.circe.generic.semiauto._
import io.circe.syntax._

case class Field(
id: Option[String] = None,
path: Seq[String] = Seq.empty,
name: Option[String] = None,
purpose: Option[String] = None,
)
object Field {
given Encoder[Field] = deriveEncoder[Field]
given Decoder[Field] = deriveDecoder[Field]
}

case class Jwt(alg: Seq[String], proof_type: Seq[String])
object Jwt {
given Encoder[Jwt] = deriveEncoder[Jwt]
given Decoder[Jwt] = deriveDecoder[Jwt]
}
case class ClaimFormat(jwt: Jwt)
object ClaimFormat {
given Encoder[ClaimFormat] = deriveEncoder[ClaimFormat]
given Decoder[ClaimFormat] = deriveDecoder[ClaimFormat]
}
case class Constraints(fields: Option[Seq[Field]])
object Constraints {
given Encoder[Constraints] = deriveEncoder[Constraints]
given Decoder[Constraints] = deriveDecoder[Constraints]
}

/** Refer to <a href="https://identity.foundation/presentation-exchange/#input-descriptor">Input Descriptors</a>
*/
case class InputDescriptor(
id: String = java.util.UUID.randomUUID.toString(),
name: Option[String] = None,
purpose: Option[String] = None,
format: Option[ClaimFormat] = None,
constraints: Constraints
)
object InputDescriptor {
given Encoder[InputDescriptor] = deriveEncoder[InputDescriptor]
given Decoder[InputDescriptor] = deriveDecoder[InputDescriptor]
}

/** Refer to <a href="https://identity.foundation/presentation-exchange/#presentation-definition">Presentation
* Definition</a>
*/
case class PresentationDefinition(
id: String = java.util.UUID.randomUUID.toString(), // UUID
input_descriptors: Seq[InputDescriptor] = Seq.empty,
name: Option[String] = None,
purpose: Option[String] = None,
format: Option[ClaimFormat] = None,
)
object PresentationDefinition {
given Encoder[PresentationDefinition] = deriveEncoder[PresentationDefinition]
given Decoder[PresentationDefinition] = deriveDecoder[PresentationDefinition]
}

case class Options(challenge: String, domain: String)
object Options {
given Encoder[Options] = deriveEncoder[Options]
given Decoder[Options] = deriveDecoder[Options]
}

case class PresentationAttachment(options: Option[Options] = None, presentation_definition: PresentationDefinition)
object PresentationAttachment {
given Encoder[PresentationAttachment] = deriveEncoder[PresentationAttachment]
given Decoder[PresentationAttachment] = deriveDecoder[PresentationAttachment]

def build(options: Option[Options] = None): PresentationAttachment = {
val presentationDefinition =
PresentationDefinition(input_descriptors = Seq.empty)
PresentationAttachment(options, presentationDefinition)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package io.iohk.atala.mercury.protocol.presentproof

import cats.implicits.*
import io.circe.Json
import io.circe.parser.*
import io.circe.syntax.*
import io.iohk.atala.mercury.model.AttachmentDescriptor.attachmentDescriptorEncoderV2
import io.iohk.atala.mercury.model.{AttachmentDescriptor, DidId}
import munit.*
import zio.*
import io.iohk.atala.mercury.model._

class PresentationAttachmentSpec extends ZSuite {

test("Verifier Request Presentation Attachment") {
val expectedConstraintJson = parse(s"""
{
"fields": [
{
"path": [
"credentialSubject.dateOfBirth",
"credentialSubject.dob",
"vc.credentialSubject.dateOfBirth",
"vc.credentialSubject.dob"
]
}
]
}
""".stripMargin).getOrElse(Json.Null)
val field = Field(
None,
path = Seq(
"credentialSubject.dateOfBirth",
"credentialSubject.dob",
"vc.credentialSubject.dateOfBirth",
"vc.credentialSubject.dob"
)
)
val constraints = Constraints(fields = Some(Seq(field)))
val result = constraints.asJson.deepDropNullValues
assertEquals(result, expectedConstraintJson)

val expectedInputDescriptorJson = parse(s"""
{
"id": "wa_driver_license",
"name": "Washington State Business License",
"purpose": "We can only allow licensed Washington State business representatives into the WA Business Conference",
"constraints": {
"fields": [
{
"path": [
"credentialSubject.dateOfBirth",
"credentialSubject.dob",
"vc.credentialSubject.dateOfBirth",
"vc.credentialSubject.dob"
]
}
]
}
}
""".stripMargin).getOrElse(Json.Null)

val inputDescriptor = InputDescriptor(
id = "wa_driver_license",
name = Some("Washington State Business License"),
purpose =
Some("We can only allow licensed Washington State business representatives into the WA Business Conference"),
constraints = constraints
)
val resultInputDescriptor = inputDescriptor.asJson.deepDropNullValues
assertEquals(resultInputDescriptor, expectedInputDescriptorJson)

val expectedPresentationDefinitionJson = parse(s"""
{
"id": "32f54163-7166-48f1-93d8-ff217bdb0653",
"input_descriptors": [
{
"id": "wa_driver_license",
"name": "Washington State Business License",
"purpose": "We can only allow licensed Washington State business representatives into the WA Business Conference",
"constraints": {
"fields": [
{
"path": [
"credentialSubject.dateOfBirth",
"credentialSubject.dob",
"vc.credentialSubject.dateOfBirth",
"vc.credentialSubject.dob"
]
}
]
}
}
]
}
""".stripMargin).getOrElse(Json.Null)

val presentationDefinition =
PresentationDefinition(id = "32f54163-7166-48f1-93d8-ff217bdb0653", input_descriptors = Seq(inputDescriptor))
val resultPresentationDefinition = presentationDefinition.asJson.deepDropNullValues
assertEquals(resultPresentationDefinition, expectedPresentationDefinitionJson)

val expectedPresentationAttachmentJson = parse(s"""
{
"options": {
"challenge": "23516943-1d79-4ebd-8981-623f036365ef",
"domain": "us.gov/DriversLicense"
},
"presentation_definition": {
"id": "32f54163-7166-48f1-93d8-ff217bdb0653",
"input_descriptors": [
{
"id": "wa_driver_license",
"name": "Washington State Business License",
"purpose": "We can only allow licensed Washington State business representatives into the WA Business Conference",
"constraints": {
"fields": [
{
"path": [
"credentialSubject.dateOfBirth",
"credentialSubject.dob",
"vc.credentialSubject.dateOfBirth",
"vc.credentialSubject.dob"
]
}
]
}
}
]
}
}
""".stripMargin).getOrElse(Json.Null)
val options = Options(challenge = "23516943-1d79-4ebd-8981-623f036365ef", domain = "us.gov/DriversLicense")
val presentationAttachment =
PresentationAttachment(presentation_definition = presentationDefinition, options = Some(options))
val resultPresentationAttachment = presentationAttachment.asJson.deepDropNullValues
println(resultPresentationAttachment)
assertEquals(resultPresentationAttachment, expectedPresentationAttachmentJson)

}
}