@onekeyfe/hd-shared 1.1.27 → 1.1.29-alpha.1

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.
@@ -3,6 +3,12 @@ export declare const ONEKEY_WEBUSB_FILTER: {
3
3
  vendorId: number;
4
4
  productId: number;
5
5
  }[];
6
+ type WebUsbIdentityDescriptor = {
7
+ vendorId?: number;
8
+ productId?: number;
9
+ manufacturerName?: string | null;
10
+ };
11
+ export declare const isKnownTrezorWebUsbDevice: (descriptor: WebUsbIdentityDescriptor) => boolean;
6
12
  export declare const ERROR_CODES_REQUIRE_RELEASE: readonly [106, 107, 109, 112, 805, 801, 802, 812];
7
13
  export declare const ERROR_CODES_REQUIRE_DISCONNECT: readonly [106, 805];
8
14
  export declare enum EOneKeyBleMessageKeys {
@@ -34,4 +40,5 @@ export declare const ONEKEY_WRITE_CHARACTERISTIC_UUID = "00000002-0000-1000-8000
34
40
  export declare const ONEKEY_NOTIFY_CHARACTERISTIC_UUID = "00000003-0000-1000-8000-00805f9b34fb";
35
41
  export declare const isHeaderChunk: (chunk: Buffer | Uint8Array) => boolean;
36
42
  export declare const isOnekeyDevice: (name: string | null, id?: string) => boolean;
43
+ export {};
37
44
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";AAEA,eAAO,MAAM,oBAAoB;;;GAOhC,CAAC;AAMF,eAAO,MAAM,2BAA2B,mDAS9B,CAAC;AAMX,eAAO,MAAM,8BAA8B,qBAGjC,CAAC;AAGX,oBAAY,qBAAqB;IAE/B,UAAU,uBAAuB;IACjC,iBAAiB,8BAA8B;IAC/C,aAAa,0BAA0B;IACvC,kBAAkB,+BAA+B;IACjD,cAAc,2BAA2B;IACzC,oBAAoB,iCAAiC;IAGrD,mBAAmB,gCAAgC;IACnD,oBAAoB,iCAAiC;IAGrD,aAAa,0BAA0B;IACvC,oBA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;IAGrD,uBAAuB,oCAAoC;IAC3D,sBAAsB,mCAAmC;IAGzD,mBAAmB,gCAAgC;IACnD,mBAAmB,gCAAgC;IACnD,oBAAoB,iCAAiC;IACrD,iBAAiB,8BAA8B;IAC/C,oBAAoB,iCAAiC;IACrD,eAAe,4BAA4B;IAC3C,mBAAmB,gCAAgC;IACnD,qBAAqB,kCAAkC;IACvD,sBAAsB,mCAAmC;IACzD,wBAAwB,qCAAqC;CAC9D;AAED,eAAO,MAAM,mBAAmB,yCAAyC,CAAC;AAC1E,eAAO,MAAM,gCAAgC,yCAAyC,CAAC;AACvF,eAAO,MAAM,iCAAiC,yCAAyC,CAAC;AAIxF,eAAO,MAAM,aAAa,UAAW,MAAM,GAAG,UAAU,KAAG,OAa1D,CAAC;AAyBF,eAAO,MAAM,cAAc,SAAU,MAAM,GAAG,IAAI,OAAO,MAAM,KAAG,OAgBjE,CAAC"}
package/dist/index.d.ts CHANGED
@@ -2,6 +2,12 @@ declare const ONEKEY_WEBUSB_FILTER: {
2
2
  vendorId: number;
3
3
  productId: number;
4
4
  }[];
5
+ type WebUsbIdentityDescriptor = {
6
+ vendorId?: number;
7
+ productId?: number;
8
+ manufacturerName?: string | null;
9
+ };
10
+ declare const isKnownTrezorWebUsbDevice: (descriptor: WebUsbIdentityDescriptor) => boolean;
5
11
  declare const ERROR_CODES_REQUIRE_RELEASE: readonly [106, 107, 109, 112, 805, 801, 802, 812];
6
12
  declare const ERROR_CODES_REQUIRE_DISCONNECT: readonly [106, 805];
