@onekeyfe/hd-core 0.1.36 → 0.1.39

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 (88) hide show
  1. package/dist/api/BaseMethod.d.ts +1 -0
  2. package/dist/api/BaseMethod.d.ts.map +1 -1
  3. package/dist/api/CheckBLEFirmwareRelease.d.ts.map +1 -1
  4. package/dist/api/CheckBridgeStatus.d.ts.map +1 -1
  5. package/dist/api/CheckFirmwareRelease.d.ts.map +1 -1
  6. package/dist/api/CheckTransportRelease.d.ts.map +1 -1
  7. package/dist/api/FirmwareUpdate.d.ts.map +1 -1
  8. package/dist/api/GetFeatures.d.ts.map +1 -1
  9. package/dist/api/GetLogs.d.ts.map +1 -1
  10. package/dist/api/GetPassphraseState.d.ts +6 -0
  11. package/dist/api/GetPassphraseState.d.ts.map +1 -0
  12. package/dist/api/RequestWebUsbDevice.d.ts.map +1 -1
  13. package/dist/api/SearchDevices.d.ts.map +1 -1
  14. package/dist/api/device/DeviceBackup.d.ts.map +1 -1
  15. package/dist/api/device/DeviceChangePin.d.ts.map +1 -1
  16. package/dist/api/device/DeviceFlags.d.ts.map +1 -1
  17. package/dist/api/device/DeviceRebootToBootloader.d.ts.map +1 -1
  18. package/dist/api/device/DeviceRecovery.d.ts.map +1 -1
  19. package/dist/api/device/DeviceReset.d.ts.map +1 -1
  20. package/dist/api/device/DeviceSettings.d.ts +7 -0
  21. package/dist/api/device/DeviceSettings.d.ts.map +1 -1
  22. package/dist/api/device/DeviceSupportFeatures.d.ts +1 -1
  23. package/dist/api/device/DeviceSupportFeatures.d.ts.map +1 -1
  24. package/dist/api/device/DeviceUpdateReboot.d.ts.map +1 -1
  25. package/dist/api/device/DeviceVerify.d.ts.map +1 -1
  26. package/dist/api/device/DeviceWipe.d.ts.map +1 -1
  27. package/dist/api/index.d.ts +1 -0
  28. package/dist/api/index.d.ts.map +1 -1
  29. package/dist/core/index.d.ts.map +1 -1
  30. package/dist/device/Device.d.ts +15 -5
  31. package/dist/device/Device.d.ts.map +1 -1
  32. package/dist/device/DeviceCommands.d.ts.map +1 -1
  33. package/dist/device/DeviceList.d.ts +2 -2
  34. package/dist/device/DeviceList.d.ts.map +1 -1
  35. package/dist/device/DevicePool.d.ts +6 -5
  36. package/dist/device/DevicePool.d.ts.map +1 -1
  37. package/dist/events/ui-request.d.ts +9 -1
  38. package/dist/events/ui-request.d.ts.map +1 -1
  39. package/dist/index.d.ts +35 -8
  40. package/dist/index.js +442 -254
  41. package/dist/inject.d.ts.map +1 -1
  42. package/dist/types/api/getPassphraseState.d.ts +3 -0
  43. package/dist/types/api/getPassphraseState.d.ts.map +1 -0
  44. package/dist/types/api/index.d.ts +2 -0
  45. package/dist/types/api/index.d.ts.map +1 -1
  46. package/dist/types/device.d.ts +5 -1
  47. package/dist/types/device.d.ts.map +1 -1
  48. package/dist/types/params.d.ts +2 -0
  49. package/dist/types/params.d.ts.map +1 -1
  50. package/dist/utils/deviceFeaturesUtils.d.ts +5 -2
  51. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  52. package/dist/utils/patch.d.ts +1 -1
  53. package/package.json +4 -4
  54. package/src/api/BaseMethod.ts +5 -0
  55. package/src/api/CheckBLEFirmwareRelease.ts +1 -0
  56. package/src/api/CheckBridgeStatus.ts +1 -0
  57. package/src/api/CheckFirmwareRelease.ts +3 -1
  58. package/src/api/CheckTransportRelease.ts +1 -0
  59. package/src/api/FirmwareUpdate.ts +1 -0
  60. package/src/api/GetFeatures.ts +1 -0
  61. package/src/api/GetLogs.ts +1 -0
  62. package/src/api/GetPassphraseState.ts +31 -0
  63. package/src/api/RequestWebUsbDevice.ts +1 -0
  64. package/src/api/SearchDevices.ts +1 -0
  65. package/src/api/device/DeviceBackup.ts +3 -1
  66. package/src/api/device/DeviceChangePin.ts +2 -0
  67. package/src/api/device/DeviceFlags.ts +2 -0
  68. package/src/api/device/DeviceRebootToBootloader.ts +3 -1
  69. package/src/api/device/DeviceRecovery.ts +2 -0
  70. package/src/api/device/DeviceReset.ts +2 -0
  71. package/src/api/device/DeviceSettings.ts +13 -2
  72. package/src/api/device/DeviceSupportFeatures.ts +3 -1
  73. package/src/api/device/DeviceUpdateReboot.ts +3 -1
  74. package/src/api/device/DeviceVerify.ts +2 -0
  75. package/src/api/device/DeviceWipe.ts +3 -1
  76. package/src/api/index.ts +1 -0
  77. package/src/core/index.ts +98 -10
  78. package/src/device/Device.ts +120 -47
  79. package/src/device/DeviceCommands.ts +13 -9
  80. package/src/device/DeviceList.ts +7 -3
  81. package/src/device/DevicePool.ts +24 -12
  82. package/src/events/ui-request.ts +10 -0
  83. package/src/inject.ts +2 -0
  84. package/src/types/api/getPassphraseState.ts +6 -0
  85. package/src/types/api/index.ts +2 -0
  86. package/src/types/device.ts +3 -1
  87. package/src/types/params.ts +8 -0
  88. package/src/utils/deviceFeaturesUtils.ts +38 -5
package/dist/index.js CHANGED
@@ -51,6 +51,7 @@ const inject = ({ call, cancel, dispose, eventEmitter, init, uiResponse, }) => {
51
51
  deviceSupportFeatures: connectId => call({ connectId, method: 'deviceSupportFeatures' }),
52
52
  deviceVerify: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'deviceVerify' })),
53
53
  deviceWipe: connectId => call({ connectId, method: 'deviceWipe' }),
54
+ getPassphraseState: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'getPassphraseState' })),
54
55
  evmGetAddress: (connectId, deviceId, params) => call(Object.assign(Object.assign({}, params), { connectId, deviceId, method: 'evmGetAddress' })),
55
56
  evmGetPublicKey: (connectId, deviceId, params) => call(Object.assign(Object.assign({}, params), { connectId, deviceId, method: 'evmGetPublicKey' })),
56
57
  evmSignMessage: (connectId, deviceId, params) => call(Object.assign(Object.assign({}, params), { connectId, deviceId, method: 'evmSignMessage' })),
@@ -613,6 +614,212 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
613
614
  }
614
615
  }
615
616
 
