use of com.github.dedis.popstellar.model.objects.security.PoPToken in project popstellar by dedis.
the class Wallet method generateKeyFromPath.
/**
* Generate a PoPToken (i.e. a key pair) from a given path.
*
* @param path a String path of the form: m/i/j/k/... where i,j,k,.. are 31-bit integer.
* @return the generated PoP Token
* @throws KeyGenerationException if an error occurs
* @throws UninitializedWalletException if the wallet is not initialized with a seed
*/
private PoPToken generateKeyFromPath(@NonNull String path) throws KeyGenerationException, UninitializedWalletException {
if (!isSetUp()) {
throw new UninitializedWalletException();
}
// convert the path string in an array of int
int[] pathValueInt = Arrays.stream(path.split("/")).skip(// remove the first element ('m')
1).mapToInt(Integer::parseInt).toArray();
try {
// derive private and public key
byte[] privateKey = SLIP10.deriveEd25519PrivateKey(aead.decrypt(seed, new byte[0]), pathValueInt);
Ed25519PrivateKeyParameters prK = new Ed25519PrivateKeyParameters(privateKey, 0);
Ed25519PublicKeyParameters puK = prK.generatePublicKey();
byte[] publicKey = puK.getEncoded();
return new PoPToken(privateKey, publicKey);
} catch (GeneralSecurityException e) {
throw new KeyGenerationException(e);
}
}
use of com.github.dedis.popstellar.model.objects.security.PoPToken in project popstellar by dedis.
the class RollCallHandler method handleCloseRollCall.
/**
* Process a CloseRollCall message.
*
* @param context the HandlerContext of the message
* @param closeRollCall the message that was received
*/
public static void handleCloseRollCall(HandlerContext context, CloseRollCall closeRollCall) throws DataHandlingException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
MessageID messageId = context.getMessageId();
Lao lao = laoRepository.getLaoByChannel(channel);
Log.d(TAG, "handleCloseRollCall: " + channel);
String updateId = closeRollCall.getUpdateId();
String closes = closeRollCall.getCloses();
Optional<RollCall> rollCallOptional = lao.getRollCall(closes);
if (!rollCallOptional.isPresent()) {
Log.w(TAG, "Cannot find roll call to close : " + closes);
throw new InvalidDataException(closeRollCall, "close id", closes);
}
RollCall rollCall = rollCallOptional.get();
rollCall.setEnd(closeRollCall.getClosedAt());
rollCall.setId(updateId);
rollCall.getAttendees().addAll(closeRollCall.getAttendees());
rollCall.setState(EventState.CLOSED);
lao.updateRollCall(closes, rollCall);
lao.updateWitnessMessage(messageId, closeRollCallWitnessMessage(messageId, rollCall));
// Subscribe to the social media channels
try {
PoPToken token = context.getKeyManager().getValidPoPToken(lao, rollCall);
context.getMessageSender().subscribe(channel.subChannel("social").subChannel(token.getPublicKey().getEncoded())).subscribe();
} catch (InvalidPoPTokenException e) {
Log.i(TAG, "Received a close roll-call that you did not attend");
} catch (KeyException e) {
Log.e(TAG, "Could not retrieve your PoP Token to subscribe you to your social media channel", e);
}
}
use of com.github.dedis.popstellar.model.objects.security.PoPToken in project popstellar by dedis.
the class SocialMediaViewModel method isOwner.
/**
* Check whether the sender of a chirp is the current user
*
* @param sender String of the PoPToken PublicKey
* @return true if the sender is the current user
*/
public boolean isOwner(String sender) {
Log.d(TAG, "Testing if the sender is also the owner");
Lao lao = getCurrentLao();
if (lao == null) {
Log.e(TAG, LAO_FAILURE_MESSAGE);
return false;
}
try {
PoPToken token = keyManager.getValidPoPToken(lao);
return sender.equals(token.getPublicKey().getEncoded());
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
return false;
}
}
use of com.github.dedis.popstellar.model.objects.security.PoPToken in project popstellar by dedis.
the class RollCallTokenFragment method onCreateView.
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRollCallTokenFragmentBinding = RollCallTokenFragmentBinding.inflate(inflater, container, false);
mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
String rollCallId = requireArguments().getString(EXTRA_ID);
Optional<RollCall> optRollCall = mLaoDetailViewModel.getCurrentLao().getValue().getRollCall(rollCallId);
if (!optRollCall.isPresent()) {
Log.d(TAG, "failed to retrieve roll call with id " + rollCallId);
mLaoDetailViewModel.openLaoWallet();
} else {
rollCall = optRollCall.get();
}
String firstLaoId = mLaoDetailViewModel.getCurrentLaoValue().getId();
String pk = "";
Log.d(TAG, "rollcall: " + rollCallId);
try {
PoPToken token = wallet.generatePoPToken(firstLaoId, rollCall.getPersistentId());
pk = token.getPublicKey().getEncoded();
} catch (KeyException e) {
Log.d(TAG, "failed to retrieve token from wallet", e);
mLaoDetailViewModel.openLaoWallet();
}
mRollCallTokenFragmentBinding.rollcallName.setText("Roll Call: " + rollCall.getName());
mRollCallTokenFragmentBinding.publicKey.setText("Public key:\n" + pk);
Bitmap myBitmap = QRCode.from(pk).bitmap();
mRollCallTokenFragmentBinding.pkQrCode.setImageBitmap(myBitmap);
mRollCallTokenFragmentBinding.setLifecycleOwner(getActivity());
return mRollCallTokenFragmentBinding.getRoot();
}
use of com.github.dedis.popstellar.model.objects.security.PoPToken in project popstellar by dedis.
the class KeyManagerTest method popTokenRetrievingWorks.
@Test
public void popTokenRetrievingWorks() throws KeyException {
PoPToken token = Base64DataUtils.generatePoPToken();
when(wallet.recoverKey(any(), any(), any())).thenReturn(token);
// create LAO and RollCalls
Lao lao = new Lao("lao", Base64DataUtils.generatePublicKey(), 54213424);
RollCall rollCall1 = new RollCall(lao.getId(), 5421364, "rollcall1");
RollCall rollCall2 = new RollCall(lao.getId(), 5421363, "rollcall2");
lao.updateRollCall(rollCall1.getId(), rollCall1);
lao.updateRollCall(rollCall2.getId(), rollCall2);
KeyManager manager = new KeyManager(androidKeysetManager, wallet);
assertEquals(token, manager.getValidPoPToken(lao));
assertEquals(token, manager.getValidPoPToken(lao, rollCall1));
// make sure that rollcall1 was taken and not rollcall2 as the oldest is rollcall 1
verify(wallet, atLeast(1)).recoverKey(eq(lao.getId()), eq(rollCall1.getId()), any());
}
Aggregations