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

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.
@@ -0,0 +1,61 @@
1
+ import {
2
+ ERRORS,
3
+ HardwareErrorCode,
4
+ isBleStaleBondErrorText,
5
+ isBleStaleBondHardwareError,
6
+ } from '@onekeyfe/hd-shared';
7
+
8
+ export { isBleStaleBondHardwareError };
9
+
10
+ const ATT_INSUFFICIENT_AUTHENTICATION = 5;
11
+ const ATT_INSUFFICIENT_ENCRYPTION = 15;
12
+ const IOS_PEER_REMOVED_PAIRING_INFORMATION = 14;
13
+
14
+ type NativeBleErrorFields = {
15
+ attErrorCode?: unknown;
16
+ iosErrorCode?: unknown;
17
+ reason?: unknown;
18
+ message?: unknown;
19
+ };
20
+
21
+ const nativeErrorText = (error: NativeBleErrorFields) =>
22
+ [error.reason, error.message]
23
+ .filter((value): value is string => typeof value === 'string')
24
+ .join(' ');
25
+
26
+ export const isNativeBleStaleBondError = (error: unknown): boolean => {
27
+ if (!error || typeof error !== 'object') {
28
+ return typeof error === 'string' ? isBleStaleBondErrorText(error) : false;
29
+ }
30
+
31
+ const nativeError = error as NativeBleErrorFields;
32
+ if (
33
+ nativeError.attErrorCode === ATT_INSUFFICIENT_AUTHENTICATION ||
34
+ nativeError.attErrorCode === ATT_INSUFFICIENT_ENCRYPTION ||
35
+ nativeError.iosErrorCode === IOS_PEER_REMOVED_PAIRING_INFORMATION
36
+ ) {
37
+ return true;
38
+ }
39
+
40
+ return isBleStaleBondErrorText(nativeErrorText(nativeError));
41
+ };
42
+
43
+ export const toBleStaleBondHardwareError = (error: unknown) => {
44
+ if (isBleStaleBondHardwareError(error)) {
45
+ return error as Error;
46
+ }
47
+
48
+ const nativeError = (error ?? {}) as NativeBleErrorFields;
49
+ const text = nativeErrorText(nativeError);
50
+ const normalizedText = text.toLowerCase();
51
+ const peerRemoved =
52
+ nativeError.iosErrorCode === IOS_PEER_REMOVED_PAIRING_INFORMATION ||
53
+ normalizedText.includes('peer removed pairing information');
54
+
55
+ return ERRORS.TypedError(
56
+ peerRemoved
57
+ ? HardwareErrorCode.BlePeerRemovedPairingInformation
58
+ : HardwareErrorCode.BleDeviceBondError,
59
+ text || undefined
60
+ );
61
+ };