617
+ const HD_HARDENED = 0x80000000;
618
+ const toHardened = (n) => (n | HD_HARDENED) >>> 0;
619
+ const fromHardened = (n) => (n & ~HD_HARDENED) >>> 0;
620
+ const PATH_NOT_VALID = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Not a valid path');
621
+ const PATH_NEGATIVE_VALUES = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Path cannot contain negative values');
622
+ const getHDPath = (path) => {
623
+ const parts = path.toLowerCase().split('/');
624
+ if (parts[0] !== 'm')
625
+ throw PATH_NOT_VALID;
626
+ return parts
627
+ .filter((p) => p !== 'm' && p !== '')
628
+ .map((p) => {
629
+ let hardened = false;
630
+ if (p.substr(p.length - 1) === "'") {
631
+ hardened = true;
632
+ p = p.substr(0, p.length - 1);
633
+ }
634
+ let n = parseInt(p);
635
+ if (Number.isNaN(n)) {
636
+ throw PATH_NOT_VALID;
637
+ }
638
+ else if (n < 0) {
639
+ throw PATH_NEGATIVE_VALUES;
640
+ }
641
+ if (hardened) {
642
+ n = toHardened(n);
643
+ }
644
+ return n;
645
+ });
646
+ };
647
+ const isMultisigPath = (path) => Array.isArray(path) && path[0] === toHardened(48);
648
+ const isSegwitPath = (path) => Array.isArray(path) && path[0] === toHardened(49);
649
+ const getScriptType = (path) => {
650
+ if (!Array.isArray(path) || path.length < 1)
651
+ return 'SPENDADDRESS';
652
+ const p1 = fromHardened(path[0]);
653
+ switch (p1) {
654
+ case 48:
655
+ return 'SPENDMULTISIG';
656
+ case 49:
657
+ return 'SPENDP2SHWITNESS';
658
+ case 84:
659
+ return 'SPENDWITNESS';
660
+ default:
661
+ return 'SPENDADDRESS';
662
+ }
663
+ };
664
+ const getOutputScriptType = (path) => {
665
+ if (!Array.isArray(path) || path.length < 1)
666
+ return 'PAYTOADDRESS';
667
+ if (path[0] === 49) {
668
+ return 'PAYTOP2SHWITNESS';
669
+ }
670
+ const p = fromHardened(path[0]);
671
+ switch (p) {
672
+ case 48:
673
+ return 'PAYTOMULTISIG';
674
+ case 49:
675
+ return 'PAYTOP2SHWITNESS';
676
+ case 84:
677
+ return 'PAYTOWITNESS';
678
+ default:
679
+ return 'PAYTOADDRESS';
680
+ }
681
+ };
682
+ const serializedPath = (path) => {
683
+ const pathStr = path
684
+ .map((p) => {
685
+ if (p & HD_HARDENED) {
686
+ return `${p & ~HD_HARDENED}'`;
687
+ }
688
+ return p;
689
+ })
690
+ .join('/');
691
+ return `m/${pathStr}`;
692
+ };
693
+ const validatePath = (path, length = 0, base = false) => {
694
+ let valid;
695
+ if (typeof path === 'string') {
696
+ valid = getHDPath(path);
697
+ }
698
+ else if (Array.isArray(path)) {
699
+ valid = path.map((p) => {
700
+ const n = parseInt(p);
701
+ if (Number.isNaN(n)) {
702
+ throw PATH_NOT_VALID;
703
+ }
704
+ else if (n < 0) {
705
+ throw PATH_NEGATIVE_VALUES;
706
+ }
707
+ return n;
708
+ });
709
+ }
710
+ else {
711
+ valid = undefined;
712
+ }
713
+ if (!valid)
714
+ throw PATH_NOT_VALID;
715
+ if (length > 0 && valid.length < length)
716
+ throw PATH_NOT_VALID;
717
+ return base ? valid.splice(0, 3) : valid;
718
+ };
719
+
720
+ const getDeviceModel = (features) => {
721
+ if (!features || typeof features !== 'object') {
722
+ return 'model_mini';
723
+ }
724
+ if (features.model === '1') {
725
+ return 'model_mini';
726
+ }
727
+ return 'model_touch';
728
+ };
729
+ const getDeviceType = (features) => {
730
+ if (!features || typeof features !== 'object' || !features.serial_no) {
731
+ return 'classic';
732
+ }
733
+ const serialNo = features.serial_no;
734
+ const miniFlag = serialNo.slice(0, 2);
735
+ if (miniFlag.toLowerCase() === 'mi')
736
+ return 'mini';
737
+ if (miniFlag.toLowerCase() === 'tc')
738
+ return 'touch';
739
+ return 'classic';
740
+ };
741
+ const getDeviceTypeOnBootloader = (features) => getDeviceType(features);
742
+ const getDeviceTypeByBleName = (name) => {
743
+ if (!name)
744
+ return 'classic';
745
+ if (name.startsWith('MI'))
746
+ return 'mini';
747
+ if (name.startsWith('T'))
748
+ return 'touch';
749
+ return 'classic';
750
+ };
751
+ const getDeviceTypeByDeviceId = (deviceId) => {
752
+ if (!deviceId) {
753
+ return 'classic';
754
+ }
755
+ const miniFlag = deviceId.slice(0, 2);
756
+ if (miniFlag.toLowerCase() === 'mi')
757
+ return 'mini';
758
+ return 'classic';
759
+ };
760
+ const getDeviceUUID = (features) => {
761
+ const deviceType = getDeviceType(features);
762
+ if (deviceType === 'classic') {
763
+ return features.onekey_serial;
764
+ }
765
+ return features.serial_no;
766
+ };
767
+ const getDeviceLabel = (features) => {
768
+ const deviceType = getDeviceType(features);
769
+ if (typeof features.label === 'string') {
770
+ return features.label;
771
+ }
772
+ return `My OneKey ${deviceType.charAt(0).toUpperCase() + deviceType.slice(1)}`;
773
+ };
774
+ const getDeviceFirmwareVersion = (features) => {
775
+ if (!features)
776
+ return [0, 0, 0];
777
+ if (features.onekey_version) {
778
+ return features.onekey_version.split('.');
779
+ }
780
+ return [features.major_version, features.minor_version, features.patch_version];
781
+ };
782
+ const getDeviceBLEFirmwareVersion = (features) => {
783
+ if (!features.ble_ver) {
784
+ return null;
785
+ }
786
+ if (!semver__default["default"].valid(features.ble_ver)) {
787
+ return null;
788
+ }
789
+ return features.ble_ver.split('.');
790
+ };
791
+ const supportInputPinOnSoftware = (features) => {
792
+ if (!features)
793
+ return { support: false };
794
+ const deviceType = getDeviceType(features);
795
+ if (deviceType === 'touch') {
796
+ return { support: false };
797
+ }
798
+ const currentVersion = getDeviceFirmwareVersion(features).join('.');
799
+ return { support: semver__default["default"].gte(currentVersion, '2.3.0'), require: '2.3.0' };
800
+ };
801
+ const supportNewPassphrase = (features) => {
802
+ if (!features)
803
+ return { support: false };
804
+ const deviceType = getDeviceType(features);
805
+ if (deviceType === 'touch' || deviceType === 'pro') {
806
+ return { support: true };
807
+ }
808
+ const currentVersion = getDeviceFirmwareVersion(features).join('.');
809
+ return { support: semver__default["default"].gte(currentVersion, '2.4.0'), require: '2.4.0' };
810
+ };
811
+ const getPassphraseState = (features, commands) => __awaiter(void 0, void 0, void 0, function* () {
812
+ if (!features)
813
+ return false;
814
+ const { message } = yield commands.typedCall('GetAddress', 'Address', {
815
+ address_n: [toHardened(44), toHardened(1), toHardened(0), 0, 0],
816
+ coin_name: 'Testnet',
817
+ script_type: 'SPENDADDRESS',
818
+ show_display: false,
819
+ });
820
+ return message.address;
821
+ });
822
+
616
823
  var nested = {
617
824
  BinanceGetAddress: {
618
825
  fields: {
@@ -9461,191 +9668,6 @@ function patchFeatures(response) {
9461
9668
  return response;
9462
9669
  }
9463
9670
 
9464
- const getDeviceModel = (features) => {
9465
- if (!features || typeof features !== 'object') {
9466
- return 'model_mini';
9467
- }
9468
- if (features.model === '1') {
9469
- return 'model_mini';
9470
- }
9471
- return 'model_touch';
9472
- };
9473
- const getDeviceType = (features) => {
9474
- if (!features || typeof features !== 'object' || !features.serial_no) {
9475
- return 'classic';
9476
- }
9477
- const serialNo = features.serial_no;
9478
- const miniFlag = serialNo.slice(0, 2);
9479
- if (miniFlag.toLowerCase() === 'mi')
9480
- return 'mini';
9481
- if (miniFlag.toLowerCase() === 'tc')
9482
- return 'touch';
9483
- return 'classic';
9484
- };
9485
- const getDeviceTypeOnBootloader = (features) => getDeviceType(features);
9486
- const getDeviceTypeByBleName = (name) => {
9487
- if (!name)
9488
- return 'classic';
9489
- if (name.startsWith('MI'))
9490
- return 'mini';
9491
- if (name.startsWith('T'))
9492
- return 'touch';
9493
- return 'classic';
9494
- };
9495
- const getDeviceTypeByDeviceId = (deviceId) => {
9496
- if (!deviceId) {
9497
- return 'classic';
9498
- }
9499
- const miniFlag = deviceId.slice(0, 2);
9500
- if (miniFlag.toLowerCase() === 'mi')
9501
- return 'mini';
9502
- return 'classic';
9503
- };
9504
- const getDeviceUUID = (features) => {
9505
- const deviceType = getDeviceType(features);
9506
- if (deviceType === 'classic') {
9507
- return features.onekey_serial;
9508
- }
9509
- return features.serial_no;
9510
- };
9511
- const getDeviceLabel = (features) => {
9512
- const deviceType = getDeviceType(features);
9513
- if (typeof features.label === 'string') {
9514
- return features.label;
9515
- }
9516
- return `My OneKey ${deviceType.charAt(0).toUpperCase() + deviceType.slice(1)}`;
9517
- };
9518
- const getDeviceFirmwareVersion = (features) => {
9519
- if (!features)
9520
- return [0, 0, 0];
9521
- if (features.onekey_version) {
9522
- return features.onekey_version.split('.');
9523
- }
9524
- return [features.major_version, features.minor_version, features.patch_version];
9525
- };
9526
- const getDeviceBLEFirmwareVersion = (features) => {
9527
- if (!features.ble_ver) {
9528
- return null;
9529
- }
9530
- if (!semver__default["default"].valid(features.ble_ver)) {
9531
- return null;
9532
- }
9533
- return features.ble_ver.split('.');
9534
- };
9535
- const supportInputPinOnSoftware = (features) => {
9536
- if (!features)
9537
- return false;
9538
- const deviceType = getDeviceType(features);
9539
- if (deviceType === 'touch') {
9540
- return false;
9541
- }
9542
- const currentVersion = getDeviceFirmwareVersion(features).join('.');
9543
- return semver__default["default"].gte(currentVersion, '2.3.0');
9544
- };
9545
-
9546
- const HD_HARDENED = 0x80000000;
9547
- const toHardened = (n) => (n | HD_HARDENED) >>> 0;
9548
- const fromHardened = (n) => (n & ~HD_HARDENED) >>> 0;
9549
- const PATH_NOT_VALID = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Not a valid path');
9550
- const PATH_NEGATIVE_VALUES = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Path cannot contain negative values');
9551
- const getHDPath = (path) => {
9552
- const parts = path.toLowerCase().split('/');
9553
- if (parts[0] !== 'm')
9554
- throw PATH_NOT_VALID;
9555
- return parts
9556
- .filter((p) => p !== 'm' && p !== '')
9557
- .map((p) => {
9558
- let hardened = false;
9559
- if (p.substr(p.length - 1) === "'") {
9560
- hardened = true;
9561
- p = p.substr(0, p.length - 1);
9562
- }
9563
- let n = parseInt(p);
9564
- if (Number.isNaN(n)) {
9565
- throw PATH_NOT_VALID;
9566
- }
9567
- else if (n < 0) {
9568
- throw PATH_NEGATIVE_VALUES;
9569
- }
9570
- if (hardened) {
9571
- n = toHardened(n);
9572
- }
9573
- return n;
9574
- });
9575
- };
9576
- const isMultisigPath = (path) => Array.isArray(path) && path[0] === toHardened(48);
9577
- const isSegwitPath = (path) => Array.isArray(path) && path[0] === toHardened(49);
9578
- const getScriptType = (path) => {
9579
- if (!Array.isArray(path) || path.length < 1)
9580
- return 'SPENDADDRESS';
9581
- const p1 = fromHardened(path[0]);
9582
- switch (p1) {
9583
- case 48:
9584
- return 'SPENDMULTISIG';
9585
- case 49:
9586
- return 'SPENDP2SHWITNESS';
9587
- case 84:
9588
- return 'SPENDWITNESS';
9589
- default:
9590
- return 'SPENDADDRESS';
9591
- }
9592
- };
9593
- const getOutputScriptType = (path) => {
9594
- if (!Array.isArray(path) || path.length < 1)
9595
- return 'PAYTOADDRESS';
9596
- if (path[0] === 49) {
9597
- return 'PAYTOP2SHWITNESS';
9598
- }
9599
- const p = fromHardened(path[0]);
9600
- switch (p) {
9601
- case 48:
9602
- return 'PAYTOMULTISIG';
9603
- case 49:
9604
- return 'PAYTOP2SHWITNESS';
9605
- case 84:
9606
- return 'PAYTOWITNESS';
9607
- default:
9608
- return 'PAYTOADDRESS';
9609
- }
9610
- };
9611
- const serializedPath = (path) => {
9612
- const pathStr = path
9613
- .map((p) => {
9614
- if (p & HD_HARDENED) {
9615
- return `${p & ~HD_HARDENED}'`;
9616
- }
9617
- return p;
9618
- })
9619
- .join('/');
9620
- return `m/${pathStr}`;
9621
- };
9622
- const validatePath = (path, length = 0, base = false) => {
9623
- let valid;
9624
- if (typeof path === 'string') {
9625
- valid = getHDPath(path);
9626
- }
9627
- else if (Array.isArray(path)) {
9628
- valid = path.map((p) => {
9629
- const n = parseInt(p);
9630
- if (Number.isNaN(n)) {
9631
- throw PATH_NOT_VALID;
9632
- }
9633
- else if (n < 0) {
9634
- throw PATH_NEGATIVE_VALUES;
9635
- }
9636
- return n;
9637
- });
9638
- }
9639
- else {
9640
- valid = undefined;
9641
- }
9642
- if (!valid)
9643
- throw PATH_NOT_VALID;
9644
- if (length > 0 && valid.length < length)
9645
- throw PATH_NOT_VALID;
9646
- return base ? valid.splice(0, 3) : valid;
9647
- };
9648
-
9649
9671
  const LOG_EVENT = 'LOG_EVENT';
9650
9672
  const LOG = {
9651
9673
  OUTPUT: 'log-output',
@@ -9972,6 +9994,7 @@ const UI_REQUEST$1 = {
9972
9994
  REQUEST_PIN: 'ui-request_pin',
9973
9995
  INVALID_PIN: 'ui-invalid_pin',
9974
9996
  REQUEST_BUTTON: 'ui-button',
9997
+ REQUEST_PASSPHRASE_ON_DEVICE: 'ui-request_passphrase_on_device',
9975
9998
  CLOSE_UI_WINDOW: 'ui-close_window',
9976
9999
  BLUETOOTH_PERMISSION: 'ui-bluetooth_permission',
9977
10000
  LOCATION_PERMISSION: 'ui-location_permission',
@@ -10093,11 +10116,11 @@ class DevicePool extends events.exports {
10093
10116
  static setConnector(connector) {
10094
10117
  this.connector = connector;
10095
10118
  }
10096
- static getDevices(descriptorList, connectId) {
10119
+ static getDevices(descriptorList, connectId, initOptions) {
10097
10120
  var descriptorList_1, descriptorList_1_1;
10098
10121
  var e_1, _a;
10099
10122
  return __awaiter(this, void 0, void 0, function* () {
10100
- Log$6.debug('get device list');
10123
+ Log$6.debug('get device list: connectId: ', connectId);
10101
10124
  const devices = {};
10102
10125
  const deviceList = [];
10103
10126
  if (connectId) {
@@ -10109,7 +10132,7 @@ class DevicePool extends events.exports {
10109
10132
  device.updateDescriptor(exist, true);
10110
10133
  devices[connectId] = device;
10111
10134
  deviceList.push(device);
10112
- yield this._checkDevicePool();
10135
+ yield this._checkDevicePool(initOptions);
10113
10136
  return { devices, deviceList };
10114
10137
  }
10115
10138
  Log$6.debug('found device in cache, but path is different: ', connectId);
@@ -10118,7 +10141,7 @@ class DevicePool extends events.exports {
10118
10141
  try {
10119
10142
  for (descriptorList_1 = __asyncValues(descriptorList); descriptorList_1_1 = yield descriptorList_1.next(), !descriptorList_1_1.done;) {
10120
10143
  const descriptor = descriptorList_1_1.value;
10121
- const device = yield this._createDevice(descriptor);
10144
+ const device = yield this._createDevice(descriptor, initOptions);
10122
10145
  if (device.features) {
10123
10146
  const uuid = getDeviceUUID(device.features);
10124
10147
  if (this.devicesCache[uuid]) {
@@ -10141,34 +10164,41 @@ class DevicePool extends events.exports {
10141
10164
  Log$6.debug('get devices result : ', devices, deviceList);
10142
10165
  console.log('device poll -> connected: ', this.connectedPool);
10143
10166
  console.log('device poll -> disconnected: ', this.disconnectPool);
10144
- yield this._checkDevicePool();
10167
+ yield this._checkDevicePool(initOptions);
10145
10168
  return { devices, deviceList };
10146
10169
  });
10147
10170
  }
10148
- static _createDevice(descriptor) {
10171
+ static clearDeviceCache(connectId) {
10172
+ Log$6.debug('clear device pool cache: connectId', connectId);
10173
+ Log$6.debug('clear device pool cache: ', this.devicesCache);
10174
+ if (connectId) {
10175
+ delete this.devicesCache[connectId];
10176
+ }
10177
+ }
10178
+ static _createDevice(descriptor, initOptions) {
10149
10179
  return __awaiter(this, void 0, void 0, function* () {
10150
10180
  let device = this.getDeviceByPath(descriptor.path);
10151
10181
  if (!device) {
10152
10182
  device = Device.fromDescriptor(descriptor);
10153
10183
  device.deviceConnector = this.connector;
10154
10184
  yield device.connect();
10155
- yield device.initialize();
10185
+ yield device.initialize(initOptions);
10156
10186
  yield device.release();
10157
10187
  }
10158
10188
  return device;
10159
10189
  });
10160
10190
  }
10161
- static _checkDevicePool() {
10191
+ static _checkDevicePool(initOptions) {
10162
10192
  return __awaiter(this, void 0, void 0, function* () {
10163
- yield this._sendConnectMessage();
10193
+ yield this._sendConnectMessage(initOptions);
10164
10194
  this._sendDisconnectMessage();
10165
10195
  });
10166
10196
  }
10167
- static _sendConnectMessage() {
10197
+ static _sendConnectMessage(initOptions) {
10168
10198
  return __awaiter(this, void 0, void 0, function* () {
10169
10199
  for (let i = this.connectedPool.length - 1; i >= 0; i--) {
10170
10200
  const descriptor = this.connectedPool[i];
10171
- const device = yield this._createDevice(descriptor);
10201
+ const device = yield this._createDevice(descriptor, initOptions);
10172
10202
  Log$6.debug('emit DEVICE.CONNECT: ', device);
10173
10203
  this.emitter.emit(DEVICE.CONNECT, device);
10174
10204
  this.connectedPool.splice(i, 1);
@@ -10402,7 +10432,6 @@ class DeviceCommands {
10402
10432
  });
10403
10433
  }
10404
10434
  _filterCommonTypes(res) {
10405
- var _a;
10406
10435
  Log$4.debug('_filterCommonTypes: ', res);
10407
10436
  if (res.type === 'Failure') {
10408
10437
  const { code } = res.message;
@@ -10420,6 +10449,9 @@ class DeviceCommands {
10420
10449
  if (code === 'Failure_PinCancelled') {
10421
10450
  error = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.PinCancelled);
10422
10451
  }
10452
+ if (code === 'Failure_DataError' && message === 'Please confirm the BlindSign enabled') {
10453
+ error = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlindSignDisabled);
10454
+ }
10423
10455
  if (error) {
10424
10456
  return Promise.reject(error);
10425
10457
  }
@@ -10450,11 +10482,10 @@ class DeviceCommands {
10450
10482
  return this._commonCall('PinMatrixAck', { pin });
10451
10483
  }, () => this._commonCall('Cancel', {}));
10452
10484
  }
10453
- if ((_a = this.device.features) === null || _a === void 0 ? void 0 : _a.passphrase_protection) {
10454
- return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotSupportPassphrase));
10455
- }
10456
10485
  if (res.type === 'PassphraseRequest') {
10457
- return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotSupportPassphrase));
10486
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotSupportPassphrase, 'Device not support passphrase', {
10487
+ require: '2.4.0',
10488
+ }));
10458
10489
  }
10459
10490
  if (res.type === 'Deprecated_PassphraseStateRequest') ;
10460
10491
  if (res.type === 'WordRequest') ;
@@ -10587,6 +10618,7 @@ const parseRunOptions = (options) => {
10587
10618
  return options;
10588
10619
  };
10589
10620
  const Log$3 = getLogger(exports.LoggerNames.Device);
10621
+ const deviceSessionCache = {};
10590
10622
  class Device extends events.exports {
10591
10623
  constructor(descriptor) {
10592
10624
  super();
@@ -10599,6 +10631,7 @@ class Device extends events.exports {
10599
10631
  this.internalState = [];
10600
10632
  this.needReloadDevice = false;
10601
10633
  this.keepSession = false;
10634
+ this.passphraseState = undefined;
10602
10635
  this.originalDescriptor = descriptor;
10603
10636
  }
10604
10637
  static fromDescriptor(originalDescriptor) {
@@ -10723,34 +10756,62 @@ class Device extends events.exports {
10723
10756
  getCommands() {
10724
10757
  return this.commands;
10725
10758
  }
10726
- getInternalState() {
10727
- return this.internalState[this.instance];
10759
+ getInternalState(_deviceId) {
10760
+ var _a, _b, _c;
10761
+ Log$3.debug('getInternalState session param: ', `device_id: ${_deviceId}`, `features.device_id: ${(_a = this.features) === null || _a === void 0 ? void 0 : _a.device_id}`, `passphraseState: ${this.passphraseState}`);
10762
+ Log$3.debug('getInternalState session cache: ', deviceSessionCache);
10763
+ const deviceId = _deviceId || ((_b = this.features) === null || _b === void 0 ? void 0 : _b.device_id);
10764
+ if (!deviceId)
10765
+ return undefined;
10766
+ const key = `${deviceId}`;
10767
+ const usePassKey = `${deviceId}@${this.passphraseState}`;
10768
+ const session = (_c = deviceSessionCache[key]) !== null && _c !== void 0 ? _c : deviceSessionCache[usePassKey];
10769
+ return this.passphraseState ? session : undefined;
10770
+ }
10771
+ setInternalState(state, initSession) {
10772
+ var _a;
10773
+ Log$3.debug('setInternalState session param: ', `state: ${state}`, `initSession: ${initSession}`, `device_id: ${(_a = this.features) === null || _a === void 0 ? void 0 : _a.device_id}`, `passphraseState: ${this.passphraseState}`);
10774
+ if (!this.features)
10775
+ return;
10776
+ if (!this.passphraseState && !initSession)
10777
+ return;
10778
+ let key = `${this.features.device_id}`;
10779
+ if (this.passphraseState) {
10780
+ key += `@${this.passphraseState}`;
10781
+ }
10782
+ if (state) {
10783
+ deviceSessionCache[key] = state;
10784
+ }
10785
+ Log$3.debug('setInternalState done session cache: ', deviceSessionCache);
10728
10786
  }
10729
- initialize() {
10787
+ clearInternalState(_deviceId) {
10788
+ var _a;
10789
+ Log$3.debug('clearInternalState param: ', _deviceId);
10790
+ const deviceId = _deviceId || ((_a = this.features) === null || _a === void 0 ? void 0 : _a.device_id);
10791
+ if (!deviceId)
10792
+ return;
10793
+ const key = `${deviceId}`;
10794
+ delete deviceSessionCache[key];
10795
+ if (this.passphraseState) {
10796
+ const usePassKey = `${deviceId}@${this.passphraseState}`;
10797
+ delete deviceSessionCache[usePassKey];
10798
+ }
10799
+ }
10800
+ initialize(options) {
10730
10801
  return __awaiter(this, void 0, void 0, function* () {
10731
- let payload;
10732
- if (this.features) {
10733
- const internalState = this.getInternalState();
10734
- payload = {};
10735
- if (internalState) {
10736
- payload.session_id = internalState;
10737
- }
10802
+ Log$3.debug('initialize param:', options);
10803
+ this.passphraseState = options === null || options === void 0 ? void 0 : options.passphraseState;
10804
+ if (options === null || options === void 0 ? void 0 : options.initSession) {
10805
+ this.clearInternalState(options === null || options === void 0 ? void 0 : options.deviceId);
10738
10806
  }
10739
- const { message } = yield this.commands.typedCall('Initialize', 'Features', payload);
10740
- this._updateFeatures(message);
10741
- if (message.passphrase_protection) {
10742
- if (this.listenerCount(DEVICE.PIN) > 0) {
10743
- Log$3.debug('try to close passpharse');
10744
- try {
10745
- yield this.commands.typedCall('ApplySettings', 'Success', { use_passphrase: false });
10746
- }
10747
- catch (e) {
10748
- yield this.release();
10749
- this.runPromise = null;
10750
- throw e;
10751
- }
10752
- }
10807
+ const internalState = this.getInternalState(options === null || options === void 0 ? void 0 : options.deviceId);
10808
+ const payload = {};
10809
+ if (internalState) {
10810
+ payload.session_id = internalState;
10753
10811
  }
10812
+ Log$3.debug('initialize payload:', payload);
10813
+ const { message } = yield this.commands.typedCall('Initialize', 'Features', payload);
10814
+ this._updateFeatures(message, options === null || options === void 0 ? void 0 : options.initSession);
10754
10815
  });
10755
10816
  }
10756
10817
  getFeatures() {
@@ -10759,10 +10820,13 @@ class Device extends events.exports {
10759
10820
  this._updateFeatures(message);
10760
10821
  });
10761
10822
  }
10762
- _updateFeatures(feat) {
10823
+ _updateFeatures(feat, initSession) {
10763
10824
  if (this.features && this.features.session_id && !feat.session_id) {
10764
10825
  feat.session_id = this.features.session_id;
10765
10826
  }
10827
+ if (this.features && this.features.device_id && feat.session_id) {
10828
+ this.setInternalState(feat.session_id, initSession);
10829
+ }
10766
10830
  feat.unlocked = feat.unlocked || true;
10767
10831
  this.features = feat;
10768
10832
  this.featuresNeedsReload = false;
@@ -10802,7 +10866,6 @@ class Device extends events.exports {
10802
10866
  });
10803
10867
  }
10804
10868
  _runInner(fn, options) {
10805
- var _a;
10806
10869
  return __awaiter(this, void 0, void 0, function* () {
10807
10870
  if (!this.isUsedHere() || this.commands.disposed) {
10808
10871
  const env = DataManager.getSettings('env');
@@ -10810,7 +10873,7 @@ class Device extends events.exports {
10810
10873
  yield this.acquire();
10811
10874
  try {
10812
10875
  if (fn) {
10813
- yield this.initialize();
10876
+ yield this.initialize(options);
10814
10877
  }
10815
10878
  }
10816
10879
  catch (error) {
@@ -10821,20 +10884,6 @@ class Device extends events.exports {
10821
10884
  return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInitializeFailed, `Initialize failed: ${error.message}, code: ${error.code}`));
10822
10885
  }
10823
10886
  }
10824
- else if (env === 'react-native') {
10825
- if ((_a = this.features) === null || _a === void 0 ? void 0 : _a.passphrase_protection) {
10826
- if (this.listenerCount(DEVICE.PIN) > 0) {
10827
- Log$3.debug('try to close passpharse for mobile');
10828
- try {
10829
- yield this.commands.typedCall('ApplySettings', 'Success', { use_passphrase: false });
10830
- }
10831
- catch (e) {
10832
- this.runPromise = null;
10833
- return Promise.reject(e);
10834
- }
10835
- }
10836
- }
10837
- }
10838
10887
  }
10839
10888
  if (options.keepSession) {
10840
10889
  this.keepSession = true;
@@ -10945,12 +10994,35 @@ class Device extends events.exports {
10945
10994
  }
10946
10995
  return null;
10947
10996
  }
10997
+ hasUsePassphrase() {
10998
+ var _a;
10999
+ const isModeT = getDeviceType(this.features) === 'touch' || getDeviceType(this.features) === 'pro';
11000
+ const preCheckTouch = isModeT && ((_a = this.features) === null || _a === void 0 ? void 0 : _a.unlocked) === true;
11001
+ return this.features && (!!this.features.passphrase_protection || preCheckTouch);
11002
+ }
10948
11003
  checkDeviceId(deviceId) {
10949
11004
  if (this.features) {
10950
11005
  return this.features.device_id === deviceId;
10951
11006
  }
10952
11007
  return false;
10953
11008
  }
11009
+ checkPassphraseState() {
11010
+ var _a;
11011
+ return __awaiter(this, void 0, void 0, function* () {
11012
+ if (!this.features)
11013
+ return false;
11014
+ const locked = ((_a = this.features) === null || _a === void 0 ? void 0 : _a.unlocked) === true;
11015
+ const isModeT = getDeviceType(this.features) === 'touch' || getDeviceType(this.features) === 'pro';
11016
+ const newState = yield getPassphraseState(this.features, this.commands);
11017
+ if (isModeT && locked) {
11018
+ yield this.getFeatures();
11019
+ }
11020
+ if (this.passphraseState && this.passphraseState !== newState) {
11021
+ this.clearInternalState();
11022
+ return newState;
11023
+ }
11024
+ });
11025
+ }
10954
11026
  }
10955
11027
 
10956
11028
  class DeviceList extends events.exports {
@@ -10958,13 +11030,13 @@ class DeviceList extends events.exports {
10958
11030
  super(...arguments);
10959
11031
  this.devices = {};
10960
11032
  }
10961
- getDeviceLists(connectId) {
11033
+ getDeviceLists(connectId, initOptions) {
10962
11034
  var _a, _b;
10963
11035
  return __awaiter(this, void 0, void 0, function* () {
10964
11036
  const deviceDiff = yield ((_a = this.connector) === null || _a === void 0 ? void 0 : _a.enumerate());
10965
11037
  const descriptorList = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
10966
11038
  this.devices = {};
10967
- const { deviceList, devices } = yield DevicePool.getDevices(descriptorList, connectId);
11039
+ const { deviceList, devices } = yield DevicePool.getDevices(descriptorList, connectId, initOptions);
10968
11040
  this.devices = devices;
10969
11041
  return deviceList;
10970
11042
  });
@@ -11002,6 +11074,7 @@ class BaseMethod {
11002
11074
  constructor(message) {
11003
11075
  this.shouldEnsureConnected = true;
11004
11076
  this.checkDeviceId = false;
11077
+ this.useDevicePassphraseState = true;
11005
11078
  const { payload } = message;
11006
11079
  this.name = payload.method;
11007
11080
  this.payload = payload;
@@ -11042,6 +11115,7 @@ class BaseMethod {
11042
11115
  class SearchDevices extends BaseMethod {
11043
11116
  init() {
11044
11117
  this.useDevice = false;
11118
+ this.useDevicePassphraseState = false;
11045
11119
  }
11046
11120
  run() {
11047
11121
  var _a, _b;
@@ -11065,6 +11139,7 @@ class SearchDevices extends BaseMethod {
11065
11139
  class GetFeatures extends BaseMethod {
11066
11140
  init() {
11067
11141
  this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE, UI_REQUEST.BOOTLOADER];
11142
+ this.useDevicePassphraseState = false;
11068
11143
  }
11069
11144
  run() {
11070
11145
  return Promise.resolve(this.device.features);
@@ -11975,7 +12050,9 @@ class BTCVerifyMessage extends BaseMethod {
11975
12050
  }
11976
12051
 
11977
12052
  class CheckFirmwareRelease extends BaseMethod {
11978
- init() { }
12053
+ init() {
12054
+ this.useDevicePassphraseState = false;
12055
+ }
11979
12056
  run() {
11980
12057
  if (this.device.features) {
11981
12058
  const releaseInfo = getFirmwareReleaseInfo(this.device.features);
@@ -11989,6 +12066,7 @@ class CheckBLEFirmwareRelease extends BaseMethod {
11989
12066
  init() {
11990
12067
  this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.BOOTLOADER];
11991
12068
  this.checkDeviceId = true;
12069
+ this.useDevicePassphraseState = false;
11992
12070
  }
11993
12071
  run() {
11994
12072
  if (this.device.features) {
@@ -12002,6 +12080,7 @@ class CheckBLEFirmwareRelease extends BaseMethod {
12002
12080
  class CheckTransportRelease extends BaseMethod {
12003
12081
  init() {
12004
12082
  this.useDevice = false;
12083
+ this.useDevicePassphraseState = false;
12005
12084
  }
12006
12085
  run() {
12007
12086
  return __awaiter(this, void 0, void 0, function* () {
@@ -12016,6 +12095,7 @@ class CheckTransportRelease extends BaseMethod {
12016
12095
  class CheckBridgeStatus$1 extends BaseMethod {
12017
12096
  init() {
12018
12097
  this.useDevice = false;
12098
+ this.useDevicePassphraseState = false;
12019
12099
  }
12020
12100
  run() {
12021
12101
  return __awaiter(this, void 0, void 0, function* () {
@@ -12042,7 +12122,9 @@ class CheckBridgeStatus$1 extends BaseMethod {
12042
12122
  }
12043
12123
 
12044
12124
  class DeviceBackup extends BaseMethod {
12045
- init() { }
12125
+ init() {
12126
+ this.useDevicePassphraseState = false;
12127
+ }
12046
12128
  run() {
12047
12129
  return __awaiter(this, void 0, void 0, function* () {
12048
12130
  const res = yield this.device.commands.typedCall('BackupDevice', 'Success');
@@ -12053,6 +12135,7 @@ class DeviceBackup extends BaseMethod {
12053
12135
 
12054
12136
  class DeviceChangePin extends BaseMethod {
12055
12137
  init() {
12138
+ this.useDevicePassphraseState = false;
12056
12139
  validateParams(this.payload, [{ name: 'remove', type: 'boolean' }]);
12057
12140
  this.params = {
12058
12141
  remove: this.payload.remove,
@@ -12068,6 +12151,7 @@ class DeviceChangePin extends BaseMethod {
12068
12151
 
12069
12152
  class DeviceFlags extends BaseMethod {
12070
12153
  init() {
12154
+ this.useDevicePassphraseState = false;
12071
12155
  validateParams(this.payload, [{ name: 'flags', type: 'number' }]);
12072
12156
  this.params = {
12073
12157
  flags: this.payload.flags,
@@ -12082,7 +12166,9 @@ class DeviceFlags extends BaseMethod {
12082
12166
  }
12083
12167
 
12084
12168
  class DeviceRebootToBootloader extends BaseMethod {
12085
- init() { }
12169
+ init() {
12170
+ this.useDevicePassphraseState = false;
12171
+ }
12086
12172
  getVersionRange() {
12087
12173
  return {
12088
12174
  classic: {
@@ -12103,6 +12189,7 @@ class DeviceRebootToBootloader extends BaseMethod {
12103
12189
 
12104
12190
  class DeviceRecovery extends BaseMethod {
12105
12191
  init() {
12192
+ this.useDevicePassphraseState = false;
12106
12193
  validateParams(this.payload, [
12107
12194
  { name: 'wordCount', type: 'number' },
12108
12195
  { name: 'passphraseProtection', type: 'boolean' },
@@ -12136,6 +12223,7 @@ class DeviceRecovery extends BaseMethod {
12136
12223
 
12137
12224
  class DeviceReset extends BaseMethod {
12138
12225
  init() {
12226
+ this.useDevicePassphraseState = false;
12139
12227
  validateParams(this.payload, [
12140
12228
  { name: 'displayRandom', type: 'boolean' },
12141
12229
  { name: 'strength', type: 'number' },
@@ -12171,6 +12259,7 @@ class DeviceReset extends BaseMethod {
12171
12259
 
12172
12260
  class DeviceSettings extends BaseMethod {
12173
12261
  init() {
12262
+ this.useDevicePassphraseState = false;
12174
12263
  validateParams(this.payload, [
12175
12264
  { name: 'language', type: 'string' },
12176
12265
  { name: 'label', type: 'string' },
@@ -12183,7 +12272,6 @@ class DeviceSettings extends BaseMethod {
12183
12272
  { name: 'safetyChecks', type: 'object' },
12184
12273
  { name: 'experimentalFeatures', type: 'boolean' },
12185
12274
  ]);
12186
- console.log('DeviceSettings payload', this.payload);
12187
12275
  this.params = {
12188
12276
  language: this.payload.language,
12189
12277
  label: this.payload.label,
@@ -12197,6 +12285,16 @@ class DeviceSettings extends BaseMethod {
12197
12285
  experimental_features: this.payload.experimentalFeatures,
12198
12286
  };
12199
12287
  }
12288
+ getVersionRange() {
12289
+ if (this.payload.usePassphrase) {
12290
+ return {
12291
+ model_mini: {
12292
+ min: '2.4.0',
12293
+ },
12294
+ };
12295
+ }
12296
+ return {};
12297
+ }
12200
12298
  run() {
12201
12299
  return __awaiter(this, void 0, void 0, function* () {
12202
12300
  const res = yield this.device.commands.typedCall('ApplySettings', 'Success', Object.assign({}, this.params));
@@ -12206,7 +12304,9 @@ class DeviceSettings extends BaseMethod {
12206
12304
  }
12207
12305
 
12208
12306
  class DeviceUpdateReboot extends BaseMethod {
12209
- init() { }
12307
+ init() {
12308
+ this.useDevicePassphraseState = false;
12309
+ }
12210
12310
  run() {
12211
12311
  return __awaiter(this, void 0, void 0, function* () {
12212
12312
  const res = yield this.device.commands.typedCall('BixinReboot', 'Success');
@@ -12216,7 +12316,9 @@ class DeviceUpdateReboot extends BaseMethod {
12216
12316
  }
12217
12317
 
12218
12318
  class DeviceSupportFeatures extends BaseMethod {
12219
- init() { }
12319
+ init() {
12320
+ this.useDevicePassphraseState = false;
12321
+ }
12220
12322
  run() {
12221
12323
  if (!this.device.features)
12222
12324
  return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'Device not initialized'));
@@ -12230,6 +12332,7 @@ class DeviceSupportFeatures extends BaseMethod {
12230
12332
 
12231
12333
  class DeviceVerify extends BaseMethod {
12232
12334
  init() {
12335
+ this.useDevicePassphraseState = false;
12233
12336
  validateParams(this.payload, [{ name: 'dataHex', type: 'hexString' }]);
12234
12337
  this.params = {
12235
12338
  data: formatAnyHex(this.payload.dataHex),
@@ -12261,7 +12364,9 @@ class DeviceVerify extends BaseMethod {
12261
12364
  }
12262
12365
 
12263
12366
  class DeviceWipe extends BaseMethod {
12264
- init() { }
12367
+ init() {
12368
+ this.useDevicePassphraseState = false;
12369
+ }
12265
12370
  run() {
12266
12371
  return __awaiter(this, void 0, void 0, function* () {
12267
12372
  const res = yield this.device.commands.typedCall('WipeDevice', 'Success');
@@ -13616,6 +13721,7 @@ class FirmwareUpdate extends BaseMethod {
13616
13721
  init() {
13617
13722
  this.allowDeviceMode = [UI_REQUEST.BOOTLOADER, UI_REQUEST.INITIALIZE];
13618
13723
  this.requireDeviceMode = [UI_REQUEST.BOOTLOADER];
13724
+ this.useDevicePassphraseState = false;
13619
13725
  const { payload } = this;
13620
13726
  validateParams(payload, [
13621
13727
  { name: 'version', type: 'array' },
@@ -13665,6 +13771,7 @@ const Log$2 = getLogger(exports.LoggerNames.Method);
13665
13771
  class RequestWebUsbDevice extends BaseMethod {
13666
13772
  init() {
13667
13773
  this.useDevice = false;
13774
+ this.useDevicePassphraseState = false;
13668
13775
  }
13669
13776
  run() {
13670
13777
  var _a, _b;
@@ -13691,9 +13798,36 @@ class RequestWebUsbDevice extends BaseMethod {
13691
13798
  }
13692
13799
  }
13693
13800
 
13801
+ class GetPassphraseState extends BaseMethod {
13802
+ init() {
13803
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
13804
+ this.useDevicePassphraseState = false;
13805
+ }
13806
+ run() {
13807
+ var _a, _b;
13808
+ return __awaiter(this, void 0, void 0, function* () {
13809
+ if (!this.device.features)
13810
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceInitializeFailed));
13811
+ let { features } = this.device;
13812
+ const locked = ((_b = (_a = this.device) === null || _a === void 0 ? void 0 : _a.features) === null || _b === void 0 ? void 0 : _b.unlocked) === true;
13813
+ const passphraseState = yield getPassphraseState(this.device.features, this.device.commands);
13814
+ const isModeT = getDeviceType(features) === 'touch' || getDeviceType(features) === 'pro';
13815
+ if (isModeT && locked) {
13816
+ const { message } = yield this.device.commands.typedCall('GetFeatures', 'Features', {});
13817
+ features = message;
13818
+ }
13819
+ if (features && features.passphrase_protection === true) {
13820
+ return Promise.resolve(passphraseState);
13821
+ }
13822
+ return Promise.resolve(undefined);
13823
+ });
13824
+ }
13825
+ }
13826
+
13694
13827
  class CheckBridgeStatus extends BaseMethod {
13695
13828
  init() {
13696
13829
  this.useDevice = false;
13830
+ this.useDevicePassphraseState = false;
13697
13831
  }
13698
13832
  run() {
13699
13833
  return __awaiter(this, void 0, void 0, function* () {
@@ -13748,6 +13882,7 @@ var ApiMethods = /*#__PURE__*/Object.freeze({
13748
13882
  stellarSignTransaction: StellarSignTransaction,
13749
13883
  firmwareUpdate: FirmwareUpdate,
13750
13884
  requestWebUsbDevice: RequestWebUsbDevice,
13885
+ getPassphraseState: GetPassphraseState,
13751
13886
  getLogs: CheckBridgeStatus
13752
13887
  });
13753
13888
 
@@ -13880,6 +14015,11 @@ class DeviceConnector {
13880
14015
  }
13881
14016
 
13882
14017
  const Log = getLogger(exports.LoggerNames.Core);
14018
+ const parseInitOptions = (method) => ({
14019
+ initSession: method === null || method === void 0 ? void 0 : method.payload.initSession,
14020
+ passphraseState: method === null || method === void 0 ? void 0 : method.payload.passphraseState,
14021
+ deviceId: method === null || method === void 0 ? void 0 : method.payload.deviceId,
14022
+ });
13883
14023
  let _core;
13884
14024
  let _deviceList;
13885
14025
  let _connector;
@@ -13889,6 +14029,9 @@ const callApiQueue = [];
13889
14029
  const deviceCacheMap = new Map();
13890
14030
  let pollingId = 1;
13891
14031
  const pollingState = {};
14032
+ let preConnectCache = {
14033
+ passphraseState: undefined,
14034
+ };
13892
14035
  const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13893
14036
  var _a;
13894
14037
  if (!message.id || !message.payload || message.type !== IFRAME.CALL) {
@@ -13919,6 +14062,14 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13919
14062
  if (callApiQueue.length > 1) {
13920
14063
  Log.debug('should cancel the previous method execution: ', callApiQueue.map(m => m.name));
13921
14064
  }
14065
+ const connectStateChange = preConnectCache.passphraseState !== method.payload.passphraseState;
14066
+ preConnectCache = {
14067
+ passphraseState: method.payload.passphraseState,
14068
+ };
14069
+ if (connectStateChange || method.payload.initSession) {
14070
+ Log.debug('passphrase state change, clear device cache');
14071
+ DevicePool.clearDeviceCache(method.payload.connectId);
14072
+ }
13922
14073
  if (pollingState[pollingId]) {
13923
14074
  pollingState[pollingId] = false;
13924
14075
  }
@@ -13934,6 +14085,7 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13934
14085
  (_a = method.setDevice) === null || _a === void 0 ? void 0 : _a.call(method, device);
13935
14086
  device.on(DEVICE.PIN, onDevicePinHandler);
13936
14087
  device.on(DEVICE.BUTTON, onDeviceButtonHandler);
14088
+ device.on(DEVICE.PASSPHRASE_ON_DEVICE, onDevicePassphraseHandler);
13937
14089
  device.on(DEVICE.FEATURES, onDeviceFeaturesHandler);
13938
14090
  try {
13939
14091
  const inner = () => __awaiter(void 0, void 0, void 0, function* () {
@@ -13971,6 +14123,21 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13971
14123
  if (_deviceList) {
13972
14124
  yield TransportManager.reconfigure(device.getFirmwareVersion());
13973
14125
  }
14126
+ checkPassphraseSafety(method, device.features);
14127
+ if (device.hasUsePassphrase() && method.useDevicePassphraseState) {
14128
+ const support = supportNewPassphrase(device.features);
14129
+ if (!support.support) {
14130
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotSupportPassphrase, `Device not support passphrase, please update to ${support.require}`, {
14131
+ require: support.require,
14132
+ }));
14133
+ }
14134
+ const passphraseState = yield device.checkPassphraseState();
14135
+ checkPassphraseSafety(method, device.features);
14136
+ if (passphraseState) {
14137
+ DevicePool.clearDeviceCache(method.payload.connectId);
14138
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceCheckPassphraseStateError));
14139
+ }
14140
+ }
13974
14141
  try {
13975
14142
  const response = yield method.run();
13976
14143
  Log.debug('Call API - Inner Method Run: ');
@@ -13984,7 +14151,8 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13984
14151
  }
13985
14152
  });
13986
14153
  Log.debug('Call API - Device Run: ', device.mainId);
13987
- const deviceRun = () => device.run(inner);
14154
+ const runOptions = Object.assign({ keepSession: method.payload.keepSession }, parseInitOptions(method));
14155
+ const deviceRun = () => device.run(inner, runOptions);
13988
14156
  _callPromise = hdShared.createDeferred(deviceRun);
13989
14157
  try {
13990
14158
  return yield _callPromise.promise;
@@ -14030,7 +14198,7 @@ function initDeviceList(method) {
14030
14198
  yield TransportManager.configure();
14031
14199
  _deviceList.connector = _connector;
14032
14200
  }
14033
- yield _deviceList.getDeviceLists(method.connectId);
14201
+ yield _deviceList.getDeviceLists(method.connectId, parseInitOptions(method));
14034
14202
  });
14035
14203
  }
14036
14204
  function initDevice(method) {
@@ -14123,7 +14291,7 @@ const ensureConnected = (method, pollingId) => __awaiter(void 0, void 0, void 0,
14123
14291
  }
14124
14292
  if (env === 'react-native') {
14125
14293
  yield device.acquire();
14126
- yield device.initialize();
14294
+ yield device.initialize(parseInitOptions(method));
14127
14295
  }
14128
14296
  resolve(device);
14129
14297
  return;
@@ -14137,6 +14305,7 @@ const ensureConnected = (method, pollingId) => __awaiter(void 0, void 0, void 0,
14137
14305
  hdShared.HardwareErrorCode.BleDeviceNotBonded,
14138
14306
  hdShared.HardwareErrorCode.BleCharacteristicNotifyError,
14139
14307
  hdShared.HardwareErrorCode.BleWriteCharacteristicError,
14308
+ hdShared.HardwareErrorCode.BleAlreadyConnected,
14140
14309
  ].includes(error.errorCode)) {
14141
14310
  reject(error);
14142
14311
  return;
@@ -14176,6 +14345,18 @@ const cancel = (connectId) => {
14176
14345
  cleanup();
14177
14346
  closePopup();
14178
14347
  };
14348
+ const checkPassphraseSafety = (method, features) => {
14349
+ if (!method.useDevicePassphraseState)
14350
+ return;
14351
+ if ((features === null || features === void 0 ? void 0 : features.passphrase_protection) === true && !method.payload.passphraseState) {
14352
+ DevicePool.clearDeviceCache(method.payload.connectId);
14353
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceOpenedPassphrase);
14354
+ }
14355
+ if ((features === null || features === void 0 ? void 0 : features.passphrase_protection) === false && method.payload.passphraseState) {
14356
+ DevicePool.clearDeviceCache(method.payload.connectId);
14357
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.DeviceNotOpenedPassphrase);
14358
+ }
14359
+ };
14179
14360
  const cleanup = () => {
14180
14361
  _uiPromises = [];
14181
14362
  Log.debug('Cleanup...');
@@ -14183,6 +14364,7 @@ const cleanup = () => {
14183
14364
  const removeDeviceListener = (device) => {
14184
14365
  device.removeListener(DEVICE.PIN, onDevicePinHandler);
14185
14366
  device.removeListener(DEVICE.BUTTON, onDeviceButtonHandler);
14367
+ device.removeListener(DEVICE.PASSPHRASE_ON_DEVICE, onDevicePassphraseHandler);
14186
14368
  device.removeListener(DEVICE.FEATURES, onDeviceFeaturesHandler);
14187
14369
  DevicePool.emitter.removeListener(DEVICE.CONNECT, onDeviceConnectHandler);
14188
14370
  };
@@ -14224,6 +14406,12 @@ const onDeviceButtonHandler = (...[device, request]) => {
14224
14406
  const onDeviceFeaturesHandler = (...[_, features]) => {
14225
14407
  postMessage(createDeviceMessage(DEVICE.FEATURES, Object.assign({}, features)));
14226
14408
  };
14409
+ const onDevicePassphraseHandler = (...[device]) => {
14410
+ postMessage(createUiMessage(UI_REQUEST$1.REQUEST_PASSPHRASE_ON_DEVICE, {
14411
+ device: device.toMessageObject(),
14412
+ passphraseState: device.passphraseState,
14413
+ }));
14414
+ };
14227
14415
  const postMessage = (message) => {
14228
14416
  _core.emit(CORE_EVENT, message);
14229
14417
  };