-
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: use the compact format in SD-JWT
- Loading branch information
1 parent
d6dfb72
commit 5136dd7
Showing
9 changed files
with
248 additions
and
162 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
26 changes: 14 additions & 12 deletions
26
...ala/org/hyperledger/identus/pollux/core/model/presentation/SdJwtPresentationPayload.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 |
---|---|---|
@@ -1,16 +1,18 @@ | ||
package org.hyperledger.identus.pollux.core.model.presentation | ||
|
||
import org.hyperledger.identus.pollux.sdjwt.PresentationJson | ||
import org.hyperledger.identus.pollux.sdjwt.PresentationCompact | ||
import zio.json.* | ||
|
||
case class SdJwtPresentationPayload( | ||
claimsToDisclose: ast.Json.Obj, | ||
presentation: PresentationJson, | ||
options: Option[Options] | ||
) | ||
object SdJwtPresentationPayload { | ||
given JsonDecoder[Options] = DeriveJsonDecoder.gen[Options] | ||
given JsonEncoder[Options] = DeriveJsonEncoder.gen[Options] | ||
given JsonDecoder[SdJwtPresentationPayload] = DeriveJsonDecoder.gen[SdJwtPresentationPayload] | ||
given JsonEncoder[SdJwtPresentationPayload] = DeriveJsonEncoder.gen[SdJwtPresentationPayload] | ||
} | ||
//FIXME | ||
type SdJwtPresentationPayload = PresentationCompact | ||
// case class SdJwtPresentationPayload( | ||
// claimsToDisclose: ast.Json.Obj, | ||
// presentation: PresentationCompact, | ||
// options: Option[Options] | ||
// ) | ||
// object SdJwtPresentationPayload { | ||
// given JsonDecoder[Options] = DeriveJsonDecoder.gen[Options] | ||
// given JsonEncoder[Options] = DeriveJsonEncoder.gen[Options] | ||
// given JsonDecoder[SdJwtPresentationPayload] = DeriveJsonDecoder.gen[SdJwtPresentationPayload] | ||
// given JsonEncoder[SdJwtPresentationPayload] = DeriveJsonEncoder.gen[SdJwtPresentationPayload] | ||
// } |
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
95 changes: 95 additions & 0 deletions
95
pollux/sd-jwt/src/main/scala/org/hyperledger/identus/pollux/sdjwt/CompactFormat.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,95 @@ | ||
package org.hyperledger.identus.pollux.sdjwt | ||
|
||
import zio.json.* | ||
|
||
type Header = String | ||
type Payload = String | ||
type Signature = String | ||
type Disclosure = String | ||
type KBJWT = (String, String, String) | ||
|
||
case class CredentialCompact( | ||
override val jwtHeader: Header, | ||
override val jwtPayload: Payload, | ||
override val jwtSignature: Signature, | ||
override val disclosures: Seq[Disclosure], | ||
) extends ModelsExtensionMethods { | ||
override def kbJWT: Option[KBJWT] = None | ||
} | ||
|
||
object CredentialCompact { | ||
// given encoder: JsonEncoder[CredentialCompact] = DeriveJsonEncoder.gen[CredentialCompact] | ||
// given decoder: JsonDecoder[CredentialCompact] = DeriveJsonDecoder.gen[CredentialCompact] | ||
given decoder: JsonDecoder[CredentialCompact] = JsonDecoder.string.map(CredentialCompact.unsafeFromCompact(_)) | ||
given encoder: JsonEncoder[CredentialCompact] = JsonEncoder.string.contramap[CredentialCompact](_.compact) | ||
|
||
// /** | ||
// * // <Issuer-signed JWT>~<Disclosure 1>~<Disclosure 2>~...~<Disclosure N>~<optional KB-JWT> | ||
// * | ||
// * @param value | ||
// * @return | ||
// */ | ||
def unsafeFromCompact(str: String): CredentialCompact = { | ||
import scala.util.matching.Regex | ||
// var str = "hhh.ppp.sss~a~b~xxxx" | ||
val patternCompact = new Regex("(^[\\w-]*)\\.([\\w-]*)\\.([\\w-]*)((?:~(?:[\\w-]*))*)~$") | ||
val patternDisclosure = new Regex("(~([\\w]+))") | ||
|
||
val patternCompact(h, p, s, disclosuresStr) = str: @unchecked | ||
CredentialCompact( | ||
jwtHeader = h, | ||
jwtPayload = p, | ||
jwtSignature = s, | ||
disclosures = patternDisclosure.findAllIn(disclosuresStr).toSeq.map(_.drop(1)), | ||
) | ||
} | ||
} | ||
|
||
case class PresentationCompact( | ||
override val jwtHeader: Header, | ||
override val jwtPayload: Payload, | ||
override val jwtSignature: Signature, | ||
override val disclosures: Seq[Disclosure], | ||
override val kbJWT: Option[KBJWT] | ||
) extends ModelsExtensionMethods | ||
|
||
object PresentationCompact { | ||
// given encoder: JsonEncoder[PresentationCompact] = DeriveJsonEncoder.gen[PresentationCompact] | ||
// given decoder: JsonDecoder[PresentationCompact] = DeriveJsonDecoder.gen[PresentationCompact] | ||
given decoder: JsonDecoder[PresentationCompact] = JsonDecoder.string.map(PresentationCompact.unsafeFromCompact(_)) | ||
given encoder: JsonEncoder[PresentationCompact] = JsonEncoder.string.contramap[PresentationCompact](_.compact) | ||
|
||
// /** | ||
// * // <Issuer-signed JWT>~<Disclosure 1>~<Disclosure 2>~...~<Disclosure N>~<optional KB-JWT> | ||
// * | ||
// * @param value | ||
// * @return | ||
// */ | ||
def unsafeFromCompact(str: String): PresentationCompact = { | ||
import scala.util.matching.Regex | ||
// var str = "hhh.ppp.sss~a~b~xxxx" | ||
val patternCompact = new Regex( | ||
"^([\\w-]*)\\.([\\w-]*)\\.([\\w-]*)((?:~(?:[\\w-]*))*)~(?:([\\w-]*)\\.([\\w-]*)\\.([\\w-]*))?$" | ||
) | ||
val patternDisclosure = new Regex("(~([\\w]+))") | ||
|
||
val patternCompact(h, p, s, disclosuresStr, kb_h, kb_p, kb_s) = str: @unchecked | ||
|
||
val kbJWT: Option[KBJWT] = ( | ||
Option(kb_h).filterNot(_.isBlank()), | ||
Option(kb_p).filterNot(_.isBlank()), | ||
Option(kb_s).filterNot(_.isBlank()) | ||
) match | ||
case (None, None, None) => None | ||
case (Some(h), Some(p), Some(s)) => Some((h, p, s)) | ||
case _ => None // FIXME error | ||
|
||
PresentationCompact( | ||
jwtHeader = h, | ||
jwtPayload = p, | ||
jwtSignature = s, | ||
disclosures = patternDisclosure.findAllIn(disclosuresStr).toSeq.map(_.drop(1)), | ||
kbJWT = kbJWT, | ||
) | ||
} | ||
} |
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
Oops, something went wrong.