Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions packages/connect-explorer/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,3 @@ export const ON_PASSPHRASE_SUBMIT = 'action__on_passphrase_submit';
export const ON_CHANGE_ACCOUNT = 'action__on_change_account';
export const ON_CUSTOM_FEE_OPEN = 'action__on_custom_fee_open';
export const ON_CUSTOM_FEE_CHANGE = 'action__on_custom_fee_change';

export const ON_SELECT_DEVICE = 'action__on_select_device';
export const ON_CHANGE_CONNECT_OPTIONS = 'action__on_change_connect_options';
export const ON_CHANGE_CONNECT_OPTION = 'action__on_change_connect_option';

export const ON_HANDSHAKE_CONFIRMED = 'action__on_handshake_confirmed';
export const ON_INIT_ERROR = 'action__on_init_error';

export { SET_SCHEMA, SET_METHOD } from './methodActions';
42 changes: 16 additions & 26 deletions packages/connect-explorer/src/actions/methodActions.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
import { TSchema } from '@sinclair/typebox';
import type { TSchema } from '@sinclair/typebox';
import JSON5 from 'json5';

import TrezorConnectMobile from '@trezor/connect-mobile';
import TrezorConnect, { TrezorConnect as TrezorConnectType } from '@trezor/connect-web';
import TrezorConnect from '@trezor/connect-web';
import { getDeepValue } from '@trezor/schema-utils/src/utils';

import { Dispatch, Field, GetState } from '../types';

export const SET_METHOD = 'method_set';
export const SET_SCHEMA = 'schema_set';
export const FIELD_CHANGE = 'method_field_change';
export const FIELD_DATA_CHANGE = 'method_field_data_change';
export const ADD_BATCH = 'method_add_batch';
export const REMOVE_BATCH = 'method_remove_batch';
export const SET_UNION = 'method_set_union';
export const RESPONSE = 'method_response';
export const SET_MANUAL_MODE = 'method_set_manual_mode';
export const SET_METHOD_PROCESSING = 'method_set_processing';

export type MethodAction =
| { type: typeof SET_METHOD; methodConfig: any }
| { type: typeof SET_SCHEMA; method: keyof TrezorConnectType; schema: TSchema }
| { type: typeof FIELD_CHANGE; field: Field<any>; value: any }
| { type: typeof FIELD_DATA_CHANGE; field: Field<any>; data: any }
| { type: typeof ADD_BATCH; field: Field<any>; item: any }
| { type: typeof REMOVE_BATCH; field: Field<any>; batch: any[] }
| { type: typeof SET_UNION; field: Field<any>; current: any }
| { type: typeof RESPONSE; response: any }
| { type: typeof SET_MANUAL_MODE; manualMode: boolean }
| { type: typeof SET_METHOD_PROCESSING; payload: boolean };
import type { Dispatch, Field, GetState } from '../types';
import {
ADD_BATCH,
FIELD_CHANGE,
FIELD_DATA_CHANGE,
REMOVE_BATCH,
RESPONSE,
SET_MANUAL_MODE,
SET_METHOD,
SET_METHOD_PROCESSING,
SET_SCHEMA,
SET_UNION,
} from '../types/actions';

