Skip to content

Commit

Permalink
Added new messages and data
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosz committed Oct 15, 2023
1 parent 9f9dcf0 commit 7834fbb
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.dedis.popstellar.model.network.method.message.data;

import java.util.Objects;
import java.util.*;
import java.util.Objects;

/** Enumerates all possible messages actions */
public enum Action {
Expand Down Expand Up @@ -30,7 +30,8 @@ public enum Action {
DELETE("delete"),
NOTIFY_DELETE("notify_delete"),
KEY("key"),
POST_TRANSACTION("post_transaction");
POST_TRANSACTION("post_transaction"),
AUTH("auth");

private static final List<Action> ALL = Collections.unmodifiableList(Arrays.asList(values()));
private final String action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum Objects {
CONSENSUS("consensus"),
CHIRP("chirp"),
REACTION("reaction"),
COIN("coin");
COIN("coin"),
POPCHA("popcha");

private static final List<Objects> ALL = Collections.unmodifiableList(Arrays.asList(values()));
private final String object;
Expand Down Expand Up @@ -62,6 +63,7 @@ public boolean hasToBePersisted() {
return true;
// TODO: add persistence for consensus when it'll have its repo
case "consensus":
case "popcha":
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.github.dedis.popstellar.model.network.method.message.data.popcha;

import androidx.annotation.Nullable;
import com.github.dedis.popstellar.model.Immutable;
import com.github.dedis.popstellar.model.network.method.message.data.*;
import com.google.gson.annotations.SerializedName;

/** Data sent to authenticate to a PoPCHA server */
@Immutable
public class PoPCHAAuthentication extends Data {

@SerializedName("client_id")
private final String clientId;

private final String nonce;
private final String identifier;

@SerializedName("identifier_proof")
private final String identifierProof;

@Nullable private final String state;

@Nullable
@SerializedName("response_mode")
private final String responseMode;

@SerializedName("popcha_address")
private final String popchaAddress;

public PoPCHAAuthentication(
String clientId,
String nonce,
String identifier,
String identifierProof,
@Nullable String state,
@Nullable String responseMode,
String popchaAddress) {
this.clientId = clientId;
this.nonce = nonce;
this.identifier = identifier;
this.identifierProof = identifierProof;
this.state = state;
this.responseMode = responseMode;
this.popchaAddress = popchaAddress;
}

@Override
public String getObject() {
return Objects.POPCHA.getObject();
}

@Override
public String getAction() {
return Action.AUTH.getAction();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PoPCHAAuthentication that = (PoPCHAAuthentication) o;
return clientId.equals(that.clientId)
&& nonce.equals(that.nonce)
&& identifier.equals(that.identifier)
&& identifierProof.equals(that.identifierProof)
&& java.util.Objects.equals(state, that.state)
&& java.util.Objects.equals(responseMode, that.responseMode)
&& popchaAddress.equals(that.popchaAddress);
}

@Override
public int hashCode() {
return java.util.Objects.hash(
clientId, nonce, identifier, identifierProof, state, responseMode, popchaAddress);
}

@Override
public String toString() {
return "PoPCHAAuthentication{"
+ "clientId='"
+ clientId
+ '\''
+ ", nonce='"
+ nonce
+ '\''
+ ", identifier='"
+ identifier
+ '\''
+ ", identifierProof='"
+ identifierProof
+ '\''
+ ", state='"
+ state
+ '\''
+ ", responseMode='"
+ responseMode
+ '\''
+ ", popchaAddress='"
+ popchaAddress
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.github.dedis.popstellar.model.qrcode;

import android.net.Uri;
import com.github.dedis.popstellar.model.Immutable;
import com.github.dedis.popstellar.utility.MessageValidator;

@Immutable
public class PoPCHAQRCode {

public static final String CLIENT_ID = "client_id";
public static final String NONCE = "nonce";
public static final String REDIRECT_URI = "redirect_uri";
public static final String STATE = "state";
public static final String RESPONSE_TYPE = "response_type";
public static final String RESPONSE_MODE = "response_mode";
public static final String LOGIN_HINT = "login_hint";

private final String clientId;
private final String nonce;
private final String state;
private final String responseMode;
private final String host;

public PoPCHAQRCode(String data, String laoId) throws IllegalArgumentException {
MessageValidator.verify().isValidPoPCHAUrl(data, laoId);

Uri uri = Uri.parse(data);
clientId = uri.getQueryParameter(CLIENT_ID);
nonce = uri.getQueryParameter(NONCE);
state = uri.getQueryParameter(STATE);
responseMode = uri.getQueryParameter(RESPONSE_MODE);
host = uri.getHost();
}

public String getClientId() {
return clientId;
}

public String getNonce() {
return nonce;
}

public String getState() {
return state;
}

public String getResponseMode() {
return responseMode;
}

public String getHost() {
return host;
}

@Override
public String toString() {
return "PoPCHAQRCode{"
+ "clientId='"
+ clientId
+ '\''
+ ", nonce='"
+ nonce
+ '\''
+ ", state='"
+ state
+ '\''
+ ", responseMode='"
+ responseMode
+ '\''
+ ", host='"
+ host
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import android.app.Application;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.*;

import com.github.dedis.popstellar.R;
import com.github.dedis.popstellar.model.objects.Wallet;
import com.github.dedis.popstellar.model.objects.view.LaoView;
Expand All @@ -22,18 +20,15 @@
import com.github.dedis.popstellar.utility.error.keys.SeedValidationException;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import dagger.hilt.android.lifecycle.HiltViewModel;
import io.reactivex.BackpressureStrategy;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;
import io.reactivex.BackpressureStrategy;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import timber.log.Timber;

@HiltViewModel
Expand Down Expand Up @@ -121,7 +116,7 @@ public void handleData(String data) {
Timber.tag(TAG).e(e, "Invalid QRCode laoData");
Toast.makeText(
getApplication().getApplicationContext(),
R.string.invalid_qrcode_data,
R.string.invalid_qrcode_lao_data,
Toast.LENGTH_LONG)
.show();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

witnessingViewModel = obtainWitnessingViewModel(this, laoId);

obtainPoPCHAViewModel(this, laoId).disableConnectingFlag();

// At creation of the lao activity the connections of the lao are restored from the persistent
// storage, such that the client resubscribes to each previous subscribed channel
laoViewModel.restoreConnections();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.dedis.popstellar.ui.lao.popcha;

import static com.github.dedis.popstellar.ui.lao.LaoActivity.setCurrentFragment;

import android.os.Bundle;
import android.view.*;
import androidx.annotation.NonNull;
Expand All @@ -9,11 +11,13 @@
import com.github.dedis.popstellar.databinding.PopchaHomeFragmentBinding;
import com.github.dedis.popstellar.ui.lao.LaoActivity;
import com.github.dedis.popstellar.ui.lao.LaoViewModel;
import com.github.dedis.popstellar.ui.qrcode.QrScannerFragment;
import com.github.dedis.popstellar.ui.qrcode.ScanningAction;

public class PoPCHAHomeFragment extends Fragment {
private PopchaHomeFragmentBinding binding;

private LaoViewModel laoViewModel;
private PoPCHAViewModel poPCHAViewModel;
private PoPCHAViewModel popCHAViewModel;

public PoPCHAHomeFragment() {
// Required public empty constructor
Expand All @@ -30,14 +34,26 @@ public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
laoViewModel = LaoActivity.obtainViewModel(requireActivity());

poPCHAViewModel = LaoActivity.obtainPoPCHAViewModel(requireActivity(), laoViewModel.getLaoId());
binding = PopchaHomeFragmentBinding.inflate(inflater, container, false);
popCHAViewModel = LaoActivity.obtainPoPCHAViewModel(requireActivity(), laoViewModel.getLaoId());
PopchaHomeFragmentBinding binding =
PopchaHomeFragmentBinding.inflate(inflater, container, false);

binding.popchaHeader.setText(
String.format(
getResources().getString(R.string.popcha_header), poPCHAViewModel.getLaoId()));
getResources().getString(R.string.popcha_header), popCHAViewModel.getLaoId()));

binding.popchaScanner.setOnClickListener(v -> openScanner());

binding.popchaScanner.setOnClickListener(v -> {});
popCHAViewModel
.getTextDisplayed()
.observe(
getViewLifecycleOwner(),
stringSingleEvent -> {
String url = stringSingleEvent.getContentIfNotHandled();
if (url != null) {
binding.popchaText.setText(url);
}
});

handleBackNav();
return binding.getRoot();
Expand All @@ -50,6 +66,14 @@ public void onResume() {
laoViewModel.setIsTab(true);
}

private void openScanner() {
laoViewModel.setIsTab(false);
setCurrentFragment(
getParentFragmentManager(),
R.id.fragment_qr_scanner,
() -> QrScannerFragment.newInstance(ScanningAction.ADD_POPCHA));
}

public static void openFragment(FragmentManager manager) {
LaoActivity.setCurrentFragment(manager, R.id.fragment_popcha_home, PoPCHAHomeFragment::new);
}
Expand Down
Loading

0 comments on commit 7834fbb

Please sign in to comment.