@onekeyfe/hd-core 0.1.26 → 0.1.29

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.
Files changed (45) hide show
  1. package/dist/api/RequestWebUsbDevice.d.ts +8 -0
  2. package/dist/api/RequestWebUsbDevice.d.ts.map +1 -0
  3. package/dist/api/SearchDevices.d.ts.map +1 -1
  4. package/dist/api/index.d.ts +1 -0
  5. package/dist/api/index.d.ts.map +1 -1
  6. package/dist/core/index.d.ts.map +1 -1
  7. package/dist/data-manager/TransportManager.d.ts.map +1 -1
  8. package/dist/device/Device.d.ts +1 -1
  9. package/dist/device/Device.d.ts.map +1 -1
  10. package/dist/device/DeviceCommands.d.ts.map +1 -1
  11. package/dist/device/DeviceConnector.d.ts +2 -13
  12. package/dist/device/DeviceConnector.d.ts.map +1 -1
  13. package/dist/device/DeviceList.d.ts.map +1 -1
  14. package/dist/device/DevicePool.d.ts +41 -0
  15. package/dist/device/DevicePool.d.ts.map +1 -0
  16. package/dist/events/device.d.ts +13 -1
  17. package/dist/events/device.d.ts.map +1 -1
  18. package/dist/index.d.ts +25 -5
  19. package/dist/index.js +347 -203
  20. package/dist/inject.d.ts.map +1 -1
  21. package/dist/types/api/index.d.ts +2 -0
  22. package/dist/types/api/index.d.ts.map +1 -1
  23. package/dist/types/api/requestWebUsbDevice.d.ts +6 -0
  24. package/dist/types/api/requestWebUsbDevice.d.ts.map +1 -0
  25. package/dist/types/settings.d.ts +1 -1
  26. package/dist/types/settings.d.ts.map +1 -1
  27. package/dist/utils/logger.d.ts +4 -0
  28. package/dist/utils/logger.d.ts.map +1 -1
  29. package/package.json +4 -4
  30. package/src/api/RequestWebUsbDevice.ts +46 -0
  31. package/src/api/SearchDevices.ts +3 -11
  32. package/src/api/index.ts +2 -0
  33. package/src/core/index.ts +25 -5
  34. package/src/data-manager/TransportManager.ts +3 -1
  35. package/src/device/Device.ts +2 -1
  36. package/src/device/DeviceCommands.ts +9 -0
  37. package/src/device/DeviceConnector.ts +7 -76
  38. package/src/device/DeviceList.ts +3 -44
  39. package/src/device/DevicePool.ts +243 -0
  40. package/src/events/device.ts +16 -1
  41. package/src/inject.ts +1 -0
  42. package/src/types/api/index.ts +3 -0
  43. package/src/types/api/requestWebUsbDevice.ts +4 -0
  44. package/src/types/settings.ts +1 -1
  45. package/src/utils/logger.ts +4 -0
package/dist/index.js CHANGED
@@ -75,6 +75,7 @@ const inject = ({ call, cancel, dispose, eventEmitter, init, uiResponse, }) => {
75
75
  stellarGetAddress: (connectId, deviceId, params) => call(Object.assign(Object.assign({}, params), { connectId, deviceId, method: 'stellarGetAddress' })),
76
76
  stellarSignTransaction: (connectId, deviceId, params) => call(Object.assign(Object.assign({}, params), { connectId, deviceId, method: 'stellarSignTransaction' })),
77
77
  firmwareUpdate: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'firmwareUpdate' })),
78
+ requestWebUsbDevice: () => call({ method: 'requestWebUsbDevice' }),
78
79
  };
79
80
  return api;
80
81
  };
