@onekeyfe/hd-transport-react-native 1.2.0-alpha.65 → 1.2.0-alpha.66

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.
package/jest.config.js CHANGED
@@ -2,9 +2,4 @@ module.exports = {
2
2
  preset: '../../jest.config.js',
3
3
  testEnvironment: 'node',
4
4
  modulePathIgnorePatterns: ['node_modules', '<rootDir>/dist'],
5
- moduleNameMapper: {
6
- // The workspace symlinks resolve to prebuilt dist outputs that can lag behind src.
7
- '^@onekeyfe/hd-transport$': '<rootDir>/../hd-transport/src/index.ts',
8
- '^@onekeyfe/hd-shared$': '<rootDir>/../shared/src/index.ts',
9
- },
10
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.65",
3
+ "version": "1.2.0-alpha.66",
4
4
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -20,11 +20,11 @@
20
20
  "lint:fix": "eslint . --fix"
21
21
  },
22
22
  "dependencies": {
23
- "@onekeyfe/hd-core": "1.2.0-alpha.65",
24
- "@onekeyfe/hd-shared": "1.2.0-alpha.65",
25
- "@onekeyfe/hd-transport": "1.2.0-alpha.65",
23
+ "@onekeyfe/hd-core": "1.2.0-alpha.66",
24
+ "@onekeyfe/hd-shared": "1.2.0-alpha.66",
25
+ "@onekeyfe/hd-transport": "1.2.0-alpha.66",
26
26
  "@onekeyfe/react-native-ble-utils": "^0.1.6",
27
27
  "react-native-ble-plx": "3.5.1"
28
28
  },
29
- "gitHead": "2850b5b34935d4e97a1fe8a6459eb5a96a5cf0f6"
29
+ "gitHead": "81629c086665bb92ce4d52087ae47f084976da79"
30
30
  }
@@ -1,3 +1,5 @@
1
+ import { Platform } from 'react-native';
2
+
1
3
  import type { Characteristic, Device, Subscription } from 'react-native-ble-plx';
2
4
 
3
5
  export default class BleTransport {
@@ -33,6 +35,10 @@ export default class BleTransport {
33
35
  }
34
36
 
35
37
  async writeWithRetry(data: string): Promise<void> {
38
+ if (Platform.OS === 'ios' && this.writeCharacteristic.isWritableWithResponse) {
39
+ await this.writeCharacteristic.writeWithResponse(data);
40
+ return;
41
+ }
36
42
  await this.writeCharacteristic.writeWithoutResponse(data);
37
43
  }
38
44
  }
@@ -1,7 +1,16 @@
1
1
  import { BleErrorCode } from 'react-native-ble-plx';
2
+ import { Platform } from 'react-native';
2
3
 
3
4
  import BleTransport from '../BleTransport';
4
5
 
6
+ jest.mock(
7
+ 'react-native',
8
+ () => ({
9
+ Platform: { OS: 'ios' },
10
+ }),
11
+ { virtual: true }
12
+ );
13
+
5
14
  jest.mock(
6
15
  'react-native-ble-plx',
7
16
  () => ({
@@ -19,12 +28,18 @@ jest.mock('@onekeyfe/hd-shared', () => ({
19
28
  }));
20
29
 
21
30
  describe('BleTransport side-effecting writes', () => {
31
+ beforeEach(() => {
32
+ Platform.OS = 'ios';
33
+ });
34
+
22
35
  test('does not reconnect or replay after the device disconnects', async () => {
23
36
  const error = Object.assign(new Error('device disconnected'), {
24
37
  errorCode: BleErrorCode.DeviceDisconnected,
25
38
  });
26
39
  const writeCharacteristic = {
27
- writeWithoutResponse: jest.fn(() => Promise.reject(error)),
40
+ isWritableWithResponse: true,
41
+ writeWithResponse: jest.fn(() => Promise.reject(error)),
42
+ writeWithoutResponse: jest.fn(() => Promise.resolve()),
28
43
  };
29
44
  const device = {
30
45
  id: 'classic-id',
@@ -35,7 +50,44 @@ describe('BleTransport side-effecting writes', () => {
35
50
 
36
51
  await expect(transport.writeWithRetry('payload')).rejects.toBe(error);
37
52
 
38
- expect(writeCharacteristic.writeWithoutResponse).toHaveBeenCalledTimes(1);
53
+ expect(writeCharacteristic.writeWithResponse).toHaveBeenCalledTimes(1);
39
54
  expect(device.connect).not.toHaveBeenCalled();
40
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
+ });
41
93
  });
@@ -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
+ });