@onekeyfe/hardware-cli 1.2.0-alpha.13 → 1.2.0-alpha.131

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,8 +1,13 @@
1
1
  import {
2
2
  ERRORS,
3
3
  HardwareErrorCode,
4
+ ONEKEY_NOTIFY_CHARACTERISTIC_UUID,
4
5
  ONEKEY_SERVICE_UUID,
5
- isOnekeyDevice,
6
+ ONEKEY_WRITE_CHARACTERISTIC_UUID,
7
+ createKnownBleUuidAliases,
8
+ hasOnekeyCommunicationService,
9
+ isOnekeyBluetoothDevice,
10
+ matchesKnownBleUuid,
6
11
  } from '@onekeyfe/hd-shared';
7
12
 
8
13
  import type { LowLevelDevice, LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport';
@@ -39,13 +44,9 @@ type NobleNotificationState = {
39
44
  };
40
45
 
41
46
  const ONEKEY_SERVICE_UUIDS = [ONEKEY_SERVICE_UUID];
42
- const PRO2_ADVERTISEMENT_SERVICE_UUID_KEYS = new Set(['fffd']);
43
- const NORMALIZED_WRITE_UUID = '0002';
44
- const NORMALIZED_NOTIFY_UUID = '0003';
45
- const NORMALIZED_ONEKEY_SERVICE_UUIDS = new Set([
46
- ...ONEKEY_SERVICE_UUIDS.map(uuid => getBleUuidKey(uuid)),
47
- '0001',
48
- ]);
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);
49
50
 
50
51
  const BLUETOOTH_INIT_TIMEOUT = 10_000;
51
52
  const DEVICE_SCAN_TIMEOUT = 8_000;
@@ -63,29 +64,16 @@ const deviceCharacteristics = new Map<string, CharacteristicPair>();
63
64
  const notificationStates = new Map<string, NobleNotificationState>();
64
65
  const notificationGenerations = new Map<string, number>();
65
66
 
66
- function getBleUuidKey(uuid?: string | null) {
67
- const normalized = (uuid ?? '').replace(/-/g, '').toLowerCase();
68
- return normalized.length >= 8 ? normalized.substring(4, 8) : normalized;
69
- }
70
-
71
- function isGenericBleService(uuid?: string | null) {
72
- return ['1800', '1801', '180a', '180f'].includes(getBleUuidKey(uuid));
73
- }
74
-
75
- function hasOneKeyAdvertisementService(peripheral: Peripheral) {
76
- const serviceUuids = peripheral.advertisement?.serviceUuids ?? [];
77
- return serviceUuids.some(uuid => {
78
- const uuidKey = getBleUuidKey(uuid);
79
- return (
80
- NORMALIZED_ONEKEY_SERVICE_UUIDS.has(uuidKey) ||
81
- PRO2_ADVERTISEMENT_SERVICE_UUID_KEYS.has(uuidKey)
82
- );
83
- });
84
- }
85
-
86
67
  function isOneKeyPeripheral(peripheral: Peripheral) {
87
- const deviceName = peripheral.advertisement?.localName || null;
88
- 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
+ );
89
77
  }
90
78
 
91
79
  function enqueueNotification(deviceId: string, generation: number, data: Buffer) {
@@ -230,7 +218,7 @@ async function scanDevices(targetDeviceId?: string) {
230
218
 
231
219
  const onDiscover = (peripheral: Peripheral) => {
232
220
  if (targetDeviceId && peripheral.id !== targetDeviceId) return;
233
- if (!targetDeviceId && !isOneKeyPeripheral(peripheral)) return;
221
+ if (!isOneKeyPeripheral(peripheral)) return;
234
222
 
235
223
  discoveredDevices.set(peripheral.id, peripheral);
236
224
  found.set(peripheral.id, peripheral);
@@ -285,13 +273,7 @@ async function discoverCharacteristics(peripheral: Peripheral): Promise<Characte
285
273
  });
286
274
  });
