From 71b7116aab7fe278e8b41d2b12584272a03feecf Mon Sep 17 00:00:00 2001 From: Bassam <84336050+CryptoKnightIOG@users.noreply.github.com> Date: Wed, 28 Sep 2022 10:15:44 -0400 Subject: [PATCH] [ATL-1342] feat(pollux): initialise VC JWT (#42) --- pollux/vc-jwt/.gitignore | 32 +++++++++++++++++++ pollux/vc-jwt/.scalafmt.conf | 4 +++ pollux/vc-jwt/README.md | 8 +++++ pollux/vc-jwt/build.sbt | 12 +++++++ pollux/vc-jwt/project/Dependencies.scala | 23 +++++++++++++ pollux/vc-jwt/project/build.properties | 1 + .../io/iohk/atala/pollux/vc/jwt/Main.scala | 24 ++++++++++++++ .../io/iohk/atala/pollux/vc/jwt/MySuite.scala | 9 ++++++ 8 files changed, 113 insertions(+) create mode 100644 pollux/vc-jwt/.gitignore create mode 100644 pollux/vc-jwt/.scalafmt.conf create mode 100644 pollux/vc-jwt/README.md create mode 100644 pollux/vc-jwt/build.sbt create mode 100644 pollux/vc-jwt/project/Dependencies.scala create mode 100644 pollux/vc-jwt/project/build.properties create mode 100644 pollux/vc-jwt/src/main/scala/io/iohk/atala/pollux/vc/jwt/Main.scala create mode 100644 pollux/vc-jwt/src/test/scala/io/iohk/atala/pollux/vc/jwt/MySuite.scala diff --git a/pollux/vc-jwt/.gitignore b/pollux/vc-jwt/.gitignore new file mode 100644 index 0000000000..9e79245eef --- /dev/null +++ b/pollux/vc-jwt/.gitignore @@ -0,0 +1,32 @@ +# macOS +.DS_Store + +# sbt specific +dist/* +target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ +project/local-plugins.sbt +.history +.ensime +.ensime_cache/ +.sbt-scripted/ +local.sbt + +# Bloop +.bsp + +# VS Code +.vscode/ + +# Metals +.bloop/ +.metals/ +metals.sbt + +# IDEA +.idea +.idea_modules +/.worksheet/ diff --git a/pollux/vc-jwt/.scalafmt.conf b/pollux/vc-jwt/.scalafmt.conf new file mode 100644 index 0000000000..688e6cbf01 --- /dev/null +++ b/pollux/vc-jwt/.scalafmt.conf @@ -0,0 +1,4 @@ +version = 3.5.8 +runner.dialect = scala3 + +maxColumn = 120 \ No newline at end of file diff --git a/pollux/vc-jwt/README.md b/pollux/vc-jwt/README.md new file mode 100644 index 0000000000..102c5cad5f --- /dev/null +++ b/pollux/vc-jwt/README.md @@ -0,0 +1,8 @@ +## sbt project compiled with Scala 3 + +### Usage + +This is a normal sbt project. You can compile code with `sbt compile`, run it with `sbt run`, and `sbt console` will start a Scala 3 REPL. + +For more information on the sbt-dotty plugin, see the +[scala3-example-project](https://github.com/scala/scala3-example-project/blob/main/README.md). diff --git a/pollux/vc-jwt/build.sbt b/pollux/vc-jwt/build.sbt new file mode 100644 index 0000000000..71e6f4b135 --- /dev/null +++ b/pollux/vc-jwt/build.sbt @@ -0,0 +1,12 @@ +import Dependencies._ + +ThisBuild / version := "0.1.0-SNAPSHOT" +ThisBuild / scalaVersion := "3.1.3" +ThisBuild / organization := "io.iohk.atala" + +lazy val root = project + .in(file(".")) + .settings( + name := "pollux-vc-jwt", + libraryDependencies ++= polluxVcJwtDependencies + ) diff --git a/pollux/vc-jwt/project/Dependencies.scala b/pollux/vc-jwt/project/Dependencies.scala new file mode 100644 index 0000000000..f6061d9e8f --- /dev/null +++ b/pollux/vc-jwt/project/Dependencies.scala @@ -0,0 +1,23 @@ +import sbt._ + +object Dependencies { + object Versions { + val circeVersion = "0.14.3" + val jwtCirceVersion = "9.1.1" + } + + private lazy val coreJwtCirce = "io.circe" %% "circe-core" % Versions.circeVersion + private lazy val genericJwtCirce = "io.circe" %% "circe-generic" % Versions.circeVersion + private lazy val parserJwtCirce = "io.circe" %% "circe-parser" % Versions.circeVersion + + private lazy val jwtCirce = "com.github.jwt-scala" %% "jwt-circe" % Versions.jwtCirceVersion + + private lazy val test = "org.scalameta" %% "munit" % "0.7.29" % Test + + // Dependency Modules + private lazy val circeDependencies: Seq[ModuleID] = Seq(coreJwtCirce, genericJwtCirce, parserJwtCirce) + private lazy val baseDependencies: Seq[ModuleID] = circeDependencies :+ jwtCirce :+ test + + // Project Dependencies + lazy val polluxVcJwtDependencies: Seq[ModuleID] = baseDependencies +} diff --git a/pollux/vc-jwt/project/build.properties b/pollux/vc-jwt/project/build.properties new file mode 100644 index 0000000000..22af2628c4 --- /dev/null +++ b/pollux/vc-jwt/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.7.1 diff --git a/pollux/vc-jwt/src/main/scala/io/iohk/atala/pollux/vc/jwt/Main.scala b/pollux/vc-jwt/src/main/scala/io/iohk/atala/pollux/vc/jwt/Main.scala new file mode 100644 index 0000000000..58f58af341 --- /dev/null +++ b/pollux/vc-jwt/src/main/scala/io/iohk/atala/pollux/vc/jwt/Main.scala @@ -0,0 +1,24 @@ +package io.iohk.atala.pollux.vc.jwt +import io.circe.* +import pdi.jwt.JwtClaim +import pdi.jwt.{JwtAlgorithm, JwtCirce} + +import java.security.* +import java.security.spec.* +import java.time.Instant + +@main def jwtDemo(): Unit = + val keyGen = KeyPairGenerator.getInstance("EC") + val ecSpec = ECGenParameterSpec("secp256r1") + keyGen.initialize(ecSpec, SecureRandom()) + val keyPair = keyGen.generateKeyPair() + val privateKey = keyPair.getPrivate + val publicKey = keyPair.getPublic + + val Right(claimJson) = jawn.parse(s"""{"expires":${Instant.now.getEpochSecond}}""") + + val jwt = JwtCirce.encode(claimJson, privateKey, JwtAlgorithm.ES256) + + println(jwt) + + println(s"Is Valid: ${JwtCirce.isValid(jwt, publicKey)}") diff --git a/pollux/vc-jwt/src/test/scala/io/iohk/atala/pollux/vc/jwt/MySuite.scala b/pollux/vc-jwt/src/test/scala/io/iohk/atala/pollux/vc/jwt/MySuite.scala new file mode 100644 index 0000000000..8cc4df2a8f --- /dev/null +++ b/pollux/vc-jwt/src/test/scala/io/iohk/atala/pollux/vc/jwt/MySuite.scala @@ -0,0 +1,9 @@ +package io.iohk.atala.pollux.vc.jwt + +class MySuite extends munit.FunSuite { + test("example test that succeeds") { + val obtained = 42 + val expected = 42 + assertEquals(obtained, expected) + } +}