export const onSetMethod = (methodConfig: any) => ({
type: SET_METHOD,
methodConfig,
Expand Down
49 changes: 18 additions & 31 deletions packages/connect-explorer/src/actions/trezorConnectActions.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
import TrezorConnectMobile from '@trezor/connect-mobile';
import TrezorConnect, {
DEVICE,
DEVICE_EVENT,
TRANSPORT_EVENT,
WEBEXTENSION,
} from '@trezor/connect-web';

import { Dispatch, Field, GetState, TrezorConnectDevice } from '../types';

import * as ACTIONS from './index';

type ConnectOptions = Parameters<(typeof TrezorConnect)['init']>[0];
export type TrezorConnectAction =
| { type: typeof ACTIONS.ON_SELECT_DEVICE; path: string }
| { type: typeof DEVICE.CONNECT; device: TrezorConnectDevice }
| { type: typeof DEVICE.CONNECT_UNACQUIRED; device: TrezorConnectDevice }
| { type: typeof DEVICE.DISCONNECT; device: TrezorConnectDevice }
| { type: typeof ACTIONS.ON_CHANGE_CONNECT_OPTIONS; payload: ConnectOptions }
| { type: typeof ACTIONS.ON_HANDSHAKE_CONFIRMED }
| { type: typeof ACTIONS.ON_INIT_ERROR; payload: string }
| {
type: typeof ACTIONS.ON_CHANGE_CONNECT_OPTION;
payload: { option: Field<any>; value: any };
};
import TrezorConnect, { DEVICE_EVENT, TRANSPORT_EVENT, WEBEXTENSION } from '@trezor/connect-web';

import type { Dispatch, Field, GetState } from '../types';
import {
type ConnectOptions,
ON_CHANGE_CONNECT_OPTION,
ON_CHANGE_CONNECT_OPTIONS,
ON_HANDSHAKE_CONFIRMED,
ON_INIT_ERROR,
ON_SELECT_DEVICE,
} from '../types/actions';

export function onSelectDevice(path: string) {
return {
type: ACTIONS.ON_SELECT_DEVICE,
type: ON_SELECT_DEVICE,
path,
};
}

export const onConnectOptionChange = (option: Field<any>, value: any) => ({
type: ACTIONS.ON_CHANGE_CONNECT_OPTION,
type: ON_CHANGE_CONNECT_OPTION,
payload: {
option,
value,
Expand Down Expand Up @@ -66,7 +53,7 @@ const testLoadingScript = (src: string) =>
});

export const init =
(options: Partial<Parameters<(typeof TrezorConnect)['init']>[0]> = {}) =>
(options: ConnectOptions = {}) =>
async (dispatch: Dispatch) => {
window.TrezorConnect = TrezorConnect;

Expand All @@ -76,7 +63,7 @@ export const init =
// @ts-expect-error
TrezorConnect.on(WEBEXTENSION.CHANNEL_HANDSHAKE_CONFIRM, event => {
if (event.type === WEBEXTENSION.CHANNEL_HANDSHAKE_CONFIRM) {
dispatch({ type: ACTIONS.ON_HANDSHAKE_CONFIRMED });
dispatch({ type: ON_HANDSHAKE_CONFIRMED });
}
});

Expand Down Expand Up @@ -122,7 +109,7 @@ export const init =
await testLoadingScript(options.connectSrc + 'js/core.js');
} catch {
dispatch({
type: ACTIONS.ON_INIT_ERROR,
type: ON_INIT_ERROR,
payload: `Invalid connectSrc: ${options.connectSrc}`,
});

Expand Down Expand Up @@ -181,12 +168,12 @@ export const init =
await TrezorConnect.init(connectOptions);
}
} catch (err) {
dispatch({ type: ACTIONS.ON_INIT_ERROR, payload: err.message });
dispatch({ type: ON_INIT_ERROR, payload: err.message });

return;
}

dispatch({ type: ACTIONS.ON_CHANGE_CONNECT_OPTIONS, payload: connectOptions });
dispatch({ type: ON_CHANGE_CONNECT_OPTIONS, payload: connectOptions });
};

export const onSubmitInit = () => async (dispatch: Dispatch, getState: GetState) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MiddlewareAPI } from 'redux';

import { SET_METHOD, SET_SCHEMA } from '../actions';
import { init } from '../actions/trezorConnectActions';
import { Action, AppState, Dispatch } from '../types';
import { SET_METHOD, SET_SCHEMA } from '../types/actions';
import { getQueryVariable } from '../utils/windowUtils';

