Skip to content

Commit

Permalink
ADded scope message for user
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonKempen committed May 23, 2024
1 parent 86c0171 commit 3736c18
Show file tree
Hide file tree
Showing 22 changed files with 301 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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.CRS
import nl.tudelft.trustchain.offlineeuro.cryptography.RandomizationElements
import nl.tudelft.trustchain.offlineeuro.entity.Participant
import nl.tudelft.trustchain.offlineeuro.entity.TransactionDetails
Expand All @@ -11,7 +10,7 @@ import java.math.BigInteger
interface ICommunicationProtocol {
var participant: Participant

fun getGroupDescriptionAndCRS(): Pair<BilinearGroup, CRS>
fun getGroupDescriptionAndCRS()

fun register(
userName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nl.tudelft.trustchain.offlineeuro.communication
import it.unisa.dia.gas.jpbc.Element
import nl.tudelft.trustchain.offlineeuro.community.OfflineEuroCommunity
import nl.tudelft.trustchain.offlineeuro.community.message.AddressMessage
import nl.tudelft.trustchain.offlineeuro.community.message.AddressRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BilinearGroupCRSReplyMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BilinearGroupCRSRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureRandomnessReplyMessage
Expand All @@ -18,8 +19,6 @@ 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.CRS
import nl.tudelft.trustchain.offlineeuro.cryptography.PairingTypes
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 +27,8 @@ import nl.tudelft.trustchain.offlineeuro.entity.CentralAuthority
import nl.tudelft.trustchain.offlineeuro.entity.Participant
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 java.math.BigInteger

class IPV8CommunicationProtocol(
Expand All @@ -44,19 +45,15 @@ class IPV8CommunicationProtocol(
private val timeOutInMS = 5000
override lateinit var participant: Participant

override fun getGroupDescriptionAndCRS(): Pair<BilinearGroup, CRS> {
override fun getGroupDescriptionAndCRS() {
community.getGroupDescriptionAndCRS()
val message =
waitForMessage(CommunityMessageType.GroupDescriptionCRSReplyMessage) as BilinearGroupCRSReplyMessage

val group = BilinearGroup(pairingType = PairingTypes.FromFile)
group.updateGroupElements(message.groupDescription)
val crs = message.crs.toCRS(group)

participant.group = group
participant.group.updateGroupElements(message.groupDescription)
val crs = message.crs.toCRS(participant.group)
participant.crs = crs
messageList.add(message.addressMessage)
return Pair(group, crs)
}

override fun register(
Expand Down Expand Up @@ -113,6 +110,9 @@ class IPV8CommunicationProtocol(
return message.result
}

fun scopePeers() {
community.scopePeers(participant.name, getParticipantRole(), participant.publicKey.toBytes())
}
override fun getPublicKeyOf(
name: String,
group: BilinearGroup
Expand Down Expand Up @@ -216,9 +216,16 @@ class IPV8CommunicationProtocol(
ttp.registerUser(message.userName, publicKey)
}

private fun handleAddressRequestMessage(message: AddressRequestMessage) {
val role = getParticipantRole()

community.sendAddressReply(participant.name, role, participant.publicKey.toBytes(), message.requestingPeer)
}

private fun handleRequestMessage(message: ICommunityMessage) {
when (message) {
is AddressMessage -> handleAddressMessage(message)
is AddressRequestMessage -> handleAddressRequestMessage(message)
// is RegistrationMessage -> handleRegistrationMessage(message)
is BilinearGroupCRSRequestMessage -> handleGetBilinearGroupAndCRSRequest(message)
is BlindSignatureRandomnessRequestMessage -> handleBlindSignatureRandomnessRequest(message)
Expand All @@ -230,4 +237,13 @@ class IPV8CommunicationProtocol(
}
return
}

private fun getParticipantRole(): Role {
return when (participant) {
is User -> Role.User
is TTP -> Role.TTP
is Bank -> Role.Bank
else -> throw Exception("Unknown role")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import nl.tudelft.ipv8.attestation.trustchain.TrustChainSettings
import nl.tudelft.ipv8.attestation.trustchain.store.TrustChainStore
import nl.tudelft.ipv8.messaging.Packet
import nl.tudelft.trustchain.offlineeuro.community.message.AddressMessage
import nl.tudelft.trustchain.offlineeuro.community.message.AddressRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BilinearGroupCRSReplyMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BilinearGroupCRSRequestMessage
import nl.tudelft.trustchain.offlineeuro.community.message.BlindSignatureRandomnessReplyMessage
Expand All @@ -19,6 +20,7 @@ import nl.tudelft.trustchain.offlineeuro.community.message.MessageList
import nl.tudelft.trustchain.offlineeuro.community.message.TTPRegistrationMessage
import nl.tudelft.trustchain.offlineeuro.community.message.TransactionRandomizationElementsReplyMessage
import nl.tudelft.trustchain.offlineeuro.community.message.TransactionRandomizationElementsRequestMessage
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
Expand All @@ -33,6 +35,7 @@ import nl.tudelft.trustchain.offlineeuro.enums.Role
import java.math.BigInteger

object MessageID {

const val GET_GROUP_DESCRIPTION_CRS = 9
const val GET_GROUP_DESCRIPTION_CRS_REPLY = 10
const val REGISTER_AT_TTP = 11
Expand All @@ -46,6 +49,8 @@ object MessageID {
const val GET_TRANSACTION_RANDOMIZATION_ELEMENTS_REPLY = 17

const val TRANSACTION = 18
const val SCOPE_PEERS = 19
const val ADDRESS_RESPONSE = 20
}

class OfflineEuroCommunity(
Expand Down Expand Up @@ -74,6 +79,9 @@ class OfflineEuroCommunity(
messageHandlers[MessageID.GET_TRANSACTION_RANDOMIZATION_ELEMENTS_REPLY] = ::onGetTransactionRandomizationElementsReplyPacket

messageHandlers[MessageID.TRANSACTION] = ::onTransactionPacket

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

fun getGroupDescriptionAndCRS() {
Expand Down Expand Up @@ -332,6 +340,7 @@ class OfflineEuroCommunity(

fun onGetTransactionRandomizationElementsReplyPacket(packet: Packet) {
val (_, payload) = packet.getAuthPayload(TransactionRandomizationElementsPayload)
onGetTransactionRandomizationElements(payload)
}

fun onGetTransactionRandomizationElements(payload: TransactionRandomizationElementsPayload) {
Expand Down Expand Up @@ -369,6 +378,53 @@ class OfflineEuroCommunity(
// TODO
}

fun sendAddressReply(name: String, role: Role, publicKeyBytes: ByteArray, requestingPeer: Peer) {
val addressPacket = serializePacket(
MessageID.ADDRESS_RESPONSE,
AddressPayload(
name, publicKeyBytes, role
)
)

send(requestingPeer, addressPacket)
}

private fun onAddressReplyPacket(packet: Packet) {
val (peer, payload) = packet.getAuthPayload(AddressPayload)
onAddressReply(payload, peer)
}

private fun onAddressReply(payload: AddressPayload, peer: Peer) {
val message = AddressMessage(payload.userName, payload.role, payload.publicKey, peer.publicKey.keyToBin())
addMessage(message)
}

fun scopePeers(name: String, role: Role, publicKeyBytes: ByteArray) {
val addressPacket = serializePacket(
MessageID.SCOPE_PEERS,
AddressPayload(
name, publicKeyBytes, role
)
)

for (peer in getPeers()) {
send(peer, addressPacket)
}

}

private fun onScopePeersPacket(packet: Packet) {
val (requestingPeer, payload) = packet.getAuthPayload(AddressPayload)
onScopePeers(payload, requestingPeer)
}

private fun onScopePeers(payload: AddressPayload, requestingPeer: Peer) {
val addressMessage = AddressMessage(payload.userName, payload.role, payload.publicKey, requestingPeer.publicKey.keyToBin())
addMessage(addressMessage)

val addressRequestMessage = AddressRequestMessage(requestingPeer)
addMessage(addressRequestMessage)
}
private fun getPeerByPublicKeyBytes(publicKey: ByteArray): Peer? {
return getPeers().find {
it.publicKey.keyToBin().contentEquals(publicKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package nl.tudelft.trustchain.offlineeuro.community.message

import nl.tudelft.trustchain.offlineeuro.enums.Role

class AddressMessage(
class AddressMessage (
val name: String,
val role: Role,
val publicKeyBytes: ByteArray,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package nl.tudelft.trustchain.offlineeuro.community.message

import nl.tudelft.ipv8.Peer

class AddressRequestMessage(
val requestingPeer: Peer
): ICommunityMessage {
override val messageType = CommunityMessageType.AddressRequestMessage
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package nl.tudelft.trustchain.offlineeuro.community.message

enum class CommunityMessageType {
AddressMessage,
AddressRequestMessage,

RegistrationMessage,

GroupDescriptionCRSRequestMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class MessageList<ICommunityMessage>(private val onRequestMessageAdded: (ICommun
private val requestMessageTypes =
setOf(
AddressMessage::class.java,
AddressRequestMessage::class.java,
BilinearGroupCRSRequestMessage::class.java,
BlindSignatureRandomnessRequestMessage::class.java,
BlindSignatureRequestMessage::class.java,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package nl.tudelft.trustchain.offlineeuro.community.payload

import nl.tudelft.ipv8.messaging.Deserializable
import nl.tudelft.ipv8.messaging.SERIALIZED_LONG_SIZE
import nl.tudelft.ipv8.messaging.Serializable
import nl.tudelft.ipv8.messaging.deserializeLong
import nl.tudelft.ipv8.messaging.deserializeVarLen
import nl.tudelft.ipv8.messaging.serializeLong
import nl.tudelft.ipv8.messaging.serializeVarLen
import nl.tudelft.trustchain.offlineeuro.enums.Role

class AddressPayload(
val userName: String,
val publicKey: ByteArray,
val role: Role
) : Serializable {
override fun serialize(): ByteArray {
var payload = ByteArray(0)
payload += serializeVarLen(userName.toByteArray())
payload += serializeVarLen(publicKey)
payload += serializeLong(role.ordinal.toLong())
return payload
}

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

val (nameBytes, nameSize) = deserializeVarLen(buffer, localOffset)
localOffset += nameSize
val (publicKeyBytes, publicKeyBytesSize) = deserializeVarLen(buffer, localOffset)
localOffset += publicKeyBytesSize

val role = deserializeLong(buffer, localOffset)
localOffset += SERIALIZED_LONG_SIZE

return Pair(
AddressPayload(
nameBytes.toString(Charsets.UTF_8),
publicKeyBytes,
Role.fromLong(role)
),
localOffset - offset
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class BilinearGroup(

PairingTypes.FromFile -> {
if (context == null) {
PairingFactory.getPairing("assets/params/a_181_603.properties")
PairingFactory.getPairing("lib/params/a_181_603.properties")
} else {
// Access the assets folder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package nl.tudelft.trustchain.offlineeuro.entity
import android.content.Context
import it.unisa.dia.gas.jpbc.Element
import nl.tudelft.trustchain.offlineeuro.communication.ICommunicationProtocol
import nl.tudelft.trustchain.offlineeuro.cryptography.BilinearGroup
import nl.tudelft.trustchain.offlineeuro.cryptography.Schnorr
import nl.tudelft.trustchain.offlineeuro.db.DepositedEuroManager
import java.math.BigInteger
import kotlin.math.min

class Bank(
name: String,
group: BilinearGroup,
communicationProtocol: ICommunicationProtocol,
private val context: Context?,
private val depositedEuroManager: DepositedEuroManager = DepositedEuroManager(context, CentralAuthority.groupDescription)
Expand All @@ -19,6 +21,7 @@ class Bank(

init {
communicationProtocol.participant = this
this.group = group
setUp()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ abstract class Participant(
}

fun getGroupDescriptionAndCRS() {
val groupAndCRS = communicationProtocol.getGroupDescriptionAndCRS()
group = groupAndCRS.first
crs = groupAndCRS.second
communicationProtocol.getGroupDescriptionAndCRS()
}

fun generateKeyPair() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class TTP(
return registeredUserManager.addRegisteredUser(name, publicKey)
}

fun getRegisteredUsers(): List<RegisteredUser> {
return registeredUserManager.getAllRegisteredUsers()
}

override fun onReceivedTransaction(
transactionDetails: TransactionDetails,
publicKeyBank: Element,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ package nl.tudelft.trustchain.offlineeuro.entity
import android.content.Context
import it.unisa.dia.gas.jpbc.Element
import nl.tudelft.trustchain.offlineeuro.communication.ICommunicationProtocol
import nl.tudelft.trustchain.offlineeuro.cryptography.BilinearGroup
import nl.tudelft.trustchain.offlineeuro.cryptography.Schnorr
import nl.tudelft.trustchain.offlineeuro.db.WalletManager
import java.util.UUID

class User(
name: String,
group: BilinearGroup,
context: Context?,
private var walletManager: WalletManager? = null,
communicationProtocol: ICommunicationProtocol
) : Participant(communicationProtocol, name) {
val wallet: Wallet

init {
setUp()
communicationProtocol.participant = this
this.group = group
setUp()
if (walletManager == null) {
walletManager = WalletManager(context, group)
}
Expand Down
Loading

0 comments on commit 3736c18

Please sign in to comment.