7
13
  declare enum EOneKeyBleMessageKeys {
@@ -239,4 +245,4 @@ declare enum EDeviceType {
239
245
  Pro = "pro"
240
246
  }
241
247
 
242
- export { CreateErrorByMessage, CreateHardwareErrorByBridgeError, Deferred, EDeviceType, EFirmwareType, EOneKeyBleMessageKeys, HardwareError$1 as ERRORS, ERROR_CODES_REQUIRE_DISCONNECT, ERROR_CODES_REQUIRE_RELEASE, 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 };
248
+ export { CreateErrorByMessage, CreateHardwareErrorByBridgeError, Deferred, EDeviceType, EFirmwareType, EOneKeyBleMessageKeys, HardwareError$1 as ERRORS, ERROR_CODES_REQUIRE_DISCONNECT, ERROR_CODES_REQUIRE_RELEASE, 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
@@ -386,6 +386,15 @@ const ONEKEY_WEBUSB_FILTER = [
386
386
  { vendorId: 0x1209, productId: 0x4f4a },
387
387
  { vendorId: 0x1209, productId: 0x4f4b },
388
388
  ];
389
+ const TREZOR_WEBUSB_MANUFACTURER_NAMES = new Set(['trezor', 'trezor company']);
390
+ const isKnownTrezorWebUsbDevice = (descriptor) => {
391
+ var _a;
392
+ const isSharedWebUsbId = ONEKEY_WEBUSB_FILTER.some(filter => descriptor.vendorId === filter.vendorId && descriptor.productId === filter.productId);
393
+ if (!isSharedWebUsbId)
394
+ return false;
395
+ const manufacturerName = (_a = descriptor.manufacturerName) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase();
396
+ return manufacturerName != null && TREZOR_WEBUSB_MANUFACTURER_NAMES.has(manufacturerName);
397
+ };
389
398
  const ERROR_CODES_REQUIRE_RELEASE = [
390
399
  HardwareErrorCode.DeviceInitializeFailed,
391
400
  HardwareErrorCode.DeviceInterruptedFromOutside,
@@ -441,16 +450,44 @@ const isHeaderChunk = (chunk) => {
441
450
  }
442
451
  return false;
443
452
  };
453
+ const isAllDigits = (value) => {
454
+ if (!value)
455
+ return false;
456
+ for (const char of value) {
457
+ if (char < '0' || char > '9')
458
+ return false;
459
+ }
460
+ return true;
461
+ };
462
+ const isOneKeyShortName = (name) => {
463
+ const upperName = name.toUpperCase();
464
+ if (upperName.length === 5 && (upperName[0] === 'K' || upperName[0] === 'T')) {
465
+ return isAllDigits(upperName.slice(1));
466
+ }
467
+ return false;
468
+ };
469
+ const isKnownNonOneKeyDeviceName = (normalizedName) => normalizedName.startsWith('trezor') ||
470
+ normalizedName.startsWith('ledger') ||
471
+ normalizedName.startsWith('nano ') ||
472
+ normalizedName.startsWith('stax') ||
473
+ normalizedName.startsWith('flex');
444
474
  const isOnekeyDevice = (name, id) => {
445
- var _a;
446
- if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
447
- return true;
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;
448
479
  }
449
- const re = /(BixinKey\d{10})|(K\d{4})|(T\d{4})|(Touch\s\w{4})|(Pro\s\w{4})/i;
450
- if (name && re.exec(name)) {
480
+ if ((_b = id === null || id === void 0 ? void 0 : id.startsWith) === null || _b === void 0 ? void 0 : _b.call(id, 'MI')) {
451
481
  return true;
452
482
  }
453
- return false;
483
+ if (!name) {
484
+ return false;
485
+ }
486
+ if (normalizedName.startsWith('onekey') || normalizedName.startsWith('bixinkey'))
487
+ return true;
488
+ if (normalizedName.startsWith('touch ') || normalizedName.startsWith('pro '))
489
+ return true;
490
+ return isOneKeyShortName(name.trim());
454
491
  };
455
492
 
456
493
  /******************************************************************************
@@ -546,6 +583,7 @@ exports.createNeedUpgradeFirmwareHardwareError = createNeedUpgradeFirmwareHardwa
546
583
  exports.createNewFirmwareForceUpdateHardwareError = createNewFirmwareForceUpdateHardwareError;
547
584
  exports.createNewFirmwareUnReleaseHardwareError = createNewFirmwareUnReleaseHardwareError;
548
585
  exports.isHeaderChunk = isHeaderChunk;
586
+ exports.isKnownTrezorWebUsbDevice = isKnownTrezorWebUsbDevice;
549
587
  exports.isOnekeyDevice = isOnekeyDevice;
550
588
  exports.serializeError = serializeError;
551
589
  exports.wait = wait;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-shared",
3
- "version": "1.1.27",
3
+ "version": "1.1.29-alpha.1",
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": "f0c80ce0246152fee79f9cae836db5d550bb82f0"
28
+ "gitHead": "db793e686fecbecbfa72aab8fd35c70159a72e35"
29
29
  }
@@ -0,0 +1,52 @@
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
+ });
9
+
10
+ it('rejects known Trezor and Ledger BLE names from OneKey discovery', () => {
11
+ expect(isOnekeyDevice('Trezor Safe 7')).toBe(false);
12
+ expect(isOnekeyDevice('Ledger Nano X')).toBe(false);
13
+ });
14
+
15
+ it('only filters WebUSB descriptors that are explicitly identified as Trezor', () => {
16
+ expect(
17
+ isKnownTrezorWebUsbDevice({
18
+ vendorId: 0x1209,
19
+ productId: 0x53c1,
20
+ manufacturerName: 'Trezor Company',
21
+ })
22
+ ).toBe(true);
23
+ expect(
24
+ isKnownTrezorWebUsbDevice({
25
+ vendorId: 0x1209,
26
+ productId: 0x53c1,
27
+ manufacturerName: 'Trezor',
28
+ })
29
+ ).toBe(true);
30
+
31
+ expect(
32
+ isKnownTrezorWebUsbDevice({
33
+ vendorId: 0x1209,
34
+ productId: 0x53c1,
35
+ manufacturerName: 'OneKey',
36
+ })
37
+ ).toBe(false);
38
+ expect(
39
+ isKnownTrezorWebUsbDevice({
40
+ vendorId: 0x1209,
41
+ productId: 0x53c1,
42
+ })
43
+ ).toBe(false);
44
+ expect(
45
+ isKnownTrezorWebUsbDevice({
46
+ vendorId: 0x1209,
47
+ productId: 0x53c1,
48
+ productName: 'Trezor Safe 7',
49
+ })
50
+ ).toBe(false);
51
+ });
52
+ });
package/src/constants.ts CHANGED
@@ -9,6 +9,24 @@ export const ONEKEY_WEBUSB_FILTER = [
9
9
  // { vendorId: 0x1209, productId: 0x4f50 }, // Touch Board
10
10
  ];
11
11
 
12
+ type WebUsbIdentityDescriptor = {
13
+ vendorId?: number;
14
+ productId?: number;
15
+ manufacturerName?: string | null;
16
+ };
17
+
18
+ const TREZOR_WEBUSB_MANUFACTURER_NAMES = new Set(['trezor', 'trezor company']);
19
+
20
+ export const isKnownTrezorWebUsbDevice = (descriptor: WebUsbIdentityDescriptor): boolean => {
21
+ const isSharedWebUsbId = ONEKEY_WEBUSB_FILTER.some(
22
+ filter => descriptor.vendorId === filter.vendorId && descriptor.productId === filter.productId
23
+ );
24
+ if (!isSharedWebUsbId) return false;
25
+
26
+ const manufacturerName = descriptor.manufacturerName?.trim().toLowerCase();
27
+ return manufacturerName != null && TREZOR_WEBUSB_MANUFACTURER_NAMES.has(manufacturerName);
28
+ };
29
+
12
30
  /**
13
31
  * Error codes that require device release after occurrence
14
32
  * These errors indicate the device is in an invalid state and needs to be released
@@ -89,16 +107,43 @@ export const isHeaderChunk = (chunk: Buffer | Uint8Array): boolean => {
89
107
  return false;
90
108
  };
91
109
 
110
+ const isAllDigits = (value: string): boolean => {
111
+ if (!value) return false;
112
+ for (const char of value) {
113
+ if (char < '0' || char > '9') return false;
114
+ }
115
+ return true;
116
+ };
117
+
118
+ const isOneKeyShortName = (name: string): boolean => {
119
+ const upperName = name.toUpperCase();
120
+ if (upperName.length === 5 && (upperName[0] === 'K' || upperName[0] === 'T')) {
121
+ return isAllDigits(upperName.slice(1));
122
+ }
123
+ return false;
124
+ };
125
+
126
+ const isKnownNonOneKeyDeviceName = (normalizedName: string): boolean =>
127
+ normalizedName.startsWith('trezor') ||
128
+ normalizedName.startsWith('ledger') ||
129
+ normalizedName.startsWith('nano ') ||
130
+ normalizedName.startsWith('stax') ||
131
+ normalizedName.startsWith('flex');
132
+
92
133
  export const isOnekeyDevice = (name: string | null, id?: string): boolean => {
134
+ const normalizedName = name?.trim().toLowerCase() ?? '';
135
+ if (isKnownNonOneKeyDeviceName(normalizedName)) {
136
+ return false;
137
+ }
138
+
93
139
  if (id?.startsWith?.('MI')) {
94
140
  return true;
95
141
  }
96
142
 
97
- // 过滤 BixinKeyxxx 和 Kxxxx 和 Txxxx
98
- // i 忽略大小写模式
99
- const re = /(BixinKey\d{10})|(K\d{4})|(T\d{4})|(Touch\s\w{4})|(Pro\s\w{4})/i;
100
- if (name && re.exec(name)) {
101
- return true;
143
+ if (!name) {
144
+ return false;
102
145
  }
103
- return false;
146
+ if (normalizedName.startsWith('onekey') || normalizedName.startsWith('bixinkey')) return true;
147
+ if (normalizedName.startsWith('touch ') || normalizedName.startsWith('pro ')) return true;
148
+ return isOneKeyShortName(name.trim());
104
149
  };