Skip to content

Commit

Permalink
working on custom serializer for Map<String, Any>
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianIOHK committed Apr 16, 2024
1 parent b10d2c0 commit 9db3e3c
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 20 deletions.
@@ -1,6 +1,8 @@
package io.iohk.atala.prism.walletsdk.castor

import io.iohk.atala.prism.apollo.base64.base64DecodedBytes
import io.iohk.atala.prism.apollo.base64.base64UrlDecodedBytes
import io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey
import io.iohk.atala.prism.walletsdk.apollo.utils.Ed25519PublicKey
import io.iohk.atala.prism.walletsdk.apollo.utils.Secp256k1PublicKey
import io.iohk.atala.prism.walletsdk.apollo.utils.X25519PublicKey
Expand Down Expand Up @@ -200,15 +202,21 @@ constructor(
val crv = jwk["crv"]
return when (DIDDocument.VerificationMethod.getCurveByType(crv!!)) {
Curve.SECP256K1 -> {
Secp256k1PublicKey(x!!.encodeToByteArray())
if (jwk.containsKey("y")) {
val y = jwk["y"]
val kmmSecp = KMMECSecp256k1PublicKey.secp256k1FromByteCoordinates(x!!.base64UrlDecodedBytes, y!!.base64UrlDecodedBytes)
Secp256k1PublicKey(kmmSecp.raw)
} else {
Secp256k1PublicKey(x!!.base64UrlDecodedBytes)
}
}

Curve.ED25519 -> {
Ed25519PublicKey(x!!.encodeToByteArray())
Ed25519PublicKey(x!!.base64UrlDecodedBytes)
}

Curve.X25519 -> {
X25519PublicKey(x!!.encodeToByteArray())
X25519PublicKey(x!!.base64UrlDecodedBytes)
}
}
}
Expand Down
@@ -1,7 +1,31 @@
package io.iohk.atala.prism.walletsdk.domain.models

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.PolymorphicSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.buildSerialDescriptor
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.double
import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.float
import kotlinx.serialization.json.floatOrNull
import kotlinx.serialization.json.int
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.long
import kotlinx.serialization.json.longOrNull

interface JWTPayload {
val iss: String
Expand All @@ -15,6 +39,53 @@ interface JWTPayload {
val verifiableCredential: JWTVerifiableCredential?
}

object AnySerializer : KSerializer<Any> {

override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Any") {
element("value", PolymorphicSerializer(Any::class).descriptor)
}

override fun deserialize(decoder: Decoder): Any {
val dec: CompositeDecoder = decoder.beginStructure(descriptor)
var value: Any? = null

val jsonElement = dec.decodeSerializableElement(descriptor, 0, JsonElement.serializer())
value = when (jsonElement) {
is JsonPrimitive -> {
when {
jsonElement.isString -> jsonElement.content
jsonElement.floatOrNull != null -> jsonElement.float
jsonElement.intOrNull != null -> jsonElement.int
jsonElement.doubleOrNull != null -> jsonElement.double
jsonElement.longOrNull != null -> jsonElement.long
else -> jsonElement.content
}
}

else -> ""
}
dec.endStructure(descriptor)
return value
}

override fun serialize(encoder: Encoder, value: Any) {
val output = encoder.beginStructure(descriptor)
if (value is String) {
output.encodeStringElement(descriptor, 0, value)
} else if (value is Int) {
output.encodeIntElement(descriptor, 0, value)
} else if (value is Float) {
output.encodeFloatElement(descriptor, 0, value)
} else if (value is Boolean) {
output.encodeBooleanElement(descriptor, 0, value)
} else {
throw SerializationException("Unsupported type")
}
output.endStructure(descriptor)
}
}


/**
* A struct representing the verifiable credential in a JWT credential payload.
*/
Expand All @@ -24,7 +95,7 @@ data class JWTVerifiableCredential @JvmOverloads constructor(
val context: Array<String> = arrayOf(),
val type: Array<String> = arrayOf(),
val credentialSchema: VerifiableCredentialTypeContainer? = null,
val credentialSubject: Map<String, String>,
val credentialSubject: Map<String, @Serializable(with = AnySerializer::class) Any>,
val credentialStatus: VerifiableCredentialTypeContainer? = null,
val refreshService: VerifiableCredentialTypeContainer? = null,
val evidence: VerifiableCredentialTypeContainer? = null,
Expand Down
Expand Up @@ -627,7 +627,7 @@ class PolluxImpl(
id = UUID.randomUUID().toString(),
optional = false,
filter = presentationClaims.claims[path],
name = path
name = path,
)
} as MutableList