@@ -9657,7 +9658,7 @@ const createLogMessage = (type, payload) => ({
9657
9658
 
9658
9659
  const MAX_ENTRIES = 500;
9659
9660
  let postMessage$1;
9660
- class Log$6 {
9661
+ class Log$7 {
9661
9662
  constructor(prefix, enabled) {
9662
9663
  this.prefix = prefix;
9663
9664
  this.enabled = enabled;
@@ -9709,7 +9710,7 @@ class Log$6 {
9709
9710
  }
9710
9711
  const _logs = {};
9711
9712
  const initLog = (prefix, enabled) => {
9712
- const instance = new Log$6(prefix, !!enabled);
9713
+ const instance = new Log$7(prefix, !!enabled);
9713
9714
  _logs[prefix] = instance;
9714
9715
  return instance;
9715
9716
  };
@@ -9770,6 +9771,8 @@ exports.LoggerNames = void 0;
9770
9771
  LoggerNames["DeviceCommands"] = "DeviceCommands";
9771
9772
  LoggerNames["DeviceConnector"] = "DeviceConnector";
9772
9773
  LoggerNames["DeviceList"] = "DeviceList";
9774
+ LoggerNames["DevicePool"] = "DevicePool";
9775
+ LoggerNames["HdCommonConnectSdk"] = "@onekey/common-connect-sdk";
9773
9776
  LoggerNames["HdBleSdk"] = "@onekey/hd-ble-sdk";
9774
9777
  LoggerNames["HdTransportHttp"] = "@onekey/hd-transport-http";
9775
9778
  LoggerNames["HdBleTransport"] = "@onekey/hd-ble-transport";
@@ -9785,6 +9788,7 @@ const LoggerMap = {
9785
9788
  [exports.LoggerNames.DeviceCommands]: initLog(exports.LoggerNames.DeviceCommands),
9786
9789
  [exports.LoggerNames.DeviceConnector]: initLog(exports.LoggerNames.DeviceConnector),
9787
9790
  [exports.LoggerNames.DeviceList]: initLog(exports.LoggerNames.DeviceList),
9791
+ [exports.LoggerNames.DevicePool]: initLog(exports.LoggerNames.DevicePool),
9788
9792
  [exports.LoggerNames.HdBleSdk]: initLog(exports.LoggerNames.HdBleSdk),
9789
9793
  [exports.LoggerNames.HdTransportHttp]: initLog(exports.LoggerNames.HdTransportHttp),
9790
9794
  [exports.LoggerNames.HdBleTransport]: initLog(exports.LoggerNames.HdBleTransport),
@@ -9792,6 +9796,7 @@ const LoggerMap = {
9792
9796
  [exports.LoggerNames.Iframe]: initLog(exports.LoggerNames.Iframe),
9793
9797
  [exports.LoggerNames.SendMessage]: initLog(exports.LoggerNames.SendMessage),
9794
9798
  [exports.LoggerNames.Method]: initLog(exports.LoggerNames.Method),
9799
+ [exports.LoggerNames.HdCommonConnectSdk]: initLog(exports.LoggerNames.Method),
9795
9800
  };
9796
9801
  const getLogger = (key) => LoggerMap[key];
9797
9802
 
@@ -9935,83 +9940,6 @@ DataManager.getTransportStatus = (localVersion) => {
9935
9940
  return isLatest ? 'valid' : 'outdated';
9936
9941
  };
9937
9942
 
9938
- const Log$5 = getLogger(exports.LoggerNames.Transport);
9939
- const BleLogger = getLogger(exports.LoggerNames.HdBleTransport);
9940
- const HttpLogger = getLogger(exports.LoggerNames.HdTransportHttp);
9941
- class TransportManager {
9942
- static load() {
9943
- Log$5.debug('transport manager load');
9944
- this.defaultMessages = DataManager.getProtobufMessages();
9945
- this.currentMessages = this.defaultMessages;
9946
- }
9947
- static configure() {
9948
- return __awaiter(this, void 0, void 0, function* () {
9949
- try {
9950
- const env = DataManager.getSettings('env');
9951
- Log$5.debug('Initializing transports');
9952
- if (env === 'react-native') {
9953
- if (!this.reactNativeInit) {
9954
- yield this.transport.init(BleLogger);
9955
- this.reactNativeInit = true;
9956
- }
9957
- else {
9958
- Log$5.debug('React Native Do Not Initializing transports');
9959
- }
9960
- }
9961
- else {
9962
- yield this.transport.init(HttpLogger);
9963
- }
9964
- Log$5.debug('Configuring transports');
9965
- yield this.transport.configure(JSON.stringify(this.defaultMessages));
9966
- Log$5.debug('Configuring transports done');
9967
- }
9968
- catch (error) {
9969
- Log$5.debug('Initializing transports error: ', error);
9970
- if (error.code === 'ECONNABORTED') {
9971
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BridgeTimeoutError);
9972
- }
9973
- }
9974
- });
9975
- }
9976
- static reconfigure(messages) {
9977
- return __awaiter(this, void 0, void 0, function* () {
9978
- if (Array.isArray(messages)) {
9979
- messages = DataManager.getProtobufMessages();
9980
- }
9981
- if (this.currentMessages === messages || !messages) {
9982
- return;
9983
- }
9984
- try {
9985
- yield this.transport.configure(JSON.stringify(messages));
9986
- this.currentMessages = messages;
9987
- }
9988
- catch (error) {
9989
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportInvalidProtobuf, `Transport_InvalidProtobuf: ${error.message}`);
9990
- }
9991
- });
9992
- }
9993
- static setTransport(TransportConstructor) {
9994
- const env = DataManager.getSettings('env');
9995
- if (env === 'react-native') {
9996
- this.transport = new TransportConstructor({ scanTimeout: 3000 });
9997
- }
9998
- else {
9999
- this.transport = new TransportConstructor();
10000
- }
10001
- Log$5.debug('set transport: ', this.transport);
10002
- }
10003
- static getTransport() {
10004
- return this.transport;
10005
- }
10006
- static getDefaultMessages() {
10007
- return this.defaultMessages;
10008
- }
10009
- static getCurrentMessages() {
10010
- return this.currentMessages;
10011
- }
10012
- }
10013
- TransportManager.reactNativeInit = false;
10014
-
10015
9943
  const CORE_EVENT = 'CORE_EVENT';
10016
9944
  const parseMessage = (messageData) => {
10017
9945
  const { data } = messageData;
@@ -10124,6 +10052,279 @@ const createFirmwareMessage = (type, payload) => ({
10124
10052
  payload,
10125
10053
  });
10126
10054
 
10055
+ const Log$6 = getLogger(exports.LoggerNames.DevicePool);
10056
+ const getDiff = (current, descriptors) => {
10057
+ const connected = descriptors.filter(d => current.find(x => x.path === d.path) === undefined);
10058
+ const disconnected = current.filter(d => descriptors.find(x => x.path === d.path) === undefined);
10059
+ const changedSessions = descriptors.filter(d => {
10060
+ const currentDescriptor = current.find(x => x.path === d.path);
10061
+ if (currentDescriptor) {
10062
+ return currentDescriptor.session !== d.session;
10063
+ }
10064
+ return false;
10065
+ });
10066
+ const acquired = changedSessions.filter(d => typeof d.session === 'string');
10067
+ const released = changedSessions.filter(d => typeof d.session !== 'string');
10068
+ const changedDebugSessions = descriptors.filter(d => {
10069
+ const currentDescriptor = current.find(x => x.path === d.path);
10070
+ if (currentDescriptor) {
10071
+ return currentDescriptor.debugSession !== d.debugSession;
10072
+ }
10073
+ return false;
10074
+ });
10075
+ const debugAcquired = changedSessions.filter(d => typeof d.debugSession === 'string');
10076
+ const debugReleased = changedSessions.filter(d => typeof d.debugSession !== 'string');
10077
+ const didUpdate = connected.length + disconnected.length + changedSessions.length + changedDebugSessions.length >
10078
+ 0;
10079
+ return {
10080
+ connected,
10081
+ disconnected,
10082
+ changedSessions,
10083
+ acquired,
10084
+ released,
10085
+ changedDebugSessions,
10086
+ debugAcquired,
10087
+ debugReleased,
10088
+ didUpdate,
10089
+ descriptors,
10090
+ };
10091
+ };
10092
+ class DevicePool extends events.exports {
10093
+ static setConnector(connector) {
10094
+ this.connector = connector;
10095
+ }
10096
+ static getDevices(descriptorList, connectId) {
10097
+ var descriptorList_1, descriptorList_1_1;
10098
+ var e_1, _a;
10099
+ return __awaiter(this, void 0, void 0, function* () {
10100
+ Log$6.debug('get device list');
10101
+ const devices = {};
10102
+ const deviceList = [];
10103
+ if (connectId) {
10104
+ const device = this.devicesCache[connectId];
10105
+ if (device) {
10106
+ const exist = descriptorList.find(d => d.path === device.originalDescriptor.path);
10107
+ if (exist) {
10108
+ Log$6.debug('find existed Device: ', connectId);
10109
+ device.updateDescriptor(exist, true);
10110
+ devices[connectId] = device;
10111
+ deviceList.push(device);
10112
+ yield this._checkDevicePool();
10113
+ return { devices, deviceList };
10114
+ }
10115
+ Log$6.debug('found device in cache, but path is different: ', connectId);
10116
+ }
10117
+ }
10118
+ try {
10119
+ for (descriptorList_1 = __asyncValues(descriptorList); descriptorList_1_1 = yield descriptorList_1.next(), !descriptorList_1_1.done;) {
10120
+ const descriptor = descriptorList_1_1.value;
10121
+ const device = yield this._createDevice(descriptor);
10122
+ if (device.features) {
10123
+ const uuid = getDeviceUUID(device.features);
10124
+ if (this.devicesCache[uuid]) {
10125
+ const cache = this.devicesCache[uuid];
10126
+ cache.updateDescriptor(descriptor, true);
10127
+ }
10128
+ this.devicesCache[uuid] = device;
10129
+ devices[uuid] = device;
10130
+ }
10131
+ deviceList.push(device);
10132
+ }
10133
+ }
10134
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
10135
+ finally {
10136
+ try {
10137
+ if (descriptorList_1_1 && !descriptorList_1_1.done && (_a = descriptorList_1.return)) yield _a.call(descriptorList_1);
10138
+ }
10139
+ finally { if (e_1) throw e_1.error; }
10140
+ }
10141
+ Log$6.debug('get devices result : ', devices, deviceList);
10142
+ console.log('device poll -> connected: ', this.connectedPool);
10143
+ console.log('device poll -> disconnected: ', this.disconnectPool);
10144
+ yield this._checkDevicePool();
10145
+ return { devices, deviceList };
10146
+ });
10147
+ }
10148
+ static _createDevice(descriptor) {
10149
+ return __awaiter(this, void 0, void 0, function* () {
10150
+ let device = this.getDeviceByPath(descriptor.path);
10151
+ if (!device) {
10152
+ device = Device.fromDescriptor(descriptor);
10153
+ device.deviceConnector = this.connector;
10154
+ yield device.connect();
10155
+ yield device.initialize();
10156
+ yield device.release();
10157
+ }
10158
+ return device;
10159
+ });
10160
+ }
10161
+ static _checkDevicePool() {
10162
+ return __awaiter(this, void 0, void 0, function* () {
10163
+ yield this._sendConnectMessage();
10164
+ this._sendDisconnectMessage();
10165
+ });
10166
+ }
10167
+ static _sendConnectMessage() {
10168
+ return __awaiter(this, void 0, void 0, function* () {
10169
+ for (let i = this.connectedPool.length - 1; i >= 0; i--) {
10170
+ const descriptor = this.connectedPool[i];
10171
+ const device = yield this._createDevice(descriptor);
10172
+ Log$6.debug('emit DEVICE.CONNECT: ', device);
10173
+ this.emitter.emit(DEVICE.CONNECT, device);
10174
+ this.connectedPool.splice(i, 1);
10175
+ }
10176
+ });
10177
+ }
10178
+ static _sendDisconnectMessage() {
10179
+ for (let i = this.disconnectPool.length - 1; i >= 0; i--) {
10180
+ const descriptor = this.connectedPool[i];
10181
+ const device = this.getDeviceByPath(descriptor.path);
10182
+ if (device) {
10183
+ this.emitter.emit(DEVICE.DISCONNECT, device);
10184
+ }
10185
+ this.disconnectPool.splice(i, 1);
10186
+ }
10187
+ }
10188
+ static reportDeviceChange(upcoming) {
10189
+ const diff = getDiff(this.current || [], upcoming);
10190
+ this.upcoming = upcoming;
10191
+ this.current = this.upcoming;
10192
+ console.log('device pool -> current: ', this.current);
10193
+ console.log('device pool -> upcomming: ', this.upcoming);
10194
+ console.log('DeviceCache.reportDeviceChange diff: ', diff);
10195
+ if (!diff.didUpdate) {
10196
+ return;
10197
+ }
10198
+ diff.connected.forEach(d => {
10199
+ const device = this.getDeviceByPath(d.path);
10200
+ if (!device) {
10201
+ this._addConnectedDeviceToPool(d);
10202
+ return;
10203
+ }
10204
+ Log$6.debug('emit DEVICE.CONNECT: ', device);
10205
+ this.emitter.emit(DEVICE.CONNECT, device);
10206
+ });
10207
+ diff.disconnected.forEach(d => {
10208
+ this._removeDeviceFromConnectedPool(d.path);
10209
+ const device = this.getDeviceByPath(d.path);
10210
+ if (!device) {
10211
+ this._addDisconnectedDeviceToPool(d);
10212
+ return;
10213
+ }
10214
+ Log$6.debug('emit DEVICE.DISCONNECT: ', device);
10215
+ this.emitter.emit(DEVICE.DISCONNECT, device);
10216
+ });
10217
+ }
10218
+ static getDeviceByPath(path) {
10219
+ return Object.values(this.devicesCache).find(d => d.originalDescriptor.path === path);
10220
+ }
10221
+ static _addConnectedDeviceToPool(descriptor) {
10222
+ const existDescriptorIndex = this.connectedPool.findIndex(d => d.path === descriptor.path);
10223
+ if (existDescriptorIndex > -1) {
10224
+ this.connectedPool.splice(existDescriptorIndex, 1, descriptor);
10225
+ return;
10226
+ }
10227
+ this.connectedPool.push(descriptor);
10228
+ }
10229
+ static _removeDeviceFromConnectedPool(path) {
10230
+ const index = this.connectedPool.findIndex(d => d.path === path);
10231
+ if (index > -1) {
10232
+ this.connectedPool.splice(index, 1);
10233
+ }
10234
+ }
10235
+ static _addDisconnectedDeviceToPool(descriptor) {
10236
+ const existDescriptorIndex = this.disconnectPool.findIndex(d => d.path === descriptor.path);
10237
+ if (existDescriptorIndex > -1) {
10238
+ this.disconnectPool.splice(existDescriptorIndex, 1, descriptor);
10239
+ return;
10240
+ }
10241
+ this.disconnectPool.push(descriptor);
10242
+ }
10243
+ }
10244
+ DevicePool.current = null;
10245
+ DevicePool.upcoming = [];
10246
+ DevicePool.connectedPool = [];
10247
+ DevicePool.disconnectPool = [];
10248
+ DevicePool.devicesCache = {};
10249
+ DevicePool.emitter = new events.exports();
10250
+
10251
+ const Log$5 = getLogger(exports.LoggerNames.Transport);
10252
+ const BleLogger = getLogger(exports.LoggerNames.HdBleTransport);
10253
+ const HttpLogger = getLogger(exports.LoggerNames.HdTransportHttp);
10254
+ class TransportManager {
10255
+ static load() {
10256
+ Log$5.debug('transport manager load');
10257
+ this.defaultMessages = DataManager.getProtobufMessages();
10258
+ this.currentMessages = this.defaultMessages;
10259
+ }
10260
+ static configure() {
10261
+ return __awaiter(this, void 0, void 0, function* () {
10262
+ try {
10263
+ const env = DataManager.getSettings('env');
10264
+ Log$5.debug('Initializing transports');
10265
+ if (env === 'react-native') {
10266
+ if (!this.reactNativeInit) {
10267
+ yield this.transport.init(BleLogger, DevicePool.emitter);
10268
+ this.reactNativeInit = true;
10269
+ }
10270
+ else {
10271
+ Log$5.debug('React Native Do Not Initializing transports');
10272
+ }
10273
+ }
10274
+ else {
10275
+ yield this.transport.init(HttpLogger);
10276
+ }
10277
+ Log$5.debug('Configuring transports');
10278
+ yield this.transport.configure(JSON.stringify(this.defaultMessages));
10279
+ Log$5.debug('Configuring transports done');
10280
+ }
10281
+ catch (error) {
10282
+ Log$5.debug('Initializing transports error: ', error);
10283
+ if (error.code === 'ECONNABORTED') {
10284
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BridgeTimeoutError);
10285
+ }
10286
+ }
10287
+ });
10288
+ }
10289
+ static reconfigure(messages) {
10290
+ return __awaiter(this, void 0, void 0, function* () {
10291
+ if (Array.isArray(messages)) {
10292
+ messages = DataManager.getProtobufMessages();
10293
+ }
10294
+ if (this.currentMessages === messages || !messages) {
10295
+ return;
10296
+ }
10297
+ try {
10298
+ yield this.transport.configure(JSON.stringify(messages));
10299
+ this.currentMessages = messages;
10300
+ }
10301
+ catch (error) {
10302
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportInvalidProtobuf, `Transport_InvalidProtobuf: ${error.message}`);
10303
+ }
10304
+ });
10305
+ }
10306
+ static setTransport(TransportConstructor) {
10307
+ const env = DataManager.getSettings('env');
10308
+ if (env === 'react-native') {
10309
+ this.transport = new TransportConstructor({ scanTimeout: 3000 });
10310
+ }
10311
+ else {
10312
+ this.transport = new TransportConstructor();
10313
+ }
10314
+ Log$5.debug('set transport: ', this.transport);
10315
+ }
10316
+ static getTransport() {
10317
+ return this.transport;
10318
+ }
10319
+ static getDefaultMessages() {
10320
+ return this.defaultMessages;
10321
+ }
10322
+ static getCurrentMessages() {
10323
+ return this.currentMessages;
10324
+ }
10325
+ }
10326
+ TransportManager.reactNativeInit = false;
10327
+
10127
10328
  const assertType = (res, resType) => {
10128
10329
  const splitResTypes = Array.isArray(resType) ? resType : resType.split('|');
10129
10330
  if (!splitResTypes.includes(res.type)) {
@@ -10240,7 +10441,11 @@ class DeviceCommands {
10240
10441
  return this._commonCall('ButtonAck', {});
10241
10442
  }
10242
10443
  if (res.type === 'EntropyRequest') ;
10444
+ const isWebusbEnv = DataManager.getSettings('env') === 'webusb';
10243
10445
  if (res.type === 'PinMatrixRequest') {
10446
+ if (isWebusbEnv) {
10447
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Please unlock your device'));
10448
+ }
10244
10449
  return this._promptPin(res.message.type).then(pin => {
10245
10450
  if (pin === '@@ONEKEY_INPUT_PIN_IN_DEVICE') {
10246
10451
  return this._commonCall('BixinPinInputOnDevice');
@@ -10711,62 +10916,19 @@ class Device extends events.exports {
10711
10916
  }
10712
10917
  }
10713
10918
 
10714
- const cacheDeviceMap = {};
10715
- const Log$2 = getLogger(exports.LoggerNames.DeviceList);
10716
10919
  class DeviceList extends events.exports {
10717
10920
  constructor() {
10718
10921
  super(...arguments);
10719
10922
  this.devices = {};
10720
10923
  }
10721
10924
  getDeviceLists(connectId) {
10722
- var e_1, _a;
10723
- var _b, _c;
10925
+ var _a, _b;
10724
10926
  return __awaiter(this, void 0, void 0, function* () {
10725
- const deviceDiff = yield ((_b = this.connector) === null || _b === void 0 ? void 0 : _b.enumerate());
10726
- const descriptorList = (_c = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _c !== void 0 ? _c : [];
10927
+ const deviceDiff = yield ((_a = this.connector) === null || _a === void 0 ? void 0 : _a.enumerate());
10928
+ const descriptorList = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
10727
10929
  this.devices = {};
10728
- const deviceList = [];
10729
- Log$2.debug('get device list');
10730
- if (connectId) {
10731
- const device = cacheDeviceMap[connectId];
10732
- if (device) {
10733
- const exist = descriptorList.find(d => d.path === device.originalDescriptor.path);
10734
- if (exist) {
10735
- device.updateDescriptor(exist, true);
10736
- Log$2.debug('find existed Device: ', connectId);
10737
- this.devices[connectId] = device;
10738
- return [device];
10739
- }
10740
- }
10741
- }
10742
- try {
10743
- for (var descriptorList_1 = __asyncValues(descriptorList), descriptorList_1_1; descriptorList_1_1 = yield descriptorList_1.next(), !descriptorList_1_1.done;) {
10744
- const descriptor = descriptorList_1_1.value;
10745
- let device = Device.fromDescriptor(descriptor);
10746
- device.deviceConnector = this.connector;
10747
- yield device.connect();
10748
- yield device.initialize();
10749
- yield device.release();
10750
- deviceList.push(device);
10751
- if (device.features) {
10752
- const uuid = getDeviceUUID(device.features);
10753
- if (cacheDeviceMap[uuid]) {
10754
- const cache = cacheDeviceMap[uuid];
10755
- cache === null || cache === void 0 ? void 0 : cache.updateFromCache(device);
10756
- device = cache;
10757
- }
10758
- this.devices[uuid] = device;
10759
- cacheDeviceMap[uuid] = device;
10760
- }
10761
- }
10762
- }
10763
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
10764
- finally {
10765
- try {
10766
- if (descriptorList_1_1 && !descriptorList_1_1.done && (_a = descriptorList_1.return)) yield _a.call(descriptorList_1);
10767
- }
10768
- finally { if (e_1) throw e_1.error; }
10769
- }
10930
+ const { deviceList, devices } = yield DevicePool.getDevices(descriptorList, connectId);
10931
+ this.devices = devices;
10770
10932
  return deviceList;
10771
10933
  });
10772
10934
  }
@@ -10845,12 +11007,11 @@ class SearchDevices extends BaseMethod {
10845
11007
  this.useDevice = false;
10846
11008
  }
10847
11009
  run() {
10848
- var e_1, _a;
10849
- var _b, _c;
11010
+ var _a, _b;
10850
11011
  return __awaiter(this, void 0, void 0, function* () {
10851
11012
  yield TransportManager.configure();
10852
- const deviceDiff = yield ((_b = this.connector) === null || _b === void 0 ? void 0 : _b.enumerate());
10853
- const devicesDescriptor = (_c = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _c !== void 0 ? _c : [];
11013
+ const deviceDiff = yield ((_a = this.connector) === null || _a === void 0 ? void 0 : _a.enumerate());
11014
+ const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
10854
11015
  const env = DataManager.getSettings('env');
10855
11016
  if (env === 'react-native') {
10856
11017
  return devicesDescriptor.map(device => {
@@ -10858,26 +11019,8 @@ class SearchDevices extends BaseMethod {
10858
11019
  return (Object.assign(Object.assign({}, device), { connectId: device.id, deviceType: getDeviceTypeByBleName((_a = device.name) !== null && _a !== void 0 ? _a : '') }));
10859
11020
  });
10860
11021
  }
10861
- const devices = [];
10862
- try {
10863
- for (var devicesDescriptor_1 = __asyncValues(devicesDescriptor), devicesDescriptor_1_1; devicesDescriptor_1_1 = yield devicesDescriptor_1.next(), !devicesDescriptor_1_1.done;) {
10864
- const descriptor = devicesDescriptor_1_1.value;
10865
- const device = Device.fromDescriptor(descriptor);
10866
- device.deviceConnector = this.connector;
10867
- yield device.connect();
10868
- yield device.initialize();
10869
- yield device.release();
10870
- devices.push(device);
10871
- }
10872
- }
10873
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
10874
- finally {
10875
- try {
10876
- if (devicesDescriptor_1_1 && !devicesDescriptor_1_1.done && (_a = devicesDescriptor_1.return)) yield _a.call(devicesDescriptor_1);
10877
- }
10878
- finally { if (e_1) throw e_1.error; }
10879
- }
10880
- return devices.map(device => device.toMessageObject());
11022
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor);
11023
+ return deviceList.map(device => device.toMessageObject());
10881
11024
  });
10882
11025
  }
10883
11026
  }
@@ -13481,6 +13624,36 @@ class FirmwareUpdate extends BaseMethod {
13481
13624
  }
13482
13625
  }
13483
13626
 
13627
+ const Log$2 = getLogger(exports.LoggerNames.Method);
13628
+ class RequestWebUsbDevice extends BaseMethod {
13629
+ init() {
13630
+ this.useDevice = false;
13631
+ }
13632
+ run() {
13633
+ var _a, _b;
13634
+ return __awaiter(this, void 0, void 0, function* () {
13635
+ yield TransportManager.configure();
13636
+ const env = DataManager.getSettings('env');
13637
+ if (env !== 'webusb') {
13638
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Not webusb environment'));
13639
+ }
13640
+ try {
13641
+ const deviceDiff = yield ((_a = this.connector) === null || _a === void 0 ? void 0 : _a.enumerate());
13642
+ const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
13643
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor);
13644
+ if (deviceList.length > 0) {
13645
+ return { device: deviceList[0].toMessageObject() };
13646
+ }
13647
+ return yield Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Please select the device to connect'));
13648
+ }
13649
+ catch (error) {
13650
+ Log$2.debug(error);
13651
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Please select the device to connect'));
13652
+ }
13653
+ });
13654
+ }
13655
+ }
13656
+
13484
13657
  class CheckBridgeStatus extends BaseMethod {
13485
13658
  init() {
13486
13659
  this.useDevice = false;
@@ -13537,6 +13710,7 @@ var ApiMethods = /*#__PURE__*/Object.freeze({
13537
13710
  stellarGetAddress: StellarGetAddress,
13538
13711
  stellarSignTransaction: StellarSignTransaction,
13539
13712
  firmwareUpdate: FirmwareUpdate,
13713
+ requestWebUsbDevice: RequestWebUsbDevice,
13540
13714
  getLogs: CheckBridgeStatus
13541
13715
  });
13542
13716
 
@@ -13572,48 +13746,6 @@ const resolveAfter = (msec, value) => new Promise(resolve => {
13572
13746
  });
13573
13747
 
13574
13748
  const Log$1 = getLogger(exports.LoggerNames.DeviceConnector);
13575
- const getDiff = (current, descriptors) => {
13576
- const env = DataManager.getSettings('env');
13577
- if (env === 'react-native') {
13578
- return {
13579
- descriptors,
13580
- };
13581
- }
13582
- const connected = descriptors.filter(d => current.find(x => x.path === d.path) === undefined);
13583
- const disconnected = current.filter(d => descriptors.find(x => x.path === d.path) === undefined);
13584
- const changedSessions = descriptors.filter(d => {
13585
- const currentDescriptor = current.find(x => x.path === d.path);
13586
- if (currentDescriptor) {
13587
- return currentDescriptor.session !== d.session;
13588
- }
13589
- return false;
13590
- });
13591
- const acquired = changedSessions.filter(d => typeof d.session === 'string');
13592
- const released = changedSessions.filter(d => typeof d.session !== 'string');
13593
- const changedDebugSessions = descriptors.filter(d => {
13594
- const currentDescriptor = current.find(x => x.path === d.path);
13595
- if (currentDescriptor) {
13596
- return currentDescriptor.debugSession !== d.debugSession;
13597
- }
13598
- return false;
13599
- });
13600
- const debugAcquired = changedSessions.filter(d => typeof d.debugSession === 'string');
13601
- const debugReleased = changedSessions.filter(d => typeof d.debugSession !== 'string');
13602
- const didUpdate = connected.length + disconnected.length + changedSessions.length + changedDebugSessions.length >
13603
- 0;
13604
- return {
13605
- connected,
13606
- disconnected,
13607
- changedSessions,
13608
- acquired,
13609
- released,
13610
- changedDebugSessions,
13611
- debugAcquired,
13612
- debugReleased,
13613
- didUpdate,
13614
- descriptors,
13615
- };
13616
- };
13617
13749
  class DeviceConnector {
13618
13750
  constructor() {
13619
13751
  this.listenTimestamp = 0;
@@ -13622,14 +13754,15 @@ class DeviceConnector {
13622
13754
  this.listening = false;
13623
13755
  TransportManager.load();
13624
13756
  this.transport = TransportManager.getTransport();
13757
+ DevicePool.setConnector(this);
13625
13758
  }
13626
13759
  enumerate() {
13627
13760
  return __awaiter(this, void 0, void 0, function* () {
13628
13761
  try {
13629
- this.upcoming = yield this.transport.enumerate();
13630
- const diff = this._reportDevicesChange();
13631
- Log$1.debug('diff result: ', diff);
13632
- return diff;
13762
+ const descriptors = yield this.transport.enumerate();
13763
+ this.upcoming = descriptors;
13764
+ this._reportDevicesChange();
13765
+ return { descriptors };
13633
13766
  }
13634
13767
  catch (error) {
13635
13768
  safeThrowError(error);
@@ -13704,9 +13837,7 @@ class DeviceConnector {
13704
13837
  });
13705
13838
  }
13706
13839
  _reportDevicesChange() {
13707
- const diff = getDiff(this.current || [], this.upcoming);
13708
- this.current = this.upcoming;
13709
- return diff;
13840
+ DevicePool.reportDeviceChange(this.upcoming);
13710
13841
  }
13711
13842
  }
13712
13843
 
@@ -13736,6 +13867,7 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13736
13867
  catch (error) {
13737
13868
  return Promise.reject(error);
13738
13869
  }
13870
+ DevicePool.emitter.on(DEVICE.CONNECT, onDeviceConnectHandler);
13739
13871
  if (!method.useDevice) {
13740
13872
  try {
13741
13873
  const response = yield method.run();
@@ -13775,12 +13907,12 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13775
13907
  if (versionRange && device.features) {
13776
13908
  const currentVersion = getDeviceFirmwareVersion(device.features).join('.');
13777
13909
  if (semver__default["default"].valid(versionRange.min) && semver__default["default"].lt(currentVersion, versionRange.min)) {
13778
- return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceFwException, `Device firmware version is too low, please update to ${versionRange.min}`));
13910
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodNeedUpgradeFirmware, `Device firmware version is too low, please update to ${versionRange.min}`, { current: currentVersion, require: versionRange.min }));
13779
13911
  }
13780
13912
  if (versionRange.max &&
13781
13913
  semver__default["default"].valid(versionRange.max) &&
13782
- semver__default["default"].gt(currentVersion, versionRange.max)) {
13783
- return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceFwException, `Device firmware version is too high, this method has been deprecated in ${versionRange.max}`));
13914
+ semver__default["default"].gte(currentVersion, versionRange.max)) {
13915
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodDeprecated, `Device firmware version is too high, this method has been deprecated in ${versionRange.max}`, { current: currentVersion, deprecated: versionRange.max }));
13784
13916
  }
13785
13917
  }
13786
13918
  const unexpectedMode = device.hasUnexpectedMode(method.allowDeviceMode, method.requireDeviceMode);
@@ -14014,10 +14146,21 @@ const removeDeviceListener = (device) => {
14014
14146
  device.removeListener(DEVICE.PIN, onDevicePinHandler);
14015
14147
  device.removeListener(DEVICE.BUTTON, onDeviceButtonHandler);
14016
14148
  device.removeListener(DEVICE.FEATURES, onDeviceFeaturesHandler);
14149
+ DevicePool.emitter.removeListener(DEVICE.CONNECT, onDeviceConnectHandler);
14017
14150
  };
14018
14151
  const closePopup = () => {
14019
14152
  postMessage(createUiMessage(UI_REQUEST$1.CLOSE_UI_WINDOW));
14020
14153
  };
14154
+ const onDeviceConnectHandler = (device) => {
14155
+ const env = DataManager.getSettings('env');
14156
+ const deviceObject = env === 'react-native' ? device : device.toMessageObject();
14157
+ postMessage(createDeviceMessage(DEVICE.CONNECT, { device: deviceObject }));
14158
+ };
14159
+ const onDeviceDisconnectHandler = (device) => {
14160
+ const env = DataManager.getSettings('env');
14161
+ const deviceObject = env === 'react-native' ? device : device.toMessageObject();
14162
+ postMessage(createDeviceMessage(DEVICE.DISCONNECT, { device: deviceObject }));
14163
+ };
14021
14164
  const onDevicePinHandler = (...[device, type, callback]) => __awaiter(void 0, void 0, void 0, function* () {
14022
14165
  Log.debug('onDevicePinHandler');
14023
14166
  const uiPromise = createUiPromise(UI_RESPONSE.RECEIVE_PIN, device);
@@ -14093,6 +14236,7 @@ const initCore = () => {
14093
14236
  };
14094
14237
  const initConnector = () => {
14095
14238
  _connector = new DeviceConnector();
14239
+ DevicePool.emitter.on(DEVICE.DISCONNECT, onDeviceDisconnectHandler);
14096
14240
  return _connector;
14097
14241
  };
14098
14242
  const initTransport = (Transport) => {