@onekeyfe/hd-core 0.1.13 → 0.1.17

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 (47) hide show
  1. package/dist/api/CheckBridgeStatus.d.ts.map +1 -1
  2. package/dist/api/GetLogs.d.ts +11 -0
  3. package/dist/api/GetLogs.d.ts.map +1 -0
  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.map +1 -1
  9. package/dist/events/core.d.ts +2 -1
  10. package/dist/events/core.d.ts.map +1 -1
  11. package/dist/events/index.d.ts +1 -0
  12. package/dist/events/index.d.ts.map +1 -1
  13. package/dist/events/log.d.ts +15 -0
  14. package/dist/events/log.d.ts.map +1 -0
  15. package/dist/index.d.ts +56 -3
  16. package/dist/index.js +484 -312
  17. package/dist/inject.d.ts.map +1 -1
  18. package/dist/types/api/getLogs.d.ts +3 -0
  19. package/dist/types/api/getLogs.d.ts.map +1 -0
  20. package/dist/types/api/index.d.ts +2 -0
  21. package/dist/types/api/index.d.ts.map +1 -1
  22. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  23. package/dist/utils/index.d.ts +1 -2
  24. package/dist/utils/index.d.ts.map +1 -1
  25. package/dist/utils/logger.d.ts +56 -0
  26. package/dist/utils/logger.d.ts.map +1 -0
  27. package/package.json +4 -4
  28. package/src/api/CheckBridgeStatus.ts +10 -2
  29. package/src/api/FirmwareUpdate.ts +1 -1
  30. package/src/api/GetLogs.ts +13 -0
  31. package/src/api/device/DeviceVerify.ts +1 -1
  32. package/src/api/index.ts +2 -0
  33. package/src/core/index.ts +9 -6
  34. package/src/data-manager/TransportManager.ts +11 -6
  35. package/src/device/Device.ts +2 -2
  36. package/src/device/DeviceCommands.ts +5 -5
  37. package/src/device/DeviceConnector.ts +3 -3
  38. package/src/device/DeviceList.ts +3 -3
  39. package/src/events/core.ts +2 -0
  40. package/src/events/index.ts +1 -0
  41. package/src/events/log.ts +23 -0
  42. package/src/inject.ts +2 -0
  43. package/src/types/api/getLogs.ts +3 -0
  44. package/src/types/api/index.ts +2 -0
  45. package/src/utils/deviceFeaturesUtils.ts +2 -10
  46. package/src/utils/index.ts +1 -3
  47. package/src/utils/logger.ts +179 -0
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ const inject = ({ call, cancel, dispose, eventEmitter, init, uiResponse, }) => {
32
32
  dispose,
33
33
  uiResponse,
34
34
  cancel,
35
+ getLogs: () => call({ method: 'getLogs' }),
35
36
  searchDevices: () => call({ method: 'searchDevices' }),
36
37
  getFeatures: connectId => call({ connectId, method: 'getFeatures' }),
37
38
  checkFirmwareRelease: connectId => call({ connectId, method: 'checkFirmwareRelease' }),
@@ -610,281 +611,6 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
610
611
  }
611
612
  }
612
613
 