export const trezorConnectMiddleware =
Expand Down
3 changes: 2 additions & 1 deletion packages/connect-explorer/src/reducers/methodCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { TrezorConnect } from '@trezor/connect-web';
import { TSchema } from '@trezor/schema-utils';
import { setDeepValue } from '@trezor/schema-utils/src/utils';

import { Field, FieldBasic, isFieldBasic } from '../types';
import type { Field, FieldBasic } from '../types/common';
import { isFieldBasic } from '../types/common';

export interface MethodState {
name?: keyof TrezorConnect;
Expand Down
5 changes: 3 additions & 2 deletions packages/connect-explorer/src/reducers/methodInit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Kind, OptionalKind, TSchema } from '@sinclair/typebox';
import { Kind, OptionalKind, type TSchema } from '@sinclair/typebox';

import type { TrezorConnect } from '@trezor/connect-web';

Expand All @@ -10,7 +10,8 @@ import {
updateParams,
} from './methodCommon';
import { coinsSelect } from '../constants/coins';
import { Field, FieldBasic, isFieldBasic } from '../types';
import type { Field, FieldBasic } from '../types/common';
import { isFieldBasic } from '../types/common';

// Convert TypeBox schema to our fields
const schemaToFields = (schema: TSchema, name = ''): Field<any>[] => {
Expand Down
24 changes: 14 additions & 10 deletions packages/connect-explorer/src/reducers/methodReducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import {
MethodState,
initialState,
prepareBundle,
setAffectedValues,
updateParams,
} from './methodCommon';
import { getMethodState, getMethodStateFromSchema } from './methodInit';
import type { MethodAction, TrezorConnectAction } from '../types/actions';
import {
ADD_BATCH,
FIELD_CHANGE,
Expand All @@ -9,16 +18,11 @@ import {
SET_METHOD_PROCESSING,
SET_SCHEMA,
SET_UNION,
} from '../actions/methodActions';
import { type Action, type Field, isFieldBasic } from '../types';
import {
MethodState,
initialState,
prepareBundle,
setAffectedValues,
updateParams,
} from './methodCommon';
import { getMethodState, getMethodStateFromSchema } from './methodInit';
} from '../types/actions';
import type { Field } from '../types/common';
import { isFieldBasic } from '../types/common';

type Action = MethodAction | TrezorConnectAction;

// Recursively find a field in the schema (inner function)
const findFieldsNested = (
Expand Down
31 changes: 20 additions & 11 deletions packages/connect-explorer/src/reducers/trezorConnectReducer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import TrezorConnect, { DEVICE } from '@trezor/connect-web';
import { DEVICE } from '@trezor/connect-web';

import * as ACTIONS from '../actions/index';
import { Action, Field, TrezorConnectDevice } from '../types';
import type { ConnectOptions, MethodAction, TrezorConnectAction } from '../types/actions';
import {
ON_CHANGE_CONNECT_OPTION,
ON_CHANGE_CONNECT_OPTIONS,
ON_HANDSHAKE_CONFIRMED,
ON_INIT_ERROR,
ON_SELECT_DEVICE,
} from '../types/actions';
import type { Field, TrezorConnectDevice } from '../types/common';

type Action = MethodAction | TrezorConnectAction;

export type ConnectState = {
devices: TrezorConnectDevice[];
selectedDevice?: string;
options?: Parameters<(typeof TrezorConnect)['init']>[0];
options?: ConnectOptions;
isHandshakeConfirmed: boolean;
isInitSuccess: boolean;
initError?: string;
Expand Down Expand Up @@ -67,8 +76,8 @@ const onOptionChange = <T>(state: ConnectState, field: Field<T>, value: T): Conn
},
};
}
// @ts-expect-error field name must be key of options
newState.options[field.name] = value;

(newState.options as any)[field.name] = value;

return newState;
};
Expand All @@ -88,28 +97,28 @@ export default function connect(state: ConnectState = initialState, action: Acti
...removeDevice(state, action.device),
};

