Skip to content

Commit

Permalink
feat(#84): [Pooja|Tilak] remove old nearby state machines and refact…
Browse files Browse the repository at this point in the history
…or selectors
  • Loading branch information
tilak-puli committed May 11, 2023
1 parent e5a3b69 commit da125f0
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 2,510 deletions.
32 changes: 14 additions & 18 deletions machines/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import { createSettingsMachine, settingsMachine } from './settings';
import { storeMachine } from './store';
import { createVcMachine, vcMachine } from './vc';
import { createActivityLogMachine, activityLogMachine } from './activityLog';
import { createRequestMachine, requestMachine } from './request';
import * as BLERequest from './openIdBle/request';
import * as BLEScan from './openIdBle/scan';
import { createScanMachine, scanMachine } from './scan';
import {
createRequestMachine,
requestMachine,
} from './openIdBle/request/machine';
import { createScanMachine, scanMachine } from './openIdBle/scan/machine';
import { createRevokeMachine, revokeVidsMachine } from './revoke';

import { pure, respond } from 'xstate/lib/actions';
import { AppServices } from '../shared/GlobalContext';
import { request } from '../shared/request';
import { isBLEEnabled } from '../lib/smartshare';

const model = createModel(
{
Expand Down Expand Up @@ -197,19 +196,15 @@ export const appMachine = model.createMachine(
activityLogMachine.id
);

serviceRefs.scan = isBLEEnabled
? spawn(
BLEScan.createScanMachine(serviceRefs),
BLEScan.scanMachine.id
)
: spawn(createScanMachine(serviceRefs), scanMachine.id);
serviceRefs.scan = spawn(
createScanMachine(serviceRefs),
scanMachine.id
);

serviceRefs.request = isBLEEnabled
? spawn(
BLERequest.createRequestMachine(serviceRefs),
BLERequest.requestMachine.id
)
: spawn(createRequestMachine(serviceRefs), requestMachine.id);
serviceRefs.request = spawn(
createRequestMachine(serviceRefs),
requestMachine.id
);

serviceRefs.revoke = spawn(
createRevokeMachine(serviceRefs),
Expand Down Expand Up @@ -318,6 +313,7 @@ interface AppInfo {
deviceId: string;
deviceName: string;
}

interface BackendInfo {
application: {
name: string;
Expand Down
58 changes: 58 additions & 0 deletions machines/openIdBle/commonSelectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { StateFrom } from 'xstate';
import { scanMachine } from './scan/machine';
import { requestMachine } from './request/machine';

type State = StateFrom<typeof scanMachine & typeof requestMachine>;

export function selectIsCancelling(state: State) {
return state.matches('cancelling');
}

export function selectIsReviewing(state: State) {
return state.matches('reviewing');
}

export function selectIsAccepted(state: State) {
return state.matches('reviewing.accepted');
}

export function selectIsRejected(state: State) {
return state.matches('reviewing.rejected');
}

export function selectIsVerifyingIdentity(state: State) {
return state.matches('reviewing.verifyingIdentity');
}

export function selectIsInvalidIdentity(state: State) {
return state.matches('reviewing.invalidIdentity');
}

export function selectIsDisconnected(state: State) {
return state.matches('disconnected');
}

export function selectIsBluetoothDenied(state: State) {
return state.matches('bluetoothDenied');
}

// TODO: Remove these selectors and respective UI code once discussed with team
export function selectIsExchangingDeviceInfo() {
return false;
}

export function selectIsExchangingDeviceInfoTimeout() {
return false;
}

export function selectIsDone(state: State) {
return state.matches('reviewing.navigatingToHome');
}

export function selectIsOffline() {
return false;
}

export function selectIsHandlingBleError(state: State) {
return state.matches('handlingBleError');
}
129 changes: 12 additions & 117 deletions machines/openIdBle/request.ts → machines/openIdBle/request/machine.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import openIdBLE from 'react-native-openid4vp-ble';
import BluetoothStateManager from 'react-native-bluetooth-state-manager';
import { EmitterSubscription, Linking, Platform } from 'react-native';
import { assign, EventFrom, send, StateFrom } from 'xstate';
import { assign, EventFrom, send } from 'xstate';
import { createModel } from 'xstate/lib/model';
import { DeviceInfo } from '../../components/DeviceInfoList';
import { DeviceInfo } from '../../../components/DeviceInfoList';
import { getDeviceNameSync } from 'react-native-device-info';
import { StoreEvents } from '../store';
import { VC } from '../../types/vc';
import { AppServices } from '../../shared/GlobalContext';
import { StoreEvents } from '../../store';
import { VC } from '../../../types/vc';
import { AppServices } from '../../../shared/GlobalContext';
import {
RECEIVED_VCS_STORE_KEY,
VC_ITEM_STORE_KEY,
} from '../../shared/constants';
import { ActivityLogEvents, ActivityLogType } from '../activityLog';
import { VcEvents } from '../vc';
import { offlineSubscribe } from '../../shared/openIdBLE/verifierEventHandler';
} from '../../../shared/constants';
import { ActivityLogEvents, ActivityLogType } from '../../activityLog';
import { VcEvents } from '../../vc';
import { offlineSubscribe } from '../../../shared/openIdBLE/verifierEventHandler';
import { log } from 'xstate/lib/actions';
import { VerifierDataEvent } from 'react-native-openid4vp-ble/lib/typescript/types/bleshare';
// import { verifyPresentation } from '../shared/vcjs/verifyPresentation';
Expand Down Expand Up @@ -70,14 +70,15 @@ const model = createModel(
},
}
);
export const RequestEvents = model.events;

export const requestMachine =
/** @xstate-layout N4IgpgJg5mDOIC5QCcwEcCucAuBiAygMIBKAoqQHID6AQgDICqxA2gAwC6ioADgPawBLbAN4A7LiAAeiAOwA2ABwA6AKwBmAJwBGDazmaF6rQBoQAT0QBaDTIAsSgExrNttXLk61rgL7fTqTBwCEnJqADEAeUIGfDZOJBA+QWExCWkEFVYZJW0ZNQVbTIc5eRLTCwRrO0cvVgU61gcHVizff3QsWDx8AHUASQAVQgAJKgAFYgiBqIi6OIkkoRFxBPS1VjUlGVZbLJUtNwPbBTlyqxUFBxqvJsMHBQV8tpAAzrx6UipSYkmWDgX+EtUgkKpYtFo6ltavV6k0WjJTOlmnJ7AoNG4HLZ9AotA4VM9XjglABjAAWYGJAGsBKIoDQADZYbC8XjYUn4MDIABuAmJYBJ5KpNKguHoDFI0ymo0oAEEPgAReYJRYpFagdL6ORKRq2DSZY7rRQKM6VfJa-YaJwOLTyDQomT4vwvDpEskU6m0hlMllsjnc3n8t1C2mixgSiJSqjyvr4OV0UiK-7KwGqtKIeoqJRYnbgjQPBzaU7mKy6q5yDQVrLg3VyOoyAkuroC93Cr1gZms9mcnl8pSErrC0PiyUDaUUOMJpU8FPLNMIcH7Gq2XGFNxqGQOk2WY7ZRQb44qFRyfY4huBJtBj10xntn1d-29-vCENi8OR6OxhVTxIz4HqxBaK4maPIBrA2k4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TFa9-GRL-U4u6tJFwLYXpS0dYAtS7IbDEbUHYAoFHFEYNc6zGli7fXfHGgQQ-UeRgiG-GucLEBso6F+EhZGd3R6tKXRZoYKhmudLGlm-fNmyAHVJmoa2Skav3ImrEEmhyEocmjLbYQZa2Rqpw-iDGNAjAr6W65W+6i4rYAsQyFoaEk0c2VQAsH3YqZiZiOgiWRg6osAVglTX06wYdXko2UQ60PECQ3wXwIAA */
model.createMachine(
{
predictableActionArguments: true,
preserveActionOrder: true,
tsTypes: {} as import('./request.typegen').Typegen0,
tsTypes: {} as import('./machine.typegen').Typegen0,
schema: {
context: model.initialContext,
events: {} as EventFrom<typeof model>,
Expand Down Expand Up @@ -185,7 +186,7 @@ export const requestMachine =
},
on: {
ADV_STARTED: {
actions: ['setOpenID4VpUri'],
actions: 'setOpenID4VpUri',
},
CONNECTED: {
target: 'waitingForVc',
Expand Down Expand Up @@ -695,109 +696,3 @@ export function createRequestMachine(serviceRefs: AppServices) {
serviceRefs,
});
}

type State = StateFrom<typeof requestMachine>;

export function selectSenderInfo(state: State) {
return state.context.senderInfo;
}

export function selectOpenId4VpUri(state: State) {
return state.context.openId4VpUri;
}

export function selectIncomingVc(state: State) {
return state.context.incomingVc;
}

export function selectSharingProtocol(state: State) {
return state.context.sharingProtocol;
}

export function selectIsIncomingVp(state: State) {
return state.context.incomingVc?.verifiablePresentation != null;
}

export function selectIsCancelling(state: State) {
return state.matches('cancelling');
}

export function selectIsReviewing(state: State) {
return state.matches('reviewing');
}

export function selectIsAccepted(state: State) {
return state.matches('reviewing.accepted');
}

export function selectIsAccepting(state: State) {
return state.matches('reviewing.accepting');
}

export function selectIsSavingFailedInIdle(state: State) {
return state.matches('reviewing.savingFailed.idle');
}

export function selectIsSavingFailedInViewingVc(state: State) {
return state.matches('reviewing.savingFailed.viewingVc');
}

export function selectStoreError(state: State) {
return state.context.storeError;
}

export function selectIsRejected(state: State) {
return state.matches('reviewing.rejected');
}

export function selectIsVerifyingIdentity(state: State) {
return state.matches('reviewing.verifyingIdentity');
}

export function selectIsVerifyingVp(state: State) {
return state.matches('reviewing.verifyingVp');
}

export function selectIsInvalidIdentity(state: State) {
return state.matches('reviewing.invalidIdentity');
}

export function selectIsDisconnected(state: State) {
return state.matches('disconnected');
}

export function selectIsWaitingForConnection(state: State) {
return state.matches('waitingForConnection');
}

export function selectIsBluetoothDenied(state: State) {
return state.matches('bluetoothDenied');
}

export function selectIsCheckingBluetoothService(state: State) {
return state.matches('checkingBluetoothService');
}
//TODO: post discussion with team remove the selectIsExchangingDeviceInfo & selectIsExchangingDeviceInfoTimeOut functions
export function selectIsExchangingDeviceInfo() {
return true;
}

export function selectIsExchangingDeviceInfoTimeout() {
return true;
}

export function selectIsWaitingForVc(state: State) {
return state.matches('waitingForVc.inProgress');
}

export function selectIsWaitingForVcTimeout(state: State) {
return state.matches('waitingForVc.timeout');
}

export function selectIsDone(state: State) {
return state.matches('reviewing.navigatingToHome');
}

export function selectIsHandlingBleError(state: State) {
return state.matches('handlingBleError');
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ export interface Typegen0 {
| 'done.invoke.request.reviewing.verifyingVp:invocation[0]';
sendVcReceived: 'STORE_RESPONSE';
setIncomingVc: 'VC_RECEIVED';
setOpenID4VpUri: 'ADV_STARTED';
setReceiveLogTypeDiscarded: 'CANCEL' | 'REJECT' | 'STORE_ERROR';
setReceiveLogTypeRegular: 'ACCEPT' | 'STORE_RESPONSE';
setReceiveLogTypeUnverified: 'FACE_INVALID';
setReceiveLogTypeVerified: 'FACE_VALID';
setReceiverInfo: 'CONNECTED';
setSenderInfo: 'CONNECTED';
setStoringError: 'STORE_ERROR';
storeVc: 'STORE_RESPONSE';
switchProtocol: 'SWITCH_PROTOCOL';
Expand Down
60 changes: 60 additions & 0 deletions machines/openIdBle/request/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { StateFrom } from 'xstate';
import { requestMachine } from './machine';

type State = StateFrom<typeof requestMachine>;

export function selectSenderInfo(state: State) {
return state.context.senderInfo;
}

export function selectIncomingVc(state: State) {
return state.context.incomingVc;
}

export function selectSharingProtocol(state: State) {
return state.context.sharingProtocol;
}

export function selectIsIncomingVp(state: State) {
return state.context.incomingVc?.verifiablePresentation != null;
}

export function selectIsReviewingInIdle(state: State) {
return state.matches('reviewing.idle');
}

export function selectIsWaitingForConnection(state: State) {
return state.matches('waitingForConnection');
}

export function selectIsCheckingBluetoothService(state: State) {
return state.matches('checkingBluetoothService');
}

export function selectIsWaitingForVc(state: State) {
return state.matches('waitingForVc.inProgress');
}

export function selectIsWaitingForVcTimeout(state: State) {
return state.matches('waitingForVc.timeout');
}

export function selectOpenId4VpUri(state: State) {
return state.context.openId4VpUri;
}

export function selectIsAccepting(state: State) {
return state.matches('reviewing.accepting');
}

export function selectIsSavingFailedInIdle(state: State) {
return state.matches('reviewing.savingFailed.idle');
}

export function selectIsSavingFailedInViewingVc(state: State) {
return state.matches('reviewing.savingFailed.viewingVc');
}

export function selectStoreError(state: State) {
return state.context.storeError;
}
Loading

0 comments on commit da125f0

Please sign in to comment.