Search in sources :

Example 1 with PoPToken

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

the class SocialMediaViewModel method sendChirp.

/**
 * Send a chirp to your own channel.
 *
 * <p>Publish a MessageGeneral containing AddChirp data.
 *
 * @param text the text written in the chirp
 * @param parentId the id of the chirp to which you replied
 * @param timestamp the time at which you sent the chirp
 */
public void sendChirp(String text, @Nullable MessageID parentId, long timestamp) {
    Log.d(TAG, "Sending a chirp");
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    AddChirp addChirp = new AddChirp(text, parentId, timestamp);
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
        Log.d(TAG, PUBLISH_MESSAGE);
        MessageGeneral msg = new MessageGeneral(token, addChirp, gson);
        Disposable disposable = networkManager.getMessageSender().publish(token, channel, addChirp).subscribe(() -> Log.d(TAG, "sent chirp with messageId: " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_sending_chirp));
        disposables.add(disposable);
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
    }
}
Also used : AddChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 2 with PoPToken

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

the class SocialMediaViewModel method deleteChirp.

public void deleteChirp(MessageID chirpId, long timestamp) {
    Log.d(TAG, "Deleting the chirp with id: " + chirpId);
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    DeleteChirp deleteChirp = new DeleteChirp(chirpId, timestamp);
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
        Log.d(TAG, PUBLISH_MESSAGE);
        MessageGeneral msg = new MessageGeneral(token, deleteChirp, gson);
        Disposable disposable = networkManager.getMessageSender().publish(token, channel, deleteChirp).subscribe(() -> {
            Log.d(TAG, "Deleted chirp with messageId: " + msg.getMessageId());
            Toast.makeText(getApplication().getApplicationContext(), "Deleted chirp!", Toast.LENGTH_LONG).show();
        }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_delete_chirp));
        disposables.add(disposable);
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) DeleteChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 3 with PoPToken

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

the class Base64DataUtils method generatePoPToken.

/**
 * @return a pseudo randomly generated valid Ed25519 keypair
 */
public static PoPToken generatePoPToken() {
    byte[] privateKey = new byte[KEY_LENGTH];
    RANDOM.nextBytes(privateKey);
    Ed25519PrivateKeyParameters privateKeyParameters = new Ed25519PrivateKeyParameters(privateKey, 0);
    Ed25519PublicKeyParameters publicKeyParameters = privateKeyParameters.generatePublicKey();
    return new PoPToken(privateKey, publicKeyParameters.getEncoded());
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 4 with PoPToken

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

the class WalletTest method importSeedAndExportSeedAreCoherent.

@Test
public void importSeedAndExportSeedAreCoherent() throws Exception {
    String Lao_ID = "1234123412341234";
    String Roll_Call_ID = "1234123412341234";
    Wallet hdw1 = new Wallet(TestKeysetModule.provideWalletKeysetManager());
    hdw1.newSeed();
    String seed = String.join(" ", hdw1.exportSeed());
    PoPToken res1 = hdw1.generatePoPToken(Lao_ID, Roll_Call_ID);
    Wallet hdw2 = new Wallet(TestKeysetModule.provideWalletKeysetManager());
    hdw2.importSeed(seed);
    PoPToken res2 = hdw2.generatePoPToken(Lao_ID, Roll_Call_ID);
    assertEquals(res1, res2);
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Wallet(com.github.dedis.popstellar.model.objects.Wallet) Test(org.junit.Test) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest)

Example 5 with PoPToken

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

the class LaoDetailViewModel method sendVote.

/**
 * Sends a ElectionCastVotes message .
 *
 * <p>Publish a GeneralMessage containing ElectionCastVotes data.
 *
 * @param votes the corresponding votes for that election
 */
public void sendVote(List<ElectionVote> votes) {
    Election election = mCurrentElection.getValue();
    if (election == null) {
        Log.d(TAG, "failed to retrieve current election");
        return;
    }
    Log.d(TAG, "sending a new vote in election : " + election + " with election start time" + election.getStartTimestamp());
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        CastVote castVote = new CastVote(votes, election.getId(), lao.getId());
        Channel electionChannel = election.getChannel();
        Log.d(TAG, PUBLISH_MESSAGE);
        Disposable disposable = networkManager.getMessageSender().publish(token, electionChannel, castVote).doFinally(this::openLaoDetail).subscribe(() -> {
            Log.d(TAG, "sent a vote successfully");
            // Toast ? + send back to election screen or details screen ?
            Toast.makeText(getApplication(), "vote successfully sent !", Toast.LENGTH_LONG).show();
        }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_send_vote));
        disposables.add(disposable);
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Channel(com.github.dedis.popstellar.model.objects.Channel) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) 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) Election(com.github.dedis.popstellar.model.objects.Election) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Aggregations

PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)12 Lao (com.github.dedis.popstellar.model.objects.Lao)7 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)6 Channel (com.github.dedis.popstellar.model.objects.Channel)4 RollCall (com.github.dedis.popstellar.model.objects.RollCall)4 HiltAndroidTest (dagger.hilt.android.testing.HiltAndroidTest)4 Test (org.junit.Test)4 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)3 Disposable (io.reactivex.disposables.Disposable)3 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)2 Wallet (com.github.dedis.popstellar.model.objects.Wallet)2 InvalidPoPTokenException (com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException)2 KeyGenerationException (com.github.dedis.popstellar.utility.error.keys.KeyGenerationException)2 UninitializedWalletException (com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Ed25519PrivateKeyParameters (org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)2 Ed25519PublicKeyParameters (org.bouncycastle.crypto.params.Ed25519PublicKeyParameters)2 Bitmap (android.graphics.Bitmap)1 Nullable (androidx.annotation.Nullable)1 CastVote (com.github.dedis.popstellar.model.network.method.message.data.election.CastVote)1