Skip to content

Commit

Permalink
Callback ui updates, IPV8 ds prosecution
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonKempen committed May 29, 2024
1 parent 1286d20 commit 584e914
Show file tree
Hide file tree
Showing 38 changed files with 1,589 additions and 616 deletions.
2 changes: 1 addition & 1 deletion offlineeuro/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity android:name=".OfflineEuroMainActivity"></activity>
<activity android:name=".OfflineEuroMainActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,4 @@ import nl.tudelft.trustchain.common.BaseActivity

class OfflineEuroMainActivity : BaseActivity() {
override val navigationGraph = R.navigation.nav_graph_offlineeuro
override val bottomNavigationMenu = R.menu.offlineeuro_navigation_menu

/**
* The values for shared preferences used by this activity.
*/
object EurotokenPreferences {
const val EUROTOKEN_SHARED_PREF_NAME = "eurotoken"
const val DEMO_MODE_ENABLED = "demo_mode_enabled"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nl.tudelft.trustchain.offlineeuro.communication

import it.unisa.dia.gas.jpbc.Element
import nl.tudelft.trustchain.offlineeuro.cryptography.BilinearGroup
import nl.tudelft.trustchain.offlineeuro.cryptography.GrothSahaiProof
import nl.tudelft.trustchain.offlineeuro.cryptography.RandomizationElements
import nl.tudelft.trustchain.offlineeuro.entity.Participant
import nl.tudelft.trustchain.offlineeuro.entity.TransactionDetails
Expand Down Expand Up @@ -40,6 +41,12 @@ interface ICommunicationProtocol {
transactionDetails: TransactionDetails
): String

fun requestFraudControl(
firstProof: GrothSahaiProof,
secondProof: GrothSahaiProof,
nameTTP: String
): String

fun getPublicKeyOf(
name: String,
group: BilinearGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureRandomn
import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureReplyMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.CommunityMessageType
import nl.tudelft.trustchain.offlineeuro.community.message.FraudControlReplyMessage
import nl.tudelft.trustchain.offlineeuro.community.message.FraudControlRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.ICommunityMessage
import nl.tudelft.trustchain.offlineeuro.community.message.MessageList
import nl.tudelft.trustchain.offlineeuro.community.message.TTPRegistrationMessage
Expand All @@ -19,6 +21,7 @@ import nl.tudelft.trustchain.offlineeuro.community.message.TransactionRandomizat
import nl.tudelft.trustchain.offlineeuro.community.message.TransactionRandomizationElementsRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.TransactionResultMessage
import nl.tudelft.trustchain.offlineeuro.cryptography.BilinearGroup
import nl.tudelft.trustchain.offlineeuro.cryptography.GrothSahaiProof
import nl.tudelft.trustchain.offlineeuro.cryptography.RandomizationElements
import nl.tudelft.trustchain.offlineeuro.db.AddressBookManager
import nl.tudelft.trustchain.offlineeuro.entity.Address
Expand All @@ -28,6 +31,7 @@ import nl.tudelft.trustchain.offlineeuro.entity.TTP
import nl.tudelft.trustchain.offlineeuro.entity.TransactionDetails
import nl.tudelft.trustchain.offlineeuro.entity.User
import nl.tudelft.trustchain.offlineeuro.enums.Role
import nl.tudelft.trustchain.offlineeuro.libraries.GrothSahaiSerializer
import java.math.BigInteger

class IPV8CommunicationProtocol(
Expand Down Expand Up @@ -113,6 +117,21 @@ class IPV8CommunicationProtocol(
return message.result
}

override fun requestFraudControl(
firstProof: GrothSahaiProof,
secondProof: GrothSahaiProof,
nameTTP: String
): String {
val ttpAddress = addressBookManager.getAddressByName(nameTTP)
community.sendFraudControlRequest(
GrothSahaiSerializer.serializeGrothSahaiProof(firstProof),
GrothSahaiSerializer.serializeGrothSahaiProof(secondProof),
ttpAddress.peerPublicKey!!
)
val message = waitForMessage(CommunityMessageType.FraudControlReplyMessage) as FraudControlReplyMessage
return message.result
}

fun scopePeers() {
community.scopePeers(participant.name, getParticipantRole(), participant.publicKey.toBytes())
}
Expand Down Expand Up @@ -146,6 +165,7 @@ class IPV8CommunicationProtocol(
val publicKey = participant.group.gElementFromBytes(message.publicKeyBytes)
val address = Address(message.name, message.role, publicKey, message.peerPublicKey)
addressBookManager.insertAddress(address)
participant.onDataChangeCallback?.invoke(null)
}

private fun handleGetBilinearGroupAndCRSRequest(message: BilinearGroupCRSRequestMessage) {
Expand Down Expand Up @@ -199,7 +219,12 @@ class IPV8CommunicationProtocol(
}

private fun handleTransactionMessage(message: TransactionMessage) {
val bankPublicKey = addressBookManager.getAddressByName("Bank").publicKey
val bankPublicKey =
if (participant is Bank) {
participant.publicKey
} else {
addressBookManager.getAddressByName("Bank").publicKey
}

val group = participant.group
val publicKey = group.gElementFromBytes(message.publicKeyBytes)
Expand All @@ -226,6 +251,17 @@ class IPV8CommunicationProtocol(
community.sendAddressReply(participant.name, role, participant.publicKey.toBytes(), message.requestingPeer)
}

private fun handleFraudControlRequestMessage(message: FraudControlRequestMessage) {
if (getParticipantRole() != Role.TTP) {
return
}
val ttp = participant as TTP
val firstProof = GrothSahaiSerializer.deserializeProofBytes(message.firstProofBytes, participant.group)
val secondProof = GrothSahaiSerializer.deserializeProofBytes(message.secondProofBytes, participant.group)
val result = ttp.getUserFromProofs(firstProof, secondProof)
community.sendFraudControlReply(result, message.requestingPeer)
}

private fun handleRequestMessage(message: ICommunityMessage) {
when (message) {
is AddressMessage -> handleAddressMessage(message)
Expand All @@ -237,6 +273,7 @@ class IPV8CommunicationProtocol(
is TransactionRandomizationElementsRequestMessage -> handleTransactionRandomizationElementsRequest(message)
is TransactionMessage -> handleTransactionMessage(message)
is TTPRegistrationMessage -> handleRegistrationMessage(message)
is FraudControlRequestMessage -> handleFraudControlRequestMessage(message)
else -> throw Exception("Unsupported message type")
}
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureRandomn
import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureRandomnessRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureReplyMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.FraudControlReplyMessage
import nl.tudelft.trustchain.offlineeuro.community.message.FraudControlRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.ICommunityMessage
import nl.tudelft.trustchain.offlineeuro.community.message.MessageList
import nl.tudelft.trustchain.offlineeuro.community.message.TTPRegistrationMessage
Expand All @@ -26,6 +28,7 @@ import nl.tudelft.trustchain.offlineeuro.community.payload.AddressPayload
import nl.tudelft.trustchain.offlineeuro.community.payload.BilinearGroupCRSPayload
import nl.tudelft.trustchain.offlineeuro.community.payload.BlindSignatureRequestPayload
import nl.tudelft.trustchain.offlineeuro.community.payload.ByteArrayPayload
import nl.tudelft.trustchain.offlineeuro.community.payload.FraudControlRequestPayload
import nl.tudelft.trustchain.offlineeuro.community.payload.TTPRegistrationPayload
import nl.tudelft.trustchain.offlineeuro.community.payload.TransactionDetailsPayload
import nl.tudelft.trustchain.offlineeuro.community.payload.TransactionRandomizationElementsPayload
Expand All @@ -50,9 +53,12 @@ object MessageID {
const val GET_TRANSACTION_RANDOMIZATION_ELEMENTS_REPLY = 17

const val TRANSACTION = 18
const val TRANSACTION_RESULT = 21
const val SCOPE_PEERS = 19
const val ADDRESS_RESPONSE = 20
const val TRANSACTION_RESULT = 19
const val SCOPE_PEERS = 20
const val ADDRESS_RESPONSE = 21

const val FRAUD_CONTROL_REQUEST = 22
const val FRAUD_CONTROL_REPLY = 23
}

class OfflineEuroCommunity(
Expand Down Expand Up @@ -85,6 +91,9 @@ class OfflineEuroCommunity(

messageHandlers[MessageID.SCOPE_PEERS] = ::onScopePeersPacket
messageHandlers[MessageID.ADDRESS_RESPONSE] = ::onAddressReplyPacket

messageHandlers[MessageID.FRAUD_CONTROL_REQUEST] = ::onFraudControlRequestPacket
messageHandlers[MessageID.FRAUD_CONTROL_REPLY] = ::onFraudControlReplyPacket
}

fun getGroupDescriptionAndCRS() {
Expand Down Expand Up @@ -136,7 +145,7 @@ class OfflineEuroCommunity(
val groupElements = payload.bilinearGroupElements
val crs = payload.crs

val ttpAddressMessage = AddressMessage("TTP", Role.Bank, payload.ttpPublicKey, peer.publicKey.keyToBin())
val ttpAddressMessage = AddressMessage("TTP", Role.TTP, payload.ttpPublicKey, peer.publicKey.keyToBin())

val message = BilinearGroupCRSReplyMessage(groupElements, crs, ttpAddressMessage)
addMessage(message)
Expand Down Expand Up @@ -445,6 +454,64 @@ class OfflineEuroCommunity(
addMessage(message)
}

fun sendFraudControlRequest(
firstProofBytes: ByteArray,
secondProofBytes: ByteArray,
ttpPublicKeyBytes: ByteArray
) {
val peer = getPeerByPublicKeyBytes(ttpPublicKeyBytes)

peer ?: throw Exception("TTP not found")

val packet =
serializePacket(
MessageID.FRAUD_CONTROL_REQUEST,
FraudControlRequestPayload(
firstProofBytes,
secondProofBytes
)
)

send(peer, packet)
}

fun onFraudControlRequestPacket(packet: Packet) {
val (peer, payload) = packet.getAuthPayload(FraudControlRequestPayload)
onFraudControlRequest(peer, payload)
}

private fun onFraudControlRequest(
peer: Peer,
payload: FraudControlRequestPayload
) {
val message = FraudControlRequestMessage(payload.firstProofBytes, payload.secondProofBytes, peer)
addMessage(message)
}

fun sendFraudControlReply(
result: String,
peer: Peer
) {
val packet =
serializePacket(
MessageID.FRAUD_CONTROL_REPLY,
ByteArrayPayload(
result.toByteArray()
)
)
send(peer, packet)
}

fun onFraudControlReplyPacket(packet: Packet) {
val (_, payload) = packet.getAuthPayload(ByteArrayPayload)
onFraudControlReply(payload)
}

fun onFraudControlReply(payload: ByteArrayPayload) {
val fraudControlResult = payload.bytes.toString(Charsets.UTF_8)
addMessage(FraudControlReplyMessage(fraudControlResult))
}

fun scopePeers(
name: String,
role: Role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ enum class CommunityMessageType {
TransactionRandomnessReplyMessage,

TransactionMessage,
TransactionResultMessage
TransactionResultMessage,

FraudControlRequestMessage,
FraudControlReplyMessage,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nl.tudelft.trustchain.offlineeuro.community.message

class FraudControlReplyMessage(
val result: String,
) : ICommunityMessage {
override val messageType = CommunityMessageType.FraudControlReplyMessage
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nl.tudelft.trustchain.offlineeuro.community.message

import nl.tudelft.ipv8.Peer

class FraudControlRequestMessage(
val firstProofBytes: ByteArray,
val secondProofBytes: ByteArray,
val requestingPeer: Peer
) : ICommunityMessage {
override val messageType = CommunityMessageType.FraudControlRequestMessage
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class MessageList<ICommunityMessage>(private val onRequestMessageAdded: (ICommun
BlindSignatureRequestMessage::class.java,
TransactionRandomizationElementsRequestMessage::class.java,
TransactionMessage::class.java,
TTPRegistrationMessage::class.java
TTPRegistrationMessage::class.java,
FraudControlRequestMessage::class.java
)

override fun add(element: ICommunityMessage): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nl.tudelft.trustchain.offlineeuro.community.payload

import nl.tudelft.ipv8.messaging.Deserializable
import nl.tudelft.ipv8.messaging.Serializable
import nl.tudelft.ipv8.messaging.deserializeVarLen
import nl.tudelft.ipv8.messaging.serializeVarLen

class FraudControlRequestPayload(
val firstProofBytes: ByteArray,
val secondProofBytes: ByteArray
) : Serializable {
override fun serialize(): ByteArray {
var payload = ByteArray(0)
payload += serializeVarLen(firstProofBytes)
payload += serializeVarLen(secondProofBytes)
return payload
}

companion object Deserializer : Deserializable<FraudControlRequestPayload> {
override fun deserialize(
buffer: ByteArray,
offset: Int
): Pair<FraudControlRequestPayload, Int> {
var localOffset = offset

val (firstProofBytes, firstProofSize) = deserializeVarLen(buffer, localOffset)
localOffset += firstProofSize

val (secondProofBytes, secondProofSize) = deserializeVarLen(buffer, localOffset)
localOffset += secondProofSize

return Pair(
FraudControlRequestPayload(firstProofBytes, secondProofBytes),
localOffset - offset
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ class AddressBookManager(
fun getAllAddresses(): List<Address> {
return queries.getAllAddresses(addressMapper).executeAsList()
}

fun clear() {
return queries.clearAddressBookTable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ class DepositedEuroManager(
fun getAllDepositedEuros(): List<DigitalEuro> {
return queries.getDepositedEuros(digitalEuroMapper).executeAsList()
}

fun clearDepositedEuros() {
return queries.clearDepositedEurosTable()
}
}
Loading

0 comments on commit 584e914

Please sign in to comment.