Skip to content

Commit

Permalink
Merge pull request #1913 from dedis/work-fe1-ljankoschek-LinkedOrgAut…
Browse files Browse the repository at this point in the history
…hFix

Work fe1 ljankoschek linked org authentication additions and data exchange
  • Loading branch information
K1li4nL authored Jun 30, 2024
2 parents 4901524 + 6cb8579 commit 14b1cdd
Show file tree
Hide file tree
Showing 37 changed files with 1,126 additions and 68 deletions.
7 changes: 6 additions & 1 deletion fe1-web/src/core/network/ingestion/Handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dispatch } from 'core/redux';

import { Broadcast, JsonRpcMethod, ExtendedJsonRpcRequest } from '../jsonrpc';
import { Broadcast, JsonRpcMethod, ExtendedJsonRpcRequest, Publish } from '../jsonrpc';
import { ActionType, MessageRegistry, ObjectType } from '../jsonrpc/messages';
import { ExtendedMessage } from './ExtendedMessage';
import { addMessages } from './MessageReducer';
Expand Down Expand Up @@ -61,6 +61,11 @@ export function handleExtendedRpcRequests(req: ExtendedJsonRpcRequest) {
broadcastParams.channel,
),
);
} else if (req.request.method === JsonRpcMethod.PUBLISH) {
const publishParams = req.request.params as Publish;
storeMessage(
ExtendedMessage.fromMessage(publishParams.message, req.receivedFrom, publishParams.channel),
);
} else {
console.warn('A request was received but it is currently unsupported:', req);
}
Expand Down
2 changes: 2 additions & 0 deletions fe1-web/src/core/network/jsonrpc/messages/MessageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export enum ActionType {
CHALLENGE = 'challenge',
FEDERATION_INIT = 'init',
FEDERATION_EXPECT = 'expect',
FEDERATION_RESULT = 'result',
TOKENS_EXCHANGE = 'tokens_exchange',
}

/** Enumeration of all possible signatures of a message */
Expand Down
4 changes: 4 additions & 0 deletions fe1-web/src/core/network/jsonrpc/messages/MessageRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const {
CHALLENGE,
FEDERATION_INIT,
FEDERATION_EXPECT,
FEDERATION_RESULT,
TOKENS_EXCHANGE,
} = ActionType;
const { KEYPAIR, POP_TOKEN } = SignatureType;

Expand Down Expand Up @@ -107,6 +109,8 @@ export class MessageRegistry {
[k(FEDERATION, CHALLENGE), { signature: KEYPAIR }],
[k(FEDERATION, FEDERATION_INIT), { signature: KEYPAIR }],
[k(FEDERATION, FEDERATION_EXPECT), { signature: KEYPAIR }],
[k(FEDERATION, FEDERATION_RESULT), { signature: KEYPAIR }],
[k(FEDERATION, TOKENS_EXCHANGE), { signature: KEYPAIR }],
]);

/**
Expand Down
2 changes: 2 additions & 0 deletions fe1-web/src/core/network/validation/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const schemaIds: Record<ObjectType, Record<string, string>> = {
[ActionType.CHALLENGE]: 'dataFederationChallenge',
[ActionType.FEDERATION_INIT]: 'dataFederationInit',
[ActionType.FEDERATION_EXPECT]: 'dataFederationExpect',
[ActionType.FEDERATION_RESULT]: 'dataFederationResult',
[ActionType.TOKENS_EXCHANGE]: 'dataFederationTokensExchange',
},
};

Expand Down
2 changes: 2 additions & 0 deletions fe1-web/src/core/network/validation/schemas/dataSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import dataFederationChallengeRequest from 'protocol/query/method/message/data/d
import dataFederationExpect from 'protocol/query/method/message/data/dataFederationExpect.json';
import dataFederationInit from 'protocol/query/method/message/data/dataFederationInit.json';
import dataFederationResult from 'protocol/query/method/message/data/dataFederationResult.json';
import dataFederationTokensExchange from 'protocol/query/method/message/data/dataFederationTokensExchange.json';
/* eslint-enable import/order */

