Search in sources :

Example 16 with Lao

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

the class LaoDetailViewModel method endElection.

public void endElection(Election election) {
    Log.d(TAG, "ending election with name : " + election.getName());
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = election.getChannel();
    String laoId = lao.getId();
    ElectionEnd electionEnd = new ElectionEnd(election.getId(), laoId, election.computerRegisteredVotes());
    Log.d(TAG, PUBLISH_MESSAGE);
    Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, electionEnd).subscribe(() -> {
        Log.d(TAG, "ended election successfully");
        endElectionEvent();
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_end_election));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) ElectionEnd(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionEnd) Channel(com.github.dedis.popstellar.model.objects.Channel) 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 17 with Lao

use of com.github.dedis.popstellar.model.objects.Lao 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 18 with Lao

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

the class ElectionStartFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ElectionStartFragmentBinding binding = ElectionStartFragmentBinding.inflate(inflater, container, false);
    electionStart = binding.electionStart;
    electionStatus = binding.electionStatus;
    LaoDetailViewModel mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
    Election election = mLaoDetailViewModel.getCurrentElection();
    if (election == null) {
        Log.e(TAG, "The current election of the LaoDetailViewModel is null");
        return null;
    }
    String scheduledDate = dateFormat.format(new Date(election.getStartTimestampInMillis()));
    String electionId = election.getId();
    String instanceId = ElectInstance.generateConsensusId("election", electionId, "state");
    binding.electionTitle.setText(getString(R.string.election_start_title, election.getName()));
    electionStatus.setText(R.string.waiting_scheduled_time);
    electionStart.setText(getString(R.string.election_scheduled, scheduledDate));
    electionStart.setEnabled(false);
    setupTimerUpdate(election);
    setupButtonListeners(binding, mLaoDetailViewModel, electionId);
    Lao lao = mLaoDetailViewModel.getCurrentLaoValue();
    List<ConsensusNode> nodes = lao.getNodes();
    ownNode = lao.getNode(mLaoDetailViewModel.getPublicKey());
    if (ownNode == null) {
        // Only possible if the user wasn't an acceptor, but shouldn't have access to this fragment
        Log.e(TAG, "Couldn't find the Node with public key : " + mLaoDetailViewModel.getPublicKey());
        throw new IllegalStateException("Only acceptors are allowed to access ElectionStartFragment");
    }
    NodesAcceptorAdapter adapter = new NodesAcceptorAdapter(nodes, ownNode, instanceId, getViewLifecycleOwner(), mLaoDetailViewModel);
    GridView gridView = binding.nodesGrid;
    gridView.setAdapter(adapter);
    if (isElectionStartTimePassed(election)) {
        updateStartAndStatus(nodes, election, instanceId);
    }
    mLaoDetailViewModel.getNodes().observe(getViewLifecycleOwner(), consensusNodes -> {
        Log.d(TAG, "got an update for nodes : " + consensusNodes);
        adapter.setList(consensusNodes);
        if (isElectionStartTimePassed(election)) {
            updateStartAndStatus(consensusNodes, election, instanceId);
        }
    });
    binding.setLifecycleOwner(getViewLifecycleOwner());
    return binding.getRoot();
}
Also used : LaoDetailViewModel(com.github.dedis.popstellar.ui.detail.LaoDetailViewModel) ElectionStartFragmentBinding(com.github.dedis.popstellar.databinding.ElectionStartFragmentBinding) Lao(com.github.dedis.popstellar.model.objects.Lao) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) GridView(android.widget.GridView) Election(com.github.dedis.popstellar.model.objects.Election) Date(java.util.Date)

Example 19 with Lao

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

the class LaoDetailViewModel method createNewElection.

/**
 * Creates new Election event.
 *
 * <p>Publish a GeneralMessage containing ElectionSetup data.
 *
 * @param name the name of the election
 * @param creation the creation time of the election
 * @param start the start time of the election
 * @param end the end time of the election
 * @param votingMethod the type of voting method (e.g Plurality)
 * @param ballotOptions the list of ballot options
 * @param question the question associated to the election
 * @return the id of the newly created election event, null if fails to create the event
 */
public String createNewElection(String name, long creation, long start, long end, List<String> votingMethod, List<Boolean> writeIn, List<List<String>> ballotOptions, List<String> question) {
    Log.d(TAG, "creating a new election with name " + name);
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return null;
    }
    Channel channel = lao.getChannel();
    ElectionSetup electionSetup = new ElectionSetup(name, creation, start, end, votingMethod, writeIn, ballotOptions, question, lao.getId());
    Log.d(TAG, PUBLISH_MESSAGE);
    Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, electionSetup).subscribe(() -> {
        Log.d(TAG, "setup an election");
        mElectionCreatedEvent.postValue(new SingleEvent<>(true));
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_create_election));
    disposables.add(disposable);
    return electionSetup.getId();
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Channel(com.github.dedis.popstellar.model.objects.Channel) 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) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup)

Example 20 with Lao

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

the class LaoDetailViewModel method updateLaoWitnesses.

/**
 * Method to update the list of witnesses of a Lao by sending an updateLao msg and a stateLao msg
 * to the backend
 */
public void updateLaoWitnesses() {
    Log.d(TAG, "Updating lao witnesses ");
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel();
    KeyPair mainKey = keyManager.getMainKeyPair();
    long now = Instant.now().getEpochSecond();
    UpdateLao updateLao = new UpdateLao(mainKey.getPublicKey(), lao.getCreation(), lao.getName(), now, witnesses);
    MessageGeneral msg = new MessageGeneral(mainKey, updateLao, gson);
    Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> {
        Log.d(TAG, "updated lao witnesses");
        dispatchLaoUpdate("lao state with new witnesses", updateLao, lao, channel, msg);
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_update_lao));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) 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)

Aggregations

Lao (com.github.dedis.popstellar.model.objects.Lao)46 Channel (com.github.dedis.popstellar.model.objects.Channel)31 LAORepository (com.github.dedis.popstellar.repository.LAORepository)24 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)18 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)18 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)16 Disposable (io.reactivex.disposables.Disposable)14 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)12 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)11 Election (com.github.dedis.popstellar.model.objects.Election)10 RollCall (com.github.dedis.popstellar.model.objects.RollCall)10 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)9 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)9 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)8 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)8 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)8 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)7 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)6 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)6 LAOState (com.github.dedis.popstellar.repository.LAOState)6