case ACTIONS.ON_SELECT_DEVICE:
case ON_SELECT_DEVICE:
return {
...state,
selectedDevice: action.path,
};

case ACTIONS.ON_CHANGE_CONNECT_OPTION:
case ON_CHANGE_CONNECT_OPTION:
return onOptionChange(state, action.payload.option, action.payload.value);

case ACTIONS.ON_CHANGE_CONNECT_OPTIONS:
case ON_CHANGE_CONNECT_OPTIONS:
return {
...state,
initError: undefined,
isInitSuccess: true,
options: action.payload,
};
case ACTIONS.ON_HANDSHAKE_CONFIRMED:
case ON_HANDSHAKE_CONFIRMED:
return {
...state,
isHandshakeConfirmed: true,
};
case ACTIONS.ON_INIT_ERROR:
case ON_INIT_ERROR:
return {
...state,
initError: action.payload,
Expand Down
54 changes: 54 additions & 0 deletions packages/connect-explorer/src/types/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { TSchema } from '@sinclair/typebox';

import TrezorConnect, { DEVICE } from '@trezor/connect-web';
import type { TrezorConnect as TrezorConnectType } from '@trezor/connect-web';

import type { Field, TrezorConnectDevice } from './common';

// Method action constants
export const SET_METHOD = 'method_set';
export const SET_SCHEMA = 'schema_set';
export const FIELD_CHANGE = 'method_field_change';
export const FIELD_DATA_CHANGE = 'method_field_data_change';
export const ADD_BATCH = 'method_add_batch';
export const REMOVE_BATCH = 'method_remove_batch';
export const SET_UNION = 'method_set_union';
export const RESPONSE = 'method_response';
export const SET_MANUAL_MODE = 'method_set_manual_mode';
export const SET_METHOD_PROCESSING = 'method_set_processing';

// TrezorConnect action constants
export const ON_SELECT_DEVICE = 'action__on_select_device';
export const ON_CHANGE_CONNECT_OPTIONS = 'action__on_change_connect_options';
export const ON_CHANGE_CONNECT_OPTION = 'action__on_change_connect_option';
export const ON_HANDSHAKE_CONFIRMED = 'action__on_handshake_confirmed';
export const ON_INIT_ERROR = 'action__on_init_error';

// Method action types
export type MethodAction =
| { type: typeof SET_METHOD; methodConfig: any }
| { type: typeof SET_SCHEMA; method: keyof TrezorConnectType; schema: TSchema }
| { type: typeof FIELD_CHANGE; field: Field<any>; value: any }
| { type: typeof FIELD_DATA_CHANGE; field: Field<any>; data: any }
| { type: typeof ADD_BATCH; field: Field<any>; item: any }
| { type: typeof REMOVE_BATCH; field: Field<any>; batch: any[] }
| { type: typeof SET_UNION; field: Field<any>; current: any }
| { type: typeof RESPONSE; response: any }
| { type: typeof SET_MANUAL_MODE; manualMode: boolean }
| { type: typeof SET_METHOD_PROCESSING; payload: boolean };

// TrezorConnect action types
export type ConnectOptions = Partial<Parameters<(typeof TrezorConnect)['init']>[0]>;

export type TrezorConnectAction =
| { type: typeof ON_SELECT_DEVICE; path: string }
| { type: typeof DEVICE.CONNECT; device: TrezorConnectDevice }
| { type: typeof DEVICE.CONNECT_UNACQUIRED; device: TrezorConnectDevice }
| { type: typeof DEVICE.DISCONNECT; device: TrezorConnectDevice }
| { type: typeof ON_CHANGE_CONNECT_OPTIONS; payload: ConnectOptions }
| { type: typeof ON_HANDSHAKE_CONFIRMED }
| { type: typeof ON_INIT_ERROR; payload: string }
| {
type: typeof ON_CHANGE_CONNECT_OPTION;
payload: { option: Field<any>; value: any };
};
Loading
Loading