-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mercury): add utils methods on issue-credential-protocol (#131)
- Loading branch information
1 parent
b39e38f
commit 5a3e2fd
Showing
9 changed files
with
262 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ssue-credential/src/main/scala/io/iohk/atala/mercury/protocol/issuecredential/Utils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.iohk.atala.mercury.protocol.issuecredential | ||
|
||
import io.circe.syntax._ | ||
import io.circe.parser._ | ||
|
||
import io.iohk.atala.mercury.model._ | ||
import io.circe.Decoder | ||
|
||
private[this] trait BodyUtils { | ||
def formats: Seq[CredentialFormat] | ||
} | ||
|
||
private[this] trait ReadAttachmentsUtils { | ||
|
||
def body: BodyUtils | ||
def attachments: Seq[AttachmentDescriptor] | ||
|
||
/** @return | ||
* maping between the credential format name and the credential data in an array of Bytes encoded in base 64 | ||
*/ | ||
// protected inline | ||
lazy val getCredentialFormatAndCredential: Map[String, Array[Byte]] = | ||
body.formats | ||
.map { case CredentialFormat(id, formatName) => | ||
val maybeAttachament = attachments | ||
.find(_.id == id) | ||
.map(_.data match { | ||
case obj: JwsData => ??? // TODO | ||
case obj: Base64 => obj.base64.getBytes() | ||
case obj: LinkData => ??? // TODO Does this make sens | ||
case obj: JsonData => | ||
java.util.Base64 | ||
.getUrlEncoder() | ||
.encode(obj.data.asJson.noSpaces.getBytes()) | ||
}) | ||
maybeAttachament.map(formatName -> _) | ||
} | ||
.flatten | ||
.toMap | ||
// eyJhIjoiYSIsImIiOjEsIngiOjIsIm5hbWUiOiJNeU5hbWUiLCJkb2IiOiI_PyJ9 | ||
def getCredential[A](credentialFormatName: String)(using decodeA: Decoder[A]): Option[A] = | ||
getCredentialFormatAndCredential | ||
.get(credentialFormatName) | ||
.map(java.util.Base64.getUrlDecoder().decode(_)) | ||
.map(String(_)) | ||
.map(e => decode[A](e)) | ||
.flatMap(_.toOption) | ||
|
||
} |
109 changes: 109 additions & 0 deletions
109
.../src/test/scala/io/iohk/atala/mercury/protocol/anotherclasspath/UtilsCredentialSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package io.iohk.atala.mercury.protocol.anotherclasspath | ||
|
||
import cats.implicits.* | ||
import io.circe.* | ||
import io.circe.parser.* | ||
import io.circe.syntax.* | ||
import io.circe.generic.semiauto.* | ||
import zio.* | ||
import munit.* | ||
|
||
import io.iohk.atala.mercury.model._ | ||
import io.iohk.atala.mercury.protocol.issuecredential._ | ||
|
||
private[this] case class TestCredentialType(a: String, b: Int, x: Long, name: String, dob: String) | ||
private[this] object TestCredentialType { | ||
given Encoder[TestCredentialType] = deriveEncoder[TestCredentialType] | ||
given Decoder[TestCredentialType] = deriveDecoder[TestCredentialType] | ||
} | ||
|
||
/** testOnly io.iohk.atala.mercury.protocol.anotherclasspath.UtilsCredentialSpec | ||
*/ | ||
class UtilsCredentialSpec extends ZSuite { | ||
val nameCredentialType = "prism/TestCredentialType" | ||
|
||
val credential = TestCredentialType( | ||
a = "a", | ||
b = 1, | ||
x = 2, | ||
name = "MyName", | ||
dob = "??" | ||
) | ||
|
||
val credentialPreview = CredentialPreview(attributes = Seq()) | ||
val body = OfferCredential.Body( | ||
goal_code = Some("Offer Credential"), | ||
credential_preview = credentialPreview | ||
) | ||
val attachmentDescriptor = AttachmentDescriptor.buildAttachment(payload = credential) | ||
|
||
test("IssueCredential encode and decode any type of Credential into the attachments") { | ||
|
||
val msg = IssueCredential | ||
.build( | ||
fromDID = DidId("did:prism:test123from"), | ||
toDID = DidId("did:prism:test123to"), | ||
credentials = Map(nameCredentialType -> credential), | ||
) | ||
.makeMessage | ||
|
||
val obj = IssueCredential.readFromMessage(msg) | ||
|
||
assertEquals(obj.getCredentialFormatAndCredential.size, 1) | ||
assertEquals(obj.getCredentialFormatAndCredential.keySet, Set(nameCredentialType)) | ||
assertEquals(obj.getCredential[TestCredentialType](nameCredentialType), Some(credential)) | ||
} | ||
|
||
test("OfferCredential encode and decode any type of Credential into the attachments") { | ||
|
||
val msg = OfferCredential | ||
.build( | ||
fromDID = DidId("did:prism:test123from"), | ||
toDID = DidId("did:prism:test123to"), | ||
credential_preview = credentialPreview, | ||
credentials = Map(nameCredentialType -> credential), | ||
) | ||
.makeMessage | ||
|
||
val obj = OfferCredential.readFromMessage(msg) | ||
|
||
assertEquals(obj.getCredentialFormatAndCredential.size, 1) | ||
assertEquals(obj.getCredentialFormatAndCredential.keySet, Set(nameCredentialType)) | ||
assertEquals(obj.getCredential[TestCredentialType](nameCredentialType), Some(credential)) | ||
} | ||
|
||
test("ProposeCredential encode and decode any type of Credential into the attachments") { | ||
|
||
val msg = ProposeCredential | ||
.build( | ||
fromDID = DidId("did:prism:test123from"), | ||
toDID = DidId("did:prism:test123to"), | ||
credential_preview = credentialPreview, | ||
credentials = Map(nameCredentialType -> credential), | ||
) | ||
.makeMessage | ||
|
||
val obj = ProposeCredential.readFromMessage(msg) | ||
|
||
assertEquals(obj.getCredentialFormatAndCredential.size, 1) | ||
assertEquals(obj.getCredentialFormatAndCredential.keySet, Set(nameCredentialType)) | ||
assertEquals(obj.getCredential[TestCredentialType](nameCredentialType), Some(credential)) | ||
} | ||
|
||
test("RequestCredential encode and decode any type of Credential into the attachments") { | ||
|
||
val msg = RequestCredential | ||
.build( | ||
fromDID = DidId("did:prism:test123from"), | ||
toDID = DidId("did:prism:test123to"), | ||
credentials = Map(nameCredentialType -> credential), | ||
) | ||
.makeMessage | ||
|
||
val obj = RequestCredential.readFromMessage(msg) | ||
|
||
assertEquals(obj.getCredentialFormatAndCredential.size, 1) | ||
assertEquals(obj.getCredentialFormatAndCredential.keySet, Set(nameCredentialType)) | ||
assertEquals(obj.getCredential[TestCredentialType](nameCredentialType), Some(credential)) | ||
} | ||
} |
Oops, something went wrong.