@onekeyfe/hd-web-sdk 0.1.27 → 0.1.28

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.
@@ -4263,7 +4263,10 @@ const inject = ({
4263
4263
  firmwareUpdate: (connectId, params) => call(Object.assign(Object.assign({}, params), {
4264
4264
  connectId,
4265
4265
  method: 'firmwareUpdate'
4266
- }))
4266
+ })),
4267
+ requestWebUsbDevice: () => call({
4268
+ method: 'requestWebUsbDevice'
4269
+ })
4267
4270
  };
4268
4271
  return api;
4269
4272
  };
@@ -13827,7 +13830,7 @@ const createLogMessage = (type, payload) => ({
13827
13830
  const MAX_ENTRIES = 500;
13828
13831
  let postMessage$1;
13829
13832
 
13830
- class Log$6 {
13833
+ class Log$7 {
13831
13834
  constructor(prefix, enabled) {
13832
13835
  this.prefix = prefix;
13833
13836
  this.enabled = enabled;
@@ -13896,7 +13899,7 @@ class Log$6 {
13896
13899
  const _logs = {};
13897
13900
 
13898
13901
  const initLog = (prefix, enabled) => {
13899
- const instance = new Log$6(prefix, !!enabled);
13902
+ const instance = new Log$7(prefix, !!enabled);
13900
13903
  _logs[prefix] = instance;
13901
13904
  return instance;
13902
13905
  };
@@ -13972,6 +13975,8 @@ exports.d0 = void 0;
13972
13975
  LoggerNames["DeviceCommands"] = "DeviceCommands";
13973
13976
  LoggerNames["DeviceConnector"] = "DeviceConnector";
13974
13977
  LoggerNames["DeviceList"] = "DeviceList";
13978
+ LoggerNames["DevicePool"] = "DevicePool";
13979
+ LoggerNames["HdCommonConnectSdk"] = "@onekey/common-connect-sdk";
13975
13980
  LoggerNames["HdBleSdk"] = "@onekey/hd-ble-sdk";
13976
13981
  LoggerNames["HdTransportHttp"] = "@onekey/hd-transport-http";
13977
13982
  LoggerNames["HdBleTransport"] = "@onekey/hd-ble-transport";
@@ -13988,13 +13993,15 @@ const LoggerMap = {
13988
13993
  [exports.d0.DeviceCommands]: initLog(exports.d0.DeviceCommands),
13989
13994
  [exports.d0.DeviceConnector]: initLog(exports.d0.DeviceConnector),
13990
13995
  [exports.d0.DeviceList]: initLog(exports.d0.DeviceList),
13996
+ [exports.d0.DevicePool]: initLog(exports.d0.DevicePool),
13991
13997
  [exports.d0.HdBleSdk]: initLog(exports.d0.HdBleSdk),
13992
13998
  [exports.d0.HdTransportHttp]: initLog(exports.d0.HdTransportHttp),
13993
13999
  [exports.d0.HdBleTransport]: initLog(exports.d0.HdBleTransport),
13994
14000
  [exports.d0.Connect]: initLog(exports.d0.Connect),
13995
14001
  [exports.d0.Iframe]: initLog(exports.d0.Iframe),
13996
14002
  [exports.d0.SendMessage]: initLog(exports.d0.SendMessage),
13997
- [exports.d0.Method]: initLog(exports.d0.Method)
14003
+ [exports.d0.Method]: initLog(exports.d0.Method),
14004
+ [exports.d0.HdCommonConnectSdk]: initLog(exports.d0.Method)
13998
14005
  };
13999
14006
 
14000
14007
  const getLogger = key => LoggerMap[key];
@@ -14171,95 +14178,6 @@ DataManager.getTransportStatus = localVersion => {
14171
14178
  return isLatest ? 'valid' : 'outdated';
14172
14179
  };
14173
14180
 
14174
- const Log$5 = getLogger(exports.d0.Transport);
14175
- const BleLogger = getLogger(exports.d0.HdBleTransport);
14176
- const HttpLogger = getLogger(exports.d0.HdTransportHttp);
14177
-
14178
- class TransportManager {
14179
- static load() {
14180
- Log$5.debug('transport manager load');
14181
- this.defaultMessages = DataManager.getProtobufMessages();
14182
- this.currentMessages = this.defaultMessages;
14183
- }
14184
-
14185
- static configure() {
14186
- return __awaiter(this, void 0, void 0, function* () {
14187
- try {
14188
- const env = DataManager.getSettings('env');
14189
- Log$5.debug('Initializing transports');
14190
-
14191
- if (env === 'react-native') {
14192
- if (!this.reactNativeInit) {
14193
- yield this.transport.init(BleLogger);
14194
- this.reactNativeInit = true;
14195
- } else {
14196
- Log$5.debug('React Native Do Not Initializing transports');
14197
- }
14198
- } else {
14199
- yield this.transport.init(HttpLogger);
14200
- }
14201
-
14202
- Log$5.debug('Configuring transports');
14203
- yield this.transport.configure(JSON.stringify(this.defaultMessages));
14204
- Log$5.debug('Configuring transports done');
14205
- } catch (error) {
14206
- Log$5.debug('Initializing transports error: ', error);
14207
-
14208
- if (error.code === 'ECONNABORTED') {
14209
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BridgeTimeoutError);
14210
- }
14211
- }
14212
- });
14213
- }
14214
-
14215
- static reconfigure(messages) {
14216
- return __awaiter(this, void 0, void 0, function* () {
14217
- if (Array.isArray(messages)) {
14218
- messages = DataManager.getProtobufMessages();
14219
- }
14220
-
14221
- if (this.currentMessages === messages || !messages) {
14222
- return;
14223
- }
14224
-
14225
- try {
14226
- yield this.transport.configure(JSON.stringify(messages));
14227
- this.currentMessages = messages;
14228
- } catch (error) {
14229
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportInvalidProtobuf, `Transport_InvalidProtobuf: ${error.message}`);
14230
- }
14231
- });
14232
- }
14233
-
14234
- static setTransport(TransportConstructor) {
14235
- const env = DataManager.getSettings('env');
14236
-
14237
- if (env === 'react-native') {
14238
- this.transport = new TransportConstructor({
14239
- scanTimeout: 3000
14240
- });
14241
- } else {
14242
- this.transport = new TransportConstructor();
14243
- }
14244
-
14245
- Log$5.debug('set transport: ', this.transport);
14246
- }
14247
-
14248
- static getTransport() {
14249
- return this.transport;
14250
- }
14251
-
14252
- static getDefaultMessages() {
14253
- return this.defaultMessages;
14254
- }
14255
-
14256
- static getCurrentMessages() {
14257
- return this.currentMessages;
14258
- }
14259
-
14260
- }
14261
-
14262
- TransportManager.reactNativeInit = false;
14263
14181
  const CORE_EVENT = 'CORE_EVENT';
14264
14182
 
14265
14183
  const parseMessage = messageData => {
@@ -14393,6 +14311,348 @@ const createFirmwareMessage = (type, payload) => ({
14393
14311
  payload
14394
14312
  });
14395
14313
 
14314
+ const Log$6 = getLogger(exports.d0.DevicePool);
14315
+
14316
+ const getDiff = (current, descriptors) => {
14317
+ const connected = descriptors.filter(d => current.find(x => x.path === d.path) === undefined);
14318
+ const disconnected = current.filter(d => descriptors.find(x => x.path === d.path) === undefined);
14319
+ const changedSessions = descriptors.filter(d => {
14320
+ const currentDescriptor = current.find(x => x.path === d.path);
14321
+
14322
+ if (currentDescriptor) {
14323
+ return currentDescriptor.session !== d.session;
14324
+ }
14325
+
14326
+ return false;
14327
+ });
14328
+ const acquired = changedSessions.filter(d => typeof d.session === 'string');
14329
+ const released = changedSessions.filter(d => typeof d.session !== 'string');
14330
+ const changedDebugSessions = descriptors.filter(d => {
14331
+ const currentDescriptor = current.find(x => x.path === d.path);
14332
+
14333
+ if (currentDescriptor) {
14334
+ return currentDescriptor.debugSession !== d.debugSession;
14335
+ }
14336
+
14337
+ return false;
14338
+ });
14339
+ const debugAcquired = changedSessions.filter(d => typeof d.debugSession === 'string');
14340
+ const debugReleased = changedSessions.filter(d => typeof d.debugSession !== 'string');
14341
+ const didUpdate = connected.length + disconnected.length + changedSessions.length + changedDebugSessions.length > 0;
14342
+ return {
14343
+ connected,
14344
+ disconnected,
14345
+ changedSessions,
14346
+ acquired,
14347
+ released,
14348
+ changedDebugSessions,
14349
+ debugAcquired,
14350
+ debugReleased,
14351
+ didUpdate,
14352
+ descriptors
14353
+ };
14354
+ };
14355
+
14356
+ class DevicePool extends events.exports {
14357
+ static setConnector(connector) {
14358
+ this.connector = connector;
14359
+ }
14360
+
14361
+ static getDevices(descriptorList, connectId) {
14362
+ var descriptorList_1, descriptorList_1_1;
14363
+
14364
+ var e_1, _a;
14365
+
14366
+ return __awaiter(this, void 0, void 0, function* () {
14367
+ Log$6.debug('get device list');
14368
+ const devices = {};
14369
+ const deviceList = [];
14370
+
14371
+ if (connectId) {
14372
+ const device = this.devicesCache[connectId];
14373
+
14374
+ if (device) {
14375
+ const exist = descriptorList.find(d => d.path === device.originalDescriptor.path);
14376
+
14377
+ if (exist) {
14378
+ Log$6.debug('find existed Device: ', connectId);
14379
+ device.updateDescriptor(exist, true);
14380
+ devices[connectId] = device;
14381
+ deviceList.push(device);
14382
+ yield this._checkDevicePool();
14383
+ return {
14384
+ devices,
14385
+ deviceList
14386
+ };
14387
+ }
14388
+
14389
+ Log$6.debug('found device in cache, but path is different: ', connectId);
14390
+ }
14391
+ }
14392
+
14393
+ try {
14394
+ for (descriptorList_1 = __asyncValues(descriptorList); descriptorList_1_1 = yield descriptorList_1.next(), !descriptorList_1_1.done;) {
14395
+ const descriptor = descriptorList_1_1.value;
14396
+ const device = yield this._createDevice(descriptor);
14397
+
14398
+ if (device.features) {
14399
+ const uuid = getDeviceUUID(device.features);
14400
+
14401
+ if (this.devicesCache[uuid]) {
14402
+ const cache = this.devicesCache[uuid];
14403
+ cache.updateDescriptor(descriptor, true);
14404
+ }
14405
+
14406
+ this.devicesCache[uuid] = device;
14407
+ devices[uuid] = device;
14408
+ }
14409
+
14410
+ deviceList.push(device);
14411
+ }
14412
+ } catch (e_1_1) {
14413
+ e_1 = {
14414
+ error: e_1_1
14415
+ };
14416
+ } finally {
14417
+ try {
14418
+ if (descriptorList_1_1 && !descriptorList_1_1.done && (_a = descriptorList_1.return)) yield _a.call(descriptorList_1);
14419
+ } finally {
14420
+ if (e_1) throw e_1.error;
14421
+ }
14422
+ }
14423
+
14424
+ Log$6.debug('get devices result : ', devices, deviceList);
14425
+ console.log('device poll -> connected: ', this.connectedPool);
14426
+ console.log('device poll -> disconnected: ', this.disconnectPool);
14427
+ yield this._checkDevicePool();
14428
+ return {
14429
+ devices,
14430
+ deviceList
14431
+ };
14432
+ });
14433
+ }
14434
+
14435
+ static _createDevice(descriptor) {
14436
+ return __awaiter(this, void 0, void 0, function* () {
14437
+ let device = this.getDeviceByPath(descriptor.path);
14438
+
14439
+ if (!device) {
14440
+ device = Device.fromDescriptor(descriptor);
14441
+ device.deviceConnector = this.connector;
14442
+ yield device.connect();
14443
+ yield device.initialize();
14444
+ yield device.release();
14445
+ }
14446
+
14447
+ return device;
14448
+ });
14449
+ }
14450
+
14451
+ static _checkDevicePool() {
14452
+ return __awaiter(this, void 0, void 0, function* () {
14453
+ yield this._sendConnectMessage();
14454
+
14455
+ this._sendDisconnectMessage();
14456
+ });
14457
+ }
14458
+
14459
+ static _sendConnectMessage() {
14460
+ return __awaiter(this, void 0, void 0, function* () {
14461
+ for (let i = this.connectedPool.length - 1; i >= 0; i--) {
14462
+ const descriptor = this.connectedPool[i];
14463
+ const device = yield this._createDevice(descriptor);
14464
+ Log$6.debug('emit DEVICE.CONNECT: ', device);
14465
+ this.emitter.emit(DEVICE.CONNECT, device);
14466
+ this.connectedPool.splice(i, 1);
14467
+ }
14468
+ });
14469
+ }
14470
+
14471
+ static _sendDisconnectMessage() {
14472
+ for (let i = this.disconnectPool.length - 1; i >= 0; i--) {
14473
+ const descriptor = this.connectedPool[i];
14474
+ const device = this.getDeviceByPath(descriptor.path);
14475
+
14476
+ if (device) {
14477
+ this.emitter.emit(DEVICE.DISCONNECT, device);
14478
+ }
14479
+
14480
+ this.disconnectPool.splice(i, 1);
14481
+ }
14482
+ }
14483
+
14484
+ static reportDeviceChange(upcoming) {
14485
+ const diff = getDiff(this.current || [], upcoming);
14486
+ this.upcoming = upcoming;
14487
+ this.current = this.upcoming;
14488
+ console.log('device pool -> current: ', this.current);
14489
+ console.log('device pool -> upcomming: ', this.upcoming);
14490
+ console.log('DeviceCache.reportDeviceChange diff: ', diff);
14491
+
14492
+ if (!diff.didUpdate) {
14493
+ return;
14494
+ }
14495
+
14496
+ diff.connected.forEach(d => {
14497
+ const device = this.getDeviceByPath(d.path);
14498
+
14499
+ if (!device) {
14500
+ this._addConnectedDeviceToPool(d);
14501
+
14502
+ return;
14503
+ }
14504
+
14505
+ Log$6.debug('emit DEVICE.CONNECT: ', device);
14506
+ this.emitter.emit(DEVICE.CONNECT, device);
14507
+ });
14508
+ diff.disconnected.forEach(d => {
14509
+ this._removeDeviceFromConnectedPool(d.path);
14510
+
14511
+ const device = this.getDeviceByPath(d.path);
14512
+
14513
+ if (!device) {
14514
+ this._addDisconnectedDeviceToPool(d);
14515
+
14516
+ return;
14517
+ }
14518
+
14519
+ Log$6.debug('emit DEVICE.DISCONNECT: ', device);
14520
+ this.emitter.emit(DEVICE.DISCONNECT, device);
14521
+ });
14522
+ }
14523
+
14524
+ static getDeviceByPath(path) {
14525
+ return Object.values(this.devicesCache).find(d => d.originalDescriptor.path === path);
14526
+ }
14527
+
14528
+ static _addConnectedDeviceToPool(descriptor) {
14529
+ const existDescriptorIndex = this.connectedPool.findIndex(d => d.path === descriptor.path);
14530
+
14531
+ if (existDescriptorIndex > -1) {
14532
+ this.connectedPool.splice(existDescriptorIndex, 1, descriptor);
14533
+ return;
14534
+ }
14535
+
14536
+ this.connectedPool.push(descriptor);
14537
+ }
14538
+
14539
+ static _removeDeviceFromConnectedPool(path) {
14540
+ const index = this.connectedPool.findIndex(d => d.path === path);
14541
+
14542
+ if (index > -1) {
14543
+ this.connectedPool.splice(index, 1);
14544
+ }
14545
+ }
14546
+
14547
+ static _addDisconnectedDeviceToPool(descriptor) {
14548
+ const existDescriptorIndex = this.disconnectPool.findIndex(d => d.path === descriptor.path);
14549
+
14550
+ if (existDescriptorIndex > -1) {
14551
+ this.disconnectPool.splice(existDescriptorIndex, 1, descriptor);
14552
+ return;
14553
+ }
14554
+
14555
+ this.disconnectPool.push(descriptor);
14556
+ }
14557
+
14558
+ }
14559
+
14560
+ DevicePool.current = null;
14561
+ DevicePool.upcoming = [];
14562
+ DevicePool.connectedPool = [];
14563
+ DevicePool.disconnectPool = [];
14564
+ DevicePool.devicesCache = {};
14565
+ DevicePool.emitter = new events.exports();
14566
+ const Log$5 = getLogger(exports.d0.Transport);
14567
+ const BleLogger = getLogger(exports.d0.HdBleTransport);
14568
+ const HttpLogger = getLogger(exports.d0.HdTransportHttp);
14569
+
14570
+ class TransportManager {
14571
+ static load() {
14572
+ Log$5.debug('transport manager load');
14573
+ this.defaultMessages = DataManager.getProtobufMessages();
14574
+ this.currentMessages = this.defaultMessages;
14575
+ }
14576
+
14577
+ static configure() {
14578
+ return __awaiter(this, void 0, void 0, function* () {
14579
+ try {
14580
+ const env = DataManager.getSettings('env');
14581
+ Log$5.debug('Initializing transports');
14582
+
14583
+ if (env === 'react-native') {
14584
+ if (!this.reactNativeInit) {
14585
+ yield this.transport.init(BleLogger, DevicePool.emitter);
14586
+ this.reactNativeInit = true;
14587
+ } else {
14588
+ Log$5.debug('React Native Do Not Initializing transports');
14589
+ }
14590
+ } else {
14591
+ yield this.transport.init(HttpLogger);
14592
+ }
14593
+
14594
+ Log$5.debug('Configuring transports');
14595
+ yield this.transport.configure(JSON.stringify(this.defaultMessages));
14596
+ Log$5.debug('Configuring transports done');
14597
+ } catch (error) {
14598
+ Log$5.debug('Initializing transports error: ', error);
14599
+
14600
+ if (error.code === 'ECONNABORTED') {
14601
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BridgeTimeoutError);
14602
+ }
14603
+ }
14604
+ });
14605
+ }
14606
+
14607
+ static reconfigure(messages) {
14608
+ return __awaiter(this, void 0, void 0, function* () {
14609
+ if (Array.isArray(messages)) {
14610
+ messages = DataManager.getProtobufMessages();
14611
+ }
14612
+
14613
+ if (this.currentMessages === messages || !messages) {
14614
+ return;
14615
+ }
14616
+
14617
+ try {
14618
+ yield this.transport.configure(JSON.stringify(messages));
14619
+ this.currentMessages = messages;
14620
+ } catch (error) {
14621
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportInvalidProtobuf, `Transport_InvalidProtobuf: ${error.message}`);
14622
+ }
14623
+ });
14624
+ }
14625
+
14626
+ static setTransport(TransportConstructor) {
14627
+ const env = DataManager.getSettings('env');
14628
+
14629
+ if (env === 'react-native') {
14630
+ this.transport = new TransportConstructor({
14631
+ scanTimeout: 3000
14632
+ });
14633
+ } else {
14634
+ this.transport = new TransportConstructor();
14635
+ }
14636
+
14637
+ Log$5.debug('set transport: ', this.transport);
14638
+ }
14639
+
14640
+ static getTransport() {
14641
+ return this.transport;
14642
+ }
14643
+
14644
+ static getDefaultMessages() {
14645
+ return this.defaultMessages;
14646
+ }
14647
+
14648
+ static getCurrentMessages() {
14649
+ return this.currentMessages;
14650
+ }
14651
+
14652
+ }
14653
+
14654
+ TransportManager.reactNativeInit = false;
14655
+
14396
14656
  const assertType = (res, resType) => {
14397
14657
  const splitResTypes = Array.isArray(resType) ? resType : resType.split('|');
14398
14658
 
@@ -14544,8 +14804,13 @@ class DeviceCommands {
14544
14804
  }
14545
14805
 
14546
14806
  if (res.type === 'EntropyRequest') ;
14807
+ const isWebusbEnv = DataManager.getSettings('env') === 'webusb';
14547
14808
 
14548
14809
  if (res.type === 'PinMatrixRequest') {
14810
+ if (isWebusbEnv) {
14811
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Please unlock your device'));
14812
+ }
14813
+
14549
14814
  return this._promptPin(res.message.type).then(pin => {
14550
14815
  if (pin === '@@ONEKEY_INPUT_PIN_IN_DEVICE') {
14551
14816
  return this._commonCall('BixinPinInputOnDevice');
@@ -14600,7 +14865,7 @@ const UI_REQUEST = {
14600
14865
  FIRMWARE_NOT_INSTALLED: 'ui-device_firmware_not_installed',
14601
14866
  NOT_USE_ONEKEY_DEVICE: 'ui-device_please_use_onekey_device'
14602
14867
  };
14603
- const DEFAULT_DOMAIN = `https://jssdk.onekey.so/${"0.1.27"}/`;
14868
+ const DEFAULT_DOMAIN = `https://jssdk.onekey.so/${"0.1.28"}/`;
14604
14869
  const DEFAULT_PRIORITY = 2;
14605
14870
  const initialSettings = {
14606
14871
  configSrc: './data/config.json',
@@ -15094,9 +15359,6 @@ class Device extends events.exports {
15094
15359
 
15095
15360
  }
15096
15361
 
15097
- const cacheDeviceMap = {};
15098
- const Log$2 = getLogger(exports.d0.DeviceList);
15099
-
15100
15362
  class DeviceList extends events.exports {
15101
15363
  constructor() {
15102
15364
  super(...arguments);
@@ -15104,67 +15366,17 @@ class DeviceList extends events.exports {
15104
15366
  }
15105
15367
 
15106
15368
  getDeviceLists(connectId) {
15107
- var e_1, _a;
15108
-
15109
- var _b, _c;
15369
+ var _a, _b;
15110
15370
 
15111
15371
  return __awaiter(this, void 0, void 0, function* () {
15112
- const deviceDiff = yield (_b = this.connector) === null || _b === void 0 ? void 0 : _b.enumerate();
15113
- const descriptorList = (_c = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _c !== void 0 ? _c : [];
15372
+ const deviceDiff = yield (_a = this.connector) === null || _a === void 0 ? void 0 : _a.enumerate();
15373
+ const descriptorList = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
15114
15374
  this.devices = {};
15115
- const deviceList = [];
15116
- Log$2.debug('get device list');
15117
-
15118
- if (connectId) {
15119
- const device = cacheDeviceMap[connectId];
15120
-
15121
- if (device) {
15122
- const exist = descriptorList.find(d => d.path === device.originalDescriptor.path);
15123
-
15124
- if (exist) {
15125
- device.updateDescriptor(exist, true);
15126
- Log$2.debug('find existed Device: ', connectId);
15127
- this.devices[connectId] = device;
15128
- return [device];
15129
- }
15130
- }
15131
- }
15132
-
15133
- try {
15134
- for (var descriptorList_1 = __asyncValues(descriptorList), descriptorList_1_1; descriptorList_1_1 = yield descriptorList_1.next(), !descriptorList_1_1.done;) {
15135
- const descriptor = descriptorList_1_1.value;
15136
- let device = Device.fromDescriptor(descriptor);
15137
- device.deviceConnector = this.connector;
15138
- yield device.connect();
15139
- yield device.initialize();
15140
- yield device.release();
15141
- deviceList.push(device);
15142
-
15143
- if (device.features) {
15144
- const uuid = getDeviceUUID(device.features);
15145
-
15146
- if (cacheDeviceMap[uuid]) {
15147
- const cache = cacheDeviceMap[uuid];
15148
- cache === null || cache === void 0 ? void 0 : cache.updateFromCache(device);
15149
- device = cache;
15150
- }
15151
-
15152
- this.devices[uuid] = device;
15153
- cacheDeviceMap[uuid] = device;
15154
- }
15155
- }
15156
- } catch (e_1_1) {
15157
- e_1 = {
15158
- error: e_1_1
15159
- };
15160
- } finally {
15161
- try {
15162
- if (descriptorList_1_1 && !descriptorList_1_1.done && (_a = descriptorList_1.return)) yield _a.call(descriptorList_1);
15163
- } finally {
15164
- if (e_1) throw e_1.error;
15165
- }
15166
- }
15167
-
15375
+ const {
15376
+ deviceList,
15377
+ devices
15378
+ } = yield DevicePool.getDevices(descriptorList, connectId);
15379
+ this.devices = devices;
15168
15380
  return deviceList;
15169
15381
  });
15170
15382
  }
@@ -15258,14 +15470,12 @@ class SearchDevices extends BaseMethod {
15258
15470
  }
15259
15471
 
15260
15472
  run() {
15261
- var e_1, _a;
15262
-
15263
- var _b, _c;
15473
+ var _a, _b;
15264
15474
 
15265
15475
  return __awaiter(this, void 0, void 0, function* () {
15266
15476
  yield TransportManager.configure();
15267
- const deviceDiff = yield (_b = this.connector) === null || _b === void 0 ? void 0 : _b.enumerate();
15268
- const devicesDescriptor = (_c = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _c !== void 0 ? _c : [];
15477
+ const deviceDiff = yield (_a = this.connector) === null || _a === void 0 ? void 0 : _a.enumerate();
15478
+ const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
15269
15479
  const env = DataManager.getSettings('env');
15270
15480
 
15271
15481
  if (env === 'react-native') {
@@ -15279,31 +15489,10 @@ class SearchDevices extends BaseMethod {
15279
15489
  });
15280
15490
  }
15281
15491
 
15282
- const devices = [];
15283
-
15284
- try {
15285
- for (var devicesDescriptor_1 = __asyncValues(devicesDescriptor), devicesDescriptor_1_1; devicesDescriptor_1_1 = yield devicesDescriptor_1.next(), !devicesDescriptor_1_1.done;) {
15286
- const descriptor = devicesDescriptor_1_1.value;
15287
- const device = Device.fromDescriptor(descriptor);
15288
- device.deviceConnector = this.connector;
15289
- yield device.connect();
15290
- yield device.initialize();
15291
- yield device.release();
15292
- devices.push(device);
15293
- }
15294
- } catch (e_1_1) {
15295
- e_1 = {
15296
- error: e_1_1
15297
- };
15298
- } finally {
15299
- try {
15300
- if (devicesDescriptor_1_1 && !devicesDescriptor_1_1.done && (_a = devicesDescriptor_1.return)) yield _a.call(devicesDescriptor_1);
15301
- } finally {
15302
- if (e_1) throw e_1.error;
15303
- }
15304
- }
15305
-
15306
- return devices.map(device => device.toMessageObject());
15492
+ const {
15493
+ deviceList
15494
+ } = yield DevicePool.getDevices(devicesDescriptor);
15495
+ return deviceList.map(device => device.toMessageObject());
15307
15496
  });
15308
15497
  }
15309
15498
 
@@ -18913,6 +19102,57 @@ class FirmwareUpdate extends BaseMethod {
18913
19102
 
18914
19103
  }
18915
19104
 
19105
+ const Log$2 = getLogger(exports.d0.Method);
19106
+
19107
+ class RequestWebUsbDevice extends BaseMethod {
19108
+ init() {
19109
+ this.useDevice = false;
19110
+ }
19111
+
19112
+ run() {
19113
+ var _a, _b;
19114
+
19115
+ return __awaiter(this, void 0, void 0, function* () {
19116
+ yield TransportManager.configure();
19117
+ const env = DataManager.getSettings('env');
19118
+
19119
+ if (env !== 'webusb') {
19120
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Not webusb environment'));
19121
+ }
19122
+
19123
+ const transport = TransportManager.getTransport();
19124
+
19125
+ try {
19126
+ const device = yield transport.requestDevice();
19127
+
19128
+ if (!device) {
19129
+ return yield Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Please select the device to connect'));
19130
+ }
19131
+
19132
+ const deviceDiff = yield (_a = this.connector) === null || _a === void 0 ? void 0 : _a.enumerate();
19133
+ const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
19134
+ const {
19135
+ deviceList
19136
+ } = yield DevicePool.getDevices(devicesDescriptor);
19137
+
19138
+ const _device = deviceList.find(d => d.originalDescriptor.path === device.serialNumber);
19139
+
19140
+ if (_device) {
19141
+ return {
19142
+ device: _device.toMessageObject()
19143
+ };
19144
+ }
19145
+
19146
+ return yield Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Please select the device to connect'));
19147
+ } catch (error) {
19148
+ Log$2.debug(error);
19149
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Please select the device to connect'));
19150
+ }
19151
+ });
19152
+ }
19153
+
19154
+ }
19155
+
18916
19156
  class CheckBridgeStatus extends BaseMethod {
18917
19157
  init() {
18918
19158
  this.useDevice = false;
@@ -18971,6 +19211,7 @@ var ApiMethods = /*#__PURE__*/Object.freeze({
18971
19211
  stellarGetAddress: StellarGetAddress,
18972
19212
  stellarSignTransaction: StellarSignTransaction,
18973
19213
  firmwareUpdate: FirmwareUpdate,
19214
+ requestWebUsbDevice: RequestWebUsbDevice,
18974
19215
  getLogs: CheckBridgeStatus
18975
19216
  });
18976
19217
 
@@ -19010,54 +19251,6 @@ const resolveAfter = (msec, value) => new Promise(resolve => {
19010
19251
 
19011
19252
  const Log$1 = getLogger(exports.d0.DeviceConnector);
19012
19253
 
19013
- const getDiff = (current, descriptors) => {
19014
- const env = DataManager.getSettings('env');
19015
-
19016
- if (env === 'react-native') {
19017
- return {
19018
- descriptors
19019
- };
19020
- }
19021
-
19022
- const connected = descriptors.filter(d => current.find(x => x.path === d.path) === undefined);
19023
- const disconnected = current.filter(d => descriptors.find(x => x.path === d.path) === undefined);
19024
- const changedSessions = descriptors.filter(d => {
19025
- const currentDescriptor = current.find(x => x.path === d.path);
19026
-
19027
- if (currentDescriptor) {
19028
- return currentDescriptor.session !== d.session;
19029
- }
19030
-
19031
- return false;
19032
- });
19033
- const acquired = changedSessions.filter(d => typeof d.session === 'string');
19034
- const released = changedSessions.filter(d => typeof d.session !== 'string');
19035
- const changedDebugSessions = descriptors.filter(d => {
19036
- const currentDescriptor = current.find(x => x.path === d.path);
19037
-
19038
- if (currentDescriptor) {
19039
- return currentDescriptor.debugSession !== d.debugSession;
19040
- }
19041
-
19042
- return false;
19043
- });
19044
- const debugAcquired = changedSessions.filter(d => typeof d.debugSession === 'string');
19045
- const debugReleased = changedSessions.filter(d => typeof d.debugSession !== 'string');
19046
- const didUpdate = connected.length + disconnected.length + changedSessions.length + changedDebugSessions.length > 0;
19047
- return {
19048
- connected,
19049
- disconnected,
19050
- changedSessions,
19051
- acquired,
19052
- released,
19053
- changedDebugSessions,
19054
- debugAcquired,
19055
- debugReleased,
19056
- didUpdate,
19057
- descriptors
19058
- };
19059
- };
19060
-
19061
19254
  class DeviceConnector {
19062
19255
  constructor() {
19063
19256
  this.listenTimestamp = 0;
@@ -19066,17 +19259,20 @@ class DeviceConnector {
19066
19259
  this.listening = false;
19067
19260
  TransportManager.load();
19068
19261
  this.transport = TransportManager.getTransport();
19262
+ DevicePool.setConnector(this);
19069
19263
  }
19070
19264
 
19071
19265
  enumerate() {
19072
19266
  return __awaiter(this, void 0, void 0, function* () {
19073
19267
  try {
19074
- this.upcoming = yield this.transport.enumerate();
19268
+ const descriptors = yield this.transport.enumerate();
19269
+ this.upcoming = descriptors;
19075
19270
 
19076
- const diff = this._reportDevicesChange();
19271
+ this._reportDevicesChange();
19077
19272
 
19078
- Log$1.debug('diff result: ', diff);
19079
- return diff;
19273
+ return {
19274
+ descriptors
19275
+ };
19080
19276
  } catch (error) {
19081
19277
  safeThrowError(error);
19082
19278
  }
@@ -19157,9 +19353,7 @@ class DeviceConnector {
19157
19353
  }
19158
19354
 
19159
19355
  _reportDevicesChange() {
19160
- const diff = getDiff(this.current || [], this.upcoming);
19161
- this.current = this.upcoming;
19162
- return diff;
19356
+ DevicePool.reportDeviceChange(this.upcoming);
19163
19357
  }
19164
19358
 
19165
19359
  }
@@ -19200,6 +19394,8 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
19200
19394
  return Promise.reject(error);
19201
19395
  }
19202
19396
 
19397
+ DevicePool.emitter.on(DEVICE.CONNECT, onDeviceConnectHandler);
19398
+
19203
19399
  if (!method.useDevice) {
19204
19400
  try {
19205
19401
  const response = yield method.run();
@@ -19250,11 +19446,17 @@ const callAPI = message => __awaiter(void 0, void 0, void 0, function* () {
19250
19446
  const currentVersion = getDeviceFirmwareVersion(device.features).join('.');
19251
19447
 
19252
19448
  if (semver__default["default"].valid(versionRange.min) && semver__default["default"].lt(currentVersion, versionRange.min)) {
19253
- return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceFwException, `Device firmware version is too low, please update to ${versionRange.min}`));
19449
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodNeedUpgradeFirmware, `Device firmware version is too low, please update to ${versionRange.min}`, {
19450
+ current: currentVersion,
19451
+ require: versionRange.min
19452
+ }));
19254
19453
  }
19255
19454
 
19256
- if (versionRange.max && semver__default["default"].valid(versionRange.max) && semver__default["default"].gt(currentVersion, versionRange.max)) {
19257
- return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceFwException, `Device firmware version is too high, this method has been deprecated in ${versionRange.max}`));
19455
+ if (versionRange.max && semver__default["default"].valid(versionRange.max) && semver__default["default"].gte(currentVersion, versionRange.max)) {
19456
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodDeprecated, `Device firmware version is too high, this method has been deprecated in ${versionRange.max}`, {
19457
+ current: currentVersion,
19458
+ deprecated: versionRange.max
19459
+ }));
19258
19460
  }
19259
19461
  }
19260
19462
 
@@ -19535,12 +19737,29 @@ const removeDeviceListener = device => {
19535
19737
  device.removeListener(DEVICE.PIN, onDevicePinHandler);
19536
19738
  device.removeListener(DEVICE.BUTTON, onDeviceButtonHandler);
19537
19739
  device.removeListener(DEVICE.FEATURES, onDeviceFeaturesHandler);
19740
+ DevicePool.emitter.removeListener(DEVICE.CONNECT, onDeviceConnectHandler);
19538
19741
  };
19539
19742
 
19540
19743
  const closePopup = () => {
19541
19744
  postMessage(createUiMessage(UI_REQUEST$1.CLOSE_UI_WINDOW));
19542
19745
  };
19543
19746
 
19747
+ const onDeviceConnectHandler = device => {
19748
+ const env = DataManager.getSettings('env');
19749
+ const deviceObject = env === 'react-native' ? device : device.toMessageObject();
19750
+ postMessage(createDeviceMessage(DEVICE.CONNECT, {
19751
+ device: deviceObject
19752
+ }));
19753
+ };
19754
+
19755
+ const onDeviceDisconnectHandler = device => {
19756
+ const env = DataManager.getSettings('env');
19757
+ const deviceObject = env === 'react-native' ? device : device.toMessageObject();
19758
+ postMessage(createDeviceMessage(DEVICE.DISCONNECT, {
19759
+ device: deviceObject
19760
+ }));
19761
+ };
19762
+
19544
19763
  const onDevicePinHandler = (...[device, type, callback]) => __awaiter(void 0, void 0, void 0, function* () {
19545
19764
  Log.debug('onDevicePinHandler');
19546
19765
  const uiPromise = createUiPromise(UI_RESPONSE.RECEIVE_PIN, device);
@@ -19642,6 +19861,7 @@ const initCore = () => {
19642
19861
 
19643
19862
  const initConnector = () => {
19644
19863
  _connector = new DeviceConnector();
19864
+ DevicePool.emitter.on(DEVICE.DISCONNECT, onDeviceDisconnectHandler);
19645
19865
  return _connector;
19646
19866
  };
19647
19867
 
@@ -19998,6 +20218,19 @@ const readHeader = buffer => {
19998
20218
  };
19999
20219
  };
20000
20220
 
20221
+ const readHeaderChunked = buffer => {
20222
+ const sharp1 = buffer.readByte();
20223
+ const sharp2 = buffer.readByte();
20224
+ const typeId = buffer.readUint16();
20225
+ const length = buffer.readUint32();
20226
+ return {
20227
+ sharp1,
20228
+ sharp2,
20229
+ typeId,
20230
+ length
20231
+ };
20232
+ };
20233
+
20001
20234
  const decode = byteBuffer => {
20002
20235
  const {
20003
20236
  typeId
@@ -20008,6 +20241,32 @@ const decode = byteBuffer => {
20008
20241
  };
20009
20242
  };
20010
20243
 
20244
+ const decodeChunked = bytes => {
20245
+ const byteBuffer = ByteBuffer__default["default"].wrap(bytes, undefined, undefined, true);
20246
+ const {
20247
+ sharp1,
20248
+ sharp2,
20249
+ typeId,
20250
+ length
20251
+ } = readHeaderChunked(byteBuffer);
20252
+
20253
+ if (sharp1 !== MESSAGE_HEADER_BYTE || sharp2 !== MESSAGE_HEADER_BYTE) {
20254
+ throw new Error("Didn't receive expected header signature.");
20255
+ }
20256
+
20257
+ return {
20258
+ length,
20259
+ typeId,
20260
+ restBuffer: byteBuffer
20261
+ };
20262
+ };
20263
+
20264
+ var decodeProtocol = /*#__PURE__*/Object.freeze({
20265
+ __proto__: null,
20266
+ decode: decode,
20267
+ decodeChunked: decodeChunked
20268
+ });
20269
+
20011
20270
  function encode(data, options) {
20012
20271
  const {
20013
20272
  addTrezorHeaders,
@@ -20059,17 +20318,21 @@ function buildOne(messages, name, data) {
20059
20318
  });
20060
20319
  }
20061
20320
 
20062
- const buildBuffers = (messages, name, data) => {
20321
+ const buildEncodeBuffers = (messages, name, data) => {
20063
20322
  const {
20064
20323
  Message,
20065
20324
  messageType
20066
20325
  } = createMessageFromName(messages, name);
20067
20326
  const buffer = encode$1(Message, data);
20068
- const encodeBuffers = encode(buffer, {
20327
+ return encode(buffer, {
20069
20328
  addTrezorHeaders: true,
20070
20329
  chunked: true,
20071
20330
  messageType
20072
20331
  });
20332
+ };
20333
+
20334
+ const buildBuffers = (messages, name, data) => {
20335
+ const encodeBuffers = buildEncodeBuffers(messages, name, data);
20073
20336
  const outBuffers = [];
20074
20337
 
20075
20338
  for (const buf of encodeBuffers) {
@@ -20752,8 +21015,10 @@ var index = {
20752
21015
  check,
20753
21016
  buildOne,
20754
21017
  buildBuffers,
21018
+ buildEncodeBuffers,
20755
21019
  receiveOne,
20756
- parseConfigure
21020
+ parseConfigure,
21021
+ decodeProtocol
20757
21022
  };
20758
21023
  exports.BUFFER_SIZE = BUFFER_SIZE;
20759
21024
  exports.COMMON_HEADER_SIZE = COMMON_HEADER_SIZE;
@@ -20862,6 +21127,7 @@ class HardwareError extends Error {
20862
21127
  super();
20863
21128
  this.errorCode = HardwareErrorCode.UnknownError;
20864
21129
  this.message = '';
21130
+ this.params = {};
20865
21131
  const errorMessageMapping = HardwareErrorCodeMessage;
20866
21132
  this.message = errorMessageMapping[HardwareErrorCode.UnknownError];
20867
21133
 
@@ -20875,6 +21141,7 @@ class HardwareError extends Error {
20875
21141
  this.message = fillStringWithArguments(message, hardwareError);
20876
21142
  }
20877
21143
 
21144
+ this.params = hardwareError.params;
20878
21145
  this.errorCode = hardwareError.errorCode;
20879
21146
  }
20880
21147
 
@@ -20906,6 +21173,8 @@ const HardwareErrorCode = {
20906
21173
  CallMethodNotResponse: 404,
20907
21174
  CallMethodInvalidParameter: 405,
20908
21175
  FirmwareUpdateDownloadFailed: 406,
21176
+ CallMethodNeedUpgradeFirmware: 407,
21177
+ CallMethodDeprecated: 408,
20909
21178
  NetworkError: 500,
20910
21179
  TransportNotConfigured: 600,
20911
21180
  TransportCallInProgress: 601,
@@ -20957,6 +21226,8 @@ const HardwareErrorCodeMessage = {
20957
21226
  [HardwareErrorCode.CallMethodNotResponse]: 'Method does not responding',
20958
21227
  [HardwareErrorCode.CallMethodInvalidParameter]: 'Call method invalid parameter',
20959
21228
  [HardwareErrorCode.FirmwareUpdateDownloadFailed]: 'Firmware update download failed',
21229
+ [HardwareErrorCode.CallMethodNeedUpgradeFirmware]: 'Call method need upgrade firmware',
21230
+ [HardwareErrorCode.CallMethodDeprecated]: 'Call method is deprecated',
20960
21231
  [HardwareErrorCode.NetworkError]: 'Network request error',
20961
21232
  [HardwareErrorCode.TransportNotConfigured]: 'Transport not configured',
20962
21233
  [HardwareErrorCode.TransportCallInProgress]: 'Transport call in progress',
@@ -20986,14 +21257,15 @@ const HardwareErrorCodeMessage = {
20986
21257
  [HardwareErrorCode.PollingStop]: 'Polling stop'
20987
21258
  };
20988
21259
 
20989
- const TypedError = (hardwareError, message) => {
21260
+ const TypedError = (hardwareError, message, params) => {
20990
21261
  if (typeof hardwareError === 'string') {
20991
21262
  return new HardwareError(hardwareError);
20992
21263
  }
20993
21264
 
20994
21265
  return new HardwareError({
20995
21266
  errorCode: hardwareError,
20996
- message: message !== null && message !== void 0 ? message : ''
21267
+ message: message !== null && message !== void 0 ? message : '',
21268
+ params
20997
21269
  });
20998
21270
  };
20999
21271
 
@@ -21001,7 +21273,8 @@ const serializeError = payload => {
21001
21273
  if (payload && payload.error instanceof HardwareError) {
21002
21274
  return {
21003
21275
  error: payload.error.message,
21004
- code: payload.error.errorCode
21276
+ code: payload.error.errorCode,
21277
+ params: payload.error.params
21005
21278
  };
21006
21279
  }
21007
21280
 
@@ -46128,11 +46401,7 @@ const handleMessage = async message => {
46128
46401
  break;
46129
46402
 
46130
46403
  case dist/* DEVICE_EVENT */.Fd:
46131
- if (message.type === dist/* DEVICE.FEATURES */.Td.FEATURES) {
46132
- eventEmitter.emit(message.type, message.payload);
46133
- }
46134
-
46135
- if (message.type === dist/* DEVICE.SUPPORT_FEATURES */.Td.SUPPORT_FEATURES) {
46404
+ if ([dist/* DEVICE.CONNECT */.Td.CONNECT, dist/* DEVICE.DISCONNECT */.Td.DISCONNECT, dist/* DEVICE.FEATURES */.Td.FEATURES, dist/* DEVICE.SUPPORT_FEATURES */.Td.SUPPORT_FEATURES].includes(message.type)) {
46136
46405
  eventEmitter.emit(message.type, message.payload);
46137
46406
  }
46138
46407
 
@@ -46232,7 +46501,7 @@ const src_init = async settings => {
46232
46501
 
46233
46502
  try {
46234
46503
  await init({ ..._settings,
46235
- version: "0.1.27"
46504
+ version: "0.1.28"
46236
46505
  });
46237
46506
  return true;
46238
46507
  } catch (e) {