613
- const httpRequest$1 = (url, type = 'text') => __awaiter(void 0, void 0, void 0, function* () {
614
- const response = yield axios__default["default"].request({
615
- url,
616
- withCredentials: false,
617
- responseType: type === 'binary' ? 'arraybuffer' : 'json',
618
- });
619
- if (+response.status === 200) {
620
- if (type === 'json') {
621
- return response.data;
622
- }
623
- if (type === 'binary') {
624
- return response.data;
625
- }
626
- return response.data;
627
- }
628
- throw new Error(`httpRequest error: ${url} ${response.statusText}`);
629
- });
630
-
631
- const httpRequest = (url, type) => httpRequest$1(url, type);
632
- const getTimeStamp = () => new Date().getTime();
633
-
634
- const VER_NUMS = 3;
635
- const versionRegex = new RegExp(/^[0-9]{1,3}(\.[0-9]{1,3}){0,2}$/);
636
- const isValidVersionString = (version) => versionRegex.test(version);
637
- const isValidVersionArray = (version) => {
638
- if (!Array.isArray(version)) {
639
- return false;
640
- }
641
- if (version.length === 0 || version.length > VER_NUMS) {
642
- return false;
643
- }
644
- if (version[0] === 0) {
645
- return false;
646
- }
647
- for (let i = 0; i < version.length; i++) {
648
- if (typeof version[i] !== 'number' || version[i] < 0) {
649
- return false;
650
- }
651
- }
652
- return true;
653
- };
654
- const normalizeVersionArray = (version) => {
655
- if (version.length === VER_NUMS) {
656
- return version;
657
- }
658
- const partialVersion = [...version];
659
- for (let i = version.length; i < VER_NUMS; i++) {
660
- partialVersion.push(0);
661
- }
662
- return partialVersion;
663
- };
664
- const versionSplit = (version) => {
665
- if (!isValidVersionString(version)) {
666
- return [0, 0, 0];
667
- }
668
- return version.split('.').map(v => Number(v));
669
- };
670
- const versionCompare = (a, b) => {
671
- if (typeof a === 'string' && typeof b === 'string' && a === b) {
672
- return 0;
673
- }
674
- const pa = typeof a === 'string' ? versionSplit(a) : a;
675
- const pb = typeof b === 'string' ? versionSplit(b) : b;
676
- const vpa = isValidVersionArray(pa);
677
- const vpb = isValidVersionArray(pb);
678
- if (!vpa && !vpb) {
679
- return 0;
680
- }
681
- if (!vpa && vpb) {
682
- return -1;
683
- }
684
- if (vpa && !vpb) {
685
- return 1;
686
- }
687
- const npa = normalizeVersionArray(pa);
688
- const npb = normalizeVersionArray(pb);
689
- for (let i = 0; i < VER_NUMS; i++) {
690
- if (npa[i] > npb[i])
691
- return 1;
692
- if (npb[i] > npa[i])
693
- return -1;
694
- }
695
- return 0;
696
- };
697
-
698
- function patchFeatures(response) {
699
- if (response.type !== 'Features') {
700
- return response;
701
- }
702
- if (response.message.major_version < 1) {
703
- response.message.major_version = 1;
704
- }
705
- return response;
706
- }
707
-
708
- const getDeviceModel = (features) => {
709
- if (!features || typeof features !== 'object') {
710
- return 'model_mini';
711
- }
712
- if (features.model === '1') {
713
- return 'model_mini';
714
- }
715
- return 'model_touch';
716
- };
717
- const getDeviceType = (features) => {
718
- if (!features || typeof features !== 'object' || !features.serial_no) {
719
- return 'classic';
720
- }
721
- const serialNo = features.serial_no;
722
- const miniFlag = serialNo.slice(0, 2);
723
- if (miniFlag.toLowerCase() === 'mi')
724
- return 'mini';
725
- if (miniFlag.toLowerCase() === 'tc')
726
- return 'touch';
727
- return 'classic';
728
- };
729
- const getDeviceTypeOnBootloader = (features) => {
730
- if (!features || typeof features !== 'object') {
731
- return 'classic';
732
- }
733
- if (features.model === 'T') {
734
- return 'touch';
735
- }
736
- return getDeviceType(features);
737
- };
738
- const getDeviceTypeByBleName = (name) => {
739
- if (!name)
740
- return 'classic';
741
- if (name.startsWith('MI'))
742
- return 'mini';
743
- if (name.startsWith('T'))
744
- return 'touch';
745
- return 'classic';
746
- };
747
- const getDeviceTypeByDeviceId = (deviceId) => {
748
- if (!deviceId) {
749
- return 'classic';
750
- }
751
- const miniFlag = deviceId.slice(0, 2);
752
- if (miniFlag.toLowerCase() === 'mi')
753
- return 'mini';
754
- return 'classic';
755
- };
756
- const getDeviceUUID = (features) => {
757
- const deviceType = getDeviceType(features);
758
- if (deviceType === 'classic') {
759
- return features.onekey_serial;
760
- }
761
- return features.serial_no;
762
- };
763
- const getDeviceLabel = (features) => {
764
- const deviceType = getDeviceType(features);
765
- if (typeof features.label === 'string') {
766
- return features.label;
767
- }
768
- return `My OneKey ${deviceType.charAt(0).toUpperCase() + deviceType.slice(1)}`;
769
- };
770
- const getDeviceFirmwareVersion = (features) => {
771
- if (!features)
772
- return [0, 0, 0];
773
- if (features.onekey_version) {
774
- return features.onekey_version.split('.');
775
- }
776
- return [features.major_version, features.minor_version, features.patch_version];
777
- };
778
- const getDeviceBLEFirmwareVersion = (features) => {
779
- if (!features.ble_ver) {
780
- return null;
781
- }
782
- return features.ble_ver.split('.');
783
- };
784
-
785
- const HD_HARDENED = 0x80000000;
786
- const toHardened = (n) => (n | HD_HARDENED) >>> 0;
787
- const fromHardened = (n) => (n & ~HD_HARDENED) >>> 0;
788
- const PATH_NOT_VALID = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Not a valid path');
789
- const PATH_NEGATIVE_VALUES = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Path cannot contain negative values');
790
- const getHDPath = (path) => {
791
- const parts = path.toLowerCase().split('/');
792
- if (parts[0] !== 'm')
793
- throw PATH_NOT_VALID;
794
- return parts
795
- .filter((p) => p !== 'm' && p !== '')
796
- .map((p) => {
797
- let hardened = false;
798
- if (p.substr(p.length - 1) === "'") {
799
- hardened = true;
800
- p = p.substr(0, p.length - 1);
801
- }
802
- let n = parseInt(p);
803
- if (Number.isNaN(n)) {
804
- throw PATH_NOT_VALID;
805
- }
806
- else if (n < 0) {
807
- throw PATH_NEGATIVE_VALUES;
808
- }
809
- if (hardened) {
810
- n = toHardened(n);
811
- }
812
- return n;
813
- });
814
- };
815
- const isMultisigPath = (path) => Array.isArray(path) && path[0] === toHardened(48);
816
- const isSegwitPath = (path) => Array.isArray(path) && path[0] === toHardened(49);
817
- const getScriptType = (path) => {
818
- if (!Array.isArray(path) || path.length < 1)
819
- return 'SPENDADDRESS';
820
- const p1 = fromHardened(path[0]);
821
- switch (p1) {
822
- case 48:
823
- return 'SPENDMULTISIG';
824
- case 49:
825
- return 'SPENDP2SHWITNESS';
826
- case 84:
827
- return 'SPENDWITNESS';
828
- default:
829
- return 'SPENDADDRESS';
830
- }
831
- };
832
- const getOutputScriptType = (path) => {
833
- if (!Array.isArray(path) || path.length < 1)
834
- return 'PAYTOADDRESS';
835
- if (path[0] === 49) {
836
- return 'PAYTOP2SHWITNESS';
837
- }
838
- const p = fromHardened(path[0]);
839
- switch (p) {
840
- case 48:
841
- return 'PAYTOMULTISIG';
842
- case 49:
843
- return 'PAYTOP2SHWITNESS';
844
- case 84:
845
- return 'PAYTOWITNESS';
846
- default:
847
- return 'PAYTOADDRESS';
848
- }
849
- };
850
- const serializedPath = (path) => {
851
- const pathStr = path
852
- .map((p) => {
853
- if (p & HD_HARDENED) {
854
- return `${p & ~HD_HARDENED}'`;
855
- }
856
- return p;
857
- })
858
- .join('/');
859
- return `m/${pathStr}`;
860
- };
861
- const validatePath = (path, length = 0, base = false) => {
862
- let valid;
863
- if (typeof path === 'string') {
864
- valid = getHDPath(path);
865
- }
866
- else if (Array.isArray(path)) {
867
- valid = path.map((p) => {
868
- const n = parseInt(p);
869
- if (Number.isNaN(n)) {
870
- throw PATH_NOT_VALID;
871
- }
872
- else if (n < 0) {
873
- throw PATH_NEGATIVE_VALUES;
874
- }
875
- return n;
876
- });
877
- }
878
- else {
879
- valid = undefined;
880
- }
881
- if (!valid)
882
- throw PATH_NOT_VALID;
883
- if (length > 0 && valid.length < length)
884
- throw PATH_NOT_VALID;
885
- return base ? valid.splice(0, 3) : valid;
886
- };
887
-
888
614
  var nested = {
889
615
  BinanceGetAddress: {
890
616
  fields: {
@@ -9634,10 +9360,427 @@ var nested = {
9634
9360
  }
9635
9361
  }
9636
9362
  };
9637
- var MessagesJSON = {
9638
- nested: nested
9363
+ var MessagesJSON = {
9364
+ nested: nested
9365
+ };
9366
+
9367
+ const httpRequest$1 = (url, type = 'text') => __awaiter(void 0, void 0, void 0, function* () {
9368
+ const response = yield axios__default["default"].request({
9369
+ url,
9370
+ withCredentials: false,
9371
+ responseType: type === 'binary' ? 'arraybuffer' : 'json',
9372
+ });
9373
+ if (+response.status === 200) {
9374
+ if (type === 'json') {
9375
+ return response.data;
9376
+ }
9377
+ if (type === 'binary') {
9378
+ return response.data;
9379
+ }
9380
+ return response.data;
9381
+ }
9382
+ throw new Error(`httpRequest error: ${url} ${response.statusText}`);
9383
+ });
9384
+
9385
+ const httpRequest = (url, type) => httpRequest$1(url, type);
9386
+ const getTimeStamp = () => new Date().getTime();
9387
+
9388
+ const VER_NUMS = 3;
9389
+ const versionRegex = new RegExp(/^[0-9]{1,3}(\.[0-9]{1,3}){0,2}$/);
9390
+ const isValidVersionString = (version) => versionRegex.test(version);
9391
+ const isValidVersionArray = (version) => {
9392
+ if (!Array.isArray(version)) {
9393
+ return false;
9394
+ }
9395
+ if (version.length === 0 || version.length > VER_NUMS) {
9396
+ return false;
9397
+ }
9398
+ if (version[0] === 0) {
9399
+ return false;
9400
+ }
9401
+ for (let i = 0; i < version.length; i++) {
9402
+ if (typeof version[i] !== 'number' || version[i] < 0) {
9403
+ return false;
9404
+ }
9405
+ }
9406
+ return true;
9407
+ };
9408
+ const normalizeVersionArray = (version) => {
9409
+ if (version.length === VER_NUMS) {
9410
+ return version;
9411
+ }
9412
+ const partialVersion = [...version];
9413
+ for (let i = version.length; i < VER_NUMS; i++) {
9414
+ partialVersion.push(0);
9415
+ }
9416
+ return partialVersion;
9417
+ };
9418
+ const versionSplit = (version) => {
9419
+ if (!isValidVersionString(version)) {
9420
+ return [0, 0, 0];
9421
+ }
9422
+ return version.split('.').map(v => Number(v));
9423
+ };
9424
+ const versionCompare = (a, b) => {
9425
+ if (typeof a === 'string' && typeof b === 'string' && a === b) {
9426
+ return 0;
9427
+ }
9428
+ const pa = typeof a === 'string' ? versionSplit(a) : a;
9429
+ const pb = typeof b === 'string' ? versionSplit(b) : b;
9430
+ const vpa = isValidVersionArray(pa);
9431
+ const vpb = isValidVersionArray(pb);
9432
+ if (!vpa && !vpb) {
9433
+ return 0;
9434
+ }
9435
+ if (!vpa && vpb) {
9436
+ return -1;
9437
+ }
9438
+ if (vpa && !vpb) {
9439
+ return 1;
9440
+ }
9441
+ const npa = normalizeVersionArray(pa);
9442
+ const npb = normalizeVersionArray(pb);
9443
+ for (let i = 0; i < VER_NUMS; i++) {
9444
+ if (npa[i] > npb[i])
9445
+ return 1;
9446
+ if (npb[i] > npa[i])
9447
+ return -1;
9448
+ }
9449
+ return 0;
9450
+ };
9451
+
9452
+ function patchFeatures(response) {
9453
+ if (response.type !== 'Features') {
9454
+ return response;
9455
+ }
9456
+ if (response.message.major_version < 1) {
9457
+ response.message.major_version = 1;
9458
+ }
9459
+ return response;
9460
+ }
9461
+
9462
+ const getDeviceModel = (features) => {
9463
+ if (!features || typeof features !== 'object') {
9464
+ return 'model_mini';
9465
+ }
9466
+ if (features.model === '1') {
9467
+ return 'model_mini';
9468
+ }
9469
+ return 'model_touch';
9470
+ };
9471
+ const getDeviceType = (features) => {
9472
+ if (!features || typeof features !== 'object' || !features.serial_no) {
9473
+ return 'classic';
9474
+ }
9475
+ const serialNo = features.serial_no;
9476
+ const miniFlag = serialNo.slice(0, 2);
9477
+ if (miniFlag.toLowerCase() === 'mi')
9478
+ return 'mini';
9479
+ if (miniFlag.toLowerCase() === 'tc')
9480
+ return 'touch';
9481
+ return 'classic';
9482
+ };
9483
+ const getDeviceTypeOnBootloader = (features) => getDeviceType(features);
9484
+ const getDeviceTypeByBleName = (name) => {
9485
+ if (!name)
9486
+ return 'classic';
9487
+ if (name.startsWith('MI'))
9488
+ return 'mini';
9489
+ if (name.startsWith('T'))
9490
+ return 'touch';
9491
+ return 'classic';
9492
+ };
9493
+ const getDeviceTypeByDeviceId = (deviceId) => {
9494
+ if (!deviceId) {
9495
+ return 'classic';
9496
+ }
9497
+ const miniFlag = deviceId.slice(0, 2);
9498
+ if (miniFlag.toLowerCase() === 'mi')
9499
+ return 'mini';
9500
+ return 'classic';
9501
+ };
9502
+ const getDeviceUUID = (features) => {
9503
+ const deviceType = getDeviceType(features);
9504
+ if (deviceType === 'classic') {
9505
+ return features.onekey_serial;
9506
+ }
9507
+ return features.serial_no;
9508
+ };
9509
+ const getDeviceLabel = (features) => {
9510
+ const deviceType = getDeviceType(features);
9511
+ if (typeof features.label === 'string') {
9512
+ return features.label;
9513
+ }
9514
+ return `My OneKey ${deviceType.charAt(0).toUpperCase() + deviceType.slice(1)}`;
9515
+ };
9516
+ const getDeviceFirmwareVersion = (features) => {
9517
+ if (!features)
9518
+ return [0, 0, 0];
9519
+ if (features.onekey_version) {
9520
+ return features.onekey_version.split('.');
9521
+ }
9522
+ return [features.major_version, features.minor_version, features.patch_version];
9523
+ };
9524
+ const getDeviceBLEFirmwareVersion = (features) => {
9525
+ if (!features.ble_ver) {
9526
+ return null;
9527
+ }
9528
+ return features.ble_ver.split('.');
9529
+ };
9530
+
9531
+ const HD_HARDENED = 0x80000000;
9532
+ const toHardened = (n) => (n | HD_HARDENED) >>> 0;
9533
+ const fromHardened = (n) => (n & ~HD_HARDENED) >>> 0;
9534
+ const PATH_NOT_VALID = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Not a valid path');
9535
+ const PATH_NEGATIVE_VALUES = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Path cannot contain negative values');
9536
+ const getHDPath = (path) => {
9537
+ const parts = path.toLowerCase().split('/');
9538
+ if (parts[0] !== 'm')
9539
+ throw PATH_NOT_VALID;
9540
+ return parts
9541
+ .filter((p) => p !== 'm' && p !== '')
9542
+ .map((p) => {
9543
+ let hardened = false;
9544
+ if (p.substr(p.length - 1) === "'") {
9545
+ hardened = true;
9546
+ p = p.substr(0, p.length - 1);
9547
+ }
9548
+ let n = parseInt(p);
9549
+ if (Number.isNaN(n)) {
9550
+ throw PATH_NOT_VALID;
9551
+ }
9552
+ else if (n < 0) {
9553
+ throw PATH_NEGATIVE_VALUES;
9554
+ }
9555
+ if (hardened) {
9556
+ n = toHardened(n);
9557
+ }
9558
+ return n;
9559
+ });
9560
+ };
9561
+ const isMultisigPath = (path) => Array.isArray(path) && path[0] === toHardened(48);
9562
+ const isSegwitPath = (path) => Array.isArray(path) && path[0] === toHardened(49);
9563
+ const getScriptType = (path) => {
9564
+ if (!Array.isArray(path) || path.length < 1)
9565
+ return 'SPENDADDRESS';
9566
+ const p1 = fromHardened(path[0]);
9567
+ switch (p1) {
9568
+ case 48:
9569
+ return 'SPENDMULTISIG';
9570
+ case 49:
9571
+ return 'SPENDP2SHWITNESS';
9572
+ case 84:
9573
+ return 'SPENDWITNESS';
9574
+ default:
9575
+ return 'SPENDADDRESS';
9576
+ }
9577
+ };
9578
+ const getOutputScriptType = (path) => {
9579
+ if (!Array.isArray(path) || path.length < 1)
9580
+ return 'PAYTOADDRESS';
9581
+ if (path[0] === 49) {
9582
+ return 'PAYTOP2SHWITNESS';
9583
+ }
9584
+ const p = fromHardened(path[0]);
9585
+ switch (p) {
9586
+ case 48:
9587
+ return 'PAYTOMULTISIG';
9588
+ case 49:
9589
+ return 'PAYTOP2SHWITNESS';
9590
+ case 84:
9591
+ return 'PAYTOWITNESS';
9592
+ default:
9593
+ return 'PAYTOADDRESS';
9594
+ }
9595
+ };
9596
+ const serializedPath = (path) => {
9597
+ const pathStr = path
9598
+ .map((p) => {
9599
+ if (p & HD_HARDENED) {
9600
+ return `${p & ~HD_HARDENED}'`;
9601
+ }
9602
+ return p;
9603
+ })
9604
+ .join('/');
9605
+ return `m/${pathStr}`;
9606
+ };
9607
+ const validatePath = (path, length = 0, base = false) => {
9608
+ let valid;
9609
+ if (typeof path === 'string') {
9610
+ valid = getHDPath(path);
9611
+ }
9612
+ else if (Array.isArray(path)) {
9613
+ valid = path.map((p) => {
9614
+ const n = parseInt(p);
9615
+ if (Number.isNaN(n)) {
9616
+ throw PATH_NOT_VALID;
9617
+ }
9618
+ else if (n < 0) {
9619
+ throw PATH_NEGATIVE_VALUES;
9620
+ }
9621
+ return n;
9622
+ });
9623
+ }
9624
+ else {
9625
+ valid = undefined;
9626
+ }
9627
+ if (!valid)
9628
+ throw PATH_NOT_VALID;
9629
+ if (length > 0 && valid.length < length)
9630
+ throw PATH_NOT_VALID;
9631
+ return base ? valid.splice(0, 3) : valid;
9639
9632
  };
9640
9633
 
9634
+ const LOG_EVENT = 'LOG_EVENT';
9635
+ const LOG = {
9636
+ OUTPUT: 'log-output',
9637
+ };
9638
+ const createLogMessage = (type, payload) => ({
9639
+ event: LOG_EVENT,
9640
+ type,
9641
+ payload,
9642
+ });
9643
+
9644
+ const MAX_ENTRIES = 500;
9645
+ let postMessage$1;
9646
+ class Log$6 {
9647
+ constructor(prefix, enabled) {
9648
+ this.prefix = prefix;
9649
+ this.enabled = enabled;
9650
+ this.messages = [];
9651
+ }
9652
+ addMessage(level, prefix, ...args) {
9653
+ this.messages.push({
9654
+ level,
9655
+ prefix,
9656
+ message: args,
9657
+ timestamp: new Date().getTime(),
9658
+ });
9659
+ if (this.messages.length > MAX_ENTRIES) {
9660
+ this.messages.shift();
9661
+ }
9662
+ }
9663
+ log(...args) {
9664
+ this.addMessage('log', this.prefix, ...args);
9665
+ sendLogMessage(this.prefix, ...args);
9666
+ if (!this.enabled) {
9667
+ return;
9668
+ }
9669
+ console.log(this.prefix, ...args);
9670
+ }
9671
+ error(...args) {
9672
+ this.addMessage('error', this.prefix, ...args);
9673
+ sendLogMessage(this.prefix, ...args);
9674
+ if (!this.enabled) {
9675
+ return;
9676
+ }
9677
+ console.error(this.prefix, ...args);
9678
+ }
9679
+ warn(...args) {
9680
+ this.addMessage('warn', this.prefix, ...args);
9681
+ sendLogMessage(this.prefix, ...args);
9682
+ if (!this.enabled) {
9683
+ return;
9684
+ }
9685
+ console.warn(this.prefix, ...args);
9686
+ }
9687
+ debug(...args) {
9688
+ this.addMessage('debug', this.prefix, ...args);
9689
+ sendLogMessage(this.prefix, ...args);
9690
+ if (!this.enabled) {
9691
+ return;
9692
+ }
9693
+ console.log(this.prefix, ...args);
9694
+ }
9695
+ }
9696
+ const _logs = {};
9697
+ const initLog = (prefix, enabled) => {
9698
+ const instance = new Log$6(prefix, !!enabled);
9699
+ _logs[prefix] = instance;
9700
+ return instance;
9701
+ };
9702
+ const enableLog = (enabled) => {
9703
+ Object.keys(_logs).forEach(key => {
9704
+ _logs[key].enabled = !!enabled;
9705
+ });
9706
+ };
9707
+ const getLog = () => {
9708
+ let logs = [];
9709
+ Object.keys(_logs).forEach(key => {
9710
+ logs = logs.concat(_logs[key].messages);
9711
+ });
9712
+ logs.sort((a, b) => a.timestamp - b.timestamp);
9713
+ return logs;
9714
+ };
9715
+ const setLoggerPostMessage = (postMessageFn) => {
9716
+ postMessage$1 = postMessageFn;
9717
+ };
9718
+ const serializeLog = (...args) => args.map(arg => {
9719
+ if (typeof arg === 'string') {
9720
+ return arg;
9721
+ }
9722
+ if (typeof arg === 'number') {
9723
+ return arg;
9724
+ }
9725
+ if (typeof arg === 'boolean') {
9726
+ return arg;
9727
+ }
9728
+ if (typeof arg === 'undefined') {
9729
+ return arg;
9730
+ }
9731
+ if (typeof arg === 'object') {
9732
+ return JSON.stringify(arg, getCircularReplacer());
9733
+ }
9734
+ return arg;
9735
+ });
9736
+ const getCircularReplacer = () => {
9737
+ const seen = new WeakSet();
9738
+ return (_, value) => {
9739
+ if (typeof value === 'object' && value !== null) {
9740
+ if (seen.has(value)) {
9741
+ return;
9742
+ }
9743
+ seen.add(value);
9744
+ }
9745
+ return value;
9746
+ };
9747
+ };
9748
+ const sendLogMessage = (prefix, ...args) => {
9749
+ postMessage$1 === null || postMessage$1 === void 0 ? void 0 : postMessage$1(createLogMessage(LOG.OUTPUT, serializeLog(prefix, ...args)));
9750
+ };
9751
+ exports.LoggerNames = void 0;
9752
+ (function (LoggerNames) {
9753
+ LoggerNames["Core"] = "Core";
9754
+ LoggerNames["Transport"] = "Transport";
9755
+ LoggerNames["Device"] = "Device";
9756
+ LoggerNames["DeviceCommands"] = "DeviceCommands";
9757
+ LoggerNames["DeviceConnector"] = "DeviceConnector";
9758
+ LoggerNames["DeviceList"] = "DeviceList";
9759
+ LoggerNames["HdBleSdk"] = "@onekey/hd-ble-sdk";
9760
+ LoggerNames["HdTransportHttp"] = "@onekey/hd-transport-http";
9761
+ LoggerNames["HdBleTransport"] = "@onekey/hd-ble-transport";
9762
+ LoggerNames["Connect"] = "@onekey/connect";
9763
+ LoggerNames["Iframe"] = "IFrame";
9764
+ LoggerNames["SendMessage"] = "[SendMessage]";
9765
+ LoggerNames["Method"] = "[Method]";
9766
+ })(exports.LoggerNames || (exports.LoggerNames = {}));
9767
+ const LoggerMap = {
9768
+ [exports.LoggerNames.Core]: initLog(exports.LoggerNames.Core),
9769
+ [exports.LoggerNames.Transport]: initLog(exports.LoggerNames.Transport),
9770
+ [exports.LoggerNames.Device]: initLog(exports.LoggerNames.Device),
9771
+ [exports.LoggerNames.DeviceCommands]: initLog(exports.LoggerNames.DeviceCommands),
9772
+ [exports.LoggerNames.DeviceConnector]: initLog(exports.LoggerNames.DeviceConnector),
9773
+ [exports.LoggerNames.DeviceList]: initLog(exports.LoggerNames.DeviceList),
9774
+ [exports.LoggerNames.HdBleSdk]: initLog(exports.LoggerNames.HdBleSdk),
9775
+ [exports.LoggerNames.HdTransportHttp]: initLog(exports.LoggerNames.HdTransportHttp),
9776
+ [exports.LoggerNames.HdBleTransport]: initLog(exports.LoggerNames.HdBleTransport),
9777
+ [exports.LoggerNames.Connect]: initLog(exports.LoggerNames.Connect),
9778
+ [exports.LoggerNames.Iframe]: initLog(exports.LoggerNames.Iframe),
9779
+ [exports.LoggerNames.SendMessage]: initLog(exports.LoggerNames.SendMessage),
9780
+ [exports.LoggerNames.Method]: initLog(exports.LoggerNames.Method),
9781
+ };
9782
+ const getLogger = (key) => LoggerMap[key];
9783
+
9641
9784
  const getReleaseStatus = (releases, currentVersion) => {
9642
9785
  const newVersions = releases.filter(r => semver__default["default"].gt(r.version.join('.'), currentVersion));
9643
9786
  if (newVersions.length === 0) {
@@ -9778,10 +9921,12 @@ DataManager.getTransportStatus = (localVersion) => {
9778
9921
  return isLatest ? 'valid' : 'outdated';
9779
9922
  };
9780
9923
 
9781
- const Log$5 = hdShared.initLog('Transport');
9924
+ const Log$5 = getLogger(exports.LoggerNames.Transport);
9925
+ const BleLogger = getLogger(exports.LoggerNames.HdBleTransport);
9926
+ const HttpLogger = getLogger(exports.LoggerNames.HdTransportHttp);
9782
9927
  class TransportManager {
9783
9928
  static load() {
9784
- console.log('transport manager load');
9929
+ Log$5.debug('transport manager load');
9785
9930
  this.defaultMessages = DataManager.getProtobufMessages();
9786
9931
  this.currentMessages = this.defaultMessages;
9787
9932
  }
@@ -9792,7 +9937,7 @@ class TransportManager {
9792
9937
  Log$5.debug('Initializing transports');
9793
9938
  if (env === 'react-native') {
9794
9939
  if (!this.reactNativeInit) {
9795
- yield this.transport.init();
9940
+ yield this.transport.init(BleLogger);
9796
9941
  this.reactNativeInit = true;
9797
9942
  }
9798
9943
  else {
@@ -9800,7 +9945,7 @@ class TransportManager {
9800
9945
  }
9801
9946
  }
9802
9947
  else {
9803
- yield this.transport.init();
9948
+ yield this.transport.init(HttpLogger);
9804
9949
  }
9805
9950
  Log$5.debug('Configuring transports');
9806
9951
  yield this.transport.configure(JSON.stringify(this.defaultMessages));
@@ -9808,6 +9953,9 @@ class TransportManager {
9808
9953
  }
9809
9954
  catch (error) {
9810
9955
  Log$5.debug('Initializing transports error: ', error);
9956
+ if (error.code === 'ECONNABORTED') {
9957
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BridgeTimeoutError);
9958
+ }
9811
9959
  }
9812
9960
  });
9813
9961
  }
@@ -9836,7 +9984,7 @@ class TransportManager {
9836
9984
  else {
9837
9985
  this.transport = new TransportConstructor();
9838
9986
  }
9839
- console.log('set transport: ', this.transport);
9987
+ Log$5.debug('set transport: ', this.transport);
9840
9988
  }
9841
9989
  static getTransport() {
9842
9990
  return this.transport;
@@ -9955,7 +10103,7 @@ const assertType = (res, resType) => {
9955
10103
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.ResponseUnexpectTypeError, `assertType: Response of unexpected type: ${res.type}. Should be ${resType}`);
9956
10104
  }
9957
10105
  };
9958
- const Log$4 = hdShared.initLog('DeviceCommands');
10106
+ const Log$4 = getLogger(exports.LoggerNames.DeviceCommands);
9959
10107
  class DeviceCommands {
9960
10108
  constructor(device, mainId) {
9961
10109
  this.device = device;
@@ -9975,7 +10123,7 @@ class DeviceCommands {
9975
10123
  call(type, msg = {}) {
9976
10124
  var _a, _b;
9977
10125
  return __awaiter(this, void 0, void 0, function* () {
9978
- console.log('[DeviceCommands] [call] Sending', type);
10126
+ Log$4.debug('[DeviceCommands] [call] Sending', type);
9979
10127
  try {
9980
10128
  const promise = this.transport.call(this.mainId, type, msg);
9981
10129
  this.callPromise = promise;
@@ -10002,7 +10150,7 @@ class DeviceCommands {
10002
10150
  assertType(response, resType);
10003
10151
  }
10004
10152
  catch (error) {
10005
- console.log('DeviceCommands typedcall error: ', error);
10153
+ Log$4.debug('DeviceCommands typedcall error: ', error);
10006
10154
  if (error instanceof hdShared.HardwareError) {
10007
10155
  if (error.errorCode === hdShared.HardwareErrorCode.ResponseUnexpectTypeError) {
10008
10156
  if (error.message.indexOf('BridgeNetworkError') > -1) {
@@ -10024,7 +10172,7 @@ class DeviceCommands {
10024
10172
  });
10025
10173
  }
10026
10174
  _filterCommonTypes(res) {
10027
- console.log('_filterCommonTypes: ', res);
10175
+ Log$4.debug('_filterCommonTypes: ', res);
10028
10176
  if (res.type === 'Failure') {
10029
10177
  const { code } = res.message;
10030
10178
  const { message } = res.message;
@@ -10198,7 +10346,7 @@ const parseRunOptions = (options) => {
10198
10346
  options = {};
10199
10347
  return options;
10200
10348
  };
10201
- const Log$3 = hdShared.initLog('Device');
10349
+ const Log$3 = getLogger(exports.LoggerNames.Device);
10202
10350
  class Device extends events.exports {
10203
10351
  constructor(descriptor) {
10204
10352
  super();
@@ -10504,7 +10652,7 @@ class Device extends events.exports {
10504
10652
  }
10505
10653
 
10506
10654
  const cacheDeviceMap = new Map();
10507
- const Log$2 = hdShared.initLog('DeviceList');
10655
+ const Log$2 = getLogger(exports.LoggerNames.DeviceList);
10508
10656
  class DeviceList extends events.exports {
10509
10657
  constructor() {
10510
10658
  super(...arguments);
@@ -10518,7 +10666,7 @@ class DeviceList extends events.exports {
10518
10666
  const descriptorList = (_c = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _c !== void 0 ? _c : [];
10519
10667
  this.devices = {};
10520
10668
  const deviceList = [];
10521
- console.log('get device list');
10669
+ Log$2.debug('get device list');
10522
10670
  try {
10523
10671
  for (var descriptorList_1 = __asyncValues(descriptorList), descriptorList_1_1; descriptorList_1_1 = yield descriptorList_1.next(), !descriptorList_1_1.done;) {
10524
10672
  const descriptor = descriptorList_1_1.value;
@@ -11578,21 +11726,29 @@ class CheckTransportRelease extends BaseMethod {
11578
11726
  }
11579
11727
  }
11580
11728
 
11581
- class CheckBridgeStatus extends BaseMethod {
11729
+ class CheckBridgeStatus$1 extends BaseMethod {
11582
11730
  init() {
11583
11731
  this.useDevice = false;
11584
11732
  }
11585
11733
  run() {
11586
11734
  return __awaiter(this, void 0, void 0, function* () {
11587
- return new Promise(resolve => {
11735
+ return new Promise((resolve, reject) => {
11588
11736
  axios__default["default"]
11589
11737
  .request({
11590
11738
  url: 'http://localhost:21320',
11591
11739
  method: 'POST',
11592
11740
  withCredentials: false,
11741
+ timeout: 3000,
11593
11742
  })
11594
11743
  .then(() => resolve(true))
11595
- .catch(() => resolve(false));
11744
+ .catch(e => {
11745
+ if (e.code === 'ECONNABORTED') {
11746
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BridgeTimeoutError));
11747
+ }
11748
+ else {
11749
+ resolve(false);
11750
+ }
11751
+ });
11596
11752
  });
11597
11753
  });
11598
11754
  }
@@ -11787,7 +11943,7 @@ class DeviceVerify extends BaseMethod {
11787
11943
  const res = yield this.device.commands.typedCall('BixinVerifyDeviceRequest', 'BixinVerifyDeviceAck', Object.assign(Object.assign({}, this.params), { data: sha256__default["default"].sha256(this.params.data) }));
11788
11944
  response = res.message;
11789
11945
  }
11790
- else if (deviceType === 'mini') {
11946
+ else {
11791
11947
  const signatureRes = yield this.device.commands.typedCall('SESignMessage', 'SEMessageSignature', {
11792
11948
  message: this.params.data,
11793
11949
  });
@@ -13159,6 +13315,7 @@ class FirmwareUpdate extends BaseMethod {
13159
13315
  }
13160
13316
  }
13161
13317
  run() {
13318
+ var _a;
13162
13319
  return __awaiter(this, void 0, void 0, function* () {
13163
13320
  const { device, params } = this;
13164
13321
  let binary;
@@ -13179,13 +13336,25 @@ class FirmwareUpdate extends BaseMethod {
13179
13336
  }
13180
13337
  }
13181
13338
  catch (err) {
13182
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateDownloadFailed, err);
13339
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateDownloadFailed, (_a = err.message) !== null && _a !== void 0 ? _a : err);
13183
13340
  }
13184
13341
  return uploadFirmware(params.updateType, this.device.getCommands().typedCall.bind(this.device.getCommands()), this.postMessage, device, { payload: binary });
13185
13342
  });
13186
13343
  }
13187
13344
  }
13188
13345
 
13346
+ class CheckBridgeStatus extends BaseMethod {
13347
+ init() {
13348
+ this.useDevice = false;
13349
+ }
13350
+ run() {
13351
+ return __awaiter(this, void 0, void 0, function* () {
13352
+ const logs = getLog();
13353
+ return Promise.resolve(logs);
13354
+ });
13355
+ }
13356
+ }
13357
+
13189
13358
  var ApiMethods = /*#__PURE__*/Object.freeze({
13190
13359
  __proto__: null,
13191
13360
  searchDevices: SearchDevices,
@@ -13199,7 +13368,7 @@ var ApiMethods = /*#__PURE__*/Object.freeze({
13199
13368
  checkFirmwareRelease: CheckFirmwareRelease,
13200
13369
  checkBLEFirmwareRelease: CheckBLEFirmwareRelease,
13201
13370
  checkTransportRelease: CheckTransportRelease,
13202
- checkBridgeStatus: CheckBridgeStatus,
13371
+ checkBridgeStatus: CheckBridgeStatus$1,
13203
13372
  deviceBackup: DeviceBackup,
13204
13373
  deviceChangePin: DeviceChangePin,
13205
13374
  deviceFlags: DeviceFlags,
@@ -13228,7 +13397,8 @@ var ApiMethods = /*#__PURE__*/Object.freeze({
13228
13397
  solSignTransaction: SolSignTransaction,
13229
13398
  stellarGetAddress: StellarGetAddress,
13230
13399
  stellarSignTransaction: StellarSignTransaction,
13231
- firmwareUpdate: FirmwareUpdate
13400
+ firmwareUpdate: FirmwareUpdate,
13401
+ getLogs: CheckBridgeStatus
13232
13402
  });
13233
13403
 
13234
13404
  function findMethod(message) {
@@ -13256,7 +13426,7 @@ const resolveAfter = (msec, value) => new Promise(resolve => {
13256
13426
  setTimeout(resolve, msec, value);
13257
13427
  });
13258
13428
 
13259
- const Log$1 = hdShared.initLog('DeviceConnector');
13429
+ const Log$1 = getLogger(exports.LoggerNames.DeviceConnector);
13260
13430
  const getDiff = (current, descriptors) => {
13261
13431
  const env = DataManager.getSettings('env');
13262
13432
  if (env === 'react-native') {
@@ -13360,7 +13530,7 @@ class DeviceConnector {
13360
13530
  }
13361
13531
  acquire(path, session) {
13362
13532
  return __awaiter(this, void 0, void 0, function* () {
13363
- console.log('acquire', path, session);
13533
+ Log$1.debug('acquire', path, session);
13364
13534
  const env = DataManager.getSettings('env');
13365
13535
  try {
13366
13536
  let res;
@@ -13395,7 +13565,7 @@ class DeviceConnector {
13395
13565
  }
13396
13566
  }
13397
13567
 
13398
- const Log = hdShared.initLog('Core');
13568
+ const Log = getLogger(exports.LoggerNames.Core);
13399
13569
  let _core;
13400
13570
  let _deviceList;
13401
13571
  let _connector;
@@ -13446,7 +13616,7 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13446
13616
  catch (error) {
13447
13617
  return Promise.reject(error);
13448
13618
  }
13449
- Log.debug('Call API - setDevice: ', device);
13619
+ Log.debug('Call API - setDevice: ', device.mainId);
13450
13620
  (_a = method.setDevice) === null || _a === void 0 ? void 0 : _a.call(method, device);
13451
13621
  device.on(DEVICE.PIN, onDevicePinHandler);
13452
13622
  device.on(DEVICE.BUTTON, (d, code) => {
@@ -13492,14 +13662,14 @@ const callAPI = (message) => __awaiter(void 0, void 0, void 0, function* () {
13492
13662
  _callPromise === null || _callPromise === void 0 ? void 0 : _callPromise.resolve(messageResponse);
13493
13663
  }
13494
13664
  });
13495
- Log.debug('Call API - Device Run: ', device);
13665
+ Log.debug('Call API - Device Run: ', device.mainId);
13496
13666
  const deviceRun = () => device.run(inner);
13497
13667
  _callPromise = hdShared.createDeferred(deviceRun);
13498
13668
  try {
13499
13669
  return yield _callPromise.promise;
13500
13670
  }
13501
13671
  catch (e) {
13502
- console.log('Device Run Error: ', e);
13672
+ Log.debug('Device Run Error: ', e);
13503
13673
  return createResponseMessage(method.responseID, false, { error: e });
13504
13674
  }
13505
13675
  }
@@ -13596,7 +13766,7 @@ const closePopup = () => {
13596
13766
  postMessage(createUiMessage(UI_REQUEST$1.CLOSE_UI_WINDOW));
13597
13767
  };
13598
13768
  const onDevicePinHandler = (...[device, type, callback]) => __awaiter(void 0, void 0, void 0, function* () {
13599
- console.log('onDevicePinHandler');
13769
+ Log.debug('onDevicePinHandler');
13600
13770
  const uiPromise = createUiPromise(UI_RESPONSE.RECEIVE_PIN, device);
13601
13771
  postMessage(createUiMessage(UI_REQUEST$1.REQUEST_PIN, {
13602
13772
  device: device.toMessageObject(),
@@ -13681,7 +13851,10 @@ const init = (settings, Transport) => __awaiter(void 0, void 0, void 0, function
13681
13851
  catch (_b) {
13682
13852
  Log.error('DataManager.load error');
13683
13853
  }
13684
- hdShared.enableLog(DataManager.getSettings('debug'));
13854
+ enableLog(DataManager.getSettings('debug'));
13855
+ if (DataManager.getSettings('env') !== 'react-native') {
13856
+ setLoggerPostMessage(postMessage);
13857
+ }
13685
13858
  initCore();
13686
13859
  initConnector();
13687
13860
  return _core;
@@ -13700,14 +13873,6 @@ const HardwareSdk = ({ init, call, dispose, eventEmitter, uiResponse, cancel, })
13700
13873
  cancel,
13701
13874
  });
13702
13875
 
13703
- Object.defineProperty(exports, 'enableLog', {
13704
- enumerable: true,
13705
- get: function () { return hdShared.enableLog; }
13706
- });
13707
- Object.defineProperty(exports, 'initLog', {
13708
- enumerable: true,
13709
- get: function () { return hdShared.initLog; }
13710
- });
13711
13876
  Object.defineProperty(exports, 'PROTO', {
13712
13877
  enumerable: true,
13713
13878
  get: function () { return hdTransport.Messages; }
@@ -13719,6 +13884,8 @@ exports.DEVICE = DEVICE;
13719
13884
  exports.DEVICE_EVENT = DEVICE_EVENT;
13720
13885
  exports.DataManager = DataManager;
13721
13886
  exports.IFRAME = IFRAME;
13887
+ exports.LOG = LOG;
13888
+ exports.LOG_EVENT = LOG_EVENT;
13722
13889
  exports.RESPONSE_EVENT = RESPONSE_EVENT;
13723
13890
  exports.UI_EVENT = UI_EVENT;
13724
13891
  exports.UI_REQUEST = UI_REQUEST$1;
@@ -13727,10 +13894,12 @@ exports.corsValidator = corsValidator;
13727
13894
  exports.createDeviceMessage = createDeviceMessage;
13728
13895
  exports.createErrorMessage = createErrorMessage;
13729
13896
  exports.createIFrameMessage = createIFrameMessage;
13897
+ exports.createLogMessage = createLogMessage;
13730
13898
  exports.createResponseMessage = createResponseMessage;
13731
13899
  exports.createUiMessage = createUiMessage;
13732
13900
  exports.createUiResponse = createUiResponse;
13733
13901
  exports["default"] = HardwareSdk;
13902
+ exports.enableLog = enableLog;
13734
13903
  exports.getDeviceLabel = getDeviceLabel;
13735
13904
  exports.getDeviceType = getDeviceType;
13736
13905
  exports.getDeviceTypeByBleName = getDeviceTypeByBleName;
@@ -13738,6 +13907,8 @@ exports.getDeviceTypeByDeviceId = getDeviceTypeByDeviceId;
13738
13907
  exports.getDeviceUUID = getDeviceUUID;
13739
13908
  exports.getEnv = getEnv;
13740
13909
  exports.getHDPath = getHDPath;
13910
+ exports.getLog = getLog;
13911
+ exports.getLogger = getLogger;
13741
13912
  exports.getScriptType = getScriptType;
13742
13913
  exports.getTimeStamp = getTimeStamp;
13743
13914
  exports.httpRequest = httpRequest;
@@ -13749,5 +13920,6 @@ exports.parseConnectSettings = parseConnectSettings;
13749
13920
  exports.parseMessage = parseMessage;
13750
13921
  exports.patchFeatures = patchFeatures;
13751
13922
  exports.safeThrowError = safeThrowError;
13923
+ exports.setLoggerPostMessage = setLoggerPostMessage;
13752
13924
  exports.versionCompare = versionCompare;
13753
13925
  exports.versionSplit = versionSplit;