const dataSchemas = [
Expand Down Expand Up @@ -75,6 +76,7 @@ const dataSchemas = [
dataFederationExpect,
dataFederationInit,
dataFederationResult,
dataFederationTokensExchange,
];

export default dataSchemas;
3 changes: 2 additions & 1 deletion fe1-web/src/features/home/screens/ConnectConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ScreenWrapper from 'core/components/ScreenWrapper';
import { AppParamList } from 'core/navigation/typing/AppParamList';
import { ConnectParamList } from 'core/navigation/typing/ConnectParamList';
import { getNetworkManager, subscribeToChannel } from 'core/network';
import { Hash } from 'core/objects';
import { getFederationChannel, Hash } from 'core/objects';
import { Typography } from 'core/styles';
import containerStyles from 'core/styles/stylesheets/containerStyles';
import { FOUR_SECONDS } from 'resources/const';
Expand Down Expand Up @@ -63,6 +63,7 @@ const ConnectConfirm = () => {
} else {
// subscribe to the lao channel on the new connection
await subscribeToChannel(laoId, dispatch, laoChannel, [connection]);
await subscribeToChannel(laoId, dispatch, getFederationChannel(laoId));
}

navigation.navigate(STRINGS.navigation_app_lao, {
Expand Down
3 changes: 2 additions & 1 deletion fe1-web/src/features/home/screens/ConnectScan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import QrCodeScanOverlay from 'core/components/QrCodeScanOverlay';
import { AppParamList } from 'core/navigation/typing/AppParamList';
import { ConnectParamList } from 'core/navigation/typing/ConnectParamList';
import { getNetworkManager, subscribeToChannel } from 'core/network';
import { Channel } from 'core/objects';
import { Channel, getFederationChannel } from 'core/objects';
import { Spacing, Typography } from 'core/styles';
import { FOUR_SECONDS } from 'resources/const';
import STRINGS from 'resources/strings';
Expand Down Expand Up @@ -164,6 +164,7 @@ const ConnectScan = () => {
);

await connectToLaoAndSubscribe(connectToLao, laoChannel);
await subscribeToChannel(connectToLao.lao, dispatch, getFederationChannel(connectToLao.lao));

isProcessingScan.current = false;
setIsConnecting(false);
Expand Down
2 changes: 2 additions & 0 deletions fe1-web/src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ export function configureFeatures() {
useCurrentLao: laoConfiguration.hooks.useCurrentLao,
getCurrentLaoId: laoConfiguration.functions.getCurrentLaoId,
getLaoOrganizerBackendPublicKey: laoConfiguration.functions.getLaoOrganizerBackendPublicKey,
getLaoById: laoConfiguration.functions.getLaoById,
getRollCallById: rollCallConfiguration.functions.getRollCallById,
});

// compose features
Expand Down
4 changes: 2 additions & 2 deletions fe1-web/src/features/lao/objects/Lao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ export class Lao {

this.name = obj.name;
this.id = obj.id;
this.last_roll_call_id = obj.last_roll_call_id;
this.creation = obj.creation;
this.last_modified = obj.last_modified;
this.organizer = obj.organizer;
this.witnesses = [...obj.witnesses];
this.last_roll_call_id = obj.last_roll_call_id;
this.last_tokenized_roll_call_id = obj.last_tokenized_roll_call_id;
this.server_addresses = obj.server_addresses || [];

Expand All @@ -93,11 +93,11 @@ export class Lao {
return new Lao({
name: lao.name,
id: Hash.fromState(lao.id),
last_roll_call_id: lao.last_roll_call_id ? Hash.fromState(lao.last_roll_call_id) : undefined,
creation: Timestamp.fromState(lao.creation),
last_modified: Timestamp.fromState(lao.last_modified),
organizer: PublicKey.fromState(lao.organizer),
witnesses: lao.witnesses.map((w) => PublicKey.fromState(w)),
last_roll_call_id: lao.last_roll_call_id ? Hash.fromState(lao.last_roll_call_id) : undefined,
last_tokenized_roll_call_id: lao.last_tokenized_roll_call_id
? Hash.fromState(lao.last_tokenized_roll_call_id)
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { expectFederation, initFederation, requestChallenge } from '../network';
import { Challenge } from '../objects/Challenge';
import { LinkedOrganization } from '../objects/LinkedOrganization';
import { makeChallengeSelector } from '../reducer';
import { addLinkedOrganization } from '../reducer/LinkedOrganizationsReducer';
import { addScannedLinkedOrganization } from '../reducer/LinkedOrganizationsReducer';
import ManualInputModal from './ManualInputModal';
import QRCodeModal from './QRCodeModal';
import QRCodeScannerModal from './QRCodeScannerModal';
Expand Down Expand Up @@ -54,6 +54,7 @@ const AddLinkedOrganizationModal = () => {
const navigation = useNavigation<NavigationProps['navigation']>();
const toast = useToast();
const laoId = LinkedOrganizationsHooks.useCurrentLaoId();
const isOrganizer = LinkedOrganizationsHooks.useIsLaoOrganizer(laoId);
const lao = LinkedOrganizationsHooks.useCurrentLao();
const challengeSelector = useMemo(() => makeChallengeSelector(laoId), [laoId]);
const challengeState = useSelector(challengeSelector);
Expand All @@ -71,6 +72,8 @@ const AddLinkedOrganizationModal = () => {
// this is needed as otherwise the camera may stay turned on
const [showScanner, setShowScanner] = useState(false);

const [linkedOrganization, setLinkedOrganization] = useState<LinkedOrganization>();

const onRequestChallenge = useCallback(() => {
requestChallenge(laoId)
.then(() => {})
Expand Down Expand Up @@ -100,6 +103,14 @@ const AddLinkedOrganizationModal = () => {
const onFederationExpect = useCallback(
(org: LinkedOrganization) => {
if (challengeState) {
const linkedorg = new LinkedOrganization({
lao_id: org.lao_id,
server_address: org.server_address,
public_key: org.public_key,
challenge: Challenge.fromState(challengeState),
});
setLinkedOrganization(linkedorg);
dispatch(addScannedLinkedOrganization(laoId, linkedorg.toState()));
expectFederation(
laoId,
org.lao_id,
Expand All @@ -108,14 +119,10 @@ const AddLinkedOrganizationModal = () => {
Challenge.fromState(challengeState),
)
.then(() => {
toast.show(`Success: Expect Federation`, {
type: 'success',
placement: 'bottom',
duration: FOUR_SECONDS,
});
dispatch(addLinkedOrganization(laoId, org.toState()));
console.log('Expect Federation successfull');
})
.catch((err) => {
console.log(err);
toast.show(`Could not expect Federation, error: ${err}`, {
type: 'danger',
placement: 'bottom',
Expand All @@ -131,14 +138,10 @@ const AddLinkedOrganizationModal = () => {
(org: LinkedOrganization) => {
initFederation(laoId, org.lao_id, org.server_address, org.public_key, org.challenge!)
.then(() => {
toast.show(`Success: Init Federation`, {
type: 'success',
placement: 'bottom',
duration: FOUR_SECONDS,
});
dispatch(addLinkedOrganization(laoId, org.toState()));
console.log('Init Federation successfull');
})
.catch((err) => {
console.log(err);
toast.show(`Could not init Federation, error: ${err}`, {
type: 'danger',
placement: 'bottom',
Expand All @@ -157,7 +160,8 @@ const AddLinkedOrganizationModal = () => {
if (isInitiatingOrganizer) {
requestChallengeAndDisplayQRCode();
setShowQRCodeModal(true);
onFederationInit(scannedLinkedOrganization);
dispatch(addScannedLinkedOrganization(laoId, scannedLinkedOrganization.toState()));
setLinkedOrganization(scannedLinkedOrganization);
} else {
onFederationExpect(scannedLinkedOrganization);
navigation.navigate(STRINGS.navigation_linked_organizations);
Expand All @@ -179,7 +183,7 @@ const AddLinkedOrganizationModal = () => {
};

useEffect(() => {
if (challengeState) {
if (challengeState && isOrganizer) {
const challenge = Challenge.fromState(challengeState);
const jsonObj = {
lao_id: laoId,
Expand All @@ -192,7 +196,7 @@ const AddLinkedOrganizationModal = () => {
};
setQRCodeData(JSON.stringify(jsonObj));
}
}, [challengeState, laoId, lao.organizer, lao.server_addresses]);
}, [challengeState, laoId, lao.organizer, lao.server_addresses, isOrganizer]);

return (
<>
Expand Down Expand Up @@ -267,6 +271,7 @@ const AddLinkedOrganizationModal = () => {
setShowQRScannerModal(true);
setShowScanner(true);
} else {
onFederationInit(linkedOrganization!);
navigation.navigate(STRINGS.navigation_linked_organizations);
}
setShowQRCodeModal(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
import { useToast } from 'react-native-toast-notifications';

import { Hash } from 'core/objects';
import { FOUR_SECONDS } from 'resources/const';

import { LinkedOrganizationsHooks } from '../hooks';
import { tokensExchange } from '../network';

interface BroadcastLinkedOrgInfoProps {
linkedLaoId: Hash;
}

const BroadcastLinkedOrgInfo: React.FC<BroadcastLinkedOrgInfoProps> = ({ linkedLaoId }) => {
const laoId = LinkedOrganizationsHooks.useCurrentLaoId();
const toast = useToast();
const fetchedLao = LinkedOrganizationsHooks.useGetLaoById(linkedLaoId);
const fetchedRollCall = LinkedOrganizationsHooks.useGetRollCallById(
fetchedLao!.last_roll_call_id!,
);

if (fetchedRollCall === undefined) {
toast.show(`Data Exchange failed: RollCall ID is undefined`, {
type: 'danger',
placement: 'bottom',
duration: FOUR_SECONDS,
});
return null;
}
if (fetchedRollCall.attendees === undefined) {
toast.show(`Data Exchange failed: RollCall Attendees is undefined`, {
type: 'danger',
placement: 'bottom',
duration: FOUR_SECONDS,
});
return null;
}
tokensExchange(laoId, linkedLaoId, fetchedRollCall.id, fetchedRollCall.attendees)
.then(() => {
console.log('Data Exchange successfull');
})
.catch((err) => {
console.log(err);
toast.show(`Could not exchange Tokens, error: ${err}`, {
type: 'danger',
placement: 'bottom',
duration: FOUR_SECONDS,
});
});
return null;
};

export default BroadcastLinkedOrgInfo;
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const styles = StyleSheet.create({
top: '25%',
bottom: '50%',
} as ViewStyle,
qrCodeMobile: {
scale: 0.65,
alignItems: 'center',
justifyContent: 'center',
opacity: 0.5,
top: '20%',
bottom: '50%',
} as ViewStyle,
scannerTextItems: {
top: '35%',
} as ViewStyle,
Expand Down Expand Up @@ -68,7 +76,7 @@ const QRCodeScannerModal: React.FC<QRCodeScannerModalProps> = ({
</Text>
<QrCodeScanner showCamera={showScanner} handleScan={onScanData}>
<View>
<View style={styles.qrCode}>
<View style={window.innerWidth <= 500 ? styles.qrCodeMobile : styles.qrCode}>
<QrCodeScanOverlay width={300} height={300} />
</View>
<View style={styles.scannerTextItems}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const contextValue = {
useCurrentLaoId: () => mockLaoId,
useIsLaoOrganizer: () => false,
useCurrentLao: () => mockLaoServerAddress,
getLaoById: () => undefined,
getRollCallById: () => undefined,
} as LinkedOrganizationsReactContext,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export namespace LinkedOrganizationsHooks {
*/
export const useCurrentLao = () => useLinkedOrganizationsContext().useCurrentLao();

/**
* Gets the function to retrieve a lao by its id
*/
export const useGetLaoById = (laoId: Hash) => useLinkedOrganizationsContext().getLaoById(laoId);

/**
* Gets the function to retrieve a rollcall by its id
*/
export const useGetRollCallById = (id: Hash) =>
useLinkedOrganizationsContext().getRollCallById(id);

/**
* Gets whether the current user is organizer of the given lao
*/
Expand Down
5 changes: 4 additions & 1 deletion fe1-web/src/features/linked-organizations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { challengeReducer, linkedOrganizationsReducer } from './reducer';
export function configure(
configuration: LinkedOrganizationsConfiguration,
): LinkedOrganizationsInterface {
const { useCurrentLao, useCurrentLaoId, useIsLaoOrganizer } = configuration;
const { useCurrentLao, useCurrentLaoId, useIsLaoOrganizer, getLaoById, getRollCallById } =
configuration;
configureNetwork(configuration);
return {
identifier: LINKED_ORGANIZATIONS_FEATURE_IDENTIFIER,
Expand All @@ -26,6 +27,8 @@ export function configure(
useCurrentLaoId: useCurrentLaoId,
useIsLaoOrganizer: useIsLaoOrganizer,
useCurrentLao: useCurrentLao,
getLaoById: getLaoById,
getRollCallById: getRollCallById,
},
};
}
Loading

0 comments on commit 14b1cdd

Please sign in to comment.