diff --git a/voter-registry/src/main/java/meerkat/SimpleRegistry.java b/voter-registry/src/main/java/meerkat/SimpleRegistry.java index 93ce5c6..89dd39a 100644 --- a/voter-registry/src/main/java/meerkat/SimpleRegistry.java +++ b/voter-registry/src/main/java/meerkat/SimpleRegistry.java @@ -1,21 +1,19 @@ package meerkat; import com.google.protobuf.ByteString; -import com.google.protobuf.Message; +import com.google.protobuf.InvalidProtocolBufferException; import meerkat.ProtobufsMessages.BasicMessage; import meerkat.ProtobufsMessages.Tag; -import meerkat.bulletinboard.BulletinBoardClient; import meerkat.bulletinboard.SimpleBulletinBoardClient; import meerkat.comm.CommunicationException; import meerkat.crypto.Encryption; -import meerkat.crypto.concrete.ECElGamalEncryption; import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.Crypto; -import meerkat.protobuf.ConcreteCrypto +import util.AccurateTimestamp; +import util.RegistryTagTypes; import java.io.IOException; -import java.util.List; -import java.util.Random; +import java.util.*; /** * Created by Vladimir Eliezer Tokarev on 1/8/2016. @@ -26,20 +24,15 @@ public class SimpleRegistry { protected Encryption signatory; - protected BulletinBoardClient communicator; - - public SimpleRegistry() { - signatory = new ECElGamalEncryption(); - communicator = new SimpleBulletinBoardClient(); - } + protected SimpleBulletinBoardClient communicator; /** * @param signatory implements the DigitalSignature interface * @param communicator implements the BulletinBoardClient interface */ - public SimpleRegistry(Encryption signatory, BulletinBoardClient communicator){ - signatory = signatory; - communicator = communicator; + public SimpleRegistry(Encryption signatory, SimpleBulletinBoardClient communicator){ + this.signatory = signatory; + this.communicator = communicator; } /** @@ -49,19 +42,22 @@ public class SimpleRegistry { * @throws throws CommunicationException * @return void */ - public void AddVoter(String voterID, String personalData) throws CommunicationException { + public void AddVoter(String voterID, String personalData) throws CommunicationException, IOException { Tag.Builder idTag = Tag.newBuilder(); idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); Tag.Builder voterEntryTag = Tag.newBuilder(); voterEntryTag.setContent(RegistryTagTypes.VOTER_ENTRY_TAG); - BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); - basicMessage.setData(ByteString.copyFrom(personalData.getBytes())); - basicMessage.addTag(idTag); - basicMessage.addTag(voterEntryTag); + Tag.Builder timestampTag = Tag.newBuilder(); + timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " " + + AccurateTimestamp.GetCurrentTimestampString()); - communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); + BasicMessage.Builder basicMessage = + BasicMessage.newBuilder().addTag(idTag).addTag(voterEntryTag).addTag(timestampTag); + basicMessage.setData(ByteString.copyFrom(personalData.getBytes())); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build())); } /** @@ -71,7 +67,7 @@ public class SimpleRegistry { * @throws CommunicationException * @return true if the adding action succeeded else return false */ - public void AddToGroup(String voterID, String groupID) throws CommunicationException { + public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException { Tag.Builder idTag = Tag.newBuilder(); idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); @@ -81,12 +77,14 @@ public class SimpleRegistry { Tag.Builder actionTag = Tag.newBuilder(); actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.ADD_TO_GROUP_TAG); - BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); - basicMessage.addTag(idTag); - basicMessage.addTag(groupIDTag); - basicMessage.addTag(actionTag); + Tag.Builder timestampTag = Tag.newBuilder(); + timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " " + +AccurateTimestamp.GetCurrentTimestampString()); - communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); + BasicMessage.Builder basicMessage = + BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build())); } /** @@ -96,7 +94,7 @@ public class SimpleRegistry { * @return true if the removing action succeeded else return false * @throws CommunicationException */ - public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException { + public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException { Tag.Builder idTag = Tag.newBuilder(); idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); @@ -106,12 +104,14 @@ public class SimpleRegistry { Tag.Builder actionTag = Tag.newBuilder(); actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.REMOVE_FROM_GROUP_TAG); - BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); - basicMessage.addTag(idTag); - basicMessage.addTag(groupIDTag); - basicMessage.addTag(actionTag); + Tag.Builder timestampTag = Tag.newBuilder(); + timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " " + +AccurateTimestamp.GetCurrentTimestampString()); - communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); + BasicMessage.Builder basicMessage = + BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build())); } /** @@ -120,18 +120,20 @@ public class SimpleRegistry { * @return true if the set voted succeded else false * @throws CommunicationException */ - public void SetVoted(String id) throws CommunicationException { + public void SetVoted(String id) throws CommunicationException, IOException { Tag.Builder idTag = Tag.newBuilder(); idTag.setContent(RegistryTagTypes.ID_TAG + " " + id); Tag.Builder voteAction = Tag.newBuilder(); voteAction.setContent(RegistryTagTypes.VOTE_ACTION_TAG); - BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); - basicMessage.addTag(idTag); - basicMessage.addTag(voteAction); + Tag.Builder timestampTag = Tag.newBuilder(); + timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " " + +AccurateTimestamp.GetCurrentTimestampString()); - communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); + BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(idTag).addTag(voteAction).addTag(timestampTag); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build())); } /** @@ -140,25 +142,93 @@ public class SimpleRegistry { * @return BulletinBoardAPI.BulletinBoardMessage */ private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BasicMessage basicMessage) throws IOException { - BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardmessage = + + BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage = BulletinBoardAPI.BulletinBoardMessage.newBuilder(); - signatory.encrypt(basicMessage, signatory.generateRandomness(new Random())); - /** - * encode the basic message - * create bulletin board message - * put the basic message in bulletin board message - * put the encoded messages signature into the bulletin board message - * return the bulletin board message - */ + + // signs the basic message + Crypto.RerandomizableEncryptedMessage encryptedMessage = + signatory.encrypt(basicMessage, signatory.generateRandomness(new Random())); + + // creates the signature of signed basic message + Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder(); + messageSignature.mergeFrom(encryptedMessage); + + // creates the unsigned basic message + BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder unsignedBulletinBoardMessage = + BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder(); + unsignedBulletinBoardMessage.setData(basicMessage.toByteString()); + + // sets the signature and the byte array + bulletinBoardMessage.addSig(messageSignature); + + bulletinBoardMessage.setMsg(unsignedBulletinBoardMessage); + + return bulletinBoardMessage.build(); } + /** + * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG + * @param id + * @return List + */ + private List GetRelevantMessages(String id) { + BulletinBoardAPI.MessageFilterList.Builder filters = + BulletinBoardAPI.MessageFilterList.newBuilder(); + + BulletinBoardAPI.MessageFilter.Builder idFilter = + BulletinBoardAPI.MessageFilter.newBuilder().setTag(RegistryTagTypes.ID_TAG + " " + id) + .setTag(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.ADD_TO_GROUP_TAG) + .setTag(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.REMOVE_FROM_GROUP_TAG); + + filters.addFilter(idFilter); + + return communicator.readMessages(filters.build()); + } + + /** + * Gets all the basic messages from bulletin board messages + * @param listOfMessages + * @return List G + * @throws InvalidProtocolBufferException + */ + private List GetBasicMessagesFromBulletinBoardMessages( + List listOfMessages) throws InvalidProtocolBufferException { + + List basicMessages = new ArrayList(); + + for (BulletinBoardAPI.BulletinBoardMessage bulletinBoardMessage : listOfMessages){ + BasicMessage.Builder basicMessage = + BasicMessage.newBuilder().mergeFrom(bulletinBoardMessage.getMsg().getData()); + basicMessages.add(basicMessage.build()); + } + + return basicMessages; + } + + + private List GetAllGroupsIdsUserIn(List messages){ + Map groupIdToMessage = new HashMap(); + List latestStateOfGroup = new ArrayList(); + + for (BasicMessage message : messages) { + BasicMessage temporary = groupIdToMessage.get(message.get) + } + } + + /** * Requests all the groups that the given id voter is in - * @param ID + * @param id * @return list of groups ids (or names), if the method fails its empty * @throws CommunicationException */ - public List GetGroups(String ID) throws CommunicationException { + public List GetGroups(String id) throws CommunicationException { + List relevantMessages = GetRelevantMessages(id); + List groups = new ArrayList(); + + + /** * Retrieve all List that contains this id * creates new list of strings with the names of the group diff --git a/voter-registry/src/main/java/meerkat/AccurateTimestamp.java b/voter-registry/src/main/java/util/AccurateTimestamp.java similarity index 98% rename from voter-registry/src/main/java/meerkat/AccurateTimestamp.java rename to voter-registry/src/main/java/util/AccurateTimestamp.java index aaaac2a..680bf18 100644 --- a/voter-registry/src/main/java/meerkat/AccurateTimestamp.java +++ b/voter-registry/src/main/java/util/AccurateTimestamp.java @@ -1,4 +1,4 @@ -package meerkat; +package util; import java.sql.Timestamp; import java.text.ParseException; diff --git a/voter-registry/src/main/java/meerkat/RegistryTagTypes.java b/voter-registry/src/main/java/util/RegistryTagTypes.java similarity index 87% rename from voter-registry/src/main/java/meerkat/RegistryTagTypes.java rename to voter-registry/src/main/java/util/RegistryTagTypes.java index 9bedeff..f3eae81 100644 --- a/voter-registry/src/main/java/meerkat/RegistryTagTypes.java +++ b/voter-registry/src/main/java/util/RegistryTagTypes.java @@ -1,4 +1,4 @@ -package meerkat; +package util; /** * Created by Vladimir Eliezer Tokarev on 1/9/2016. @@ -20,6 +20,10 @@ public abstract class RegistryTagTypes { public static final String ADD_TO_GROUP_TAG = "Add To Group"; + public static final String ACTION_TIMESTAMP_TAG = "Action timestamp: "; + public static final String VOTE_ACTION_TAG = "Vote Action"; } + +