Search in sources :

Example 6 with WitnessMessage

use of com.github.dedis.popstellar.model.objects.WitnessMessage in project popstellar by dedis.

the class RollCallHandlerTest method testHandleOpenRollCall.

@Test
public void testHandleOpenRollCall() throws DataHandlingException {
    // Create the open Roll Call message
    OpenRollCall openRollCall = new OpenRollCall(CREATE_LAO.getId(), rollCall.getId(), rollCall.getStart(), EventState.CREATED);
    MessageGeneral message = new MessageGeneral(SENDER_KEY, openRollCall, GSON);
    // Call the message handler
    messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
    // Check the Roll Call is present with state OPENED and the correct ID
    Optional<RollCall> rollCallOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getRollCall(openRollCall.getUpdateId());
    assertTrue(rollCallOpt.isPresent());
    assertEquals(EventState.OPENED, rollCallOpt.get().getState());
    assertEquals(openRollCall.getUpdateId(), rollCallOpt.get().getId());
    // Check the WitnessMessage has been created
    Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
    assertTrue(witnessMessage.isPresent());
    // Check the Witness message contains the expected title and description
    WitnessMessage expectedMessage = openRollCallWitnessMessage(message.getMessageId(), rollCallOpt.get());
    assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
    assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) RollCallHandler.closeRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.closeRollCallWitnessMessage) RollCallHandler.createRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.createRollCallWitnessMessage) RollCallHandler.openRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.openRollCallWitnessMessage) Test(org.junit.Test)

Example 7 with WitnessMessage

use of com.github.dedis.popstellar.model.objects.WitnessMessage in project popstellar by dedis.

the class LaoDetailViewModel method signMessage.

public void signMessage(WitnessMessage witnessMessage) {
    Log.d(TAG, "signing message with ID " + witnessMessage.getMessageId());
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel();
    try {
        KeyPair mainKey = keyManager.getMainKeyPair();
        // generate the signature of the message
        Signature signature = mainKey.sign(witnessMessage.getMessageId());
        Log.d(TAG, PUBLISH_MESSAGE);
        WitnessMessageSignature signatureMessage = new WitnessMessageSignature(witnessMessage.getMessageId(), signature);
        disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, signatureMessage).subscribe(() -> Log.d(TAG, "Verifying the signature of  message  with id: " + witnessMessage.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_sign_message)));
    } catch (GeneralSecurityException e) {
        Log.d(TAG, PK_FAILURE_MESSAGE, e);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) HomeViewModel(com.github.dedis.popstellar.ui.home.HomeViewModel) NonNull(androidx.annotation.NonNull) ElectionEnd(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionEnd) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) Lao(com.github.dedis.popstellar.model.objects.Lao) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) Gson(com.google.gson.Gson) Schedulers(io.reactivex.schedulers.Schedulers) Transformations(androidx.lifecycle.Transformations) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) ConsensusElect(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElect) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Set(java.util.Set) ElectionVote(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionVote) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) Objects(java.util.Objects) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Stream(java.util.stream.Stream) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) Application(android.app.Application) RollCall(com.github.dedis.popstellar.model.objects.RollCall) Optional(java.util.Optional) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) MutableLiveData(androidx.lifecycle.MutableLiveData) R(com.github.dedis.popstellar.R) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) EventType(com.github.dedis.popstellar.model.objects.event.EventType) EventState(com.github.dedis.popstellar.model.objects.event.EventState) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) Signature(com.github.dedis.popstellar.model.objects.security.Signature) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) Election(com.github.dedis.popstellar.model.objects.Election) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) Barcode(com.google.android.gms.vision.barcode.Barcode) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) VisibleForTesting(androidx.annotation.VisibleForTesting) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) Channel(com.github.dedis.popstellar.model.objects.Channel) Signature(com.github.dedis.popstellar.model.objects.security.Signature) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) GeneralSecurityException(java.security.GeneralSecurityException) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)

Example 8 with WitnessMessage

use of com.github.dedis.popstellar.model.objects.WitnessMessage in project popstellar by dedis.

the class LaoHandler method handleUpdateLao.

/**
 * Process an UpdateLao message.
 *
 * @param context the HandlerContext of the message
 * @param updateLao the message that was received
 */
