@onekeyfe/hardware-cli 1.2.0-alpha.12 → 1.2.0-alpha.121

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,13 @@
1
+ export function selectSearchDevice<T extends { connectId?: string | null }>(
2
+ devices: T[],
3
+ preferredConnectId?: string
4
+ ): T | undefined {
5
+ if (preferredConnectId) {
6
+ return (
7
+ devices.find(device => device.connectId === preferredConnectId) ??
8
+ ({ connectId: preferredConnectId } as T)
9
+ );
10
+ }
11
+
12
+ return devices[0];
13
+ }
@@ -0,0 +1,71 @@
1
+ import type { CoreApi, DeviceStateScope, KnownDevice, SearchDevice } from '@onekeyfe/hd-core';
2
+
3
+ type DeviceStateSdk = Pick<CoreApi, 'searchDevices' | 'getDeviceState' | 'getFeatures'>;
4
+
5
+ type DiscoveredDevice = SearchDevice & Partial<Pick<KnownDevice, 'features' | 'state'>>;
6
+
7
+ const createDeviceNotFoundResult = (connectId?: string) => ({
8
+ success: false as const,
9
+ payload: {
10
+ code: 'DEVICE_NOT_FOUND',
11
+ error: connectId ? `Device not found: ${connectId}` : 'No device found',
12
+ },
13
+ });
14
+
15
+ const resolveSearchDevice = async (sdk: DeviceStateSdk, connectId?: string) => {
16
+ const searchResult = await sdk.searchDevices();
17
+ if (!searchResult.success) return searchResult;
18
+
19
+ const devices = searchResult.payload as DiscoveredDevice[];
20
+ const device = connectId ? devices.find(item => item.connectId === connectId) : devices[0];
21
+
22
+ if (!device?.connectId) return createDeviceNotFoundResult(connectId);
23
+ return { success: true as const, payload: device };
24
+ };
25
+
26
+ /**
27
+ * Unified state entry for the new CLI. Resolve the user-facing connectId through
28
+ * discovery so V1 serial IDs map to the process-local USB path.
29
+ */
30
+ export const getCanonicalDeviceState = async (
31
+ sdk: DeviceStateSdk,
32
+ connectId: string | undefined,
33
+ scope: DeviceStateScope
34
+ ) => {
35
+ const deviceResult = await resolveSearchDevice(sdk, connectId);
36
+ if (!deviceResult.success) return deviceResult;
37
+
38
+ if (scope === 'runtime' && deviceResult.payload.state) {
39
+ return { success: true as const, payload: deviceResult.payload.state };
40
+ }
41
+
42
+ const resolvedConnectId = deviceResult.payload.connectId ?? undefined;
43
+ if (!resolvedConnectId) return createDeviceNotFoundResult();
44
+ return sdk.getDeviceState(resolvedConnectId, { scope });
45
+ };
46
+
47
+ /**
48
+ * Legacy CLI only: retain getFeatures for V1 and reuse the discovery projection for V2.
49
+ * Public SDK.getFeatures remains V1-only.
50
+ */
51
+ export const getCompatibleFeatures = async (sdk: DeviceStateSdk, connectId?: string) => {
52
+ const deviceResult = await resolveSearchDevice(sdk, connectId);
53
+ if (!deviceResult.success) return deviceResult;
54
+
55
+ const device = deviceResult.payload;
56
+ const protocol = device.state?.protocol ?? device.features?.protocol;
57
+ if (protocol === 'V2') {
58
+ if (!device.features) {
59
+ return {
60
+ success: false as const,
61
+ payload: {
62
+ code: 'DEVICE_FEATURES_UNAVAILABLE',
63
+ error: 'Protocol V2 compatibility features are unavailable',
64
+ },
65
+ };
66
+ }
67
+ return { success: true as const, payload: device.features };
68
+ }
69
+
70
+ return sdk.getFeatures(device.connectId ?? '');
71
+ };
package/src/pinentry.ts CHANGED
@@ -73,6 +73,7 @@ export function findPinentry(): string | null {
73
73
  export interface PinentryResult {
74
74
  value: string;
75
75
  passphraseOnDevice: boolean;
76
+ attachPinOnDevice?: boolean;
76
77
  }
77
78
 
78
79
  // CLI-variant policy differs from app-monorepo: we fall back to on-device
package/src/sdk.ts CHANGED
@@ -5,8 +5,8 @@
5
5
  * Passphrase flow aligns with app-monorepo CLI:
6
6
  * - Standard wallet: --use-empty-passphrase, auto-respond
7
7
  * - Hidden wallet: interactive 1/2/3 selection (standard / pinentry / on-device)
8
- * - Session caching: passphraseState + sessionId stored in OS keychain,
9
- * preloaded via preloadSessionCache on next invocation
8
+ * - Legacy session caching: existing OS keychain entries may still be preloaded,
9
+ * but public SDK responses never expose new device session ids
10
10
  */
11
11
 
12
12
  import * as readline from 'node:readline';
@@ -16,7 +16,7 @@ import { DEVICE, UI_EVENT, UI_REQUEST, UI_RESPONSE } from '@onekeyfe/hd-core';
16
16
  import { promptPassphraseViaPinentry } from './pinentry';
17
17
  import { createNobleBlePlugin } from './transports/nobleBlePlugin';
18
18
 
19
- import type { ConnectSettings } from '@onekeyfe/hd-core';
19
+ import type { ConnectSettings, KnownDevice } from '@onekeyfe/hd-core';
20
20
  import type { PinentryResult } from './pinentry';
21
21
 
22
22
  export interface SDKOptions {
@@ -59,9 +59,15 @@ let sdkReadyPromise: Promise<typeof HardwareSDK> | null = null;
59
59
  * 2. Hidden wallet — enter passphrase via pinentry (secure OS dialog)
60
60
  * 3. Hidden wallet — enter passphrase on device screen
61
61
  */
62
- function resolvePassphraseByChoice(choice: '1' | '2' | '3'): Promise<PinentryResult> {
62
+ export function resolvePassphraseByChoice(choice: '1' | '2' | '3' | '4'): Promise<PinentryResult> {
63
63
  if (choice === '1') return Promise.resolve({ value: '', passphraseOnDevice: false });
64
64
  if (choice === '2') return promptPassphraseViaPinentry();
65
+ if (choice === '4')
66
+ return Promise.resolve({
67
+ value: '',
68
+ passphraseOnDevice: false,
69
+ attachPinOnDevice: true,
70
+ });
65
71
  return Promise.resolve({ value: '', passphraseOnDevice: true });
66
72
  }
67
73
 
@@ -84,18 +90,19 @@ function promptPassphraseMode(): Promise<PinentryResult> {
84
90
  ' 1. Standard wallet (no passphrase)',
85
91
  ' 2. Hidden wallet — enter passphrase on this computer (pinentry)',
86
92
  ' 3. Hidden wallet — enter passphrase on device screen',
93
+ ' 4. Attach PIN wallet — enter Attach PIN on device screen',
87
94
  '',
88
95
  ].join('\n')
89
96
  );
90
97
 
91
98
  rl.question('Enter selection [1/2/3]: ', answer => {
92
- const n = answer.trim() as '1' | '2' | '3';
93
- if (n === '1' || n === '2' || n === '3') {
99
+ const n = answer.trim() as '1' | '2' | '3' | '4';
100
+ if (n === '1' || n === '2' || n === '3' || n === '4') {
94
101
  rl.close();
95
102
  resolvePassphraseByChoice(n).then(resolve);
96
103
  return;
97
104
  }
98
- process.stderr.write('Invalid selection. Enter 1, 2, or 3.\n');
105
+ process.stderr.write('Invalid selection. Enter 1, 2, 3, or 4.\n');
99
106
  prompt();
100
107
  });
101
108
  };
@@ -143,6 +150,7 @@ function registerEventHandlers(sdk: typeof HardwareSDK): void {
143
150
  payload: {
144
151
  value: result.value,
145
152
  passphraseOnDevice: result.passphraseOnDevice,
153
+ attachPinOnDevice: result.attachPinOnDevice,
146
154
  save: false,
147
155
  },
148
156
  });
@@ -169,12 +177,12 @@ function registerEventHandlers(sdk: typeof HardwareSDK): void {
169
177
  }
170
178
  });
