Skip to content

Commit dc4bbc5

Browse files
committed
wip
1 parent 0fcfe4e commit dc4bbc5

File tree

11 files changed

+219
-149
lines changed

11 files changed

+219
-149
lines changed

packages/connect-explorer/src/actions/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ export const ON_CHANGE_ACCOUNT = 'action__on_change_account';
1919
export const ON_CUSTOM_FEE_OPEN = 'action__on_custom_fee_open';
2020
export const ON_CUSTOM_FEE_CHANGE = 'action__on_custom_fee_change';
2121

22-
export const ON_SELECT_DEVICE = 'action__on_select_device';
23-
export const ON_CHANGE_CONNECT_OPTIONS = 'action__on_change_connect_options';
24-
export const ON_CHANGE_CONNECT_OPTION = 'action__on_change_connect_option';
22+
export {
23+
ON_SELECT_DEVICE,
24+
ON_CHANGE_CONNECT_OPTIONS,
25+
ON_CHANGE_CONNECT_OPTION,
26+
ON_HANDSHAKE_CONFIRMED,
27+
ON_INIT_ERROR,
28+
} from '../types/actions';
2529

26-
export const ON_HANDSHAKE_CONFIRMED = 'action__on_handshake_confirmed';
27-
export const ON_INIT_ERROR = 'action__on_init_error';
28-
29-
export { SET_SCHEMA, SET_METHOD } from './methodActions';
30+
export { SET_SCHEMA, SET_METHOD } from '../types/actions';

packages/connect-explorer/src/actions/methodActions.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
import { TSchema } from '@sinclair/typebox';
1+
import type { TSchema } from '@sinclair/typebox';
22
import JSON5 from 'json5';
33

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