public static void handleUpdateLao(HandlerContext context, UpdateLao updateLao) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    Log.d(TAG, " Receive Update Lao Broadcast msg=" + updateLao);
    Lao lao = laoRepository.getLaoByChannel(channel);
    if (lao.getLastModified() > updateLao.getLastModified()) {
        // the current state we have is more up to date
        throw new DataHandlingException(updateLao, "The current Lao is more up to date than the update lao message");
    }
    WitnessMessage message;
    if (!updateLao.getName().equals(lao.getName())) {
        message = updateLaoNameWitnessMessage(messageId, updateLao, lao);
    } else if (!updateLao.getWitnesses().equals(lao.getWitnesses())) {
        message = updateLaoWitnessesWitnessMessage(messageId, updateLao, lao);
    } else {
        Log.d(TAG, "Cannot set the witness message title to update lao");
        throw new DataHandlingException(updateLao, "Cannot set the witness message title to update lao");
    }
    lao.updateWitnessMessage(messageId, message);
    if (!lao.getWitnesses().isEmpty()) {
        // We send a pending update only if there are already some witness that need to sign this
        // UpdateLao
        lao.getPendingUpdates().add(new PendingUpdate(updateLao.getLastModified(), messageId));
    }
    laoRepository.updateNodes(channel);
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) PendingUpdate(com.github.dedis.popstellar.model.objects.PendingUpdate) LAORepository(com.github.dedis.popstellar.repository.LAORepository) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) DataHandlingException(com.github.dedis.popstellar.utility.error.DataHandlingException) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 9 with WitnessMessage

use of com.github.dedis.popstellar.model.objects.WitnessMessage in project popstellar by dedis.

the class LaoHandler method updateLaoWitnessesWitnessMessage.

public static WitnessMessage updateLaoWitnessesWitnessMessage(MessageID messageId, UpdateLao updateLao, Lao lao) {
    WitnessMessage message = new WitnessMessage(messageId);
    List<PublicKey> tempList = new ArrayList<>(updateLao.getWitnesses());
    message.setTitle("Update Lao Witnesses");
    message.setDescription("Lao Name : " + lao.getName() + "\n" + "Message ID : " + messageId + "\n" + "New Witness ID : " + tempList.get(tempList.size() - 1));
    return message;
}
Also used : PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) ArrayList(java.util.ArrayList) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage)

Example 10 with WitnessMessage

use of com.github.dedis.popstellar.model.objects.WitnessMessage in project popstellar by dedis.

the class ElectionHandlerTest method testHandleElectionSetup.

@Test
public void testHandleElectionSetup() throws DataHandlingException {
    // Create the setup Election message
    ElectionSetup electionSetup = new ElectionSetup("election 2", election.getCreation(), election.getStartTimestamp(), election.getEndTimestamp(), Collections.singletonList(electionQuestion.getVotingMethod()), Collections.singletonList(electionQuestion.getWriteIn()), Collections.singletonList(electionQuestion.getBallotOptions()), Collections.singletonList(electionQuestion.getQuestion()), lao.getId());
    MessageGeneral message = new MessageGeneral(SENDER_KEY, electionSetup, GSON);
    // Call the message handler
    messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
    // Check the Election is present with state OPENED and the correct ID
    Optional<Election> electionOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getElection(electionSetup.getId());
    assertTrue(electionOpt.isPresent());
    assertEquals(EventState.OPENED, electionOpt.get().getState());
    assertEquals(electionSetup.getId(), electionOpt.get().getId());
    // Check the WitnessMessage has been created
    Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
    assertTrue(witnessMessage.isPresent());
    // Check the Witness message contains the expected title and description
    WitnessMessage expectedMessage = electionSetupWitnessMessage(message.getMessageId(), electionOpt.get());
    assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
    assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Election(com.github.dedis.popstellar.model.objects.Election) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) ElectionHandler.electionSetupWitnessMessage(com.github.dedis.popstellar.utility.handler.data.ElectionHandler.electionSetupWitnessMessage) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) Test(org.junit.Test)

Aggregations

WitnessMessage (com.github.dedis.popstellar.model.objects.WitnessMessage)13 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)6 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)4 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)4 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)4 RollCall (com.github.dedis.popstellar.model.objects.RollCall)4 Test (org.junit.Test)4 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)3 ElectionSetup (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup)2 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)2 Channel (com.github.dedis.popstellar.model.objects.Channel)2 Election (com.github.dedis.popstellar.model.objects.Election)2 Lao (com.github.dedis.popstellar.model.objects.Lao)2 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)2 RollCallHandler.closeRollCallWitnessMessage (com.github.dedis.popstellar.utility.handler.data.RollCallHandler.closeRollCallWitnessMessage)2 RollCallHandler.createRollCallWitnessMessage (com.github.dedis.popstellar.utility.handler.data.RollCallHandler.createRollCallWitnessMessage)2 RollCallHandler.openRollCallWitnessMessage (com.github.dedis.popstellar.utility.handler.data.RollCallHandler.openRollCallWitnessMessage)2 Manifest (android.Manifest)1 Application (android.app.Application)1 PackageManager (android.content.pm.PackageManager)1