Skip to content

Commit

Permalink
ATL-1932: Create JWT Verifiable Presentation (#69)
Browse files Browse the repository at this point in the history
* [ATL-1932] feat(pollux): replaced cats by zio

* [ATL-1932] feat(pollux): use opeque tupe alias

* [ATL-1932] feat(pollux): add VerifiablePayload Abstraction

* [ATL-1932] feat(pollux): added VerifiablePresentation
  • Loading branch information
CryptoKnightIOG authored Oct 19, 2022
1 parent 7ce014a commit de2af2f
Show file tree
Hide file tree
Showing 10 changed files with 822 additions and 467 deletions.
7 changes: 6 additions & 1 deletion pollux/vc-jwt/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ object Dependencies {
object Versions {
val circeVersion = "0.14.3"
val jwtCirceVersion = "9.1.1"
val zioPreludeVersion = "1.0.0-RC15"
}

private lazy val coreJwtCirce = ("io.circe" %% "circe-core" % Versions.circeVersion).cross(CrossVersion.for3Use2_13)
Expand All @@ -18,11 +19,15 @@ object Dependencies {
private lazy val jwtCirce =
("com.github.jwt-scala" %% "jwt-circe" % Versions.jwtCirceVersion).cross(CrossVersion.for3Use2_13)

private lazy val zioPrelude = "dev.zio" %% "zio-prelude" % Versions.zioPreludeVersion

private lazy val test = "org.scalameta" %% "munit" % "0.7.29" % Test

// Dependency Modules
private lazy val zioDependencies: Seq[ModuleID] = Seq(zioPrelude)
private lazy val circeDependencies: Seq[ModuleID] = Seq(coreJwtCirce, genericJwtCirce, parserJwtCirce)
private lazy val baseDependencies: Seq[ModuleID] = circeDependencies :+ jwtCirce :+ circeJsonSchema :+ test
private lazy val baseDependencies: Seq[ModuleID] =
circeDependencies ++ zioDependencies :+ jwtCirce :+ circeJsonSchema :+ test

// Project Dependencies
lazy val polluxVcJwtDependencies: Seq[ModuleID] = baseDependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
package io.iohk.atala.pollux.vc.jwt

import java.security.PrivateKey
import java.security.spec.ECGenParameterSpec
import java.sql.Timestamp
import java.time.{Instant, ZonedDateTime}
import io.circe
import io.circe.*
import pdi.jwt.JwtClaim
import pdi.jwt.{JwtAlgorithm, JwtCirce}
import io.circe.generic.auto.*
import io.circe.parser.decode
import io.circe.syntax.*
import net.reactivecore.cjs.resolver.Downloader
import net.reactivecore.cjs.{DocumentValidator, Loader, Result}
import pdi.jwt.algorithms.JwtECDSAAlgorithm
import pdi.jwt.{JwtAlgorithm, JwtCirce, JwtClaim}

import java.security.*
import java.security.spec.*
import java.time.Instant
import net.reactivecore.cjs.Loader
import net.reactivecore.cjs.{DocumentValidator, Loader, Result}
import net.reactivecore.cjs.resolver.Downloader
import cats.implicits.*
import io.circe.Json
import pdi.jwt.algorithms.JwtECDSAAlgorithm
import java.sql.Timestamp
import java.time.{Instant, ZonedDateTime}

opaque type JWT = String

object JWT {
def apply(value: String): JWT = value

extension (jwt: JWT) {
def value: String = jwt
}

object Implicits {
implicit val jwtEncoder: Encoder[JWT] =
(jwt: JWT) => jwt.value.asJson
}
}


case class EncodedJWT(jwt: String)
case class JWTHeader(typ: String = "JWT", alg: Option[String])

case class JWTPayload(
iss: Option[String],
sub: Option[String],
aud: Vector[String],
iat: Option[Instant],
nbf: Option[Instant],
exp: Option[Instant],
iss: Option[String],
sub: Option[String],
aud: Vector[String],
iat: Option[Instant],
nbf: Option[Instant],
exp: Option[Instant],
rexp: Option[Instant]
)
trait JWTVerified(
Expand All @@ -47,12 +60,13 @@ case class JWTVerifyPolicies(
)

trait Signer {
def encode(claim: Json): String
def encode(claim: Json): JWT
}

class ES256Signer(privateKey: PrivateKey) extends Signer {
val algorithm: JwtECDSAAlgorithm = JwtAlgorithm.ES256
override def encode(claim: Json): String = {
return JwtCirce.encode(claim, privateKey, algorithm)

override def encode(claim: Json): JWT = {
return JWT(JwtCirce.encode(claim, privateKey, algorithm))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.iohk.atala.pollux.vc.jwt

import io.circe
import io.circe.*
import io.circe.generic.auto.*
import io.circe.parser.decode
import io.circe.syntax.*

trait Verifiable(proof: Proof)

case class Proof(`type`: String, other: Json)

object Proof {

object Implicits {
implicit val proofEncoder: Encoder[Proof] =
(proof: Proof) => proof.other.deepMerge(Map("type" -> proof.`type`).asJson)
implicit val proofDecoder: Decoder[Proof] =
(c: HCursor) =>
for {
`type` <- c.downField("type").as[String]
other <- c.downField("type").delete.up.as[Json]
} yield {
Proof(
`type` = `type`,
other = other
)
}

}
}
Loading

0 comments on commit de2af2f

Please sign in to comment.