@onekeyfe/hd-core 1.2.0-alpha.16 → 1.2.0-alpha.17
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/__tests__/deviceFeaturesUtils.test.ts +27 -0
- package/__tests__/evmLedgerLegacySafety.test.ts +3 -0
- package/__tests__/homescreen.test.ts +24 -0
- package/__tests__/protocol-v2.test.ts +1006 -204
- package/__tests__/protocolV2FileWrite.test.ts +28 -3
- package/__tests__/protocolV2UiInteraction.test.ts +266 -0
- package/dist/api/BaseMethod.d.ts +4 -0
- package/dist/api/BaseMethod.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV4.d.ts +3 -3
- package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
- package/dist/api/GetDeviceInfo.d.ts.map +1 -1
- package/dist/api/GetPassphraseState.d.ts.map +1 -1
- package/dist/api/UploadPortfolio.d.ts.map +1 -1
- package/dist/api/device/DeviceCancel.d.ts.map +1 -1
- package/dist/api/device/DeviceChangePin.d.ts.map +1 -1
- package/dist/api/device/DeviceLock.d.ts.map +1 -1
- package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
- package/dist/api/device/DeviceWipe.d.ts.map +1 -1
- package/dist/api/helpers/protocolV2FileWrite.d.ts +1 -0
- package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
- package/dist/api/index.d.ts +1 -1
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/protocol-v2/DeviceInfoGet.d.ts.map +1 -1
- package/dist/api/protocol-v2/DeviceSessionOpen.d.ts +28 -0
- package/dist/api/protocol-v2/DeviceSessionOpen.d.ts.map +1 -0
- package/dist/api/protocol-v2/DeviceSettingsPageShow.d.ts.map +1 -1
- package/dist/api/protocol-v2/DeviceSettingsSet.d.ts.map +1 -1
- package/dist/api/protocol-v2/DeviceStatusGet.d.ts.map +1 -1
- package/dist/api/protocol-v2/DeviceUploadWallpaper.d.ts.map +1 -1
- package/dist/core/deviceEventRegistration.d.ts +1 -0
- package/dist/core/deviceEventRegistration.d.ts.map +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/device/Device.d.ts +3 -3
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/device/DeviceCommands.d.ts +3 -1
- package/dist/device/DeviceCommands.d.ts.map +1 -1
- package/dist/device/DevicePool.d.ts +2 -2
- package/dist/device/DevicePool.d.ts.map +1 -1
- package/dist/deviceProfile/buildDeviceFeatures.d.ts +6 -2
- package/dist/deviceProfile/buildDeviceFeatures.d.ts.map +1 -1
- package/dist/deviceProfile/buildDeviceProfile.d.ts +3 -2
- package/dist/deviceProfile/buildDeviceProfile.d.ts.map +1 -1
- package/dist/events/device.d.ts +4 -0
- package/dist/events/device.d.ts.map +1 -1
- package/dist/events/ui-request.d.ts +18 -2
- package/dist/events/ui-request.d.ts.map +1 -1
- package/dist/index.d.ts +56 -11
- package/dist/index.js +1000 -537
- package/dist/protocols/protocol-v2/features.d.ts +6 -18
- package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/index.d.ts +1 -1
- package/dist/protocols/protocol-v2/index.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/uiInteraction.d.ts +39 -0
- package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -0
- package/dist/protocols/protocol-v2/unlockRetry.d.ts +5 -3
- package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/walletSession.d.ts +2 -1
- package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
- package/dist/types/api/getDeviceInfo.d.ts +2 -1
- package/dist/types/api/getDeviceInfo.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +2 -2
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/api/protocolV2.d.ts +4 -2
- package/dist/types/api/protocolV2.d.ts.map +1 -1
- package/dist/types/device.d.ts +2 -1
- package/dist/types/device.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
- package/dist/utils/homescreen.d.ts.map +1 -1
- package/dist/utils/patch.d.ts +1 -1
- package/dist/utils/patch.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/BaseMethod.ts +10 -2
- package/src/api/FirmwareUpdateV4.ts +27 -73
- package/src/api/GetDeviceInfo.ts +9 -2
- package/src/api/GetPassphraseState.ts +5 -2
- package/src/api/UploadPortfolio.ts +2 -0
- package/src/api/device/DeviceCancel.ts +1 -0
- package/src/api/device/DeviceChangePin.ts +23 -0
- package/src/api/device/DeviceLock.ts +1 -0
- package/src/api/device/DeviceUnlock.ts +9 -0
- package/src/api/device/DeviceWipe.ts +18 -0
- package/src/api/helpers/protocolV2FileWrite.ts +35 -11
- package/src/api/index.ts +1 -1
- package/src/api/protocol-v2/DeviceInfoGet.ts +0 -1
- package/src/api/protocol-v2/DeviceSessionOpen.ts +100 -0
- package/src/api/protocol-v2/DeviceSettingsPageShow.ts +9 -1
- package/src/api/protocol-v2/DeviceSettingsSet.ts +11 -1
- package/src/api/protocol-v2/DeviceStatusGet.ts +1 -0
- package/src/api/protocol-v2/DeviceUploadWallpaper.ts +2 -1
- package/src/core/deviceEventRegistration.ts +2 -0
- package/src/core/index.ts +36 -3
- package/src/data/messages/messages-protocol-v2.json +122 -23
- package/src/device/Device.ts +33 -41
- package/src/device/DeviceCommands.ts +22 -4
- package/src/device/DevicePool.ts +14 -6
- package/src/deviceProfile/buildDeviceFeatures.ts +21 -9
- package/src/deviceProfile/buildDeviceProfile.ts +27 -11
- package/src/events/device.ts +4 -0
- package/src/events/ui-request.ts +24 -2
- package/src/inject.ts +2 -2
- package/src/protocols/protocol-v2/features.ts +27 -20
- package/src/protocols/protocol-v2/index.ts +0 -1
- package/src/protocols/protocol-v2/uiInteraction.ts +209 -0
- package/src/protocols/protocol-v2/unlockRetry.ts +43 -3
- package/src/protocols/protocol-v2/walletSession.ts +122 -31
- package/src/types/api/getDeviceInfo.ts +6 -1
- package/src/types/api/index.ts +2 -2
- package/src/types/api/protocolV2.ts +5 -3
- package/src/types/device.ts +6 -1
- package/src/utils/deviceFeaturesUtils.ts +13 -0
- package/src/utils/homescreen.ts +10 -0
- package/dist/api/protocol-v2/DeviceSessionGet.d.ts +0 -6
- package/dist/api/protocol-v2/DeviceSessionGet.d.ts.map +0 -1
- package/src/api/protocol-v2/DeviceSessionGet.ts +0 -15
package/src/inject.ts
CHANGED
|
@@ -164,8 +164,8 @@ export const createCoreApi = (
|
|
|
164
164
|
deviceStatusGet: (connectId, params) => call({ ...params, connectId, method: 'deviceStatusGet' }),
|
|
165
165
|
deviceGetOnboardingStatus: (connectId, params) =>
|
|
166
166
|
call({ ...params, connectId, method: 'deviceGetOnboardingStatus' }),
|
|
167
|
-
|
|
168
|
-
call({ ...params, connectId, method: '
|
|
167
|
+
deviceSessionOpen: (connectId, params) =>
|
|
168
|
+
call({ ...params, connectId, method: 'deviceSessionOpen' }),
|
|
169
169
|
deviceFirmwareUpdate: (connectId, params) =>
|
|
170
170
|
call({ ...params, connectId, method: 'deviceFirmwareUpdate' }),
|
|
171
171
|
deviceGetFirmwareUpdateStatus: (connectId, params) =>
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { DeviceSEState, DeviceSeType } from '@onekeyfe/hd-transport';
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
DeviceInfoGet,
|
|
5
|
+
DeviceSEInfo,
|
|
6
|
+
DeviceStatus,
|
|
7
|
+
ProtocolV2DeviceInfo,
|
|
8
|
+
} from '@onekeyfe/hd-transport';
|
|
4
9
|
import type { DeviceCommands } from '../../device/DeviceCommands';
|
|
5
10
|
|
|
6
11
|
// 单源类型:直接使用 hd-transport 生成的 ProtocolV2DeviceInfo / DeviceSEInfo /
|
|
@@ -56,9 +61,12 @@ export const getProtocolV2SeType = (se?: DeviceSEInfo): string | null =>
|
|
|
56
61
|
* 当前 romloader 只上报 hw、fw.romloader 和 fw.bootloader;bootloader
|
|
57
62
|
* 还会上报 application/application_data、coprocessor 或 SE 信息。
|
|
58
63
|
*/
|
|
59
|
-
export const isProtocolV2RomloaderDeviceInfo = (
|
|
64
|
+
export const isProtocolV2RomloaderDeviceInfo = (
|
|
65
|
+
deviceInfo?: ProtocolV2DeviceInfo | null,
|
|
66
|
+
deviceStatus?: DeviceStatus | null
|
|
67
|
+
) =>
|
|
60
68
|
!!deviceInfo &&
|
|
61
|
-
|
|
69
|
+
deviceStatus == null &&
|
|
62
70
|
deviceInfo.fw?.romloader != null &&
|
|
63
71
|
deviceInfo.fw?.application == null &&
|
|
64
72
|
deviceInfo.fw?.application_data == null &&
|
|
@@ -68,15 +76,19 @@ export const isProtocolV2RomloaderDeviceInfo = (deviceInfo?: ProtocolV2DeviceInf
|
|
|
68
76
|
deviceInfo.se3 == null &&
|
|
69
77
|
deviceInfo.se4 == null;
|
|
70
78
|
|
|
71
|
-
export const isProtocolV2BootloaderDeviceInfo = (
|
|
72
|
-
|
|
79
|
+
export const isProtocolV2BootloaderDeviceInfo = (
|
|
80
|
+
deviceInfo?: ProtocolV2DeviceInfo | null,
|
|
81
|
+
deviceStatus?: DeviceStatus | null
|
|
82
|
+
) =>
|
|
83
|
+
!!deviceInfo &&
|
|
84
|
+
deviceStatus == null &&
|
|
85
|
+
!isProtocolV2RomloaderDeviceInfo(deviceInfo, deviceStatus);
|
|
73
86
|
|
|
74
87
|
export const PROTOCOL_V2_FEATURES_DEVICE_INFO_REQUEST = {
|
|
75
88
|
targets: {
|
|
76
89
|
hw: true,
|
|
77
90
|
fw: true,
|
|
78
91
|
coprocessor: true,
|
|
79
|
-
status: true,
|
|
80
92
|
},
|
|
81
93
|
types: {
|
|
82
94
|
version: true,
|
|
@@ -90,18 +102,6 @@ export const PROTOCOL_V2_FEATURES_DEVICE_INFO_REQUEST = {
|
|
|
90
102
|
* status 提供 init_states / passphrase_enabled 等会在设备端变化的字段;
|
|
91
103
|
* hw / coprocessor 提供 serialNo / bleName 等身份字段;不含 fw/SE targets,单帧请求开销很小。
|
|
92
104
|
*/
|
|
93
|
-
export const PROTOCOL_V2_STATUS_DEVICE_INFO_REQUEST = {
|
|
94
|
-
targets: {
|
|
95
|
-
hw: true,
|
|
96
|
-
coprocessor: true,
|
|
97
|
-
status: true,
|
|
98
|
-
},
|
|
99
|
-
types: {
|
|
100
|
-
version: true,
|
|
101
|
-
specific: true,
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
|
|
105
105
|
export const PROTOCOL_V2_VERSIONS_DEVICE_INFO_REQUEST = {
|
|
106
106
|
targets: {
|
|
107
107
|
hw: true,
|
|
@@ -111,7 +111,6 @@ export const PROTOCOL_V2_VERSIONS_DEVICE_INFO_REQUEST = {
|
|
|
111
111
|
se2: true,
|
|
112
112
|
se3: true,
|
|
113
113
|
se4: true,
|
|
114
|
-
status: true,
|
|
115
114
|
},
|
|
116
115
|
types: {
|
|
117
116
|
version: true,
|
|
@@ -128,7 +127,6 @@ export const PROTOCOL_V2_FULL_DEVICE_INFO_REQUEST = {
|
|
|
128
127
|
se2: true,
|
|
129
128
|
se3: true,
|
|
130
129
|
se4: true,
|
|
131
|
-
status: true,
|
|
132
130
|
},
|
|
133
131
|
types: {
|
|
134
132
|
version: true,
|
|
@@ -157,3 +155,12 @@ export async function requestProtocolV2DeviceInfo({
|
|
|
157
155
|
// DeviceInfoGet 是 V2-only 消息,这里收窄到 V2 形态。
|
|
158
156
|
return message as ProtocolV2DeviceInfo;
|
|
159
157
|
}
|
|
158
|
+
|
|
159
|
+
export async function requestProtocolV2DeviceStatus({
|
|
160
|
+
commands,
|
|
161
|
+
}: {
|
|
162
|
+
commands: DeviceCommands;
|
|
163
|
+
}): Promise<DeviceStatus> {
|
|
164
|
+
const { message } = await commands.typedCall('DeviceStatusGet', 'DeviceStatus', {});
|
|
165
|
+
return message;
|
|
166
|
+
}
|
|
@@ -2,7 +2,6 @@ export {
|
|
|
2
2
|
PROTOCOL_V2_DEVICE_INFO_REQUEST,
|
|
3
3
|
PROTOCOL_V2_DEVICE_INFO_TIMEOUT_MS,
|
|
4
4
|
PROTOCOL_V2_FEATURES_DEVICE_INFO_REQUEST,
|
|
5
|
-
PROTOCOL_V2_STATUS_DEVICE_INFO_REQUEST,
|
|
6
5
|
PROTOCOL_V2_VERSIONS_DEVICE_INFO_REQUEST,
|
|
7
6
|
isProtocolV2BootloaderDeviceInfo,
|
|
8
7
|
isProtocolV2RomloaderDeviceInfo,
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { UI_REQUEST, createUiMessage } from '../../events';
|
|
2
|
+
|
|
3
|
+
import type { CoreMessage, ProtocolV2UiCompletion } from '../../events';
|
|
4
|
+
import type { Device } from '../../device/Device';
|
|
5
|
+
import type { KnownDevice } from '../../types';
|
|
6
|
+
|
|
7
|
+
export type ProtocolV2InteractionDescriptor = {
|
|
8
|
+
request: 'button' | 'pin';
|
|
9
|
+
source: 'method-lifecycle';
|
|
10
|
+
reason:
|
|
11
|
+
| 'change-pin'
|
|
12
|
+
| 'device-unlock'
|
|
13
|
+
| 'settings-page'
|
|
14
|
+
| 'address-confirmation'
|
|
15
|
+
| 'public-key-confirmation'
|
|
16
|
+
| 'signing-confirmation'
|
|
17
|
+
| 'device-management';
|
|
18
|
+
completion: ProtocolV2UiCompletion;
|
|
19
|
+
deviceOnly: true;
|
|
20
|
+
page?: string | number;
|
|
21
|
+
operation?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type ProtocolV2InteractionMethod = {
|
|
25
|
+
name?: string;
|
|
26
|
+
params?: unknown;
|
|
27
|
+
payload?: unknown;
|
|
28
|
+
protocolV2UiInteraction?: ProtocolV2InteractionDescriptor;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type InteractionDevice = Pick<Device, 'isProtocolV2' | 'toMessageObject'>;
|
|
32
|
+
type PostMessage = (message: CoreMessage) => void;
|
|
33
|
+
|
|
34
|
+
export const isProtocolV2UiEnabled = (method: { protocolV2UiMode?: 'auto' | 'none' }) =>
|
|
35
|
+
method.protocolV2UiMode !== 'none';
|
|
36
|
+
|
|
37
|
+
const getDisplayState = (value: unknown) => {
|
|
38
|
+
const visited = new Set<object>();
|
|
39
|
+
let seen = false;
|
|
40
|
+
let enabled = false;
|
|
41
|
+
|
|
42
|
+
const visit = (current: unknown) => {
|
|
43
|
+
if (current == null || typeof current !== 'object') return;
|
|
44
|
+
if (visited.has(current)) return;
|
|
45
|
+
visited.add(current);
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(current)) {
|
|
48
|
+
current.forEach(visit);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
Object.entries(current).forEach(([key, item]) => {
|
|
53
|
+
if ((key === 'show_display' || key === 'showOnOneKey') && typeof item === 'boolean') {
|
|
54
|
+
seen = true;
|
|
55
|
+
enabled ||= item;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
visit(item);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
visit(value);
|
|
63
|
+
return { seen, enabled };
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const createMethodInteraction = (
|
|
67
|
+
reason: 'address-confirmation' | 'public-key-confirmation' | 'signing-confirmation',
|
|
68
|
+
operation: string
|
|
69
|
+
): ProtocolV2InteractionDescriptor => ({
|
|
70
|
+
request: 'button',
|
|
71
|
+
source: 'method-lifecycle',
|
|
72
|
+
reason,
|
|
73
|
+
completion: 'operation-completed',
|
|
74
|
+
deviceOnly: true,
|
|
75
|
+
operation,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const hasCipherConfirmation = (value: unknown): boolean => {
|
|
79
|
+
if (Array.isArray(value)) return value.some(hasCipherConfirmation);
|
|
80
|
+
if (value == null || typeof value !== 'object') return false;
|
|
81
|
+
|
|
82
|
+
const params = value as {
|
|
83
|
+
encrypt?: boolean;
|
|
84
|
+
ask_on_encrypt?: boolean;
|
|
85
|
+
ask_on_decrypt?: boolean;
|
|
86
|
+
};
|
|
87
|
+
return params.encrypt === true ? params.ask_on_encrypt === true : params.ask_on_decrypt === true;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Protocol V2 不再依赖固件 ButtonRequest,因此在统一层根据业务方法生成兼容 UI Event。
|
|
92
|
+
* 显式声明始终优先;地址和公钥只在设备实际展示时提示,签名统一提示一次。
|
|
93
|
+
*/
|
|
94
|
+
export const resolveProtocolV2UiInteraction = (
|
|
95
|
+
method: ProtocolV2InteractionMethod
|
|
96
|
+
): ProtocolV2InteractionDescriptor | undefined => {
|
|
97
|
+
if (method.protocolV2UiInteraction) return method.protocolV2UiInteraction;
|
|
98
|
+
|
|
99
|
+
const operation = method.name;
|
|
100
|
+
if (!operation) return undefined;
|
|
101
|
+
|
|
102
|
+
const isAddress = /getAddress(?:ByLoop)?$/i.test(operation);
|
|
103
|
+
const isPublicKey = /getPublicKey$/i.test(operation);
|
|
104
|
+
if (isAddress || isPublicKey) {
|
|
105
|
+
const paramsDisplay = getDisplayState(method.params);
|
|
106
|
+
const payloadDisplay = getDisplayState(method.payload);
|
|
107
|
+
const display = paramsDisplay.seen ? paramsDisplay : payloadDisplay;
|
|
108
|
+
|
|
109
|
+
// 地址接口通常默认在设备展示;公钥接口存在默认静默读取,优先依赖归一化参数。
|
|
110
|
+
const shouldDisplay = display.seen ? display.enabled : isAddress;
|
|
111
|
+
if (!shouldDisplay) return undefined;
|
|
112
|
+
|
|
113
|
+
return createMethodInteraction(
|
|
114
|
+
isAddress ? 'address-confirmation' : 'public-key-confirmation',
|
|
115
|
+
operation
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (/sign/i.test(operation) || /verifyMessage$/i.test(operation) || operation === 'lnurlAuth') {
|
|
120
|
+
return createMethodInteraction('signing-confirmation', operation);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (operation === 'nostrEncryptMessage' || operation === 'nostrDecryptMessage') {
|
|
124
|
+
const display = getDisplayState(method.params);
|
|
125
|
+
if (!display.enabled) return undefined;
|
|
126
|
+
return createMethodInteraction('signing-confirmation', operation);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (operation === 'cipherKeyValue' && hasCipherConfirmation(method.params)) {
|
|
130
|
+
return createMethodInteraction('signing-confirmation', operation);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return undefined;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export class ProtocolV2UiInteractionCoordinator {
|
|
137
|
+
private readonly device: InteractionDevice;
|
|
138
|
+
|
|
139
|
+
private readonly postMessage: PostMessage;
|
|
140
|
+
|
|
141
|
+
private methodInteraction?: ProtocolV2InteractionDescriptor;
|
|
142
|
+
|
|
143
|
+
private currentPhase?: string;
|
|
144
|
+
|
|
145
|
+
private opened = false;
|
|
146
|
+
|
|
147
|
+
private closed = false;
|
|
148
|
+
|
|
149
|
+
constructor(device: InteractionDevice, postMessage: PostMessage) {
|
|
150
|
+
this.device = device;
|
|
151
|
+
this.postMessage = postMessage;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
enterMethodInteraction(interaction?: ProtocolV2InteractionDescriptor) {
|
|
155
|
+
this.methodInteraction = interaction;
|
|
156
|
+
if (!interaction) return;
|
|
157
|
+
const { request, ...metadata } = interaction;
|
|
158
|
+
|
|
159
|
+
this.emit(
|
|
160
|
+
request === 'pin' ? UI_REQUEST.REQUEST_PIN : UI_REQUEST.REQUEST_BUTTON,
|
|
161
|
+
{
|
|
162
|
+
device: this.device.toMessageObject() as KnownDevice,
|
|
163
|
+
...metadata,
|
|
164
|
+
},
|
|
165
|
+
`method:${request}:${interaction.reason}:${interaction.page ?? ''}:${
|
|
166
|
+
interaction.operation ?? ''
|
|
167
|
+
}`
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
enterUnlockInteraction(method?: string) {
|
|
172
|
+
this.emit(
|
|
173
|
+
UI_REQUEST.REQUEST_PIN,
|
|
174
|
+
{
|
|
175
|
+
device: this.device.toMessageObject() as KnownDevice,
|
|
176
|
+
source: 'unlock-coordinator',
|
|
177
|
+
reason: 'device-locked',
|
|
178
|
+
deviceOnly: true,
|
|
179
|
+
method,
|
|
180
|
+
},
|
|
181
|
+
`unlock:${method ?? ''}`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
resumeMethodInteraction() {
|
|
186
|
+
this.enterMethodInteraction(this.methodInteraction);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
close() {
|
|
190
|
+
if (!this.device.isProtocolV2() || !this.opened || this.closed) return;
|
|
191
|
+
this.closed = true;
|
|
192
|
+
this.postMessage(createUiMessage(UI_REQUEST.CLOSE_UI_WINDOW));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private emit(
|
|
196
|
+
type: typeof UI_REQUEST.REQUEST_BUTTON | typeof UI_REQUEST.REQUEST_PIN,
|
|
197
|
+
payload: any,
|
|
198
|
+
phase: string
|
|
199
|
+
) {
|
|
200
|
+
if (!this.device.isProtocolV2() || this.closed || this.currentPhase === phase) return;
|
|
201
|
+
this.currentPhase = phase;
|
|
202
|
+
this.opened = true;
|
|
203
|
+
const message =
|
|
204
|
+
type === UI_REQUEST.REQUEST_BUTTON
|
|
205
|
+
? createUiMessage(UI_REQUEST.REQUEST_BUTTON, payload)
|
|
206
|
+
: createUiMessage(UI_REQUEST.REQUEST_PIN, payload);
|
|
207
|
+
this.postMessage(message);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -1,15 +1,49 @@
|
|
|
1
1
|
import { LoggerNames, getLogger } from '../../utils';
|
|
2
2
|
import { isDeviceLockedError } from './lockedError';
|
|
3
|
+
import { isProtocolV2UiEnabled, resolveProtocolV2UiInteraction } from './uiInteraction';
|
|
3
4
|
|
|
4
5
|
import type { BaseMethod } from '../../api/BaseMethod';
|
|
5
6
|
import type { Device } from '../../device/Device';
|
|
7
|
+
import type { ProtocolV2UiInteractionCoordinator } from './uiInteraction';
|
|
6
8
|
|
|
7
9
|
const Log = getLogger(LoggerNames.Core);
|
|
8
10
|
|
|
9
|
-
type RunnableMethod = Pick<
|
|
10
|
-
|
|
11
|
+
type RunnableMethod = Pick<
|
|
12
|
+
BaseMethod,
|
|
13
|
+
'run' | 'unlockPolicy' | 'protocolV2UiInteraction' | 'protocolV2UiMode' | 'params' | 'payload'
|
|
14
|
+
> & { name?: string };
|
|
15
|
+
type UnlockableDevice = Pick<Device, 'isProtocolV2' | 'unlockDevice' | 'features'>;
|
|
16
|
+
type UiInteractionCoordinator = Pick<
|
|
17
|
+
ProtocolV2UiInteractionCoordinator,
|
|
18
|
+
'enterMethodInteraction' | 'enterUnlockInteraction' | 'resumeMethodInteraction'
|
|
19
|
+
>;
|
|
11
20
|
|
|
12
|
-
export async function runMethodWithUnlockRetry(
|
|
21
|
+
export async function runMethodWithUnlockRetry(
|
|
22
|
+
method: RunnableMethod,
|
|
23
|
+
device: UnlockableDevice,
|
|
24
|
+
uiCoordinator?: UiInteractionCoordinator
|
|
25
|
+
) {
|
|
26
|
+
const shouldEmitUi = isProtocolV2UiEnabled(method);
|
|
27
|
+
const shouldUnlockBeforeRun =
|
|
28
|
+
device.isProtocolV2() &&
|
|
29
|
+
method.unlockPolicy === 'retry-on-locked' &&
|
|
30
|
+
device.features?.unlocked === false;
|
|
31
|
+
|
|
32
|
+
if (shouldUnlockBeforeRun) {
|
|
33
|
+
if (shouldEmitUi) {
|
|
34
|
+
uiCoordinator?.enterUnlockInteraction(method.name);
|
|
35
|
+
}
|
|
36
|
+
await device.unlockDevice();
|
|
37
|
+
Log.debug('Protocol V2 pre-unlock completed', { method: method.name });
|
|
38
|
+
if (shouldEmitUi) {
|
|
39
|
+
uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
|
|
40
|
+
}
|
|
41
|
+
return method.run();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (shouldEmitUi) {
|
|
45
|
+
uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
|
|
46
|
+
}
|
|
13
47
|
try {
|
|
14
48
|
return await method.run();
|
|
15
49
|
} catch (error) {
|
|
@@ -22,8 +56,14 @@ export async function runMethodWithUnlockRetry(method: RunnableMethod, device: U
|
|
|
22
56
|
}
|
|
23
57
|
|
|
24
58
|
Log.debug('Protocol V2 unlock retry triggered', { method: method.name });
|
|
59
|
+
if (shouldEmitUi) {
|
|
60
|
+
uiCoordinator?.enterUnlockInteraction(method.name);
|
|
61
|
+
}
|
|
25
62
|
await device.unlockDevice();
|
|
26
63
|
Log.debug('Protocol V2 unlock completed', { method: method.name });
|
|
64
|
+
if (shouldEmitUi) {
|
|
65
|
+
uiCoordinator?.resumeMethodInteraction();
|
|
66
|
+
}
|
|
27
67
|
try {
|
|
28
68
|
const response = await method.run();
|
|
29
69
|
Log.debug('Protocol V2 method retry completed', { method: method.name, success: true });
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
2
|
|
|
3
|
+
import { DEVICE, type PassphraseRequestPayload } from '../../events';
|
|
3
4
|
import { isDeviceLockedError } from './lockedError';
|
|
4
5
|
|
|
6
|
+
import type { DeviceSessionOpen } from '@onekeyfe/hd-transport';
|
|
5
7
|
import type { Device } from '../../device/Device';
|
|
8
|
+
import type { PassphrasePromptResponse } from '../../device/DeviceCommands';
|
|
6
9
|
|
|
7
10
|
const getErrorText = (error: unknown) => {
|
|
8
11
|
if (error instanceof Error) return `${error.name} ${error.message}`;
|
|
@@ -19,6 +22,17 @@ const getErrorText = (error: unknown) => {
|
|
|
19
22
|
export const isProtocolV2InvalidSessionError = (error: unknown) =>
|
|
20
23
|
getErrorText(error).toLowerCase().includes('failure_invalidsession');
|
|
21
24
|
|
|
25
|
+
const normalizeHostPassphrase = (passphrase: string | undefined) => {
|
|
26
|
+
const normalized = (passphrase ?? '').normalize('NFKD');
|
|
27
|
+
if (!normalized || normalized.includes('\0') || normalized.length > 50) {
|
|
28
|
+
throw ERRORS.TypedError(
|
|
29
|
+
HardwareErrorCode.CallMethodInvalidParameter,
|
|
30
|
+
'Invalid hidden wallet passphrase.'
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return normalized;
|
|
34
|
+
};
|
|
35
|
+
|
|
22
36
|
export async function requestProtocolV2DeviceStatus(device: Device) {
|
|
23
37
|
const { message } = await device.commands.typedCall('DeviceStatusGet', 'DeviceStatus', {});
|
|
24
38
|
return message;
|
|
@@ -29,54 +43,131 @@ export async function refreshProtocolV2DeviceStatus(device: Device) {
|
|
|
29
43
|
return device.updateProtocolV2Status(status);
|
|
30
44
|
}
|
|
31
45
|
|
|
46
|
+
const openDeviceSession = async (device: Device, request: DeviceSessionOpen) => {
|
|
47
|
+
try {
|
|
48
|
+
return await device.commands.typedCall('DeviceSessionOpen', 'DeviceSession', request);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (!isDeviceLockedError(error)) {
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
await device.unlockDevice();
|
|
54
|
+
return device.commands.typedCall('DeviceSessionOpen', 'DeviceSession', request);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const createHiddenWalletRequest = (
|
|
59
|
+
response: PassphrasePromptResponse
|
|
60
|
+
): {
|
|
61
|
+
request: DeviceSessionOpen;
|
|
62
|
+
deviceEvent?: typeof DEVICE.PASSPHRASE_ON_DEVICE | typeof DEVICE.ATTACH_PIN_ON_DEVICE;
|
|
63
|
+
} => {
|
|
64
|
+
if (response.attachPinOnDevice) {
|
|
65
|
+
return {
|
|
66
|
+
request: {
|
|
67
|
+
select: {
|
|
68
|
+
attach_pin_on_device: {},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
deviceEvent: DEVICE.ATTACH_PIN_ON_DEVICE,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (response.passphraseOnDevice) {
|
|
76
|
+
return {
|
|
77
|
+
request: {
|
|
78
|
+
select: {
|
|
79
|
+
passphrase_on_device: {},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
deviceEvent: DEVICE.PASSPHRASE_ON_DEVICE,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
request: {
|
|
88
|
+
select: {
|
|
89
|
+
host_passphrase: { passphrase: normalizeHostPassphrase(response.passphrase) },
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const openSelectedHiddenWallet = async (
|
|
96
|
+
device: Device,
|
|
97
|
+
promptPayload: PassphraseRequestPayload
|
|
98
|
+
) => {
|
|
99
|
+
const response = await device.commands.promptPassphrase(promptPayload, {
|
|
100
|
+
cancelDeviceOnReject: false,
|
|
101
|
+
});
|
|
102
|
+
const { request, deviceEvent } = createHiddenWalletRequest(response);
|
|
103
|
+
|
|
104
|
+
if (deviceEvent) {
|
|
105
|
+
device.emit(deviceEvent, device, promptPayload);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return openDeviceSession(device, request);
|
|
109
|
+
};
|
|
110
|
+
|
|
32
111
|
export async function getProtocolV2WalletSession(
|
|
33
112
|
device: Device,
|
|
34
|
-
options?: {
|
|
113
|
+
options?: {
|
|
114
|
+
initSession?: boolean;
|
|
115
|
+
expectedPassphraseState?: string;
|
|
116
|
+
onlyMainPin?: boolean;
|
|
117
|
+
}
|
|
35
118
|
) {
|
|
36
119
|
if (options?.initSession) {
|
|
37
120
|
device.clearInternalState();
|
|
38
121
|
}
|
|
39
122
|
|
|
40
|
-
|
|
41
|
-
typeof device.getInternalState === 'function' ? device.getInternalState() : undefined;
|
|
123
|
+
const expectedPassphraseState = options?.expectedPassphraseState ?? device.passphraseState;
|
|
42
124
|
|
|
43
125
|
try {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
126
|
+
// 锁屏时的 passphrase_enabled 不可信。先完成 PIN 解锁,unlockDevice 会立即
|
|
127
|
+
// 获取并合并一份新的 DeviceStatus,之后再决定打开主钱包还是隐藏钱包。
|
|
128
|
+
if (device.features?.unlocked === false) {
|
|
129
|
+
await device.unlockDevice();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (options?.onlyMainPin || device.getCurrentPassphraseProtection() === false) {
|
|
133
|
+
return {
|
|
134
|
+
passphraseState: undefined,
|
|
135
|
+
newSession: undefined,
|
|
136
|
+
unlockedAttachPin: false,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const cachedSessionId =
|
|
141
|
+
typeof device.getInternalState === 'function' ? device.getInternalState() : undefined;
|
|
142
|
+
let response;
|
|
143
|
+
let recoveryFailed = false;
|
|
144
|
+
|
|
145
|
+
if (cachedSessionId && expectedPassphraseState) {
|
|
52
146
|
try {
|
|
53
|
-
|
|
147
|
+
response = await openDeviceSession(device, {
|
|
148
|
+
resume: { session_id: cachedSessionId },
|
|
149
|
+
});
|
|
54
150
|
} catch (error) {
|
|
55
|
-
if (!
|
|
151
|
+
if (!isProtocolV2InvalidSessionError(error)) {
|
|
56
152
|
throw error;
|
|
57
153
|
}
|
|
58
154
|
device.clearInternalState();
|
|
59
|
-
|
|
60
|
-
return requestDeviceSession();
|
|
155
|
+
recoveryFailed = true;
|
|
61
156
|
}
|
|
62
|
-
}
|
|
157
|
+
}
|
|
63
158
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
await device
|
|
72
|
-
response = await requestWalletSession();
|
|
159
|
+
if (!response) {
|
|
160
|
+
const promptPayload: PassphraseRequestPayload = {
|
|
161
|
+
source: 'wallet-session-coordinator',
|
|
162
|
+
reason: recoveryFailed ? 'session-recovery' : 'open-wallet',
|
|
163
|
+
expectedPassphraseState,
|
|
164
|
+
existsAttachPinUser: device.features?.attachToPinEnabled === true,
|
|
165
|
+
};
|
|
166
|
+
response = await openSelectedHiddenWallet(device, promptPayload);
|
|
73
167
|
}
|
|
74
|
-
const { message } = response;
|
|
75
168
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
options.expectedPassphraseState !== message.btc_test_address
|
|
79
|
-
) {
|
|
169
|
+
const { message } = response;
|
|
170
|
+
if (expectedPassphraseState && expectedPassphraseState !== message.btc_test_address) {
|
|
80
171
|
device.clearInternalState();
|
|
81
172
|
throw ERRORS.TypedError(HardwareErrorCode.DeviceCheckPassphraseStateError);
|
|
82
173
|
}
|
|
@@ -86,7 +177,7 @@ export async function getProtocolV2WalletSession(
|
|
|
86
177
|
}
|
|
87
178
|
|
|
88
179
|
device.updateInternalState(
|
|
89
|
-
|
|
180
|
+
true,
|
|
90
181
|
message.btc_test_address,
|
|
91
182
|
device.getCurrentDeviceId(),
|
|
92
183
|
message.session_id,
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { CommonParams, Response } from '../params';
|
|
2
2
|
import type { Features, IDeviceType, OnekeyFeatures } from '../device';
|
|
3
3
|
import type { EFirmwareType } from '@onekeyfe/hd-shared';
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
DeviceStatus,
|
|
6
|
+
ProtocolType,
|
|
7
|
+
ProtocolV2DeviceInfo,
|
|
8
|
+
} from '@onekeyfe/hd-transport';
|
|
5
9
|
|
|
6
10
|
// 协议类型单源:从 hd-transport 的 ProtocolType 派生,只额外允许 'unknown'。
|
|
7
11
|
export type DeviceInfoProtocol = ProtocolType | 'unknown';
|
|
@@ -78,6 +82,7 @@ export type DeviceProfileRaw = {
|
|
|
78
82
|
features?: Features;
|
|
79
83
|
protocolV1OneKeyFeatures?: OnekeyFeatures;
|
|
80
84
|
protocolV2DeviceInfo?: ProtocolV2DeviceInfo;
|
|
85
|
+
protocolV2DeviceStatus?: DeviceStatus;
|
|
81
86
|
};
|
|
82
87
|
|
|
83
88
|
export type DeviceProfile = {
|
package/src/types/api/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
deviceGetOnboardingStatus,
|
|
7
7
|
deviceInfoGet,
|
|
8
8
|
deviceReboot,
|
|
9
|
-
|
|
9
|
+
deviceSessionOpen,
|
|
10
10
|
deviceSettingsGet,
|
|
11
11
|
deviceSettingsPageShow,
|
|
12
12
|
deviceSettingsSet,
|
|
@@ -267,7 +267,7 @@ export type CoreApi = {
|
|
|
267
267
|
deviceInfoGet: typeof deviceInfoGet;
|
|
268
268
|
deviceStatusGet: typeof deviceStatusGet;
|
|
269
269
|
deviceGetOnboardingStatus: typeof deviceGetOnboardingStatus;
|
|
270
|
-
|
|
270
|
+
deviceSessionOpen: typeof deviceSessionOpen;
|
|
271
271
|
deviceFirmwareUpdate: typeof deviceFirmwareUpdate;
|
|
272
272
|
deviceGetFirmwareUpdateStatus: typeof deviceGetFirmwareUpdateStatus;
|
|
273
273
|
deviceFactoryInfoSet: typeof deviceFactoryInfoSet;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { CommonParams, Response } from '../params';
|
|
2
2
|
import type {
|
|
3
|
+
DevOnboardingStatus,
|
|
3
4
|
DeviceFactoryInfo,
|
|
4
5
|
DeviceFirmwareUpdateStatus,
|
|
5
|
-
DevOnboardingStatus,
|
|
6
6
|
DeviceSession,
|
|
7
7
|
DeviceSettings,
|
|
8
8
|
DeviceStatus,
|
|
@@ -17,6 +17,7 @@ import type {
|
|
|
17
17
|
DeviceRebootParams,
|
|
18
18
|
} from '../../api/protocol-v2/helpers';
|
|
19
19
|
import type { DeviceInfoGetParams } from '../../api/protocol-v2/DeviceInfoGet';
|
|
20
|
+
import type { DeviceSessionOpenParams } from '../../api/protocol-v2/DeviceSessionOpen';
|
|
20
21
|
import type { DeviceSettingsSetParams } from '../../api/protocol-v2/DeviceSettingsSet';
|
|
21
22
|
import type { DeviceSettingsPageShowParams } from '../../api/protocol-v2/DeviceSettingsPageShow';
|
|
22
23
|
import type {
|
|
@@ -38,6 +39,7 @@ export type {
|
|
|
38
39
|
DeviceInfoGetTargets,
|
|
39
40
|
DeviceInfoGetTypes,
|
|
40
41
|
} from '../../api/protocol-v2/DeviceInfoGet';
|
|
42
|
+
export type { DeviceSessionOpenParams } from '../../api/protocol-v2/DeviceSessionOpen';
|
|
41
43
|
export type { DeviceSettingsSetParams } from '../../api/protocol-v2/DeviceSettingsSet';
|
|
42
44
|
export type {
|
|
43
45
|
DeviceSettingsPageShowParams,
|
|
@@ -117,9 +119,9 @@ export declare function deviceGetOnboardingStatus(
|
|
|
117
119
|
params?: CommonParams
|
|
118
120
|
): Response<DevOnboardingStatus>;
|
|
119
121
|
|
|
120
|
-
export declare function
|
|
122
|
+
export declare function deviceSessionOpen(
|
|
121
123
|
connectId: string,
|
|
122
|
-
params
|
|
124
|
+
params: CommonParams & DeviceSessionOpenParams
|
|
123
125
|
): Response<DeviceSession>;
|
|
124
126
|
|
|
125
127
|
export declare function deviceFirmwareUpdate(
|