8-
import { Dispatch, Field, GetState } from '../types';
9-
10-
export const SET_METHOD = 'method_set';
11-
export const SET_SCHEMA = 'schema_set';
12-
export const FIELD_CHANGE = 'method_field_change';
13-
export const FIELD_DATA_CHANGE = 'method_field_data_change';
14-
export const ADD_BATCH = 'method_add_batch';
15-
export const REMOVE_BATCH = 'method_remove_batch';
16-
export const SET_UNION = 'method_set_union';
17-
export const RESPONSE = 'method_response';
18-
export const SET_MANUAL_MODE = 'method_set_manual_mode';
19-
export const SET_METHOD_PROCESSING = 'method_set_processing';
20-
21-
export type MethodAction =
22-
| { type: typeof SET_METHOD; methodConfig: any }
23-
| { type: typeof SET_SCHEMA; method: keyof TrezorConnectType; schema: TSchema }
24-
| { type: typeof FIELD_CHANGE; field: Field<any>; value: any }
25-
| { type: typeof FIELD_DATA_CHANGE; field: Field<any>; data: any }
26-
| { type: typeof ADD_BATCH; field: Field<any>; item: any }
27-
| { type: typeof REMOVE_BATCH; field: Field<any>; batch: any[] }
28-
| { type: typeof SET_UNION; field: Field<any>; current: any }
29-
| { type: typeof RESPONSE; response: any }
30-
| { type: typeof SET_MANUAL_MODE; manualMode: boolean }
31-
| { type: typeof SET_METHOD_PROCESSING; payload: boolean };
8+
import type { Dispatch, Field, GetState } from '../types';
9+
import {
10+
ADD_BATCH,
11+
FIELD_CHANGE,
12+
FIELD_DATA_CHANGE,
13+
REMOVE_BATCH,
14+
RESPONSE,
15+
SET_MANUAL_MODE,
16+
SET_METHOD,
17+
SET_METHOD_PROCESSING,
18+
SET_SCHEMA,
19+
SET_UNION,
20+
} from '../types/actions';
21+
22+
export {
23+
SET_METHOD,
24+
SET_SCHEMA,
25+
FIELD_CHANGE,
26+
FIELD_DATA_CHANGE,
27+
ADD_BATCH,
28+
REMOVE_BATCH,
29+
SET_UNION,
30+
RESPONSE,
31+
SET_MANUAL_MODE,
32+
SET_METHOD_PROCESSING,
33+
};
34+
3235
export const onSetMethod = (methodConfig: any) => ({
3336
type: SET_METHOD,
3437
methodConfig,

packages/connect-explorer/src/actions/trezorConnectActions.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
import TrezorConnectMobile from '@trezor/connect-mobile';
2-
import TrezorConnect, {
3-
DEVICE,
4-
DEVICE_EVENT,
5-
TRANSPORT_EVENT,
6-
WEBEXTENSION,
7-
} from '@trezor/connect-web';
8-
9-
import { Dispatch, Field, GetState, TrezorConnectDevice } from '../types';
2+
import TrezorConnect, { DEVICE_EVENT, TRANSPORT_EVENT, WEBEXTENSION } from '@trezor/connect-web';
3+
4+
import type { Dispatch, Field, GetState } from '../types';
5+
import {
6+
type ConnectOptions,
7+
ON_CHANGE_CONNECT_OPTION,
8+
ON_CHANGE_CONNECT_OPTIONS,
9+
ON_HANDSHAKE_CONFIRMED,
10+
ON_INIT_ERROR,
11+
ON_SELECT_DEVICE,
12+
} from '../types/actions';
1013

1114
import * as ACTIONS from './index';
1215

13-
type ConnectOptions = Parameters<(typeof TrezorConnect)['init']>[0];
14-
export type TrezorConnectAction =
15-
| { type: typeof ACTIONS.ON_SELECT_DEVICE; path: string }
16-
| { type: typeof DEVICE.CONNECT; device: TrezorConnectDevice }
17-
| { type: typeof DEVICE.CONNECT_UNACQUIRED; device: TrezorConnectDevice }
18-
| { type: typeof DEVICE.DISCONNECT; device: TrezorConnectDevice }
19-
| { type: typeof ACTIONS.ON_CHANGE_CONNECT_OPTIONS; payload: ConnectOptions }
20-
| { type: typeof ACTIONS.ON_HANDSHAKE_CONFIRMED }
21-
| { type: typeof ACTIONS.ON_INIT_ERROR; payload: string }
22-
| {
23-
type: typeof ACTIONS.ON_CHANGE_CONNECT_OPTION;
24-
payload: { option: Field<any>; value: any };
25-
};
26-
16+
export {
17+
ON_SELECT_DEVICE,
18+
ON_CHANGE_CONNECT_OPTIONS,
19+
ON_CHANGE_CONNECT_OPTION,
20+
ON_HANDSHAKE_CONFIRMED,
21+
ON_INIT_ERROR,
22+
};
2723
export function onSelectDevice(path: string) {
2824
return {
2925
type: ACTIONS.ON_SELECT_DEVICE,

packages/connect-explorer/src/reducers/methodCommon.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { TrezorConnect } from '@trezor/connect-web';
22
import { TSchema } from '@trezor/schema-utils';
33
import { setDeepValue } from '@trezor/schema-utils/src/utils';
44

5-
import { Field, FieldBasic, isFieldBasic } from '../types';
5+
import type { Field, FieldBasic } from '../types/common';
6+
import { isFieldBasic } from '../types/common';
67

78
export interface MethodState {
89
name?: keyof TrezorConnect;

packages/connect-explorer/src/reducers/methodInit.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Kind, OptionalKind, TSchema } from '@sinclair/typebox';
1+
import { Kind, OptionalKind, type TSchema } from '@sinclair/typebox';
22

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

@@ -10,7 +10,8 @@ import {
1010
updateParams,
1111
} from './methodCommon';
1212
import { coinsSelect } from '../constants/coins';
13-
import { Field, FieldBasic, isFieldBasic } from '../types';
13+
import type { Field, FieldBasic } from '../types/common';
14+
import { isFieldBasic } from '../types/common';
1415

1516
// Convert TypeBox schema to our fields
1617
const schemaToFields = (schema: TSchema, name = ''): Field<any>[] => {

packages/connect-explorer/src/reducers/methodReducer.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
import type { MethodAction, TrezorConnectAction } from '../types/actions';
2+
import type { Field } from '../types/common';
3+
import { isFieldBasic } from '../types/common';
4+
import {
5+
MethodState,
6+
initialState,
7+
prepareBundle,
8+
setAffectedValues,
9+
updateParams,
10+
} from './methodCommon';
11+
import { getMethodState, getMethodStateFromSchema } from './methodInit';
112
import {
213
ADD_BATCH,
314
FIELD_CHANGE,
@@ -9,16 +20,9 @@ import {
920
SET_METHOD_PROCESSING,
1021
SET_SCHEMA,
1122
SET_UNION,
12-
} from '../actions/methodActions';
13-
import { type Action, type Field, isFieldBasic } from '../types';
14-
import {
15-
MethodState,
16-
initialState,
17-
prepareBundle,
18-
setAffectedValues,
19-
updateParams,
20-
} from './methodCommon';
21-
import { getMethodState, getMethodStateFromSchema } from './methodInit';
23+
} from '../types/actions';
24+
25+
type Action = MethodAction | TrezorConnectAction;
2226

2327
// Recursively find a field in the schema (inner function)
2428
const findFieldsNested = (

packages/connect-explorer/src/reducers/trezorConnectReducer.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import TrezorConnect, { DEVICE } from '@trezor/connect-web';
1+
import { DEVICE } from '@trezor/connect-web';
22

33
import * as ACTIONS from '../actions/index';
4-
import { Action, Field, TrezorConnectDevice } from '../types';
4+
import type { MethodAction, TrezorConnectAction, ConnectOptions } from '../types/actions';
5+
import type { Field, TrezorConnectDevice } from '../types/common';
6+
7+
type Action = MethodAction | TrezorConnectAction;
58

69
export type ConnectState = {
710
devices: TrezorConnectDevice[];
811
selectedDevice?: string;
9-
options?: Parameters<(typeof TrezorConnect)['init']>[0];
12+
options?: ConnectOptions;
1013
isHandshakeConfirmed: boolean;
1114
isInitSuccess: boolean;
1215
initError?: string;
@@ -67,8 +70,8 @@ const onOptionChange = <T>(state: ConnectState, field: Field<T>, value: T): Conn
6770
},
6871
};
6972
}
70-
// @ts-expect-error field name must be key of options
71-
newState.options[field.name] = value;
73+
74+
(newState.options as any)[field.name] = value;
7275

7376
return newState;
7477
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type { TSchema } from '@sinclair/typebox';
2+
3+
import { DEVICE } from '@trezor/connect-web';
4+
import type { TrezorConnect as TrezorConnectType } from '@trezor/connect-web';
5+
6+
import type { Field, TrezorConnectDevice } from './common';
7+
8+
// Method action constants
9+
export const SET_METHOD = 'method_set';
10+
export const SET_SCHEMA = 'schema_set';
11+
export const FIELD_CHANGE = 'method_field_change';
12+
export const FIELD_DATA_CHANGE = 'method_field_data_change';
13+
export const ADD_BATCH = 'method_add_batch';
14+
export const REMOVE_BATCH = 'method_remove_batch';
15+
export const SET_UNION = 'method_set_union';
16+
export const RESPONSE = 'method_response';
17+
export const SET_MANUAL_MODE = 'method_set_manual_mode';
18+
export const SET_METHOD_PROCESSING = 'method_set_processing';
19+
20+
// TrezorConnect action constants
21+
export const ON_SELECT_DEVICE = 'action__on_select_device';
22+
export const ON_CHANGE_CONNECT_OPTIONS = 'action__on_change_connect_options';
23+
export const ON_CHANGE_CONNECT_OPTION = 'action__on_change_connect_option';
24+
export const ON_HANDSHAKE_CONFIRMED = 'action__on_handshake_confirmed';
25+
export const ON_INIT_ERROR = 'action__on_init_error';
26+
27+
// Method action types
28+
export type MethodAction =
29+
| { type: typeof SET_METHOD; methodConfig: any }
30+
| { type: typeof SET_SCHEMA; method: keyof TrezorConnectType; schema: TSchema }
31+
| { type: typeof FIELD_CHANGE; field: Field<any>; value: any }
32+
| { type: typeof FIELD_DATA_CHANGE; field: Field<any>; data: any }
33+
| { type: typeof ADD_BATCH; field: Field<any>; item: any }
34+
| { type: typeof REMOVE_BATCH; field: Field<any>; batch: any[] }
35+
| { type: typeof SET_UNION; field: Field<any>; current: any }
36+
| { type: typeof RESPONSE; response: any }
37+
| { type: typeof SET_MANUAL_MODE; manualMode: boolean }
38+
| { type: typeof SET_METHOD_PROCESSING; payload: boolean };
39+
40+
// TrezorConnect action types
41+
export type ConnectOptions = {
42+
manifest?: {
43+
email: string;
44+
appUrl: string;
45+
appName: string;
46+
appIcon?: string;
47+
};
48+
connectSrc?: string;
49+
coreMode?: 'auto' | 'popup' | 'deeplink';
50+
transportReconnect?: boolean;
51+
popup?: boolean;
52+
debug?: boolean;
53+
lazyLoad?: boolean;
54+
trustedHost?: boolean;
55+
[key: string]: any;
56+
};
57+
58+
export type TrezorConnectAction =
59+
| { type: typeof ON_SELECT_DEVICE; path: string }
60+
| { type: typeof DEVICE.CONNECT; device: TrezorConnectDevice }
61+
| { type: typeof DEVICE.CONNECT_UNACQUIRED; device: TrezorConnectDevice }
62+
| { type: typeof DEVICE.DISCONNECT; device: TrezorConnectDevice }
63+
| { type: typeof ON_CHANGE_CONNECT_OPTIONS; payload: ConnectOptions }
64+
| { type: typeof ON_HANDSHAKE_CONFIRMED }
65+
| { type: typeof ON_INIT_ERROR; payload: string }
66+
| {
67+
type: typeof ON_CHANGE_CONNECT_OPTION;
68+
payload: { option: Field<any>; value: any };
69+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { KnownDevice, UnknownDevice, UnreadableDevice } from '@trezor/connect-web';
2+
3+
export type TrezorConnectDevice = KnownDevice | UnknownDevice | UnreadableDevice;
4+
5+
export interface FieldData {
6+
value: string;
7+
label: string;
8+
affectedValue?: string;
9+
}
10+
11+
// Field path
12+
// - string: object name
13+
// - number: batch index
14+
export type FieldPath = (string | number)[];
15+
16+
export interface FieldCommon {
17+
path?: FieldPath;
18+
name: string;
19+
optional?: boolean;
20+
omit?: boolean;
21+
}
22+
23+
export interface FieldBasic<Value> extends FieldCommon {
24+
type:
25+
| 'input'
26+
| 'input-long'
27+
| 'select'
28+
| 'checkbox'
29+
| 'textarea'
30+
| 'number'
31+
| 'address'
32+
| 'json'
33+
| 'function'
34+
| 'file';
35+
value: Value;
36+
defaultValue?: Value;
37+
affect?: string;
38+
data?: FieldData[];
39+
}
40+
41+
interface Batch<Value> {
42+
type: string;
43+
fields: Field<Value>[];
44+
}
45+
46+
export interface FieldWithBundle<Value> extends FieldCommon {
47+
type: 'array';
48+
batch: Batch<Value>[];
49+
items: Field<Value>[][];
50+
affect?: undefined;
51+
}
52+
53+
export interface FieldWithUnion<Value> extends FieldCommon {
54+
type: 'union';
55+
labels: string[];
56+
options: Field<Value>[][];
57+
current: Field<Value>[];
58+
affect?: undefined;
59+
}
60+
61+
export type Field<Value> = FieldBasic<Value> | FieldWithBundle<Value> | FieldWithUnion<Value>;
62+
63+
export const isFieldBasic = <T>(field: Field<T>): field is FieldBasic<T> =>
64+
field.type !== 'array' && field.type !== 'union';

0 commit comments

Comments
 (0)