@onekeyfe/hd-shared 1.2.0-alpha.12 → 1.2.0-alpha.14
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 +7 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +39 -5
- package/package.json +2 -2
- package/src/constants.test.ts +60 -0
- package/src/constants.ts +49 -5
package/dist/constants.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ export declare const ONEKEY_WEBUSB_FILTER: {
|
|
|
8
8
|
vendorId: number;
|
|
9
9
|
productId: number;
|
|
10
10
|
}[];
|
|
11
|
+
type WebUsbIdentityDescriptor = {
|
|
12
|
+
vendorId?: number;
|
|
13
|
+
productId?: number;
|
|
14
|
+
manufacturerName?: string | null;
|
|
15
|
+
};
|
|
16
|
+
export declare const isKnownTrezorWebUsbDevice: (descriptor: WebUsbIdentityDescriptor) => boolean;
|
|
11
17
|
export declare const ERROR_CODES_REQUIRE_RELEASE: readonly [106, 107, 109, 112, 805, 801, 802, 812];
|
|
12
18
|
export declare const ERROR_CODES_REQUIRE_DISCONNECT: readonly [106, 805];
|
|
13
19
|
export declare enum EOneKeyBleMessageKeys {
|
|
@@ -39,4 +45,5 @@ export declare const ONEKEY_WRITE_CHARACTERISTIC_UUID = "00000002-0000-1000-8000
|
|
|
39
45
|
export declare const ONEKEY_NOTIFY_CHARACTERISTIC_UUID = "00000003-0000-1000-8000-00805f9b34fb";
|
|
40
46
|
export declare const isHeaderChunk: (chunk: Buffer | Uint8Array) => boolean;
|
|
41
47
|
export declare const isOnekeyDevice: (name: string | null, id?: string) => boolean;
|
|
48
|
+
export {};
|
|
42
49
|
//# 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,MAAM,y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9
|
}[];
|
|
10
|
+
type WebUsbIdentityDescriptor = {
|
|
11
|
+
vendorId?: number;
|
|
12
|
+
productId?: number;
|
|
13
|
+
manufacturerName?: string | null;
|
|
14
|
+
};
|
|
15
|
+
declare const isKnownTrezorWebUsbDevice: (descriptor: WebUsbIdentityDescriptor) => boolean;
|
|
10
16
|
declare const ERROR_CODES_REQUIRE_RELEASE: readonly [106, 107, 109, 112, 805, 801, 802, 812];
|
|
11
17
|
declare const ERROR_CODES_REQUIRE_DISCONNECT: readonly [106, 805];
|
|
12
18
|
declare enum EOneKeyBleMessageKeys {
|
|
@@ -246,4 +252,4 @@ declare enum EDeviceType {
|
|
|
246
252
|
Pro2 = "pro2"
|
|
247
253
|
}
|
|
248
254
|
|
|
249
|
-
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, isOnekeyDevice, serializeError, wait };
|
|
255
|
+
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 };
|
package/dist/index.js
CHANGED
|
@@ -393,6 +393,15 @@ const ONEKEY_WEBUSB_FILTER = [
|
|
|
393
393
|
{ vendorId: 0x1209, productId: 0x4f4b },
|
|
394
394
|
{ vendorId: 0x1209, productId: 0x4f4c },
|
|
395
395
|
];
|
|
396
|
+
const TREZOR_WEBUSB_MANUFACTURER_NAMES = new Set(['trezor', 'trezor company', 'satoshilabs']);
|
|
397
|
+
const isKnownTrezorWebUsbDevice = (descriptor) => {
|
|
398
|
+
var _a;
|
|
399
|
+
const isSharedWebUsbId = ONEKEY_WEBUSB_FILTER.some(filter => descriptor.vendorId === filter.vendorId && descriptor.productId === filter.productId);
|
|
400
|
+
if (!isSharedWebUsbId)
|
|
401
|
+
return false;
|
|
402
|
+
const manufacturerName = (_a = descriptor.manufacturerName) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase();
|
|
403
|
+
return manufacturerName != null && TREZOR_WEBUSB_MANUFACTURER_NAMES.has(manufacturerName);
|
|
404
|
+
};
|
|
396
405
|
const ERROR_CODES_REQUIRE_RELEASE = [
|
|
397
406
|
HardwareErrorCode.DeviceInitializeFailed,
|
|
398
407
|
HardwareErrorCode.DeviceInterruptedFromOutside,
|
|
@@ -448,16 +457,40 @@ const isHeaderChunk = (chunk) => {
|
|
|
448
457
|
}
|
|
449
458
|
return false;
|
|
450
459
|
};
|
|
460
|
+
const isKnownNonOneKeyDeviceName = (normalizedName) => normalizedName.startsWith('trezor') ||
|
|
461
|
+
normalizedName.startsWith('ledger') ||
|
|
462
|
+
normalizedName.startsWith('nano ') ||
|
|
463
|
+
normalizedName.startsWith('stax') ||
|
|
464
|
+
normalizedName.startsWith('flex');
|
|
465
|
+
const ONEKEY_EXACT_SHORT_NAMES = new Set(['s8']);
|
|
466
|
+
const isOneKeyShortName = (normalizedName) => {
|
|
467
|
+
if (ONEKEY_EXACT_SHORT_NAMES.has(normalizedName))
|
|
468
|
+
return true;
|
|
469
|
+
if (normalizedName.length !== 5)
|
|
470
|
+
return false;
|
|
471
|
+
const firstChar = normalizedName[0];
|
|
472
|
+
return (firstChar === 'k' || firstChar === 't') && /^\d{4}$/.test(normalizedName.slice(1));
|
|
473
|
+
};
|
|
451
474
|
const isOnekeyDevice = (name, id) => {
|
|
452
|
-
var _a;
|
|
453
|
-
|
|
475
|
+
var _a, _b;
|
|
476
|
+
const normalizedName = (_a = name === null || name === void 0 ? void 0 : name.trim().toLowerCase()) !== null && _a !== void 0 ? _a : '';
|
|
477
|
+
if (isKnownNonOneKeyDeviceName(normalizedName)) {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
if ((_b = id === null || id === void 0 ? void 0 : id.startsWith) === null || _b === void 0 ? void 0 : _b.call(id, 'MI')) {
|
|
454
481
|
return true;
|
|
455
482
|
}
|
|
456
|
-
|
|
457
|
-
|
|
483
|
+
if (!name) {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
if (normalizedName.startsWith('onekey') || normalizedName.startsWith('bixinkey'))
|
|
487
|
+
return true;
|
|
488
|
+
if (normalizedName.startsWith('touch ') ||
|
|
489
|
+
normalizedName.startsWith('pro ') ||
|
|
490
|
+
normalizedName.startsWith('pro2 ')) {
|
|
458
491
|
return true;
|
|
459
492
|
}
|
|
460
|
-
return
|
|
493
|
+
return isOneKeyShortName(normalizedName);
|
|
461
494
|
};
|
|
462
495
|
|
|
463
496
|
/******************************************************************************
|
|
@@ -555,6 +588,7 @@ exports.createNeedUpgradeFirmwareHardwareError = createNeedUpgradeFirmwareHardwa
|
|
|
555
588
|
exports.createNewFirmwareForceUpdateHardwareError = createNewFirmwareForceUpdateHardwareError;
|
|
556
589
|
exports.createNewFirmwareUnReleaseHardwareError = createNewFirmwareUnReleaseHardwareError;
|
|
557
590
|
exports.isHeaderChunk = isHeaderChunk;
|
|
591
|
+
exports.isKnownTrezorWebUsbDevice = isKnownTrezorWebUsbDevice;
|
|
558
592
|
exports.isOnekeyDevice = isOnekeyDevice;
|
|
559
593
|
exports.serializeError = serializeError;
|
|
560
594
|
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.14",
|
|
4
4
|
"description": "Hardware SDK's shared tool library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Hardware-SDK",
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"lint": "eslint .",
|
|
26
26
|
"lint:fix": "eslint . --fix"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "c1edaed6435fb6049c7516c64b298ee62519e797"
|
|
29
29
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { isKnownTrezorWebUsbDevice, isOnekeyDevice } from './constants';
|
|
2
|
+
|
|
3
|
+
describe('hardware device identity filters', () => {
|
|
4
|
+
it('accepts known OneKey BLE names', () => {
|
|
5
|
+
expect(isOnekeyDevice('Touch A1B2')).toBe(true);
|
|
6
|
+
expect(isOnekeyDevice('Pro A1B2')).toBe(true);
|
|
7
|
+
expect(isOnekeyDevice('K1234')).toBe(true);
|
|
8
|
+
expect(isOnekeyDevice('S8')).toBe(true);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('rejects known Trezor and Ledger BLE names from OneKey discovery', () => {
|
|
12
|
+
expect(isOnekeyDevice('Trezor Safe 7')).toBe(false);
|
|
13
|
+
expect(isOnekeyDevice('Ledger Nano X')).toBe(false);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('only filters WebUSB descriptors that are explicitly identified as Trezor', () => {
|
|
17
|
+
expect(
|
|
18
|
+
isKnownTrezorWebUsbDevice({
|
|
19
|
+
vendorId: 0x1209,
|
|
20
|
+
productId: 0x53c1,
|
|
21
|
+
manufacturerName: 'Trezor Company',
|
|
22
|
+
})
|
|
23
|
+
).toBe(true);
|
|
24
|
+
expect(
|
|
25
|
+
isKnownTrezorWebUsbDevice({
|
|
26
|
+
vendorId: 0x1209,
|
|
27
|
+
productId: 0x53c1,
|
|
28
|
+
manufacturerName: 'Trezor',
|
|
29
|
+
})
|
|
30
|
+
).toBe(true);
|
|
31
|
+
expect(
|
|
32
|
+
isKnownTrezorWebUsbDevice({
|
|
33
|
+
vendorId: 0x1209,
|
|
34
|
+
productId: 0x53c1,
|
|
35
|
+
manufacturerName: 'SatoshiLabs',
|
|
36
|
+
})
|
|
37
|
+
).toBe(true);
|
|
38
|
+
|
|
39
|
+
expect(
|
|
40
|
+
isKnownTrezorWebUsbDevice({
|
|
41
|
+
vendorId: 0x1209,
|
|
42
|
+
productId: 0x53c1,
|
|
43
|
+
manufacturerName: 'OneKey',
|
|
44
|
+
})
|
|
45
|
+
).toBe(false);
|
|
46
|
+
expect(
|
|
47
|
+
isKnownTrezorWebUsbDevice({
|
|
48
|
+
vendorId: 0x1209,
|
|
49
|
+
productId: 0x53c1,
|
|
50
|
+
})
|
|
51
|
+
).toBe(false);
|
|
52
|
+
expect(
|
|
53
|
+
isKnownTrezorWebUsbDevice({
|
|
54
|
+
vendorId: 0x1209,
|
|
55
|
+
productId: 0x53c1,
|
|
56
|
+
productName: 'Trezor Safe 7',
|
|
57
|
+
})
|
|
58
|
+
).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
});
|
package/src/constants.ts
CHANGED
|
@@ -17,6 +17,24 @@ export const ONEKEY_WEBUSB_FILTER = [
|
|
|
17
17
|
// { vendorId: 0x1209, productId: 0x4f50 }, // Touch Board
|
|
18
18
|
];
|
|
19
19
|
|
|
20
|
+
type WebUsbIdentityDescriptor = {
|
|
21
|
+
vendorId?: number;
|
|
22
|
+
productId?: number;
|
|
23
|
+
manufacturerName?: string | null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const TREZOR_WEBUSB_MANUFACTURER_NAMES = new Set(['trezor', 'trezor company', 'satoshilabs']);
|
|
27
|
+
|
|
28
|
+
export const isKnownTrezorWebUsbDevice = (descriptor: WebUsbIdentityDescriptor): boolean => {
|
|
29
|
+
const isSharedWebUsbId = ONEKEY_WEBUSB_FILTER.some(
|
|
30
|
+
filter => descriptor.vendorId === filter.vendorId && descriptor.productId === filter.productId
|
|
31
|
+
);
|
|
32
|
+
if (!isSharedWebUsbId) return false;
|
|
33
|
+
|
|
34
|
+
const manufacturerName = descriptor.manufacturerName?.trim().toLowerCase();
|
|
35
|
+
return manufacturerName != null && TREZOR_WEBUSB_MANUFACTURER_NAMES.has(manufacturerName);
|
|
36
|
+
};
|
|
37
|
+
|
|
20
38
|
/**
|
|
21
39
|
* Error codes that require device release after occurrence
|
|
22
40
|
* These errors indicate the device is in an invalid state and needs to be released
|
|
@@ -97,16 +115,42 @@ export const isHeaderChunk = (chunk: Buffer | Uint8Array): boolean => {
|
|
|
97
115
|
return false;
|
|
98
116
|
};
|
|
99
117
|
|
|
118
|
+
const isKnownNonOneKeyDeviceName = (normalizedName: string): boolean =>
|
|
119
|
+
normalizedName.startsWith('trezor') ||
|
|
120
|
+
normalizedName.startsWith('ledger') ||
|
|
121
|
+
normalizedName.startsWith('nano ') ||
|
|
122
|
+
normalizedName.startsWith('stax') ||
|
|
123
|
+
normalizedName.startsWith('flex');
|
|
124
|
+
|
|
125
|
+
const ONEKEY_EXACT_SHORT_NAMES = new Set(['s8']);
|
|
126
|
+
|
|
127
|
+
const isOneKeyShortName = (normalizedName: string): boolean => {
|
|
128
|
+
if (ONEKEY_EXACT_SHORT_NAMES.has(normalizedName)) return true;
|
|
129
|
+
if (normalizedName.length !== 5) return false;
|
|
130
|
+
const firstChar = normalizedName[0];
|
|
131
|
+
return (firstChar === 'k' || firstChar === 't') && /^\d{4}$/.test(normalizedName.slice(1));
|
|
132
|
+
};
|
|
133
|
+
|
|
100
134
|
export const isOnekeyDevice = (name: string | null, id?: string): boolean => {
|
|
135
|
+
const normalizedName = name?.trim().toLowerCase() ?? '';
|
|
136
|
+
if (isKnownNonOneKeyDeviceName(normalizedName)) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
101
140
|
if (id?.startsWith?.('MI')) {
|
|
102
141
|
return true;
|
|
103
142
|
}
|
|
104
143
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (
|
|
144
|
+
if (!name) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
if (normalizedName.startsWith('onekey') || normalizedName.startsWith('bixinkey')) return true;
|
|
148
|
+
if (
|
|
149
|
+
normalizedName.startsWith('touch ') ||
|
|
150
|
+
normalizedName.startsWith('pro ') ||
|
|
151
|
+
normalizedName.startsWith('pro2 ')
|
|
152
|
+
) {
|
|
109
153
|
return true;
|
|
110
154
|
}
|
|
111
|
-
return
|
|
155
|
+
return isOneKeyShortName(normalizedName);
|
|
112
156
|
};
|