1+ package com.example.blockchainaccess
2+
3+ import com.google.gson.annotations.SerializedName
4+ import com.google.gson.*
5+ import java.lang.reflect.Type
6+
7+ sealed class BlockchainEvent {
8+ abstract val type: String
9+ abstract val blockHeight: Int
10+ abstract val timestamp: String
11+ abstract val previousBlockHash: String
12+
13+ abstract val aggregatorSignature: String
14+ abstract val blockHash: String
15+ }
16+
17+ data class AddUserEvent (
18+ @SerializedName(" type" ) override val type : String ,
19+ @SerializedName(" block_height" ) override val blockHeight : Int ,
20+ @SerializedName(" room" ) val room : String ,
21+ @SerializedName(" timestamp" ) override val timestamp : String ,
22+ @SerializedName(" previous_block_hash" ) override val previousBlockHash : String ,
23+ @SerializedName(" votes" ) val votes : List <Vote >,
24+ @SerializedName(" new_user_id" ) val newUserId : String ,
25+ @SerializedName(" voting_summary" ) val votingSummary : VotingSummary ,
26+ @SerializedName(" added_to_chain" ) val addedToChain : Boolean ,
27+ @SerializedName(" aggregator_signature" ) override val aggregatorSignature : String ,
28+ @SerializedName(" block_hash" ) override val blockHash : String
29+ ) : BlockchainEvent()
30+
31+ data class AccessEvent (
32+ @SerializedName(" type" ) override val type : String ,
33+ @SerializedName(" block_height" ) override val blockHeight : Int ,
34+ @SerializedName(" room" ) val room : String ,
35+ @SerializedName(" timestamp" ) override val timestamp : String ,
36+ @SerializedName(" previous_block_hash" ) override val previousBlockHash : String ,
37+ @SerializedName(" votes" ) val votes : List <Vote >,
38+ @SerializedName(" user_id" ) val userId : String ,
39+ @SerializedName(" voting_summary" ) val votingSummary : VotingSummary ,
40+ @SerializedName(" added_to_chain" ) val addedToChain : Boolean ,
41+ @SerializedName(" aggregator_signature" ) override val aggregatorSignature : String ,
42+ @SerializedName(" block_hash" ) override val blockHash : String
43+ ) : BlockchainEvent()
44+
45+ data class AddPubkeyEvent (
46+ @SerializedName(" type" ) override val type : String ,
47+ @SerializedName(" block_height" ) override val blockHeight : Int ,
48+ @SerializedName(" user_id" ) val userId : String ,
49+ @SerializedName(" public_key" ) val publicKey : String ,
50+ @SerializedName(" timestamp" ) override val timestamp : String ,
51+ @SerializedName(" previous_block_hash" ) override val previousBlockHash : String ,
52+ @SerializedName(" aggregator_signature" ) override val aggregatorSignature : String ,
53+ @SerializedName(" block_hash" ) override val blockHash : String
54+ ) : BlockchainEvent()
55+
56+ data class Vote (
57+ @SerializedName(" node_id" ) val nodeId : String ,
58+ @SerializedName(" vote" ) val vote : String ,
59+ @SerializedName(" signature" ) val signature : String
60+ )
61+
62+ data class VotingSummary (
63+ @SerializedName(" total_nodes" ) val totalNodes : Int ,
64+ @SerializedName(" yes_votes" ) val yesVotes : Int ,
65+ @SerializedName(" no_votes" ) val noVotes : Int
66+ )
67+
68+ /* *
69+ * A custom TypeAdapter for deserializing BlockchainEvent objects.
70+ * This adapter inspects the "type" field to determine the correct subclass
71+ * to instantiate.
72+ */
73+ class BlockchainEventTypeAdapter : JsonDeserializer <BlockchainEvent > {
74+ override fun deserialize (
75+ json : JsonElement ,
76+ typeOfT : Type ? ,
77+ context : JsonDeserializationContext
78+ ): BlockchainEvent {
79+ val jsonObject = json.asJsonObject
80+ val eventType = jsonObject.get(" type" ).asString
81+
82+ return when (eventType) {
83+ " add_user_event" -> context.deserialize(jsonObject, AddUserEvent ::class .java)
84+ " access_event" -> context.deserialize(jsonObject, AccessEvent ::class .java)
85+ " add_pubkey_event" -> context.deserialize(jsonObject, AddPubkeyEvent ::class .java)
86+ else -> throw IllegalArgumentException (" Unknown event type: $eventType " )
87+ }
88+ }
89+ }
0 commit comments