Files
inji-wallet/machines/settings.ts
Alka Prasad 3e5fb5fde4 [Inji 267] sunbird telemetry configuration (#838)
* feat(INJI-267): add and export deviceId constant in constants file

Use react-native-device-info library to get the unique identifier for each and every device

* refactor(INJI-267): extracts AppId global variable into a global variable file and renames it to __AppId

* refactor(INJI-267): extracts inji version and tuvali version handling into global variable file

* feat(INJI-267): add telemetry configuration in the application

* feat(INJI-267): generate unique session id for every session of the app

After launching the app until we kill the app we consider it as a single session

* feat(INJI-267): add start and end telemetry events for the QR login success flow

* feat(INJI-267): pass session id value in sunbird config

* feat(INJI-267): add start and end telemetry events for the VC sharing success flow

* feat(INJI-267): add start and end telemetry events for the VC activation success flow

* refactor(INJI-267): change QR login, VC share telemetry events type

* refactor(INJI-267): extract generateSessionId function, deviceId into global variables file

* feat(INJI-267): adds device related information in the config value

* feat(INJI-267): add and export deviceId constant in constants file

Use react-native-device-info library to get the unique identifier for each and every device

* refactor(INJI-267): extracts AppId global variable into a global variable file and renames it to __AppId

* refactor(INJI-267): extracts inji version and tuvali version handling into global variable file

* feat(INJI-267): add telemetry configuration in the application

* feat(INJI-267): generate unique session id for every session of the app

After launching the app until we kill the app we consider it as a single session

* feat(INJI-267): add start and end telemetry events for the QR login success flow

* feat(INJI-267): pass session id value in sunbird config

* feat(INJI-267): add start and end telemetry events for the VC sharing success flow

* feat(INJI-267): add start and end telemetry events for the VC activation success flow

* refactor(INJI-267): change QR login, VC share telemetry events type

* refactor(INJI-267): extract generateSessionId function, deviceId into global variables file

* feat(INJI-267): adds device related information in the config value

* feat(INJI-267): add selected language in global variables file to pass it into sunbird config

* feat(INJI-267): updates config structure and add AppInfo event

* feat(INJI-267): create dummy end event data to make the schema and send AppInfo Event on app launch

* feat(INJI-267): add and export deviceId constant in constants file

Use react-native-device-info library to get the unique identifier for each and every device

* refactor(INJI-267): extracts AppId global variable into a global variable file and renames it to __AppId

* refactor(INJI-267): extracts inji version and tuvali version handling into global variable file

* feat(INJI-267): add telemetry configuration in the application

* feat(INJI-267): generate unique session id for every session of the app

After launching the app until we kill the app we consider it as a single session

* feat(INJI-267): add start and end telemetry events for the QR login success flow

* feat(INJI-267): pass session id value in sunbird config

* feat(INJI-267): add start and end telemetry events for the VC sharing success flow

* feat(INJI-267): add start and end telemetry events for the VC activation success flow

* refactor(INJI-267): change QR login, VC share telemetry events type

* refactor(INJI-267): extract generateSessionId function, deviceId into global variables file

* feat(INJI-267): adds device related information in the config value

* feat(INJI-267): add and export deviceId constant in constants file

Use react-native-device-info library to get the unique identifier for each and every device

* feat(INJI-267): add telemetry configuration in the application

* feat(INJI-267): pass session id value in sunbird config

* refactor(INJI-267): change QR login, VC share telemetry events type

* refactor(INJI-267): extract generateSessionId function, deviceId into global variables file

* feat(INJI-267): adds device related information in the config value

* feat(INJI-267): add selected language in global variables file to pass it into sunbird config

* feat(INJI-267): updates config structure and add AppInfo event

* feat(INJI-267): create dummy end event data to make the schema and send AppInfo Event on app launch

* feat(INJI-267): bump up react-native-localize and remove unnecessary code

* feat(INJI-267): remove some unused imports and unnecessary formattings

* feat(INJI-267): remove unnecessary formattings

* feat(INJI-267): extracts obsrv host in an env variable

* feat(INJI-267): add env variable in react-native-dot-env index file

* feat(INJI-267): bump up react-native-localize

---------

Signed-off-by: Swati Goel <meet2swati@gmail.com>
Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
Co-authored-by: Swati Goel <meet2swati@gmail.com>
2023-09-18 15:32:14 +05:30

322 lines
8.7 KiB
TypeScript

import { assign, ContextFrom, EventFrom, send, StateFrom } from 'xstate';
import { createModel } from 'xstate/lib/model';
import { AppServices } from '../shared/GlobalContext';
import {
APP_ID_DICTIONARY,
APP_ID_LENGTH,
MIMOTO_BASE_URL,
isIOS,
SETTINGS_STORE_KEY,
ESIGNET_BASE_URL,
} from '../shared/constants';
import { VCLabel } from '../types/vc';
import { StoreEvents } from './store';
import getAllConfigurations, {
COMMON_PROPS_KEY,
} from '../shared/commonprops/commonProps';
import Storage from '../shared/storage';
import ShortUniqueId from 'short-unique-id';
import { __AppId } from '../shared/GlobalVariables';
import { isCustomSecureKeystore } from '../shared/cryptoutil/cryptoUtil';
const model = createModel(
{
serviceRefs: {} as AppServices,
name: '',
vcLabel: {
singular: 'Card',
plural: 'Cards',
} as VCLabel,
isBiometricUnlockEnabled: false,
credentialRegistry: MIMOTO_BASE_URL,
esignetHostUrl: ESIGNET_BASE_URL,
appId: null,
hasUserShownWithHardwareKeystoreNotExists: false,
credentialRegistryResponse: '' as string,
},
{
events: {
UPDATE_NAME: (name: string) => ({ name }),
UPDATE_VC_LABEL: (label: string) => ({ label }),
TOGGLE_BIOMETRIC_UNLOCK: (enable: boolean) => ({ enable }),
STORE_RESPONSE: (response: unknown) => ({ response }),
CHANGE_LANGUAGE: (language: string) => ({ language }),
UPDATE_MIMOTO_HOST: (credentialRegistry: string) => ({
credentialRegistry,
}),
UPDATE_ESIGNET_HOST: (esignetHostUrl: string) => ({ esignetHostUrl }),
UPDATE_CREDENTIAL_REGISTRY_RESPONSE: (
credentialRegistryResponse: string
) => ({
credentialRegistryResponse: credentialRegistryResponse,
}),
INJI_TOUR_GUIDE: () => ({}),
BACK: () => ({}),
CANCEL: () => ({}),
ACCEPT_HARDWARE_SUPPORT_NOT_EXISTS: () => ({}),
},
}
);
export const SettingsEvents = model.events;
export const settingsMachine = model.createMachine(
{
predictableActionArguments: true,
preserveActionOrder: true,
tsTypes: {} as import('./settings.typegen').Typegen0,
schema: {
context: model.initialContext,
events: {} as EventFrom<typeof model>,
},
id: 'settings',
initial: 'init',
states: {
init: {
entry: ['requestStoredContext'],
on: {
STORE_RESPONSE: [
{
cond: 'hasPartialData',
target: 'idle',
actions: ['setContext', 'updatePartialDefaults', 'storeContext'],
},
{ cond: 'hasData', target: 'idle', actions: ['setContext'] },
{ target: 'storingDefaults' },
],
},
},
storingDefaults: {
entry: ['updateDefaults', 'storeContext'],
on: {
STORE_RESPONSE: 'idle',
},
},
idle: {
entry: ['injiTourGuide'],
on: {
TOGGLE_BIOMETRIC_UNLOCK: {
actions: ['toggleBiometricUnlock', 'storeContext'],
},
UPDATE_NAME: {
actions: ['updateName', 'storeContext'],
},
UPDATE_VC_LABEL: {
actions: ['updateVcLabel', 'storeContext'],
},
UPDATE_MIMOTO_HOST: {
actions: ['resetCredentialRegistry'],
target: 'resetInjiProps',
},
UPDATE_ESIGNET_HOST: {
actions: ['updateEsignetHostUrl', 'storeContext'],
},
CANCEL: {
actions: ['resetCredentialRegistry'],
},
ACCEPT_HARDWARE_SUPPORT_NOT_EXISTS: {
actions: [
'updateUserShownWithHardwareKeystoreNotExists',
'storeContext',
],
target: 'idle',
},
},
},
resetInjiProps: {
invoke: {
src: 'resetInjiProps',
onDone: {
actions: [
'updateCredentialRegistrySuccess',
'updateCredentialRegistry',
'storeContext',
],
target: 'idle',
},
onError: {
actions: ['updateCredentialRegistryResponse'],
target: 'idle',
},
},
on: {
CANCEL: {
actions: ['resetCredentialRegistry'],
target: 'idle',
},
},
},
injiTourGuide: {
on: {
INJI_TOUR_GUIDE: {
target: 'showInjiTourGuide',
},
},
},
showInjiTourGuide: {
on: {
BACK: {
target: 'idle',
},
},
},
},
},
{
actions: {
requestStoredContext: send(StoreEvents.GET(SETTINGS_STORE_KEY), {
to: (context) => context.serviceRefs.store,
}),
updateDefaults: model.assign({
appId: () => {
const appId = generateAppId();
__AppId.setValue(appId);
return appId;
},
hasUserShownWithHardwareKeystoreNotExists: () => false,
}),
updatePartialDefaults: model.assign({
appId: (context) => context.appId || generateAppId(),
}),
storeContext: send(
(context) => {
const { serviceRefs, ...data } = context;
return StoreEvents.SET(SETTINGS_STORE_KEY, data);
},
{ to: (context) => context.serviceRefs.store }
),
setContext: model.assign((context, event) => {
const newContext = event.response as ContextFrom<typeof model>;
__AppId.setValue(newContext.appId);
return {
...context,
...newContext,
};
}),
updateName: model.assign({
name: (_, event) => event.name,
}),
updateEsignetHostUrl: model.assign({
esignetHostUrl: (_, event) => event.esignetHostUrl,
}),
updateVcLabel: model.assign({
vcLabel: (_, event) => ({
singular: event.label,
plural: event.label + 's',
}),
}),
updateCredentialRegistry: assign({
credentialRegistry: (_context, event) => event.data.warningDomainName,
}),
updateCredentialRegistryResponse: assign({
credentialRegistryResponse: () => 'error',
}),
updateCredentialRegistrySuccess: assign({
credentialRegistryResponse: () => 'success',
}),
resetCredentialRegistry: model.assign({
credentialRegistryResponse: () => '',
}),
updateUserShownWithHardwareKeystoreNotExists: model.assign({
hasUserShownWithHardwareKeystoreNotExists: () => true,
}),
toggleBiometricUnlock: model.assign({
isBiometricUnlockEnabled: (_, event) => event.enable,
}),
},
services: {
resetInjiProps: async (context, event) => {
try {
await Storage.removeItem(COMMON_PROPS_KEY);
return await getAllConfigurations(event.credentialRegistry);
} catch (error) {
console.log('Error from resetInjiProps ', error);
throw error;
}
},
},
guards: {
hasData: (_, event) => event.response != null,
hasPartialData: (_, event) =>
event.response != null && event.response.appId == null,
},
}
);
export function createSettingsMachine(serviceRefs: AppServices) {
return settingsMachine.withContext({
...settingsMachine.context,
serviceRefs,
});
}
function generateAppId() {
const shortUUID = new ShortUniqueId({
length: APP_ID_LENGTH,
dictionary: APP_ID_DICTIONARY,
});
return shortUUID.randomUUID();
}
function deviceSupportsHardwareKeystore() {
return isIOS() ? true : isCustomSecureKeystore();
}
type State = StateFrom<typeof settingsMachine>;
export function selectName(state: State) {
return state.context.name;
}
export function selectAppId(state: State) {
return state.context.appId;
}
/** Alerting the user when the hardware keystore not supported by device and
* not shown to user atlease once */
export function selectShowHardwareKeystoreNotExistsAlert(state: State) {
const hasShown = state.context.hasUserShownWithHardwareKeystoreNotExists;
const deviceSupports = deviceSupportsHardwareKeystore();
return !hasShown && !deviceSupports;
}
export function selectVcLabel(state: State) {
return state.context.vcLabel;
}
export function selectCredentialRegistry(state: State) {
return state.context.credentialRegistry;
}
export function selectEsignetHostUrl(state: State) {
return state.context.esignetHostUrl;
}
export function selectCredentialRegistryResponse(state: State) {
return state.context.credentialRegistryResponse;
}
export function selectBiometricUnlockEnabled(state: State) {
return state.context.isBiometricUnlockEnabled;
}
export function selectIsResetInjiProps(state: State) {
return state.matches('resetInjiProps');
}