@onekeyfe/hd-core 0.1.27 → 0.1.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/RequestWebUsbDevice.d.ts +8 -0
- package/dist/api/RequestWebUsbDevice.d.ts.map +1 -0
- package/dist/api/SearchDevices.d.ts.map +1 -1
- package/dist/api/evm/EVMSignTransaction.d.ts +1 -1
- package/dist/api/evm/EVMSignTransaction.d.ts.map +1 -1
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/data-manager/TransportManager.d.ts.map +1 -1
- package/dist/device/Device.d.ts +1 -1
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/device/DeviceCommands.d.ts.map +1 -1
- package/dist/device/DeviceConnector.d.ts +2 -13
- package/dist/device/DeviceConnector.d.ts.map +1 -1
- package/dist/device/DeviceList.d.ts.map +1 -1
- package/dist/device/DevicePool.d.ts +41 -0
- package/dist/device/DevicePool.d.ts.map +1 -0
- package/dist/events/device.d.ts +13 -1
- package/dist/events/device.d.ts.map +1 -1
- package/dist/index.d.ts +25 -5
- package/dist/index.js +348 -204
- package/dist/inject.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +2 -0
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/api/requestWebUsbDevice.d.ts +6 -0
- package/dist/types/api/requestWebUsbDevice.d.ts.map +1 -0
- package/dist/types/settings.d.ts +1 -1
- package/dist/types/settings.d.ts.map +1 -1
- package/dist/utils/logger.d.ts +4 -0
- package/dist/utils/logger.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/RequestWebUsbDevice.ts +46 -0
- package/src/api/SearchDevices.ts +3 -11
- package/src/api/evm/EVMSignTransaction.ts +2 -2
- package/src/api/index.ts +2 -0
- package/src/core/index.ts +25 -5
- package/src/data-manager/TransportManager.ts +3 -1
- package/src/device/Device.ts +2 -1
- package/src/device/DeviceCommands.ts +9 -0
- package/src/device/DeviceConnector.ts +7 -76
- package/src/device/DeviceList.ts +3 -44
- package/src/device/DevicePool.ts +243 -0
- package/src/events/device.ts +16 -1
- package/src/inject.ts +1 -0
- package/src/types/api/index.ts +3 -0
- package/src/types/api/requestWebUsbDevice.ts +4 -0
- package/src/types/settings.ts +1 -1
- package/src/utils/logger.ts +4 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { OneKeyDeviceInfo as DeviceDescriptor } from '@onekeyfe/hd-transport';
|
|
3
|
+
// eslint-disable-next-line import/no-cycle
|
|
4
|
+
import { Device } from './Device';
|
|
5
|
+
import { DEVICE } from '../events';
|
|
6
|
+
import type DeviceConnector from './DeviceConnector';
|
|
7
|
+
import { getDeviceUUID, getLogger, LoggerNames } from '../utils';
|
|
8
|
+
|
|
9
|
+
const Log = getLogger(LoggerNames.DevicePool);
|
|
10
|
+
|
|
11
|
+
export type DeviceDescriptorDiff = {
|
|
12
|
+
didUpdate: boolean;
|
|
13
|
+
connected: DeviceDescriptor[];
|
|
14
|
+
disconnected: DeviceDescriptor[];
|
|
15
|
+
changedSessions: DeviceDescriptor[];
|
|
16
|
+
changedDebugSessions: DeviceDescriptor[];
|
|
17
|
+
acquired: DeviceDescriptor[];
|
|
18
|
+
debugAcquired: DeviceDescriptor[];
|
|
19
|
+
released: DeviceDescriptor[];
|
|
20
|
+
debugReleased: DeviceDescriptor[];
|
|
21
|
+
descriptors: DeviceDescriptor[];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const getDiff = (
|
|
25
|
+
current: DeviceDescriptor[],
|
|
26
|
+
descriptors: DeviceDescriptor[]
|
|
27
|
+
): DeviceDescriptorDiff => {
|
|
28
|
+
const connected = descriptors.filter(d => current.find(x => x.path === d.path) === undefined);
|
|
29
|
+
const disconnected = current.filter(d => descriptors.find(x => x.path === d.path) === undefined);
|
|
30
|
+
const changedSessions = descriptors.filter(d => {
|
|
31
|
+
const currentDescriptor = current.find(x => x.path === d.path);
|
|
32
|
+
if (currentDescriptor) {
|
|
33
|
+
// return currentDescriptor.debug ? (currentDescriptor.debugSession !== d.debugSession) : (currentDescriptor.session !== d.session);
|
|
34
|
+
return currentDescriptor.session !== d.session;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
});
|
|
38
|
+
const acquired = changedSessions.filter(d => typeof d.session === 'string');
|
|
39
|
+
const released = changedSessions.filter(
|
|
40
|
+
d =>
|
|
41
|
+
// const session = descriptor.debug ? descriptor.debugSession : descriptor.session;
|
|
42
|
+
typeof d.session !== 'string'
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const changedDebugSessions = descriptors.filter(d => {
|
|
46
|
+
const currentDescriptor = current.find(x => x.path === d.path);
|
|
47
|
+
if (currentDescriptor) {
|
|
48
|
+
return currentDescriptor.debugSession !== d.debugSession;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
});
|
|
52
|
+
const debugAcquired = changedSessions.filter(d => typeof d.debugSession === 'string');
|
|
53
|
+
const debugReleased = changedSessions.filter(d => typeof d.debugSession !== 'string');
|
|
54
|
+
|
|
55
|
+
const didUpdate =
|
|
56
|
+
connected.length + disconnected.length + changedSessions.length + changedDebugSessions.length >
|
|
57
|
+
0;
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
connected,
|
|
61
|
+
disconnected,
|
|
62
|
+
changedSessions,
|
|
63
|
+
acquired,
|
|
64
|
+
released,
|
|
65
|
+
changedDebugSessions,
|
|
66
|
+
debugAcquired,
|
|
67
|
+
debugReleased,
|
|
68
|
+
didUpdate,
|
|
69
|
+
descriptors,
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export class DevicePool extends EventEmitter {
|
|
74
|
+
static current: DeviceDescriptor[] | null = null;
|
|
75
|
+
|
|
76
|
+
static upcoming: DeviceDescriptor[] = [];
|
|
77
|
+
|
|
78
|
+
static connectedPool: DeviceDescriptor[] = [];
|
|
79
|
+
|
|
80
|
+
static disconnectPool: DeviceDescriptor[] = [];
|
|
81
|
+
|
|
82
|
+
static devicesCache: Record<string, Device> = {};
|
|
83
|
+
|
|
84
|
+
static emitter = new EventEmitter();
|
|
85
|
+
|
|
86
|
+
static connector: DeviceConnector;
|
|
87
|
+
|
|
88
|
+
static setConnector(connector: DeviceConnector) {
|
|
89
|
+
this.connector = connector;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static async getDevices(descriptorList: DeviceDescriptor[], connectId?: string) {
|
|
93
|
+
Log.debug('get device list');
|
|
94
|
+
|
|
95
|
+
const devices: Record<string, Device> = {};
|
|
96
|
+
const deviceList = [];
|
|
97
|
+
|
|
98
|
+
// If there is a connectId
|
|
99
|
+
// it means that you only want to get data from cache
|
|
100
|
+
if (connectId) {
|
|
101
|
+
const device = this.devicesCache[connectId];
|
|
102
|
+
if (device) {
|
|
103
|
+
const exist = descriptorList.find(d => d.path === device.originalDescriptor.path);
|
|
104
|
+
if (exist) {
|
|
105
|
+
Log.debug('find existed Device: ', connectId);
|
|
106
|
+
device.updateDescriptor(exist, true);
|
|
107
|
+
devices[connectId] = device;
|
|
108
|
+
deviceList.push(device);
|
|
109
|
+
await this._checkDevicePool();
|
|
110
|
+
return { devices, deviceList };
|
|
111
|
+
}
|
|
112
|
+
Log.debug('found device in cache, but path is different: ', connectId);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for await (const descriptor of descriptorList) {
|
|
117
|
+
const device = await this._createDevice(descriptor);
|
|
118
|
+
|
|
119
|
+
if (device.features) {
|
|
120
|
+
const uuid = getDeviceUUID(device.features);
|
|
121
|
+
if (this.devicesCache[uuid]) {
|
|
122
|
+
const cache = this.devicesCache[uuid];
|
|
123
|
+
cache.updateDescriptor(descriptor, true);
|
|
124
|
+
}
|
|
125
|
+
this.devicesCache[uuid] = device;
|
|
126
|
+
devices[uuid] = device;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
deviceList.push(device);
|
|
130
|
+
}
|
|
131
|
+
Log.debug('get devices result : ', devices, deviceList);
|
|
132
|
+
console.log('device poll -> connected: ', this.connectedPool);
|
|
133
|
+
console.log('device poll -> disconnected: ', this.disconnectPool);
|
|
134
|
+
await this._checkDevicePool();
|
|
135
|
+
return { devices, deviceList };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static async _createDevice(descriptor: DeviceDescriptor) {
|
|
139
|
+
let device = this.getDeviceByPath(descriptor.path);
|
|
140
|
+
if (!device) {
|
|
141
|
+
device = Device.fromDescriptor(descriptor);
|
|
142
|
+
device.deviceConnector = this.connector;
|
|
143
|
+
await device.connect();
|
|
144
|
+
await device.initialize();
|
|
145
|
+
await device.release();
|
|
146
|
+
}
|
|
147
|
+
return device;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
static async _checkDevicePool() {
|
|
151
|
+
await this._sendConnectMessage();
|
|
152
|
+
this._sendDisconnectMessage();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static async _sendConnectMessage() {
|
|
156
|
+
for (let i = this.connectedPool.length - 1; i >= 0; i--) {
|
|
157
|
+
const descriptor = this.connectedPool[i];
|
|
158
|
+
const device = await this._createDevice(descriptor);
|
|
159
|
+
Log.debug('emit DEVICE.CONNECT: ', device);
|
|
160
|
+
this.emitter.emit(DEVICE.CONNECT, device);
|
|
161
|
+
this.connectedPool.splice(i, 1);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static _sendDisconnectMessage() {
|
|
166
|
+
for (let i = this.disconnectPool.length - 1; i >= 0; i--) {
|
|
167
|
+
const descriptor = this.connectedPool[i];
|
|
168
|
+
const device = this.getDeviceByPath(descriptor.path);
|
|
169
|
+
if (device) {
|
|
170
|
+
this.emitter.emit(DEVICE.DISCONNECT, device);
|
|
171
|
+
}
|
|
172
|
+
this.disconnectPool.splice(i, 1);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
static reportDeviceChange(upcoming: DeviceDescriptor[]) {
|
|
177
|
+
const diff = getDiff(this.current || [], upcoming);
|
|
178
|
+
|
|
179
|
+
this.upcoming = upcoming;
|
|
180
|
+
this.current = this.upcoming;
|
|
181
|
+
|
|
182
|
+
console.log('device pool -> current: ', this.current);
|
|
183
|
+
console.log('device pool -> upcomming: ', this.upcoming);
|
|
184
|
+
console.log('DeviceCache.reportDeviceChange diff: ', diff);
|
|
185
|
+
|
|
186
|
+
if (!diff.didUpdate) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
diff.connected.forEach(d => {
|
|
191
|
+
const device = this.getDeviceByPath(d.path);
|
|
192
|
+
if (!device) {
|
|
193
|
+
this._addConnectedDeviceToPool(d);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
Log.debug('emit DEVICE.CONNECT: ', device);
|
|
197
|
+
this.emitter.emit(DEVICE.CONNECT, device);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
diff.disconnected.forEach(d => {
|
|
201
|
+
this._removeDeviceFromConnectedPool(d.path);
|
|
202
|
+
const device = this.getDeviceByPath(d.path);
|
|
203
|
+
if (!device) {
|
|
204
|
+
this._addDisconnectedDeviceToPool(d);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
Log.debug('emit DEVICE.DISCONNECT: ', device);
|
|
209
|
+
this.emitter.emit(DEVICE.DISCONNECT, device);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static getDeviceByPath(path: string) {
|
|
214
|
+
return Object.values(this.devicesCache).find(d => d.originalDescriptor.path === path);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
static _addConnectedDeviceToPool(descriptor: DeviceDescriptor) {
|
|
218
|
+
const existDescriptorIndex = this.connectedPool.findIndex(d => d.path === descriptor.path);
|
|
219
|
+
if (existDescriptorIndex > -1) {
|
|
220
|
+
this.connectedPool.splice(existDescriptorIndex, 1, descriptor);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
this.connectedPool.push(descriptor);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static _removeDeviceFromConnectedPool(path: string) {
|
|
228
|
+
const index = this.connectedPool.findIndex(d => d.path === path);
|
|
229
|
+
if (index > -1) {
|
|
230
|
+
this.connectedPool.splice(index, 1);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
static _addDisconnectedDeviceToPool(descriptor: DeviceDescriptor) {
|
|
235
|
+
const existDescriptorIndex = this.disconnectPool.findIndex(d => d.path === descriptor.path);
|
|
236
|
+
if (existDescriptorIndex > -1) {
|
|
237
|
+
this.disconnectPool.splice(existDescriptorIndex, 1, descriptor);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
this.disconnectPool.push(descriptor);
|
|
242
|
+
}
|
|
243
|
+
}
|
package/src/events/device.ts
CHANGED
|
@@ -29,6 +29,16 @@ export const DEVICE = {
|
|
|
29
29
|
FEATURES: 'features',
|
|
30
30
|
} as const;
|
|
31
31
|
|
|
32
|
+
export interface DeviceConnnectRequest {
|
|
33
|
+
type: typeof DEVICE.CONNECT;
|
|
34
|
+
payload: { device: Device };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface DeviceDisconnnectRequest {
|
|
38
|
+
type: typeof DEVICE.DISCONNECT;
|
|
39
|
+
payload: { device: Device };
|
|
40
|
+
}
|
|
41
|
+
|
|
32
42
|
export interface DeviceButtonRequestPayload extends Omit<PROTO.ButtonRequest, 'code'> {
|
|
33
43
|
code?: PROTO.ButtonRequest['code'] | 'ButtonRequest_FirmwareUpdate';
|
|
34
44
|
}
|
|
@@ -51,7 +61,12 @@ export interface DeviceSendSupportFeatures {
|
|
|
51
61
|
payload: DeviceSupportFeaturesPayload;
|
|
52
62
|
}
|
|
53
63
|
|
|
54
|
-
export type DeviceEvent =
|
|
64
|
+
export type DeviceEvent =
|
|
65
|
+
| DeviceButtonRequest
|
|
66
|
+
| DeviceSendFeatures
|
|
67
|
+
| DeviceSendSupportFeatures
|
|
68
|
+
| DeviceDisconnnectRequest
|
|
69
|
+
| DeviceConnnectRequest;
|
|
55
70
|
|
|
56
71
|
export type DeviceEventMessage = DeviceEvent & { event: typeof DEVICE_EVENT };
|
|
57
72
|
|
package/src/inject.ts
CHANGED
|
@@ -142,6 +142,7 @@ export const inject = ({
|
|
|
142
142
|
call({ ...params, connectId, deviceId, method: 'stellarSignTransaction' }),
|
|
143
143
|
|
|
144
144
|
firmwareUpdate: (connectId, params) => call({ ...params, connectId, method: 'firmwareUpdate' }),
|
|
145
|
+
requestWebUsbDevice: () => call({ method: 'requestWebUsbDevice' }),
|
|
145
146
|
};
|
|
146
147
|
return api;
|
|
147
148
|
};
|
package/src/types/api/index.ts
CHANGED
|
@@ -44,6 +44,7 @@ import { cipherKeyValue } from './cipherKeyValue';
|
|
|
44
44
|
import { firmwareUpdate } from './firmwareUpdate';
|
|
45
45
|
import { getLogs } from './getLogs';
|
|
46
46
|
import { deviceSupportFeatures } from './deviceSupportFeatures';
|
|
47
|
+
import { requestWebUsbDevice } from './requestWebUsbDevice';
|
|
47
48
|
|
|
48
49
|
export * from './export';
|
|
49
50
|
|
|
@@ -66,6 +67,8 @@ export type CoreApi = {
|
|
|
66
67
|
*/
|
|
67
68
|
searchDevices: typeof searchDevices;
|
|
68
69
|
|
|
70
|
+
requestWebUsbDevice: typeof requestWebUsbDevice;
|
|
71
|
+
|
|
69
72
|
getFeatures: typeof getFeatures;
|
|
70
73
|
|
|
71
74
|
checkFirmwareRelease: typeof checkFirmwareRelease;
|
package/src/types/settings.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type ConnectSettings = {
|
|
|
14
14
|
priority: number;
|
|
15
15
|
trustedHost: boolean;
|
|
16
16
|
supportedBrowser?: boolean;
|
|
17
|
-
env: 'node' | 'web' | 'webextension' | 'electron' | 'react-native';
|
|
17
|
+
env: 'node' | 'web' | 'webextension' | 'electron' | 'react-native' | 'webusb';
|
|
18
18
|
timestamp: number;
|
|
19
19
|
isFrame?: boolean;
|
|
20
20
|
};
|
package/src/utils/logger.ts
CHANGED
|
@@ -151,6 +151,8 @@ export enum LoggerNames {
|
|
|
151
151
|
DeviceCommands = 'DeviceCommands',
|
|
152
152
|
DeviceConnector = 'DeviceConnector',
|
|
153
153
|
DeviceList = 'DeviceList',
|
|
154
|
+
DevicePool = 'DevicePool',
|
|
155
|
+
HdCommonConnectSdk = '@onekey/common-connect-sdk',
|
|
154
156
|
HdBleSdk = '@onekey/hd-ble-sdk',
|
|
155
157
|
HdTransportHttp = '@onekey/hd-transport-http',
|
|
156
158
|
HdBleTransport = '@onekey/hd-ble-transport',
|
|
@@ -167,6 +169,7 @@ export const LoggerMap = {
|
|
|
167
169
|
[LoggerNames.DeviceCommands]: initLog(LoggerNames.DeviceCommands),
|
|
168
170
|
[LoggerNames.DeviceConnector]: initLog(LoggerNames.DeviceConnector),
|
|
169
171
|
[LoggerNames.DeviceList]: initLog(LoggerNames.DeviceList),
|
|
172
|
+
[LoggerNames.DevicePool]: initLog(LoggerNames.DevicePool),
|
|
170
173
|
[LoggerNames.HdBleSdk]: initLog(LoggerNames.HdBleSdk),
|
|
171
174
|
[LoggerNames.HdTransportHttp]: initLog(LoggerNames.HdTransportHttp),
|
|
172
175
|
[LoggerNames.HdBleTransport]: initLog(LoggerNames.HdBleTransport),
|
|
@@ -174,6 +177,7 @@ export const LoggerMap = {
|
|
|
174
177
|
[LoggerNames.Iframe]: initLog(LoggerNames.Iframe),
|
|
175
178
|
[LoggerNames.SendMessage]: initLog(LoggerNames.SendMessage),
|
|
176
179
|
[LoggerNames.Method]: initLog(LoggerNames.Method),
|
|
180
|
+
[LoggerNames.HdCommonConnectSdk]: initLog(LoggerNames.Method),
|
|
177
181
|
};
|
|
178
182
|
|
|
179
183
|
export const getLogger = (key: LoggerNames) => LoggerMap[key];
|