287
275
 
288
- let service = services.find(s => NORMALIZED_ONEKEY_SERVICE_UUIDS.has(getBleUuidKey(s.uuid)));
289
- if (!service) {
290
- service =
291
- services.find(s => PRO2_ADVERTISEMENT_SERVICE_UUID_KEYS.has(getBleUuidKey(s.uuid))) ||
292
- services.find(s => !isGenericBleService(s.uuid)) ||
293
- services[0];
294
- }
276
+ const service = services.find(s => matchesKnownBleUuid(s.uuid, ONEKEY_SERVICE_UUID_ALIASES));
295
277
  if (!service) {
296
278
  throw ERRORS.TypedError(HardwareErrorCode.BleServiceNotFound, 'No BLE service found');
297
279
  }
@@ -310,10 +292,9 @@ async function discoverCharacteristics(peripheral: Peripheral): Promise<Characte
310
292
  let writeCharacteristic: Characteristic | undefined;
311
293
  let notifyCharacteristic: Characteristic | undefined;
312
294
  for (const characteristic of characteristics) {
313
- const uuidKey = getBleUuidKey(characteristic.uuid);
314
- if (uuidKey === NORMALIZED_WRITE_UUID) {
295
+ if (matchesKnownBleUuid(characteristic.uuid, ONEKEY_WRITE_UUID_ALIASES)) {
315
296
  writeCharacteristic = characteristic;
316
- } else if (uuidKey === NORMALIZED_NOTIFY_UUID) {
297
+ } else if (matchesKnownBleUuid(characteristic.uuid, ONEKEY_NOTIFY_UUID_ALIASES)) {
317
298
  notifyCharacteristic = characteristic;
318
299
  }
319
300
  }
@@ -437,23 +418,31 @@ export function createNobleBlePlugin(): LowlevelTransportSharedPlugin {
437
418
  }
438
419
 
439
420
  await connectPeripheral(peripheral);
440
- const characteristics = await discoverCharacteristics(peripheral);
441
- const notificationState = createNotificationState(uuid);
421
+ let characteristics: CharacteristicPair | undefined;
442
422
  try {
423
+ characteristics = await discoverCharacteristics(peripheral);
424
+ const notificationState = createNotificationState(uuid);
443
425
  await subscribeNotifications(uuid, notificationState.generation, characteristics.notify);
426
+ connectedDevices.set(uuid, peripheral);
427
+ deviceCharacteristics.set(uuid, characteristics);
444
428
  } catch (error) {
445
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
+ }
446
437
  throw error;
447
438
  }
448
- connectedDevices.set(uuid, peripheral);
449
- deviceCharacteristics.set(uuid, characteristics);
450
439
  },
451
440
 
452
441
  async disconnect(uuid: string) {
453
442
  await disconnectDevice(uuid);
454
443
  },
455
444
 
456
- async send(uuid: string, data: string) {
445
+ async send(uuid: string, data: string, options?: { withoutResponse?: boolean }) {
457
446
  const characteristics = deviceCharacteristics.get(uuid);
458
447
  if (!characteristics) {
459
448
  throw ERRORS.TypedError(
@@ -463,9 +452,10 @@ export function createNobleBlePlugin(): LowlevelTransportSharedPlugin {
463
452
  }
464
453
 
465
454
  const buffer = Buffer.from(data, 'hex');
455
+ const withoutResponse = options?.withoutResponse ?? true;
466
456
  for (let offset = 0; offset < buffer.length; offset += BLE_PACKET_SIZE) {
467
457
  const chunk = buffer.subarray(offset, Math.min(offset + BLE_PACKET_SIZE, buffer.length));
468
- await writeCharacteristic(characteristics.write, chunk, true);
458
+ await writeCharacteristic(characteristics.write, chunk, withoutResponse);
469
459
  }
470
460
  },
471
461