@onekeyfe/hd-transport-react-native 1.2.0-alpha.6 → 1.2.0-alpha.61

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.
@@ -1 +1 @@
1
- {"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAqB9E,CAAC"}
1
+ {"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAmB9E,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { createTransportCallLog } from '@onekeyfe/hd-transport';
2
+ //# sourceMappingURL=transportLog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transportLog.d.ts","sourceRoot":"","sources":["../src/transportLog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC"}
package/dist/types.d.ts CHANGED
@@ -7,5 +7,6 @@ export type BleAcquireInput = {
7
7
  uuid: string;
8
8
  forceCleanRunPromise?: boolean;
9
9
  expectedProtocol?: ProtocolType;
10
+ protocolHint?: ProtocolType;
10
11
  };
11
12
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,YAAY,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAExE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,gBAAgB,CAAC,EAAE,YAAY,CAAC;CACjC,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,YAAY,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAExE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,gBAAgB,CAAC,EAAE,YAAY,CAAC;IAChC,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC"}
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ preset: '../../jest.config.js',
3
+ testEnvironment: 'node',
4
+ modulePathIgnorePatterns: ['node_modules', '<rootDir>/dist'],
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "1.2.0-alpha.6",
3
+ "version": "1.2.0-alpha.61",
4
4
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -15,15 +15,16 @@
15
15
  "scripts": {
16
16
  "dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
17
17
  "build": "rimraf dist && rollup -c ../../build/rollup.config.js",
18
+ "test": "jest",
18
19
  "lint": "eslint .",
19
20
  "lint:fix": "eslint . --fix"
20
21
  },
21
22
  "dependencies": {
22
- "@onekeyfe/hd-core": "1.2.0-alpha.6",
23
- "@onekeyfe/hd-shared": "1.2.0-alpha.6",
24
- "@onekeyfe/hd-transport": "1.2.0-alpha.6",
25
- "@onekeyfe/react-native-ble-utils": "^0.1.4",
23
+ "@onekeyfe/hd-core": "1.2.0-alpha.61",
24
+ "@onekeyfe/hd-shared": "1.2.0-alpha.61",
25
+ "@onekeyfe/hd-transport": "1.2.0-alpha.61",
26
+ "@onekeyfe/react-native-ble-utils": "^0.1.6",
26
27
  "react-native-ble-plx": "3.5.1"
27
28
  },
28
- "gitHead": "b0a8d032546250b02bc737d72ac23932bdc07a61"
29
+ "gitHead": "19c6dc68cec2250bab703e802209ec7be586521d"
29
30
  }
@@ -1,12 +1,7 @@
1
- import { BleErrorCode } from 'react-native-ble-plx';
2
- import { wait } from '@onekeyfe/hd-shared';
3
-
4
- import { bleLogger } from './logger';
1
+ import { Platform } from 'react-native';
5
2
 
6
3
  import type { Characteristic, Device, Subscription } from 'react-native-ble-plx';
7
4
 
