Skip to content

Commit 2b15bb3

Browse files
committed
[FEAT] Functionality to take a blockchainJSON and display the logs + votes details
1 parent cd645cc commit 2b15bb3

6 files changed

Lines changed: 567 additions & 28 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ dependencies {
5050
implementation("io.ktor:ktor-serialization-kotlinx-json:3.2.3")
5151
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
5252
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
53+
implementation("com.google.code.gson:gson:2.13.1")
54+
implementation("androidx.navigation:navigation-compose:2.9.3")
5355
implementation(libs.androidx.core.ktx)
5456
implementation(libs.androidx.lifecycle.runtime.ktx)
5557
implementation(libs.androidx.activity.compose)

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<activity
3131
android:name=".WhitelistActivity"
3232
android:label="Authority Whitelist" />
33+
<activity
34+
android:name=".LogsActivity"
35+
android:label="Logs" />
3336
<service
3437
android:name=".MyHostApduService"
3538
android:exported="true"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)