Expand All @@ -651,12 +651,6 @@ class PolluxImpl(
limitDisclosure = InputDescriptor.Constraints.LimitDisclosure.REQUIRED
)

val inputDescriptor = InputDescriptor(
name = options.name,
purpose = options.purpose,
constraints = constraints
)

val format =
InputDescriptor.PresentationFormat(
jwt = jwt.let {
Expand All @@ -666,6 +660,13 @@ class PolluxImpl(
}
)

val inputDescriptor = InputDescriptor(
name = options.name,
purpose = options.purpose,
constraints = constraints,
format = format
)

return PresentationDefinitionRequest(
presentationDefinition = PresentationDefinitionRequest.PresentationDefinition(
inputDescriptors = arrayOf(inputDescriptor),
Expand Down
Expand Up @@ -62,7 +62,7 @@ data class JWTCredential(
override val claims: Array<Claim>
get() {
return verifiableCredential?.credentialSubject?.map {
Claim(key = it.key, value = ClaimType.StringValue(it.value))
Claim(key = it.key, value = ClaimType.StringValue(it.value.toString()))
}?.toTypedArray()
?: emptyArray<Claim>()
}
Expand Down Expand Up @@ -135,7 +135,7 @@ data class JWTCredential(

override val claims: Array<Claim>
get() = verifiableCredential?.credentialSubject?.map {
Claim(key = it.key, value = ClaimType.StringValue(it.value))
Claim(key = it.key, value = ClaimType.StringValue(it.value.toString()))
}?.toTypedArray() ?: emptyArray()

override val properties: Map<String, Any?>
Expand Down
Expand Up @@ -1058,7 +1058,6 @@ class PrismAgent {
to = toDID,
direction = Message.Direction.SENT
)

connectionManager.sendMessage(presentationRequest.makeMessage())
}

Expand Down
Expand Up @@ -39,7 +39,7 @@ data class PresentationDefinitionRequest(
val id: String = UUID.randomUUID().toString(),
val name: String? = null,
val purpose: String? = null,
val format: String? = null,
val format: PresentationFormat? = null,
val constraints: Constraints
) {

Expand Down
Expand Up @@ -125,9 +125,12 @@ data class RequestPresentation(
fromMessage.from != null &&
fromMessage.to != null
) {
val json = Json {
ignoreUnknownKeys = true
}
return RequestPresentation(
id = fromMessage.id,
body = Json.decodeFromString(fromMessage.body) ?: Body(proofTypes = emptyArray()),
body = json.decodeFromString(fromMessage.body) ?: Body(proofTypes = emptyArray()),
attachments = fromMessage.attachments,
thid = fromMessage.thid,
from = fromMessage.from,
Expand Down
Expand Up @@ -25,6 +25,7 @@ import io.iohk.atala.prism.walletsdk.domain.models.DIDDocument
import io.iohk.atala.prism.walletsdk.domain.models.DIDResolver
import io.iohk.atala.prism.walletsdk.domain.models.DIDUrl
import io.iohk.atala.prism.walletsdk.domain.models.HttpResponse
import io.iohk.atala.prism.walletsdk.domain.models.JWTVerifiableCredential
import io.iohk.atala.prism.walletsdk.domain.models.KeyCurve
import io.iohk.atala.prism.walletsdk.domain.models.Mediator
import io.iohk.atala.prism.walletsdk.domain.models.Message
Expand Down Expand Up @@ -53,6 +54,7 @@ import java.util.*
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.SerializationException
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
Expand Down Expand Up @@ -1046,6 +1048,16 @@ class PrismAgentTests {
assertTrue(agent.handlePresentation(msg))
}

@Test
fun test() {
val vc = JWTVerifiableCredential(
credentialSubject = mapOf("string" to "string", "int" to 1, "float" to 2f, "boolean" to false)
)

val encoded = Json.encodeToString(vc)
val decoded = Json.decodeFromString<JWTVerifiableCredential>(encoded)
}

val getCredentialDefinitionResponse =
"{\"schemaId\":\"http://host.docker.internal:8000/prism-agent/schema-registry/schemas/5e0d5a93-4bfd-3111-a956-5d5bc82f76cc\",\"type\":\"CL\",\"tag\":\"licence\",\"value\":{\"primary\":{\"n\":\"105195159277979097653318357586659371305119697478469834190626350283715795188687389523188659352120689851168860621983864738336838773213022505168653440146374011050277159372491059901432822905781969400722059341786498751125483895348734607382548396665339315322605154516776326303787844694026898270194867398625429469096229269732265502538641116512214652017416624138065704599041020588805936844771273861390913500753293895219370960892829297672575154196820931047049021760519166121287056337193413235473255257349024671869248216238831094979209384406168241010010012567685965827447177652200129684927663161550376084422586141212281146491949\",\"s\":\"85376740935726732134199731472843597191822272986425414914465211197069650618238336366149699822721009443794877925725075553195071288777117865451699414058058985000654277974066307286552934230286237253977472401290858765904161191229985245519871949378628131263513153683765553672655918133136828182050729012388157183851720391379381006921499997765191873729408614024320763554099291141052786589157823043612948619201525441997065264492145372001259366749278235381762443117203343617927241093647322654346302447381494008414208398219626199373278313446814209403507903682881070548386699522575055488393512785511441688197244526708647113340516\",\"r\":{\"dateofissuance\":\"16159515692057558658031632775257139859912833740243870833808276956469677196577164655991169139545328065546186056342530531355718904597216453319851305621683589202769847381737819412615902541110462703838858425423753481085962114120185123089078513531045426316918036549403698066078445947881055316312848598741184161901260446303171175343050250045452903485086185722998336149005743485268486377824763449026501058416292877646187105446333888525480394665310217044483841168928926515929150167890936706159800372381200383816724043496032886366767166850459338411710056171379538841845247931898550165532492578625954615979453881721709564750235\",\"drivingclass\":\"83649701835078373520097916558245060224505938113940626586910000950978790663411517512280043632278010831292224659523658613504637416710001103641231226266903556936380105758523760424939825687213460920436570466066231912959327201876189240504388424799892400351592593406285436824571943165913587899115814843543998396726679289422080229750418336051741708013580146373647528674381958028243228435161765957312248113519708734663989428761879029086059388435772829434952754093999424834120341657211221855300108096057633128467059590470639772605075954658131680801785637700237403873940041665483384938586320674338994185073499523485570537331062\",\"emailaddress\":\"96995643129591814391344614133120459563648002327749700279517548454036811217735867585059116635583558148259032071807493674533230465312311981127622542797279917256478867847832932893748528200469349058284133058865149153179959849308383505167342565738382180666525211256221655129861213392455759272915565057394420728271409215556596974900718332893753172173500744392522771654048192448229319313386967045678744665093451560743782910263014930200762027209565313884859542996067229707388839912195826334964819133016500346618083969320902775088800287566711941842968839787149808739739233388585677095545116231323172342995837636586249573194609\",\"drivinglicenseid\":\"102840929811153624977554462471309185033977661854754815794111114507549576719389525167082631547450413573293352276930065480432301200611396989595571202142654033217842162456070556560693402484110499573693863745648118310258284468114751958738878996458420605301017450868522680454545537837403398645500541915771765220093329728663621098538954397330411649083351383375839056527007892276284168437065687748085384178113959961057476582871100422859953560730152958588610850909069434658487744782540788968302663076149478487413357533660817020800754493642858564081116318655661240523146995256712471572605700346459123074377380656921337264554594\",\"familyname\":\"2428690037146701497427424649573806616639612325136606164619283916796880313617677563507218774958436668407050506838114136163250163675016510113975582318007560622124292458766639319715064358235569650961433812439763343736699708535945693241909905707497180931492818502593885932421170612418693515054756633264933222189766691632082890045477718331705366111669009551578289182848340651375008362238266590844461708981816856194045325523248527964502118319210042254240848590574645476930113881493472578612352948284862674703949781070309344526122291448990325949065193279599181502524961004046979227803224474342778516917124487012958845744311\",\"master_secret\":\"96236339155824229583363924057798366491998077727991424922911165403434522806469328114407334094535810942859512352089785125683335350062474092708044674085769524387654467267128528564551803293661877480971961092735622606052503557881856409855812611523475975566606131897917979412576797874632169829901968854843162299366867885636535326810998541141840561418097240137120398317445832694001031827068485975315937269024666370665530455146256019590700349556357390218401217383173228376078058967743472704019765210324846681867991543267171763037513180046865961560351035005185946817643006206395175857900512245900162751815626427008481585714891\"},\"rctxt\":\"54359809198312125478916383106913469635175253891208897419510030559787479974126666313900084654632259260010008369569778456071591398552341004538623276997178295939490854663263886825856426285604332554317424030793691008221895556474599466123873279022389276698551452690414982831059651505731449763128921782866843113361548859434294057249048041670761184683271568216202174527891374770703485794299697663353847310928998125365841476766767508733046891626759537001358973715760759776149482147060701775948253839125589216812475133616408444838011643485797584321993661048373877626880635937563283836661934456534313802815974883441215836680800\",\"z\":\"99592262675748359673042256590146366586480829950402370244401571195191609039150608482506917768910598228167758026656953725016982562881531475875469671976107506976812319765644401707559997823702387678953647104105378063905395973550729717937712350758544336716556268064226491839700352305793370980462034813589488455836259737325502578253339820590260554457468082536249525493340350556649403477875367398139579018197084796440810685458274393317299082017275568964540311198115802021902455672385575542594821996060452628805634468222196284384514736044680778624637228114693554834388824212714580770066729185685978935409859595244639193538156\"}},\"issuerId\":\"did:prism:604ba1764ab89993f9a74625cc4f3e04737919639293eb382cc7adc53767f550\"}"
}
Expand Up @@ -10,6 +10,7 @@ import androidx.fragment.app.viewModels
import io.iohk.atala.prism.sampleapp.databinding.CredentialDialogBinding
import io.iohk.atala.prism.sampleapp.databinding.FragmentMessagesBinding
import io.iohk.atala.prism.walletsdk.domain.models.Credential
import io.iohk.atala.prism.walletsdk.domain.models.DID

class MessagesFragment : Fragment() {

Expand All @@ -32,7 +33,7 @@ class MessagesFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
binding.list.adapter = adapter
binding.sendMessage.setOnClickListener {
viewModel.sendMessage()
viewModel.sendMessage(DID("did:peer:2.Ez6LSkjhgJcoGRTSTpjN5XBSKGpNtDSa55qidsahb1s3ucWkJ.Vz6MkgG8bJA2P2HNhCwh4DGHmBtUbKiCafYwBtDMjKnAihaE9.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6ImRpZDpwZWVyOjIuRXo2TFNnaHdTRTQzN3duREUxcHQzWDZoVkRVUXpTanNIemlucFgzWEZ2TWpSQW03eS5WejZNa2hoMWU1Q0VZWXE2SkJVY1RaNkNwMnJhbkNXUnJ2N1lheDNMZTRONTlSNmRkLlNleUowSWpvaVpHMGlMQ0p6SWpwN0luVnlhU0k2SW1oMGRIQnpPaTh2WTNKcGMzUnBZVzR0YldWa2FXRjBiM0l1YW5KcFltOHVhMmwzYVNJc0ltRWlPbHNpWkdsa1kyOXRiUzkyTWlKZGZYMC5TZXlKMElqb2laRzBpTENKeklqcDdJblZ5YVNJNkluZHpjem92TDJOeWFYTjBhV0Z1TFcxbFpHbGhkRzl5TG1weWFXSnZMbXRwZDJrdmQzTWlMQ0poSWpwYkltUnBaR052YlcwdmRqSWlYWDE5IiwiciI6W10sImEiOltdfX0"))
}
binding.sendVerification.setOnClickListener {
InitiateVerificationDialogFragment(viewModel).show(
Expand Down
Expand Up @@ -96,12 +96,16 @@ class MessagesViewModel(application: Application) : AndroidViewModel(application
toDID = DID(toDID),
presentationClaims = PresentationClaims(
claims = mapOf(
"$.issuer" to InputFieldFilter(
// "$.issuer" to InputFieldFilter(
// type = "string",
// value = "did:prism:8b5321c004d4a11e87ef3601350eaa8b472a546d11677c9c6845c55a490f02f4"
// ),
"$.vp.emailAddress" to InputFieldFilter(
type = "string",
value = StringPredicate("did:prism:b0bde43c0f749705c34e1c1e1b70647068a80a861de14f73b73b03cd1fa472ca:CnoKeBI5CgVrZXktMRACSi4KCXNlY3AyNTZrMRIhA_HT0IOH-l7M5PdHtYXtEMs80xSnwZ8vnGwtSCvBTZYYEjsKB21hc3RlcjAQAUouCglzZWNwMjU2azESIQMxGlCcLnODDcnw9W2949Y5yDyMr2KxPqiKnN1-khXQwQ")
pattern = "corporate@tomate.domain.com"
)
),
issuer = "did:prism:b0bde43c0f749705c34e1c1e1b70647068a80a861de14f73b73b03cd1fa472ca:CnoKeBI5CgVrZXktMRACSi4KCXNlY3AyNTZrMRIhA_HT0IOH-l7M5PdHtYXtEMs80xSnwZ8vnGwtSCvBTZYYEjsKB21hc3RlcjAQAUouCglzZWNwMjU2azESIQMxGlCcLnODDcnw9W2949Y5yDyMr2KxPqiKnN1-khXQwQ"
issuer = "did:prism:8b5321c004d4a11e87ef3601350eaa8b472a546d11677c9c6845c55a490f02f4"
),
domain = "domain",
challenge = "challenge"
Expand Down

0 comments on commit 9db3e3c

Please sign in to comment.