-
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.
- Loading branch information
1 parent
cf156d9
commit b8d2469
Showing
3 changed files
with
178 additions
and
4 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
71 changes: 71 additions & 0 deletions
71
pollux/sd-jwt/src/main/scala/org/hyperledger/identus/pollux/sdjwt/SDJWT.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,71 @@ | ||
package org.hyperledger.identus.pollux.sdjwt | ||
|
||
import sd_jwt_rs.* | ||
|
||
opaque type SDJWTPublicKey = String | ||
object SDJWTPublicKey: | ||
def fromPem(pemKey: String): SDJWTPublicKey = pemKey | ||
extension (pemKey: SDJWTPublicKey) | ||
def value: String = pemKey | ||
def pem: String = pemKey | ||
|
||
opaque type SDJWTPrivateKey = String | ||
object SDJWTPrivateKey: | ||
def fromPem(pemKey: String): SDJWTPrivateKey = pemKey | ||
// def makeNew = { | ||
// // val pr = org.hyperledger.identus.shared.crypto.Apollo.default.ed25519.generateKeyPair | ||
// } | ||
extension (keyPem: SDJWTPrivateKey) | ||
def value: String = keyPem | ||
def encodingKey = new sd_jwt_rs.EncodingKeyValue(keyPem.value) | ||
|
||
opaque type SDJWTCredentialJson = String | ||
object SDJWTCredentialJson: | ||
def apply(value: String): SDJWTCredentialJson = value | ||
extension (c: SDJWTCredentialJson) | ||
def value: String = c | ||
// | ||
|
||
opaque type SDJWTPresentationJson = String | ||
object SDJWTPresentationJson: | ||
def apply(value: String): SDJWTPresentationJson = value | ||
extension (c: SDJWTPresentationJson) | ||
def value: String = c | ||
// | ||
|
||
object SDJWT { | ||
|
||
def issueCredential(key: SDJWTPrivateKey, claims: String): SDJWTCredentialJson = { | ||
val issuer = new SdjwtIssuerWrapper(key.encodingKey, "ES256") // null) | ||
val sdjwt = issuer.issueSdJwtAllLevel( | ||
claims, // user_claims | ||
null, // holder_key | ||
false, // add_decoy_claims | ||
SdjwtSerializationFormat.JSON // COMPACT // serialization_format | ||
) | ||
SDJWTCredentialJson(sdjwt) | ||
} | ||
def createPresentation(sdjwt: SDJWTCredentialJson, claimsToDisclose: String): SDJWTPresentationJson = { | ||
val holder = SdjwtHolderWrapper(sdjwt.value, SdjwtSerializationFormat.JSON) | ||
val presentation = holder.createPresentation( | ||
claimsToDisclose, | ||
null, // nonce | ||
null, // aud | ||
null, // holder_key | ||
null, // sign_alg | ||
) | ||
SDJWTPresentationJson(presentation) | ||
} | ||
|
||
def verifiePresentation(key: SDJWTPublicKey, presentation: SDJWTPresentationJson, claims: String) = { | ||
val verifier = SdjwtVerifierWrapper( | ||
presentation.value, // sd_jwt_presentation | ||
key.pem, // public_key | ||
null, // expected_aud | ||
null, // expected_nonce | ||
SdjwtSerializationFormat.JSON // serialization_format | ||
) | ||
val result: Boolean = verifier.verify(claims) | ||
result | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
pollux/sd-jwt/src/test/scala/org/hyperledger/identus/pollux/sdjwt/SDJWTSpec.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,91 @@ | ||
package org.hyperledger.identus.pollux.sdjwt | ||
|
||
import sd_jwt_rs.* | ||
|
||
import zio.* | ||
import zio.test.* | ||
import zio.test.Assertion.* | ||
import org.hyperledger.identus.pollux.sdjwt.* | ||
|
||
val ISSUER_KEY = SDJWTPrivateKey.fromPem( | ||
"""-----BEGIN PRIVATE KEY----- | ||
|MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgUr2bNKuBPOrAaxsR | ||
|nbSH6hIhmNTxSGXshDSUD1a1y7ihRANCAARvbx3gzBkyPDz7TQIbjF+ef1IsxUwz | ||
|X1KWpmlVv+421F7+c1sLqGk4HUuoVeN8iOoAcE547pJhUEJyf5Asc6pP | ||
|-----END PRIVATE KEY----- | ||
|""".stripMargin | ||
) | ||
|
||
val ISSUER_KEY_PUBLIC = SDJWTPublicKey.fromPem( | ||
"""-----BEGIN PUBLIC KEY----- | ||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEb28d4MwZMjw8+00CG4xfnn9SLMVM | ||
M19SlqZpVb/uNtRe/nNbC6hpOB1LqFXjfIjqAHBOeO6SYVBCcn+QLHOqTw== | ||
-----END PUBLIC KEY----- | ||
|""".stripMargin | ||
) | ||
|
||
val CLAIMS = | ||
"""{ | ||
| "sub": "6c5c0a49-b589-431d-bae7-219122a9ec2c", | ||
| "iss": "https://example.com/issuer", | ||
| "iat": 1683000000, | ||
| "exp": 1883000000, | ||
| "address": { | ||
| "street_address": "Schulstr. 12", | ||
| "locality": "Schulpforta", | ||
| "region": "Sachsen-Anhalt", | ||
| "country": "DE" | ||
| } | ||
|}""".stripMargin | ||
|
||
val CLAIMS_PRESENTED = | ||
"""{ | ||
| "sub": "6c5c0a49-b589-431d-bae7-219122a9ec2c", | ||
| "iss": "https://example.com/issuer", | ||
| "iat": 1683000000, | ||
| "exp": 1883000000, | ||
| "address": { | ||
| "region": "Sachsen-Anhalt", | ||
| "country": "DE" | ||
| } | ||
|}""".stripMargin | ||
|
||
val FAlSE_CLAIMS_PRESENTED = | ||
"""{ | ||
| "sub": "6c5c0a49-b589-431d-bae7-219122a9ec2c", | ||
| "iss": "https://example.com/issuer", | ||
| "iat": 1683000000, | ||
| "exp": 1883000000, | ||
| "address": { | ||
| "region": "Sachsen-Anhalt", | ||
| "country": "PT" | ||
| } | ||
|}""".stripMargin | ||
|
||
object SDJWTSpec extends ZIOSpecDefault { | ||
|
||
override def spec = suite("SDJWTRawSpec")( | ||
test("issue credential") { | ||
val credential = SDJWT.issueCredential(ISSUER_KEY, CLAIMS) | ||
assertTrue(!credential.value.isEmpty()) | ||
}, | ||
test("make presentation") { | ||
val credential = SDJWT.issueCredential(ISSUER_KEY, CLAIMS) | ||
val presentation = SDJWT.createPresentation(credential, CLAIMS_PRESENTED) | ||
assertTrue(!presentation.value.isEmpty()) | ||
}, | ||
test("verifie presentation") { | ||
val credential = SDJWT.issueCredential(ISSUER_KEY, CLAIMS) | ||
val presentation = SDJWT.createPresentation(credential, CLAIMS_PRESENTED) | ||
val ret = SDJWT.verifiePresentation(ISSUER_KEY_PUBLIC, presentation, CLAIMS_PRESENTED) | ||
assertTrue(ret) | ||
}, | ||
test("fail to verifie false claimes presentation") { | ||
val credential = SDJWT.issueCredential(ISSUER_KEY, CLAIMS) | ||
val presentation = SDJWT.createPresentation(credential, CLAIMS_PRESENTED) | ||
val ret = SDJWT.verifiePresentation(ISSUER_KEY_PUBLIC, presentation, FAlSE_CLAIMS_PRESENTED) | ||
assertTrue(!ret) | ||
}, | ||
) | ||
|
||
} |