8
- const Log = bleLogger;
9
-
10
5
  export default class BleTransport {
11
6
  id: string;
12
7
 
@@ -28,10 +23,6 @@ export default class BleTransport {
28
23
 
29
24
  monitorToken?: number;
30
25
 
31
- static MAX_RETRIES = 5;
32
-
33
- static RETRY_DELAY = 2000;
34
-
35
26
  constructor(
36
27
  device: Device,
37
28
  writeCharacteristic: Characteristic,
@@ -43,37 +34,11 @@ export default class BleTransport {
43
34
  this.notifyCharacteristic = notifyCharacteristic;
44
35
  }
45
36
 
46
- /**
47
- * @description only for pro / touch , while upgrade firmware
48
- * @param data
49
- * @param retryCount
50
- * @returns
51
- */
52
- async writeWithRetry(data: string, retryCount = BleTransport.MAX_RETRIES): Promise<void> {
53
- try {
54
- await this.writeCharacteristic.writeWithoutResponse(data);
55
- } catch (error) {
56
- Log?.debug(
57
- `Write retry attempt ${BleTransport.MAX_RETRIES - retryCount + 1}, error: ${error}`
58
- );
59
- if (retryCount > 0) {
60
- await wait(BleTransport.RETRY_DELAY);
61
- if (
62
- error.errorCode === BleErrorCode.DeviceDisconnected ||
63
- error.errorCode === BleErrorCode.CharacteristicNotFound
64
- ) {
65
- try {
66
- await this.device.connect();
67
- await this.device.discoverAllServicesAndCharacteristics();
68
- } catch (e) {
69
- Log?.debug(`Connect or discoverAllServicesAndCharacteristics error: ${e}`);
70
- }
71
- } else {
72
- Log?.debug(`writeCharacteristic error: ${error}`);
73
- }
74
- return this.writeWithRetry(data, retryCount - 1);
75
- }
76
- throw error;
37
+ async writeWithRetry(data: string): Promise<void> {
38
+ if (Platform.OS === 'ios' && this.writeCharacteristic.isWritableWithResponse) {
39
+ await this.writeCharacteristic.writeWithResponse(data);
40
+ return;
77
41
  }
42
+ await this.writeCharacteristic.writeWithoutResponse(data);
78
43
  }
79
44
  }
@@ -0,0 +1,93 @@
1
+ import { BleErrorCode } from 'react-native-ble-plx';
2
+ import { Platform } from 'react-native';
3
+
4
+ import BleTransport from '../BleTransport';
5
+
6
+ jest.mock(
7
+ 'react-native',
8
+ () => ({
9
+ Platform: { OS: 'ios' },
10
+ }),
11
+ { virtual: true }
12
+ );
13
+
14
+ jest.mock(
15
+ 'react-native-ble-plx',
16
+ () => ({
17
+ BleErrorCode: {
18
+ DeviceDisconnected: 205,
19
+ CharacteristicNotFound: 404,
20
+ },
21
+ }),
22
+ { virtual: true }
23
+ );
24
+
25
+ jest.mock('@onekeyfe/hd-shared', () => ({
26
+ ...jest.requireActual('@onekeyfe/hd-shared'),
27
+ wait: jest.fn(() => Promise.resolve()),
28
+ }));
29
+
30
+ describe('BleTransport side-effecting writes', () => {
31
+ beforeEach(() => {
32
+ Platform.OS = 'ios';
33
+ });
34
+
35
+ test('does not reconnect or replay after the device disconnects', async () => {
36
+ const error = Object.assign(new Error('device disconnected'), {
37
+ errorCode: BleErrorCode.DeviceDisconnected,
38
+ });
39
+ const writeCharacteristic = {
40
+ isWritableWithResponse: true,
41
+ writeWithResponse: jest.fn(() => Promise.reject(error)),
42
+ writeWithoutResponse: jest.fn(() => Promise.resolve()),
43
+ };
44
+ const device = {
45
+ id: 'classic-id',
46
+ connect: jest.fn(() => Promise.resolve()),
47
+ discoverAllServicesAndCharacteristics: jest.fn(() => Promise.resolve()),
48
+ };
49
+ const transport = new BleTransport(device as any, writeCharacteristic as any, {} as any);
50
+
51
+ await expect(transport.writeWithRetry('payload')).rejects.toBe(error);
52
+
53
+ expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(1);
54
+ expect(device.connect).not.toHaveBeenCalled();
55
+ });
56
+
57
+ test('keeps Android Protocol V1 writes on writeWithoutResponse', async () => {
58
+ Platform.OS = 'android';
59
+ const writeCharacteristic = {
60
+ isWritableWithResponse: true,
61
+ writeWithResponse: jest.fn(() => Promise.resolve()),
62
+ writeWithoutResponse: jest.fn(() => Promise.resolve()),
63
+ };
64
+ const transport = new BleTransport(
65
+ { id: 'classic-id' } as any,
66
+ writeCharacteristic as any,
67
+ {} as any
68
+ );
69
+
70
+ await transport.writeWithRetry('payload');
71
+
72
+ expect(writeCharacteristic.writeWithResponse).not.toHaveBeenCalled();
73
+ expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledWith('payload');
74
+ });
75
+
76
+ test('uses writeWithoutResponse on iOS when the characteristic lacks response support', async () => {
77
+ const writeCharacteristic = {
78
+ isWritableWithResponse: false,
79
+ writeWithResponse: jest.fn(() => Promise.resolve()),
80
+ writeWithoutResponse: jest.fn(() => Promise.resolve()),
81
+ };
82
+ const transport = new BleTransport(
83
+ { id: 'classic-id' } as any,
84
+ writeCharacteristic as any,
85
+ {} as any
86
+ );
87
+
88
+ await transport.writeWithRetry('payload');
89
+
90
+ expect(writeCharacteristic.writeWithResponse).not.toHaveBeenCalled();
91
+ expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledWith('payload');
92
+ });
93
+ });
@@ -1,8 +1,4 @@
1
- import {
2
- hasWritableCapability,
3
- resolveBleWriteMode,
4
- resolveProtocolV2PacketCapacity,
5
- } from '../bleStrategy';
1
+ import { hasWritableCapability, resolveProtocolV2PacketCapacity } from '../bleStrategy';
6
2
 
7
3
  describe('React Native BLE strategy', () => {
8
4
  test('accepts writeWithoutResponse-only characteristics', () => {
@@ -12,7 +8,6 @@ describe('React Native BLE strategy', () => {
12
8
  };
13
9
 
14
10
  expect(hasWritableCapability(characteristic)).toBe(true);
15
- expect(resolveBleWriteMode(characteristic)).toBe('withoutResponse');
16
11
  });
17
12
 
18
13
  test('falls back to Android default ATT payload when MTU is unavailable', () => {
@@ -0,0 +1,20 @@
1
+ import { getBluetoothServiceUuids, getInfosForServiceUuid } from '../constants';
2
+
3
+ describe('React Native BLE service filters', () => {
4
+ test('does not scan or configure the ignored FFFD service', () => {
5
+ expect(getBluetoothServiceUuids()).not.toContain('fffd');
6
+ expect(getInfosForServiceUuid('fffd', 'classic')).toBeNull();
7
+ expect(getInfosForServiceUuid('0000fffd-0000-1000-8000-00805f9b34fb', 'classic')).toBeNull();
8
+ });
9
+
10
+ test('keeps the OneKey communication service configured', () => {
11
+ expect(getBluetoothServiceUuids()).toContain('00000001-0000-1000-8000-00805f9b34fb');
12
+ expect(getInfosForServiceUuid('0001', 'classic')).toMatchObject({
13
+ serviceUuid: '00000001-0000-1000-8000-00805f9b34fb',
14
+ });
15
+ });
16
+
17
+ test('does not match a vendor-specific UUID containing the OneKey short key', () => {
18
+ expect(getInfosForServiceUuid('abcd0001-1234-5678-9012-abcdefabcdef', 'classic')).toBeNull();
19
+ });
20
+ });
@@ -0,0 +1,132 @@
1
+ import { EventEmitter } from 'events';
2
+
3
+ import { getConnectedDeviceIds } from '../BleManager';
4
+ import ReactNativeBleTransport from '../index';
5
+
6
+ jest.mock(
7
+ 'react-native',
8
+ () => ({
9
+ PermissionsAndroid: {},
10
+ Platform: { OS: 'ios' },
11
+ }),
12
+ { virtual: true }
13
+ );
14
+
15
+ jest.mock('react-native-ble-plx', () => ({
16
+ BleError: class BleError extends Error {},
17
+ BleErrorCode: {},
18
+ BleManager: jest.fn(),
19
+ ScanMode: { LowLatency: 2 },
20
+ }));
21
+
22
+ jest.mock('../BleManager', () => ({
23
+ getConnectedDeviceIds: jest.fn(),
24
+ onDeviceBondState: jest.fn(),
25
+ pairDevice: jest.fn(),
26
+ }));
27
+
28
+ jest.mock('../subscribeBleOn', () => ({
29
+ subscribeBleOn: jest.fn(() => Promise.resolve()),
30
+ }));
31
+
32
+ const ONEKEY_SERVICE_UUID = '00000001-0000-1000-8000-00805f9b34fb';
33
+
34
+ describe('ReactNativeBleTransport iOS discovery', () => {
35
+ test('filters a bonded Pro2 Find My peripheral while keeping the wallet peripheral', async () => {
36
+ jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([
37
+ {
38
+ id: 'find-my-peripheral',
39
+ name: 'Pro2 6E9E - Find My',
40
+ localName: null,
41
+ serviceUUIDs: [ONEKEY_SERVICE_UUID],
42
+ },
43
+ {
44
+ id: 'wallet-peripheral',
45
+ name: 'Pro2 6E9E',
46
+ localName: 'Pro2 6E9E',
47
+ serviceUUIDs: [ONEKEY_SERVICE_UUID],
48
+ },
49
+ ] as never);
50
+ const blePlxManager = {
51
+ startDeviceScan: jest.fn(),
52
+ stopDeviceScan: jest.fn(),
53
+ };
54
+ const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
55
+ transport.blePlxManager = blePlxManager as never;
56
+ transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
57
+
58
+ const devices = await transport.enumerate();
59
+
60
+ expect(devices.map(device => device.id)).toEqual(['wallet-peripheral']);
61
+ });
62
+
63
+ test('filters a scanned Pro2 Find My peripheral by name when localName is null', async () => {
64
+ jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([]);
65
+ const blePlxManager = {
66
+ startDeviceScan: jest.fn((_serviceUUIDs, _options, listener) => {
67
+ queueMicrotask(() => {
68
+ listener(null, {
69
+ id: 'find-my-peripheral',
70
+ name: 'Pro2 6E9E - Find My',
71
+ localName: null,
72
+ serviceUUIDs: [ONEKEY_SERVICE_UUID],
73
+ });
74
+ listener(null, {
75
+ id: 'wallet-peripheral',
76
+ name: 'Pro2 6E9E',
77
+ localName: 'Pro2 6E9E',
78
+ serviceUUIDs: [ONEKEY_SERVICE_UUID],
79
+ });
80
+ });
81
+ }),
82
+ stopDeviceScan: jest.fn(),
83
+ };
84
+ const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
85
+ transport.blePlxManager = blePlxManager as never;
86
+ transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
87
+
88
+ const devices = await transport.enumerate();
89
+
90
+ expect(devices.map(device => device.id)).toEqual(['wallet-peripheral']);
91
+ });
92
+
93
+ test('ignores an unnamed scanned advertisement while keeping the named wallet peripheral', async () => {
94
+ jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([]);
95
+ const blePlxManager = {
96
+ startDeviceScan: jest.fn((_serviceUUIDs, _options, listener) => {
97
+ queueMicrotask(() => {
98
+ listener(null, {
99
+ id: 'unnamed-peripheral',
100
+ name: null,
101
+ localName: null,
102
+ serviceUUIDs: [
103
+ '0000180a-0000-1000-8000-00805f9b34fb',
104
+ '0000180f-0000-1000-8000-00805f9b34fb',
105
+ '0000fffd-0000-1000-8000-00805f9b34fb',
106
+ ONEKEY_SERVICE_UUID,
107
+ ],
108
+ });
109
+ listener(null, {
110
+ id: 'wallet-peripheral',
111
+ name: 'Pro2 769D',
112
+ localName: 'Pro2 769D',
113
+ serviceUUIDs: [
114
+ '0000180a-0000-1000-8000-00805f9b34fb',
115
+ '0000180f-0000-1000-8000-00805f9b34fb',
116
+ '0000fffd-0000-1000-8000-00805f9b34fb',
117
+ ONEKEY_SERVICE_UUID,
118
+ ],
119
+ });
120
+ });
121
+ }),
122
+ stopDeviceScan: jest.fn(),
123
+ };
124
+ const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
125
+ transport.blePlxManager = blePlxManager as never;
126
+ transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
127
+
128
+ const devices = await transport.enumerate();
129
+
130
+ expect(devices.map(device => device.id)).toEqual(['wallet-peripheral']);
131
+ });
132
+ });