171
179
 
172
- sdk.on(DEVICE.CONNECT, (device: any) => {
180
+ sdk.on(DEVICE.CONNECT, ({ device }: { device: KnownDevice }) => {
173
181
  const name = device?.label || device?.name;
174
182
  if (name) process.stderr.write(`[onekey-hw] Device connected: ${name}\n`);
175
183
  });
176
184
 
177
- sdk.on(DEVICE.DISCONNECT, (device: any) => {
185
+ sdk.on(DEVICE.DISCONNECT, ({ device }: { device: KnownDevice }) => {
178
186
  const name = device?.label || device?.name;
179
187
  if (name) process.stderr.write(`[onekey-hw] Device disconnected: ${name}\n`);
180
188
  });
@@ -231,7 +239,7 @@ export async function disposeSDK(): Promise<void> {
231
239
  if (!sdkReadyPromise) return;
232
240
  try {
233
241
  const sdk = await sdkReadyPromise;
234
- sdk.dispose();
242
+ await Promise.resolve(sdk.dispose());
235
243
  } catch {
236
244
  // ignore errors during cleanup
237
245
  } finally {
package/src/session.ts CHANGED
@@ -1,11 +1,8 @@
1
1
  /**
2
2
  * Passphrase session management for hd-cli.
3
3
  *
4
- * Aligns with app-monorepo's CLI pattern:
5
- * Login: getPassphraseState passphraseState + sessionId keychain
6
- * Command: keychain → preloadSessionCache → SDK call (no passphrase prompt)
7
- * Stale: error 112 → clear keychain → re-prompt → retry
8
- * Logout: keychain delete
4
+ * Existing keychain entries remain readable for compatibility, but the public
5
+ * SDK no longer exposes new device session ids for persistence.
9
6
  */
10
7
 
11
8
  import { preloadSessionCache } from '@onekeyfe/hd-core';
@@ -55,25 +52,6 @@ export async function preloadSessionFromKeychain(deviceId: string): Promise<stri
55
52
  return undefined;
56
53
  }
57
54
 
58
- /**
59
- * Save passphraseState + sessionId to keychain for next CLI invocation.
60
- */
61
- export async function saveSessionToKeychain(
62
- deviceId: string,
63
- passphraseState: string,
64
- sessionId: string
65
- ): Promise<void> {
66
- try {
67
- const storage = getStorage();
68
- await Promise.all([
69
- storage.set(psKey(deviceId), Buffer.from(passphraseState, 'utf-8')),
70
- storage.set(sidKey(deviceId), Buffer.from(sessionId, 'utf-8')),
71
- ]);
72
- } catch {
73
- // Non-fatal — session still works in-memory for this invocation
74
- }
75
- }
76
-
77
55
  /**
78
56
  * Clear cached session from keychain.
79
57
  */
@@ -1,9 +1,13 @@
1
1
  import {
2
2
  ERRORS,
3
3
  HardwareErrorCode,
4
+ ONEKEY_NOTIFY_CHARACTERISTIC_UUID,
4
5
  ONEKEY_SERVICE_UUID,
5
- isOnekeyDevice,
6
- wait,
6
+ ONEKEY_WRITE_CHARACTERISTIC_UUID,
7
+ createKnownBleUuidAliases,
8
+ hasOnekeyCommunicationService,
9
+ isOnekeyBluetoothDevice,
10
+ matchesKnownBleUuid,
7
11
  } from '@onekeyfe/hd-shared';
8
12
 
9
13
  import type { LowLevelDevice, LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport';
@@ -40,13 +44,9 @@ type NobleNotificationState = {
40
44
  };
41
45
 
42
46
  const ONEKEY_SERVICE_UUIDS = [ONEKEY_SERVICE_UUID];
43
- const PRO2_ADVERTISEMENT_SERVICE_UUID_KEYS = new Set(['fffd']);
44
- const NORMALIZED_WRITE_UUID = '0002';
45
- const NORMALIZED_NOTIFY_UUID = '0003';
46
- const NORMALIZED_ONEKEY_SERVICE_UUIDS = new Set([
47
- ...ONEKEY_SERVICE_UUIDS.map(uuid => getBleUuidKey(uuid)),
48
- '0001',
49
- ]);
47
+ const ONEKEY_SERVICE_UUID_ALIASES = createKnownBleUuidAliases(ONEKEY_SERVICE_UUID);
48
+ const ONEKEY_WRITE_UUID_ALIASES = createKnownBleUuidAliases(ONEKEY_WRITE_CHARACTERISTIC_UUID);
49
+ const ONEKEY_NOTIFY_UUID_ALIASES = createKnownBleUuidAliases(ONEKEY_NOTIFY_CHARACTERISTIC_UUID);
50
50
 
51
51
  const BLUETOOTH_INIT_TIMEOUT = 10_000;
52
52
  const DEVICE_SCAN_TIMEOUT = 8_000;
@@ -54,7 +54,6 @@ const CONNECTION_TIMEOUT = 8_000;
54
54
  const SERVICE_DISCOVERY_TIMEOUT = 10_000;
55
55
  const BLE_CLEANUP_TIMEOUT = 100;
56
56
  const BLE_PACKET_SIZE = 192;
57
- const BLE_WRITE_DELAY = 5;
58
57
  const BLE_ENCRYPTION_ERROR_PATTERNS = [/encryption is insufficient/i, /insufficient encryption/i];
59
58
 
60
59
  let noble: NobleModule | null = null;
@@ -65,29 +64,16 @@ const deviceCharacteristics = new Map<string, CharacteristicPair>();
65
64
  const notificationStates = new Map<string, NobleNotificationState>();
66
65
  const notificationGenerations = new Map<string, number>();
67
66
 
68
- function getBleUuidKey(uuid?: string | null) {
69
- const normalized = (uuid ?? '').replace(/-/g, '').toLowerCase();
70
- return normalized.length >= 8 ? normalized.substring(4, 8) : normalized;
71
- }
72
-
73
- function isGenericBleService(uuid?: string | null) {
74
- return ['1800', '1801', '180a', '180f'].includes(getBleUuidKey(uuid));
75
- }
76
-
77
- function hasOneKeyAdvertisementService(peripheral: Peripheral) {
78
- const serviceUuids = peripheral.advertisement?.serviceUuids ?? [];
79
- return serviceUuids.some(uuid => {
80
- const uuidKey = getBleUuidKey(uuid);
81
- return (
82
- NORMALIZED_ONEKEY_SERVICE_UUIDS.has(uuidKey) ||
83
- PRO2_ADVERTISEMENT_SERVICE_UUID_KEYS.has(uuidKey)
84
- );
85
- });
86
- }
87
-
88
67
  function isOneKeyPeripheral(peripheral: Peripheral) {
89
- const deviceName = peripheral.advertisement?.localName || null;
90
- return isOnekeyDevice(deviceName, peripheral.id) || hasOneKeyAdvertisementService(peripheral);
68
+ const serviceUuids = peripheral.advertisement?.serviceUuids;
69
+ return (
70
+ hasOnekeyCommunicationService(serviceUuids) &&
71
+ isOnekeyBluetoothDevice({
72
+ id: peripheral.id,
73
+ localName: peripheral.advertisement?.localName,
74
+ serviceUuids,
75
+ })
76
+ );
91
77
  }
92
78
 
93
79
  function enqueueNotification(deviceId: string, generation: number, data: Buffer) {
@@ -232,7 +218,7 @@ async function scanDevices(targetDeviceId?: string) {
232
218
 
233
219
  const onDiscover = (peripheral: Peripheral) => {
234
220
  if (targetDeviceId && peripheral.id !== targetDeviceId) return;
235
- if (!targetDeviceId && !isOneKeyPeripheral(peripheral)) return;
221
+ if (!isOneKeyPeripheral(peripheral)) return;
236
222
 
237
223
  discoveredDevices.set(peripheral.id, peripheral);
238
224
  found.set(peripheral.id, peripheral);
@@ -287,13 +273,7 @@ async function discoverCharacteristics(peripheral: Peripheral): Promise<Characte
287
273
  });
288
274
  });
289
275
 
290
- let service = services.find(s => NORMALIZED_ONEKEY_SERVICE_UUIDS.has(getBleUuidKey(s.uuid)));
291
- if (!service) {
292
- service =
293
- services.find(s => PRO2_ADVERTISEMENT_SERVICE_UUID_KEYS.has(getBleUuidKey(s.uuid))) ||
294
- services.find(s => !isGenericBleService(s.uuid)) ||
295
- services[0];
296
- }
276
+ const service = services.find(s => matchesKnownBleUuid(s.uuid, ONEKEY_SERVICE_UUID_ALIASES));
297
277
  if (!service) {
298
278
  throw ERRORS.TypedError(HardwareErrorCode.BleServiceNotFound, 'No BLE service found');
299
279
  }
@@ -312,10 +292,9 @@ async function discoverCharacteristics(peripheral: Peripheral): Promise<Characte
312
292
  let writeCharacteristic: Characteristic | undefined;
313
293
  let notifyCharacteristic: Characteristic | undefined;
314
294
  for (const characteristic of characteristics) {
315
- const uuidKey = getBleUuidKey(characteristic.uuid);
316
- if (uuidKey === NORMALIZED_WRITE_UUID) {
295
+ if (matchesKnownBleUuid(characteristic.uuid, ONEKEY_WRITE_UUID_ALIASES)) {
317
296
  writeCharacteristic = characteristic;
318
- } else if (uuidKey === NORMALIZED_NOTIFY_UUID) {
297
+ } else if (matchesKnownBleUuid(characteristic.uuid, ONEKEY_NOTIFY_UUID_ALIASES)) {
319
298
  notifyCharacteristic = characteristic;
320
299
  }
321
300
  }
@@ -439,23 +418,31 @@ export function createNobleBlePlugin(): LowlevelTransportSharedPlugin {
439
418
  }
440
419
 
441
420
  await connectPeripheral(peripheral);
442
- const characteristics = await discoverCharacteristics(peripheral);
443
- const notificationState = createNotificationState(uuid);
421
+ let characteristics: CharacteristicPair | undefined;
444
422
  try {
423
+ characteristics = await discoverCharacteristics(peripheral);
424
+ const notificationState = createNotificationState(uuid);
445
425
  await subscribeNotifications(uuid, notificationState.generation, characteristics.notify);
426
+ connectedDevices.set(uuid, peripheral);
427
+ deviceCharacteristics.set(uuid, characteristics);
446
428
  } catch (error) {
447
429
  clearNotificationState(uuid, `BLE notification subscription failed: ${uuid}`);
430
+ if (characteristics) {
431
+ characteristics.notify.removeAllListeners('data');
432
+ await waitForNobleCleanup(callback => characteristics?.notify.unsubscribe(callback));
433
+ }
434
+ if (peripheral.state !== 'disconnected') {
435
+ await waitForNobleCleanup(callback => peripheral?.disconnect(callback));
436
+ }
448
437
  throw error;
449
438
  }
450
- connectedDevices.set(uuid, peripheral);
451
- deviceCharacteristics.set(uuid, characteristics);
452
439
  },
453
440
 
454
441
  async disconnect(uuid: string) {
455
442
  await disconnectDevice(uuid);
456
443
  },
457
444
 
458
- async send(uuid: string, data: string) {
445
+ async send(uuid: string, data: string, options?: { withoutResponse?: boolean }) {
459
446
  const characteristics = deviceCharacteristics.get(uuid);
460
447
  if (!characteristics) {
461
448
  throw ERRORS.TypedError(
@@ -465,12 +452,10 @@ export function createNobleBlePlugin(): LowlevelTransportSharedPlugin {
465
452
  }
466
453
 
467
454
  const buffer = Buffer.from(data, 'hex');
455
+ const withoutResponse = options?.withoutResponse ?? true;
468
456
  for (let offset = 0; offset < buffer.length; offset += BLE_PACKET_SIZE) {
469
457
  const chunk = buffer.subarray(offset, Math.min(offset + BLE_PACKET_SIZE, buffer.length));
470
- await writeCharacteristic(characteristics.write, chunk, true);
471
- if (offset + BLE_PACKET_SIZE < buffer.length) {
472
- await wait(BLE_WRITE_DELAY);
473
- }
458
+ await writeCharacteristic(characteristics.write, chunk, withoutResponse);
474
459
  }
475
460
  },
476
461