@onekeyfe/hd-shared 1.2.0-alpha.32 → 1.2.0-alpha.34
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/dist/constants.d.ts +11 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +38 -0
- package/package.json +2 -2
- package/src/constants.test.ts +73 -1
- package/src/constants.ts +68 -0
package/dist/constants.d.ts
CHANGED
|
@@ -45,5 +45,16 @@ export declare const ONEKEY_WRITE_CHARACTERISTIC_UUID = "00000002-0000-1000-8000
|
|
|
45
45
|
export declare const ONEKEY_NOTIFY_CHARACTERISTIC_UUID = "00000003-0000-1000-8000-00805f9b34fb";
|
|
46
46
|
export declare const isHeaderChunk: (chunk: Buffer | Uint8Array) => boolean;
|
|
47
47
|
export declare const isOnekeyDevice: (name: string | null, id?: string) => boolean;
|
|
48
|
+
type BluetoothDeviceIdentity = {
|
|
49
|
+
id?: string;
|
|
50
|
+
name?: string | null;
|
|
51
|
+
localName?: string | null;
|
|
52
|
+
serviceUuids?: Array<string | null | undefined> | null;
|
|
53
|
+
};
|
|
54
|
+
export declare const normalizeBleUuid: (uuid?: string | null) => string;
|
|
55
|
+
export declare const createKnownBleUuidAliases: (uuid: string) => ReadonlySet<string>;
|
|
56
|
+
export declare const matchesKnownBleUuid: (actualUuid: string | null | undefined, aliases: ReadonlySet<string>) => boolean;
|
|
57
|
+
export declare const hasOnekeyCommunicationService: (serviceUuids: Array<string | null | undefined> | null | undefined) => boolean;
|
|
58
|
+
export declare const isOnekeyBluetoothDevice: ({ id, name, localName, serviceUuids, }: BluetoothDeviceIdentity) => boolean;
|
|
48
59
|
export {};
|
|
49
60
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";AAEA,eAAO,M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,eAAO,MAAM,iCAAiC,yCAAyC,CAAC;AAIxF,eAAO,MAAM,aAAa,UAAW,MAAM,GAAG,UAAU,KAAG,OAa1D,CAAC;AAkBF,eAAO,MAAM,cAAc,SAAU,MAAM,GAAG,IAAI,OAAO,MAAM,KAAG,OAsBjE,CAAC;AAEF,KAAK,uBAAuB,GAAG;IAC7B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,YAAY,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC;CACxD,CAAC;AAIF,eAAO,MAAM,gBAAgB,UAAW,MAAM,GAAG,IAAI,WACP,CAAC;AAE/C,eAAO,MAAM,yBAAyB,SAAU,MAAM,KAAG,YAAY,MAAM,CAc1E,CAAC;AAEF,eAAO,MAAM,mBAAmB,eAClB,MAAM,GAAG,IAAI,GAAG,SAAS,WAC5B,YAAY,MAAM,CAAC,YACgB,CAAC;AAQ/C,eAAO,MAAM,6BAA6B,iBAC1B,MAAM,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,IAAI,GAAG,SAAS,YAIhE,CAAC;AAEJ,eAAO,MAAM,uBAAuB,2CAKjC,uBAAuB,KAAG,OAe5B,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -52,6 +52,17 @@ declare const ONEKEY_WRITE_CHARACTERISTIC_UUID = "00000002-0000-1000-8000-00805f
|
|
|
52
52
|
declare const ONEKEY_NOTIFY_CHARACTERISTIC_UUID = "00000003-0000-1000-8000-00805f9b34fb";
|
|
53
53
|
declare const isHeaderChunk: (chunk: Buffer | Uint8Array) => boolean;
|
|
54
54
|
declare const isOnekeyDevice: (name: string | null, id?: string) => boolean;
|
|
55
|
+
type BluetoothDeviceIdentity = {
|
|
56
|
+
id?: string;
|
|
57
|
+
name?: string | null;
|
|
58
|
+
localName?: string | null;
|
|
59
|
+
serviceUuids?: Array<string | null | undefined> | null;
|
|
60
|
+
};
|
|
61
|
+
declare const normalizeBleUuid: (uuid?: string | null) => string;
|
|
62
|
+
declare const createKnownBleUuidAliases: (uuid: string) => ReadonlySet<string>;
|
|
63
|
+
declare const matchesKnownBleUuid: (actualUuid: string | null | undefined, aliases: ReadonlySet<string>) => boolean;
|
|
64
|
+
declare const hasOnekeyCommunicationService: (serviceUuids: Array<string | null | undefined> | null | undefined) => boolean;
|
|
65
|
+
declare const isOnekeyBluetoothDevice: ({ id, name, localName, serviceUuids, }: BluetoothDeviceIdentity) => boolean;
|
|
55
66
|
|
|
56
67
|
type Deferred<T, I = any, D = any> = {
|
|
57
68
|
id?: I;
|
|
@@ -513,4 +524,4 @@ declare enum EDeviceType {
|
|
|
513
524
|
Pro2 = "pro2"
|
|
514
525
|
}
|
|
515
526
|
|
|
516
|
-
export { CreateErrorByMessage, CreateHardwareErrorByBridgeError, Deferred, EDeviceType, EFirmwareType, EOneKeyBleMessageKeys, HardwareError$1 as ERRORS, ERROR_CODES_REQUIRE_DISCONNECT, ERROR_CODES_REQUIRE_RELEASE, HARDWARE_CONNECT_PROTOCOL, HardwareConnectProtocol, HardwareError, HardwareErrorCode, HardwareErrorCodeMessage, IHardwareError, ONEKEY_NOTIFY_CHARACTERISTIC_UUID, ONEKEY_SERVICE_UUID, ONEKEY_WEBUSB_FILTER, ONEKEY_WRITE_CHARACTERISTIC_UUID, TypedError, createDefectiveFirmwareError, createDeferred, createDeprecatedHardwareError, createDeviceNotSupportMethodError, createNeedUpgradeFirmwareHardwareError, createNewFirmwareForceUpdateHardwareError, createNewFirmwareUnReleaseHardwareError, isHeaderChunk, isKnownTrezorWebUsbDevice, isOnekeyDevice, serializeError, wait };
|
|
527
|
+
export { CreateErrorByMessage, CreateHardwareErrorByBridgeError, Deferred, EDeviceType, EFirmwareType, EOneKeyBleMessageKeys, HardwareError$1 as ERRORS, ERROR_CODES_REQUIRE_DISCONNECT, ERROR_CODES_REQUIRE_RELEASE, HARDWARE_CONNECT_PROTOCOL, HardwareConnectProtocol, HardwareError, HardwareErrorCode, HardwareErrorCodeMessage, IHardwareError, ONEKEY_NOTIFY_CHARACTERISTIC_UUID, ONEKEY_SERVICE_UUID, ONEKEY_WEBUSB_FILTER, ONEKEY_WRITE_CHARACTERISTIC_UUID, TypedError, createDefectiveFirmwareError, createDeferred, createDeprecatedHardwareError, createDeviceNotSupportMethodError, createKnownBleUuidAliases, createNeedUpgradeFirmwareHardwareError, createNewFirmwareForceUpdateHardwareError, createNewFirmwareUnReleaseHardwareError, hasOnekeyCommunicationService, isHeaderChunk, isKnownTrezorWebUsbDevice, isOnekeyBluetoothDevice, isOnekeyDevice, matchesKnownBleUuid, normalizeBleUuid, serializeError, wait };
|
package/dist/index.js
CHANGED
|
@@ -496,6 +496,39 @@ const isOnekeyDevice = (name, id) => {
|
|
|
496
496
|
}
|
|
497
497
|
return isOneKeyShortName(normalizedName);
|
|
498
498
|
};
|
|
499
|
+
const BLUETOOTH_BASE_UUID_SUFFIX = '00001000800000805f9b34fb';
|
|
500
|
+
const normalizeBleUuid = (uuid) => (uuid !== null && uuid !== void 0 ? uuid : '').replace(/-/g, '').toLowerCase();
|
|
501
|
+
const createKnownBleUuidAliases = (uuid) => {
|
|
502
|
+
const normalized = normalizeBleUuid(uuid);
|
|
503
|
+
const aliases = new Set([normalized]);
|
|
504
|
+
if (normalized.length !== 32 || !normalized.endsWith(BLUETOOTH_BASE_UUID_SUFFIX)) {
|
|
505
|
+
return aliases;
|
|
506
|
+
}
|
|
507
|
+
const assignedNumber = normalized.slice(0, 8);
|
|
508
|
+
aliases.add(assignedNumber);
|
|
509
|
+
if (assignedNumber.startsWith('0000')) {
|
|
510
|
+
aliases.add(assignedNumber.slice(4));
|
|
511
|
+
}
|
|
512
|
+
return aliases;
|
|
513
|
+
};
|
|
514
|
+
const matchesKnownBleUuid = (actualUuid, aliases) => aliases.has(normalizeBleUuid(actualUuid));
|
|
515
|
+
const ONEKEY_COMMUNICATION_SERVICE_ALIASES = createKnownBleUuidAliases(ONEKEY_SERVICE_UUID);
|
|
516
|
+
const FIDO_SERVICE_ALIASES = createKnownBleUuidAliases('0000fffd-0000-1000-8000-00805f9b34fb');
|
|
517
|
+
const isFindMyAdvertisementName = (value) => { var _a; return /\bfinde?\s+my\b/.test((_a = value === null || value === void 0 ? void 0 : value.trim().toLowerCase()) !== null && _a !== void 0 ? _a : ''); };
|
|
518
|
+
const hasOnekeyCommunicationService = (serviceUuids) => (serviceUuids !== null && serviceUuids !== void 0 ? serviceUuids : []).some(uuid => matchesKnownBleUuid(uuid, ONEKEY_COMMUNICATION_SERVICE_ALIASES));
|
|
519
|
+
const isOnekeyBluetoothDevice = ({ id, name, localName, serviceUuids, }) => {
|
|
520
|
+
const advertisedServiceUuids = serviceUuids !== null && serviceUuids !== void 0 ? serviceUuids : [];
|
|
521
|
+
if (isFindMyAdvertisementName(name) || isFindMyAdvertisementName(localName)) {
|
|
522
|
+
return false;
|
|
523
|
+
}
|
|
524
|
+
if (hasOnekeyCommunicationService(advertisedServiceUuids)) {
|
|
525
|
+
return true;
|
|
526
|
+
}
|
|
527
|
+
if (advertisedServiceUuids.some(uuid => matchesKnownBleUuid(uuid, FIDO_SERVICE_ALIASES))) {
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
return isOnekeyDevice(name !== null && name !== void 0 ? name : null, id) || isOnekeyDevice(localName !== null && localName !== void 0 ? localName : null, id);
|
|
531
|
+
};
|
|
499
532
|
|
|
500
533
|
/******************************************************************************
|
|
501
534
|
Copyright (c) Microsoft Corporation.
|
|
@@ -588,11 +621,16 @@ exports.createDefectiveFirmwareError = createDefectiveFirmwareError;
|
|
|
588
621
|
exports.createDeferred = createDeferred;
|
|
589
622
|
exports.createDeprecatedHardwareError = createDeprecatedHardwareError;
|
|
590
623
|
exports.createDeviceNotSupportMethodError = createDeviceNotSupportMethodError;
|
|
624
|
+
exports.createKnownBleUuidAliases = createKnownBleUuidAliases;
|
|
591
625
|
exports.createNeedUpgradeFirmwareHardwareError = createNeedUpgradeFirmwareHardwareError;
|
|
592
626
|
exports.createNewFirmwareForceUpdateHardwareError = createNewFirmwareForceUpdateHardwareError;
|
|
593
627
|
exports.createNewFirmwareUnReleaseHardwareError = createNewFirmwareUnReleaseHardwareError;
|
|
628
|
+
exports.hasOnekeyCommunicationService = hasOnekeyCommunicationService;
|
|
594
629
|
exports.isHeaderChunk = isHeaderChunk;
|
|
595
630
|
exports.isKnownTrezorWebUsbDevice = isKnownTrezorWebUsbDevice;
|
|
631
|
+
exports.isOnekeyBluetoothDevice = isOnekeyBluetoothDevice;
|
|
596
632
|
exports.isOnekeyDevice = isOnekeyDevice;
|
|
633
|
+
exports.matchesKnownBleUuid = matchesKnownBleUuid;
|
|
634
|
+
exports.normalizeBleUuid = normalizeBleUuid;
|
|
597
635
|
exports.serializeError = serializeError;
|
|
598
636
|
exports.wait = wait;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-shared",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.34",
|
|
4
4
|
"description": "Hardware SDK's shared tool library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Hardware-SDK",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"lint": "eslint .",
|
|
27
27
|
"lint:fix": "eslint . --fix"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "07f10b4c9fab177da7c36d976ec18b075492b57f"
|
|
30
30
|
}
|
package/src/constants.test.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createKnownBleUuidAliases,
|
|
3
|
+
hasOnekeyCommunicationService,
|
|
4
|
+
isKnownTrezorWebUsbDevice,
|
|
5
|
+
isOnekeyBluetoothDevice,
|
|
6
|
+
isOnekeyDevice,
|
|
7
|
+
matchesKnownBleUuid,
|
|
8
|
+
} from './constants';
|
|
2
9
|
|
|
3
10
|
describe('hardware device identity filters', () => {
|
|
4
11
|
it('accepts known OneKey BLE names', () => {
|
|
@@ -13,6 +20,71 @@ describe('hardware device identity filters', () => {
|
|
|
13
20
|
expect(isOnekeyDevice('Ledger Nano X')).toBe(false);
|
|
14
21
|
});
|
|
15
22
|
|
|
23
|
+
it('does not identify an unnamed FFFD or Find My advertisement as OneKey', () => {
|
|
24
|
+
expect(isOnekeyBluetoothDevice({ name: 'Find My', serviceUuids: ['fffd'] })).toBe(false);
|
|
25
|
+
expect(
|
|
26
|
+
isOnekeyBluetoothDevice({ serviceUuids: ['0000fffd-0000-1000-8000-00805f9b34fb'] })
|
|
27
|
+
).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('keeps Pro2 communication advertisements and rejects its Find My advertisement', () => {
|
|
31
|
+
expect(
|
|
32
|
+
isOnekeyBluetoothDevice({
|
|
33
|
+
name: 'Pro2 A1B2',
|
|
34
|
+
serviceUuids: ['0001', 'fffd'],
|
|
35
|
+
})
|
|
36
|
+
).toBe(true);
|
|
37
|
+
expect(
|
|
38
|
+
isOnekeyBluetoothDevice({
|
|
39
|
+
localName: 'Pro2 A1B2 - Find My',
|
|
40
|
+
serviceUuids: ['fffd'],
|
|
41
|
+
})
|
|
42
|
+
).toBe(false);
|
|
43
|
+
expect(
|
|
44
|
+
isOnekeyBluetoothDevice({
|
|
45
|
+
name: 'Pro2 5E9D - Finde My',
|
|
46
|
+
serviceUuids: ['0001', 'fffd'],
|
|
47
|
+
})
|
|
48
|
+
).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('keeps OneKey discovery on the communication service', () => {
|
|
52
|
+
expect(
|
|
53
|
+
isOnekeyBluetoothDevice({
|
|
54
|
+
name: 'OneKey Pro 2',
|
|
55
|
+
serviceUuids: ['00000001-0000-1000-8000-00805f9b34fb'],
|
|
56
|
+
})
|
|
57
|
+
).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('only aliases standard Bluetooth Base UUID representations', () => {
|
|
61
|
+
const aliases = createKnownBleUuidAliases('00000001-0000-1000-8000-00805f9b34fb');
|
|
62
|
+
|
|
63
|
+
expect(matchesKnownBleUuid('0001', aliases)).toBe(true);
|
|
64
|
+
expect(matchesKnownBleUuid('00000001', aliases)).toBe(true);
|
|
65
|
+
expect(matchesKnownBleUuid('abcd0001-1234-5678-9012-abcdefabcdef', aliases)).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('does not treat vendor-specific UUID fragments as communication or FIDO services', () => {
|
|
69
|
+
expect(
|
|
70
|
+
isOnekeyBluetoothDevice({
|
|
71
|
+
name: 'OneKey Pro 2',
|
|
72
|
+
serviceUuids: ['abcdfffd-1234-5678-9012-abcdefabcdef'],
|
|
73
|
+
})
|
|
74
|
+
).toBe(true);
|
|
75
|
+
expect(
|
|
76
|
+
isOnekeyBluetoothDevice({
|
|
77
|
+
serviceUuids: ['abcd0001-1234-5678-9012-abcdefabcdef'],
|
|
78
|
+
})
|
|
79
|
+
).toBe(false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('requires an explicitly advertised communication service for strict discovery', () => {
|
|
83
|
+
expect(hasOnekeyCommunicationService([])).toBe(false);
|
|
84
|
+
expect(hasOnekeyCommunicationService(['0001'])).toBe(true);
|
|
85
|
+
expect(hasOnekeyCommunicationService(['abcd0001-1234-5678-9012-abcdefabcdef'])).toBe(false);
|
|
86
|
+
});
|
|
87
|
+
|
|
16
88
|
it('only filters WebUSB descriptors that are explicitly identified as Trezor', () => {
|
|
17
89
|
expect(
|
|
18
90
|
isKnownTrezorWebUsbDevice({
|
package/src/constants.ts
CHANGED
|
@@ -154,3 +154,71 @@ export const isOnekeyDevice = (name: string | null, id?: string): boolean => {
|
|
|
154
154
|
}
|
|
155
155
|
return isOneKeyShortName(normalizedName);
|
|
156
156
|
};
|
|
157
|
+
|
|
158
|
+
type BluetoothDeviceIdentity = {
|
|
159
|
+
id?: string;
|
|
160
|
+
name?: string | null;
|
|
161
|
+
localName?: string | null;
|
|
162
|
+
serviceUuids?: Array<string | null | undefined> | null;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const BLUETOOTH_BASE_UUID_SUFFIX = '00001000800000805f9b34fb';
|
|
166
|
+
|
|
167
|
+
export const normalizeBleUuid = (uuid?: string | null) =>
|
|
168
|
+
(uuid ?? '').replace(/-/g, '').toLowerCase();
|
|
169
|
+
|
|
170
|
+
export const createKnownBleUuidAliases = (uuid: string): ReadonlySet<string> => {
|
|
171
|
+
const normalized = normalizeBleUuid(uuid);
|
|
172
|
+
const aliases = new Set([normalized]);
|
|
173
|
+
|
|
174
|
+
if (normalized.length !== 32 || !normalized.endsWith(BLUETOOTH_BASE_UUID_SUFFIX)) {
|
|
175
|
+
return aliases;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const assignedNumber = normalized.slice(0, 8);
|
|
179
|
+
aliases.add(assignedNumber);
|
|
180
|
+
if (assignedNumber.startsWith('0000')) {
|
|
181
|
+
aliases.add(assignedNumber.slice(4));
|
|
182
|
+
}
|
|
183
|
+
return aliases;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export const matchesKnownBleUuid = (
|
|
187
|
+
actualUuid: string | null | undefined,
|
|
188
|
+
aliases: ReadonlySet<string>
|
|
189
|
+
) => aliases.has(normalizeBleUuid(actualUuid));
|
|
190
|
+
|
|
191
|
+
const ONEKEY_COMMUNICATION_SERVICE_ALIASES = createKnownBleUuidAliases(ONEKEY_SERVICE_UUID);
|
|
192
|
+
const FIDO_SERVICE_ALIASES = createKnownBleUuidAliases('0000fffd-0000-1000-8000-00805f9b34fb');
|
|
193
|
+
|
|
194
|
+
const isFindMyAdvertisementName = (value?: string | null) =>
|
|
195
|
+
/\bfinde?\s+my\b/.test(value?.trim().toLowerCase() ?? '');
|
|
196
|
+
|
|
197
|
+
export const hasOnekeyCommunicationService = (
|
|
198
|
+
serviceUuids: Array<string | null | undefined> | null | undefined
|
|
199
|
+
) =>
|
|
200
|
+
(serviceUuids ?? []).some(uuid =>
|
|
201
|
+
matchesKnownBleUuid(uuid, ONEKEY_COMMUNICATION_SERVICE_ALIASES)
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
export const isOnekeyBluetoothDevice = ({
|
|
205
|
+
id,
|
|
206
|
+
name,
|
|
207
|
+
localName,
|
|
208
|
+
serviceUuids,
|
|
209
|
+
}: BluetoothDeviceIdentity): boolean => {
|
|
210
|
+
const advertisedServiceUuids = serviceUuids ?? [];
|
|
211
|
+
if (isFindMyAdvertisementName(name) || isFindMyAdvertisementName(localName)) {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (hasOnekeyCommunicationService(advertisedServiceUuids)) {
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (advertisedServiceUuids.some(uuid => matchesKnownBleUuid(uuid, FIDO_SERVICE_ALIASES))) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return isOnekeyDevice(name ?? null, id) || isOnekeyDevice(localName ?? null, id);
|
|
224
|
+
};
|