@onekeyfe/hd-core 1.2.8-alpha.5 → 1.2.8-alpha.6
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/README.md +27 -0
- package/__tests__/DeviceCommands.test.ts +13 -66
- package/__tests__/btc-derive-context-hash.test.ts +200 -0
- package/__tests__/core-error-output.test.ts +3 -34
- package/__tests__/device-lifecycle-events.test.ts +17 -64
- package/__tests__/device-pool-state.test.ts +0 -50
- package/__tests__/protocol-v2-bootloader-mode.test.ts +2 -31
- package/__tests__/protocol-v2.test.ts +47 -0
- package/__tests__/refresh-device-state.test.ts +17 -19
- package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
- package/dist/api/GetDeviceState.d.ts.map +1 -1
- package/dist/api/btc/BTCDeriveContextHash.d.ts +16 -0
- package/dist/api/btc/BTCDeriveContextHash.d.ts.map +1 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/device/Device.d.ts +0 -2
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/device/DeviceCommands.d.ts +4 -4
- package/dist/device/DeviceCommands.d.ts.map +1 -1
- package/dist/device/DevicePool.d.ts.map +1 -1
- package/dist/events/logBlockEvent.d.ts.map +1 -1
- package/dist/index.d.ts +14 -9
- package/dist/index.js +148 -38
- package/dist/inject.d.ts.map +1 -1
- package/dist/types/api/btcDeriveContextHash.d.ts +10 -0
- package/dist/types/api/btcDeriveContextHash.d.ts.map +1 -0
- package/dist/types/api/export.d.ts +1 -0
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +2 -0
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/params.d.ts +0 -1
- package/dist/types/params.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/FirmwareUpdateV4.ts +25 -8
- package/src/api/GetDeviceState.ts +5 -2
- package/src/api/btc/BTCDeriveContextHash.ts +90 -0
- package/src/api/index.ts +1 -0
- package/src/core/index.ts +7 -3
- package/src/data/messages/messages.json +51 -0
- package/src/device/Device.ts +1 -11
- package/src/device/DeviceCommands.ts +1 -15
- package/src/device/DevicePool.ts +2 -12
- package/src/events/core.ts +2 -2
- package/src/events/logBlockEvent.ts +1 -0
- package/src/inject.ts +2 -0
- package/src/types/api/btcDeriveContextHash.ts +15 -0
- package/src/types/api/export.ts +1 -0
- package/src/types/api/index.ts +2 -0
- package/src/types/params.ts +1 -1
package/README.md
CHANGED
|
@@ -29,4 +29,31 @@ import Core from '@onekeyfe/hd-core';
|
|
|
29
29
|
Documentation is available [Hardware SDK Getting Started](https://developer.onekey.so/en/hardware-sdk/getting-started)
|
|
30
30
|
|
|
31
31
|
## Examples
|
|
32
|
+
|
|
33
|
+
### Babylon context hash
|
|
34
|
+
|
|
35
|
+
`btcDeriveContextHash(connectId, deviceId, params)` requires Classic 1s firmware
|
|
36
|
+
3.21.0 or later and Protocol V1. It uses the existing wallet session and device
|
|
37
|
+
confirmation flow. Other device families and Protocol V2 are not supported.
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
const result = await HardwareSDK.btcDeriveContextHash(connectId, deviceId, {
|
|
41
|
+
path: "m/84'/0'/0'/0/0", // The currently selected Bitcoin key, not the IKM path.
|
|
42
|
+
appName: 'babylon-btc-vault',
|
|
43
|
+
context: 'deadbeef',
|
|
44
|
+
network: 'bitcoin-mainnet',
|
|
45
|
+
});
|
|
46
|
+
// result.payload.secret is a 32-byte value encoded as 64 lowercase hex characters.
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`appName` must contain 1–64 ASCII lowercase letters, digits or hyphens. `context`
|
|
50
|
+
must be lowercase hex without `0x`, containing 1–1024 decoded bytes. `network` is
|
|
51
|
+
required: `bitcoin-mainnet`, `bitcoin-testnet` (testnet3/testnet4), `bitcoin-signet`,
|
|
52
|
+
or `bitcoin-regtest`. The SDK infers the Bitcoin input script type from `path` and
|
|
53
|
+
encodes `appName` as ASCII bytes; the device performs the derivation internally.
|
|
54
|
+
Device rejection and unsupported firmware errors propagate through the normal
|
|
55
|
+
SDK response. Treat the returned secret as sensitive and do not log or persist it.
|
|
56
|
+
|
|
57
|
+
See the [Babylon specification](https://github.com/babylonlabs-io/babylon-toolkit/blob/main/docs/specs/derive-context-hash.md).
|
|
58
|
+
|
|
32
59
|
// TODO: add example url
|
|
@@ -130,6 +130,19 @@ describe('DeviceCommands failure mapping', () => {
|
|
|
130
130
|
);
|
|
131
131
|
});
|
|
132
132
|
|
|
133
|
+
it('preserves the Protocol V2 peer-removed pairing error code', async () => {
|
|
134
|
+
const commands = createCommands();
|
|
135
|
+
const transportError = Object.assign(new Error('Peer removed pairing information'), {
|
|
136
|
+
errorCode: HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
137
|
+
});
|
|
138
|
+
commands.mainId = 'main-id';
|
|
139
|
+
commands.transport = {
|
|
140
|
+
call: jest.fn().mockRejectedValue(transportError),
|
|
141
|
+
} as any;
|
|
142
|
+
|
|
143
|
+
await expect(commands._commonCall('DeviceInfoGet', {})).rejects.toBe(transportError);
|
|
144
|
+
});
|
|
145
|
+
|
|
133
146
|
it('logs canonical DeviceStatus response fields without exposing the device ID', async () => {
|
|
134
147
|
const commands = createCommands();
|
|
135
148
|
const log = getLogger(LoggerNames.DeviceCommands);
|
|
@@ -401,27 +414,6 @@ describe('DeviceCommands failure mapping', () => {
|
|
|
401
414
|
).rejects.toMatchObject({ errorCode: HardwareErrorCode.DeviceBusy });
|
|
402
415
|
});
|
|
403
416
|
|
|
404
|
-
it('maps commands rejected during a Protocol V2 firmware update to DeviceBusy', async () => {
|
|
405
|
-
const commands = createCommands();
|
|
406
|
-
const message = 'Requested command not allowed while a firmware update is in progress';
|
|
407
|
-
|
|
408
|
-
await expect(
|
|
409
|
-
commands._filterCommonTypes(
|
|
410
|
-
{
|
|
411
|
-
type: 'Failure',
|
|
412
|
-
message: { code: 'Failure_ProcessError', message },
|
|
413
|
-
} as any,
|
|
414
|
-
'ProtocolInfoRequest'
|
|
415
|
-
)
|
|
416
|
-
).rejects.toMatchObject({
|
|
417
|
-
errorCode: HardwareErrorCode.DeviceBusy,
|
|
418
|
-
params: {
|
|
419
|
-
failureCode: 'Failure_ProcessError',
|
|
420
|
-
firmwareMessage: message,
|
|
421
|
-
},
|
|
422
|
-
});
|
|
423
|
-
});
|
|
424
|
-
|
|
425
417
|
it('maps the current AskPin passphrase-disabled response without relying on a subcode', async () => {
|
|
426
418
|
const commands = createCommands();
|
|
427
419
|
|
|
@@ -559,51 +551,6 @@ describe('DeviceCommands failure mapping', () => {
|
|
|
559
551
|
}
|
|
560
552
|
);
|
|
561
553
|
|
|
562
|
-
it('maps the Protocol V2 seed-session busy response to DeviceBusy', async () => {
|
|
563
|
-
const commands = createCommands();
|
|
564
|
-
|
|
565
|
-
await expect(
|
|
566
|
-
commands._filterCommonTypes(
|
|
567
|
-
{
|
|
568
|
-
type: 'Failure',
|
|
569
|
-
message: {
|
|
570
|
-
code: 'Failure_ProcessError',
|
|
571
|
-
subcode: DeviceSessionErrorCode.DeviceSessionError_UserCancelled,
|
|
572
|
-
message: 'PIN cancelled',
|
|
573
|
-
},
|
|
574
|
-
} as any,
|
|
575
|
-
'SignTx'
|
|
576
|
-
)
|
|
577
|
-
).rejects.toMatchObject({
|
|
578
|
-
errorCode: HardwareErrorCode.DeviceBusy,
|
|
579
|
-
params: {
|
|
580
|
-
failureCode: 'Failure_ProcessError',
|
|
581
|
-
subcode: DeviceSessionErrorCode.DeviceSessionError_UserCancelled,
|
|
582
|
-
firmwareMessage: 'PIN cancelled',
|
|
583
|
-
},
|
|
584
|
-
});
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
it('keeps the same Protocol V2 response as ActionCancelled for DeviceSession calls', async () => {
|
|
588
|
-
const commands = createCommands();
|
|
589
|
-
|
|
590
|
-
await expect(
|
|
591
|
-
commands._filterCommonTypes(
|
|
592
|
-
{
|
|
593
|
-
type: 'Failure',
|
|
594
|
-
message: {
|
|
595
|
-
code: 'Failure_ProcessError',
|
|
596
|
-
subcode: DeviceSessionErrorCode.DeviceSessionError_UserCancelled,
|
|
597
|
-
message: 'PIN cancelled',
|
|
598
|
-
},
|
|
599
|
-
} as any,
|
|
600
|
-
'DeviceSessionAskPin'
|
|
601
|
-
)
|
|
602
|
-
).rejects.toMatchObject({
|
|
603
|
-
errorCode: HardwareErrorCode.ActionCancelled,
|
|
604
|
-
});
|
|
605
|
-
});
|
|
606
|
-
|
|
607
554
|
it('does not use the legacy cancellation-message fallback for Protocol V1', async () => {
|
|
608
555
|
const commands = createCommands();
|
|
609
556
|
(commands.device.isProtocolV2 as jest.Mock).mockReturnValue(false);
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { EDeviceType, EFirmwareType, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
+
|
|
3
|
+
import BTCDeriveContextHash from '../src/api/btc/BTCDeriveContextHash';
|
|
4
|
+
import { findMethod } from '../src/api/utils';
|
|
5
|
+
import { getLogBlockLabel, getSafeLogPayload } from '../src/events/logBlockEvent';
|
|
6
|
+
import { createCoreApi } from '../src/inject';
|
|
7
|
+
import { Device } from '../src/device/Device';
|
|
8
|
+
|
|
9
|
+
import type { DeviceFirmwareRange } from '../src/types';
|
|
10
|
+
|
|
11
|
+
jest.mock('../src/data/config', () => ({
|
|
12
|
+
getSDKVersion: jest.fn(() => '1.0.0'),
|
|
13
|
+
DEFAULT_DOMAIN: 'https://jssdk.onekey.so/1.0.0/',
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
const createMethod = (overrides: Record<string, unknown> = {}) =>
|
|
17
|
+
new BTCDeriveContextHash({
|
|
18
|
+
id: 1,
|
|
19
|
+
payload: {
|
|
20
|
+
method: 'btcDeriveContextHash',
|
|
21
|
+
path: "m/84'/0'/0'/0/0",
|
|
22
|
+
appName: 'test-app',
|
|
23
|
+
context: 'deadbeef',
|
|
24
|
+
network: 'bitcoin-mainnet',
|
|
25
|
+
...overrides,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('btcDeriveContextHash', () => {
|
|
30
|
+
test('forwards the public API call through the existing dispatcher', async () => {
|
|
31
|
+
const call = jest
|
|
32
|
+
.fn()
|
|
33
|
+
.mockResolvedValue({ success: true, payload: { secret: '00'.repeat(32) } });
|
|
34
|
+
const api = createCoreApi(call);
|
|
35
|
+
const params = {
|
|
36
|
+
path: "m/84'/0'/0'/0/0",
|
|
37
|
+
appName: 'test-app',
|
|
38
|
+
context: 'deadbeef',
|
|
39
|
+
network: 'bitcoin-mainnet' as const,
|
|
40
|
+
};
|
|
41
|
+
await api.btcDeriveContextHash('connect-id', 'device-id', params);
|
|
42
|
+
expect(call).toHaveBeenCalledWith({
|
|
43
|
+
...params,
|
|
44
|
+
connectId: 'connect-id',
|
|
45
|
+
deviceId: 'device-id',
|
|
46
|
+
method: 'btcDeriveContextHash',
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test.each([
|
|
51
|
+
['bitcoin-mainnet', 'BITCOIN_MAINNET'],
|
|
52
|
+
['bitcoin-testnet', 'BITCOIN_TESTNET'],
|
|
53
|
+
['bitcoin-signet', 'BITCOIN_SIGNET'],
|
|
54
|
+
['bitcoin-regtest', 'BITCOIN_REGTEST'],
|
|
55
|
+
])('maps %s and returns the device result', async (network, wireNetwork) => {
|
|
56
|
+
const method = createMethod({ network });
|
|
57
|
+
method.init();
|
|
58
|
+
const response = { secret: '00'.repeat(32) };
|
|
59
|
+
const typedCall = jest.fn().mockResolvedValue({ message: response });
|
|
60
|
+
method.device = { commands: { typedCall } } as unknown as Device;
|
|
61
|
+
|
|
62
|
+
await expect(method.run()).resolves.toBe(response);
|
|
63
|
+
expect(typedCall).toHaveBeenCalledWith(
|
|
64
|
+
'BabylonDeriveContextHash',
|
|
65
|
+
'BabylonDerivedContextHash',
|
|
66
|
+
{
|
|
67
|
+
address_n: [2147483732, 2147483648, 2147483648, 0, 0],
|
|
68
|
+
script_type: 'SPENDWITNESS',
|
|
69
|
+
app_name: '746573742d617070',
|
|
70
|
+
context: 'deadbeef',
|
|
71
|
+
network: wireNetwork,
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test.each([
|
|
77
|
+
["m/44'/0'/0'/0/0", 'SPENDADDRESS'],
|
|
78
|
+
["m/49'/0'/0'/0/0", 'SPENDP2SHWITNESS'],
|
|
79
|
+
[[2147483734, 2147483648, 2147483648, 0, 0], 'SPENDTAPROOT'],
|
|
80
|
+
])('uses the selected path script type for %s', (path, scriptType) => {
|
|
81
|
+
const method = createMethod({ path });
|
|
82
|
+
method.init();
|
|
83
|
+
expect(method.params.script_type).toBe(scriptType);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('accepts the maximum app name and context sizes', () => {
|
|
87
|
+
const method = createMethod({ appName: 'a'.repeat(64), context: 'ab'.repeat(1024) });
|
|
88
|
+
expect(() => method.init()).not.toThrow();
|
|
89
|
+
expect(method.params.context).toHaveLength(2048);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test.each([
|
|
93
|
+
{ appName: '' },
|
|
94
|
+
{ appName: 'a'.repeat(65) },
|
|
95
|
+
{ appName: 'Test-App' },
|
|
96
|
+
{ appName: 'test_app' },
|
|
97
|
+
{ appName: '你好' },
|
|
98
|
+
{ appName: null },
|
|
99
|
+
{ context: '' },
|
|
100
|
+
{ context: 'abc' },
|
|
101
|
+
{ context: 'DEADBEEF' },
|
|
102
|
+
{ context: '0xdeadbeef' },
|
|
103
|
+
{ context: 'gg' },
|
|
104
|
+
{ context: 'ab'.repeat(1025) },
|
|
105
|
+
{ context: null },
|
|
106
|
+
{ network: '' },
|
|
107
|
+
{ network: 'litecoin' },
|
|
108
|
+
{ network: 'toString' },
|
|
109
|
+
{ network: 1 },
|
|
110
|
+
{ path: 'm' },
|
|
111
|
+
{ path: "m/84x'/0'/0'/0/0" },
|
|
112
|
+
{ path: "m/2147483648'/0'/0'/0/0" },
|
|
113
|
+
{ path: [2147483732, 2147483648, 2147483648, 0, 0.5] },
|
|
114
|
+
{ path: [2147483732, 2147483648, 2147483648, 0, 4294967296] },
|
|
115
|
+
])('rejects invalid inputs before a device call: %j', overrides => {
|
|
116
|
+
expect(() => createMethod(overrides).init()).toThrow(
|
|
117
|
+
expect.objectContaining({ errorCode: HardwareErrorCode.CallMethodInvalidParameter })
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('rejects sparse numeric paths before a device call', () => {
|
|
122
|
+
const sparsePath = [2147483732, 2147483648, 2147483648, 0, 5];
|
|
123
|
+
delete sparsePath[3];
|
|
124
|
+
|
|
125
|
+
[Array<number>(3), sparsePath].forEach(path => {
|
|
126
|
+
expect(() => createMethod({ path }).init()).toThrow(
|
|
127
|
+
expect.objectContaining({ errorCode: HardwareErrorCode.CallMethodInvalidParameter })
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('registers the public method and restricts it to supported firmware and protocol', () => {
|
|
133
|
+
const method = findMethod({ id: 1, payload: { ...createMethod().payload } });
|
|
134
|
+
method.init();
|
|
135
|
+
expect(method).toBeInstanceOf(BTCDeriveContextHash);
|
|
136
|
+
expect(method.strictCheckDeviceSupport).toBe(true);
|
|
137
|
+
expect(() => method.assertProtocolSupported('V2', EFirmwareType.Universal)).toThrow(
|
|
138
|
+
expect.objectContaining({ errorCode: HardwareErrorCode.DeviceNotSupportMethod })
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test.each([EDeviceType.Classic1s, EDeviceType.ClassicPure])(
|
|
143
|
+
'resolves the firmware requirement for %s through the device support check',
|
|
144
|
+
deviceType => {
|
|
145
|
+
const method = createMethod();
|
|
146
|
+
const device = Object.create(Device.prototype) as Device;
|
|
147
|
+
device.getCurrentDeviceType = jest.fn(() => deviceType);
|
|
148
|
+
const range: DeviceFirmwareRange = method.getVersionRange();
|
|
149
|
+
|
|
150
|
+
expect(device.getCurrentMethodVersionRange(type => range[type])).toEqual({ min: '3.21.0' });
|
|
151
|
+
expect(() => method.assertProtocolSupported('V1', EFirmwareType.Universal)).not.toThrow();
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
test.each([EDeviceType.Classic, EDeviceType.Mini, EDeviceType.Touch, EDeviceType.Pro])(
|
|
156
|
+
'keeps %s outside the supported firmware range',
|
|
157
|
+
deviceType => {
|
|
158
|
+
const method = createMethod();
|
|
159
|
+
const device = Object.create(Device.prototype) as Device;
|
|
160
|
+
device.getCurrentDeviceType = jest.fn(() => deviceType);
|
|
161
|
+
const range: DeviceFirmwareRange = method.getVersionRange();
|
|
162
|
+
|
|
163
|
+
expect(device.getCurrentMethodVersionRange(type => range[type])).toBeUndefined();
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
test('propagates device rejection without returning a secret', async () => {
|
|
168
|
+
const method = createMethod();
|
|
169
|
+
method.init();
|
|
170
|
+
const error = new Error('User rejected');
|
|
171
|
+
method.device = {
|
|
172
|
+
commands: { typedCall: jest.fn().mockRejectedValue(error) },
|
|
173
|
+
} as unknown as Device;
|
|
174
|
+
await expect(method.run()).rejects.toBe(error);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test.each([undefined, '00', '00'.repeat(33)])(
|
|
178
|
+
'rejects malformed device responses',
|
|
179
|
+
async secret => {
|
|
180
|
+
const method = createMethod();
|
|
181
|
+
method.init();
|
|
182
|
+
method.device = {
|
|
183
|
+
commands: { typedCall: jest.fn().mockResolvedValue({ message: { secret } }) },
|
|
184
|
+
} as unknown as Device;
|
|
185
|
+
await expect(method.run()).rejects.toMatchObject({
|
|
186
|
+
errorCode: HardwareErrorCode.CallMethodError,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
test('blocks API request and response payloads in logs', () => {
|
|
192
|
+
const request = { payload: { method: 'btcDeriveContextHash' } };
|
|
193
|
+
const label = getLogBlockLabel(request);
|
|
194
|
+
expect(label).toBe('btcDeriveContextHash');
|
|
195
|
+
expect(getSafeLogPayload({ payload: { secret: '00'.repeat(32) } }, label)).toEqual({
|
|
196
|
+
method: 'btcDeriveContextHash',
|
|
197
|
+
payload: '[REDACTED]',
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -3,7 +3,7 @@ import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
|
3
3
|
import { initConnector, initCore } from '../src/core';
|
|
4
4
|
import { DataManager } from '../src/data-manager';
|
|
5
5
|
import TransportManager from '../src/data-manager/TransportManager';
|
|
6
|
-
import { IFRAME
|
|
6
|
+
import { IFRAME } from '../src/events';
|
|
7
7
|
import SearchDevices from '../src/api/SearchDevices';
|
|
8
8
|
import { DeviceList } from '../src/device/DeviceList';
|
|
9
9
|
import { Device } from '../src/device/Device';
|
|
@@ -28,23 +28,6 @@ describe('Core 错误输出边界', () => {
|
|
|
28
28
|
jest.restoreAllMocks();
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
test('entry-point errors preserve USB recovery context across JSON serialization', () => {
|
|
32
|
-
const params = {
|
|
33
|
-
operation: 'open',
|
|
34
|
-
nativeErrorName: 'SecurityError',
|
|
35
|
-
nativeErrorMessage: 'Access denied',
|
|
36
|
-
};
|
|
37
|
-
const error = ERRORS.TypedError(
|
|
38
|
-
HardwareErrorCode.BridgeNeedsPermission,
|
|
39
|
-
'Access denied',
|
|
40
|
-
params
|
|
41
|
-
);
|
|
42
|
-
expect(JSON.parse(JSON.stringify(createErrorMessage(error)))).toEqual({
|
|
43
|
-
success: false,
|
|
44
|
-
payload: { code: HardwareErrorCode.BridgeNeedsPermission, error: 'Access denied', params },
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
31
|
test.each([
|
|
49
32
|
['webusb', false],
|
|
50
33
|
['desktop-webusb', false],
|
|
@@ -125,28 +108,14 @@ describe('Core 错误输出边界', () => {
|
|
|
125
108
|
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
126
109
|
HardwareErrorCode.BleDeviceDisconnected,
|
|
127
110
|
HardwareErrorCode.PollingTimeout,
|
|
128
|
-
HardwareErrorCode.BlePoweredOff,
|
|
129
|
-
HardwareErrorCode.BleUnsupported,
|
|
130
|
-
HardwareErrorCode.BridgeNeedsPermission,
|
|
131
|
-
HardwareErrorCode.WebUsbDeviceAccessError,
|
|
132
111
|
])(
|
|
133
|
-
'preserves terminal
|
|
112
|
+
'preserves terminal BLE error %s in the public response without polling again',
|
|
134
113
|
async errorCode => {
|
|
135
|
-
jest
|
|
136
|
-
.spyOn(DataManager, 'getSettings')
|
|
137
|
-
.mockReturnValue(
|
|
138
|
-
[
|
|
139
|
-
HardwareErrorCode.BridgeNeedsPermission,
|
|
140
|
-
HardwareErrorCode.WebUsbDeviceAccessError,
|
|
141
|
-
].includes(errorCode)
|
|
142
|
-
? ('desktop-webusb' as never)
|
|
143
|
-
: ('react-native' as never)
|
|
144
|
-
);
|
|
114
|
+
jest.spyOn(DataManager, 'getSettings').mockReturnValue('react-native' as never);
|
|
145
115
|
const error = ERRORS.TypedError(errorCode);
|
|
146
116
|
const acquire = jest.fn().mockRejectedValue(error);
|
|
147
117
|
jest.spyOn(TransportManager, 'getTransport').mockReturnValue({
|
|
148
118
|
acquire,
|
|
149
|
-
enumerate: jest.fn().mockResolvedValue([{ path: 'ble-pairing-test' }]),
|
|
150
119
|
release: jest.fn().mockResolvedValue(true),
|
|
151
120
|
stop: jest.fn().mockResolvedValue(undefined),
|
|
152
121
|
} as never);
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
isMissingDetectedProtocolV2Error,
|
|
11
11
|
isRetryableBleConnectionError,
|
|
12
12
|
isRetryableBleProtocolV2ProbeError,
|
|
13
|
+
isTerminalBleStaleBondError,
|
|
13
14
|
resolveBleConnectProtocol,
|
|
14
15
|
} from '../src/core';
|
|
15
16
|
import { DataManager } from '../src/data-manager';
|
|
@@ -610,6 +611,7 @@ describe('public device lifecycle events', () => {
|
|
|
610
611
|
test.each([
|
|
611
612
|
[HardwareErrorCode.RuntimeError, true],
|
|
612
613
|
[HardwareErrorCode.BleDeviceBondError, false],
|
|
614
|
+
[HardwareErrorCode.BlePeerRemovedPairingInformation, false],
|
|
613
615
|
] as const)(
|
|
614
616
|
'retries a Protocol V2 probe mismatch with error code %s: %s',
|
|
615
617
|
(errorCode, expected) => {
|
|
@@ -629,6 +631,8 @@ describe('public device lifecycle events', () => {
|
|
|
629
631
|
[HardwareErrorCode.BleTimeoutError, true],
|
|
630
632
|
[HardwareErrorCode.PollingTimeout, false],
|
|
631
633
|
[HardwareErrorCode.BleDeviceBondError, false],
|
|
634
|
+
[HardwareErrorCode.BlePeerRemovedPairingInformation, false],
|
|
635
|
+
[HardwareErrorCode.BleBondInvalid, false],
|
|
632
636
|
] as const)('retries a BLE connection error with error code %s: %s', (errorCode, expected) => {
|
|
633
637
|
const method = { payload: { connectProtocol: 'V2' } } as never;
|
|
634
638
|
const error = {
|
|
@@ -681,6 +685,19 @@ describe('public device lifecycle events', () => {
|
|
|
681
685
|
}
|
|
682
686
|
);
|
|
683
687
|
|
|
688
|
+
test.each([
|
|
689
|
+
[HardwareErrorCode.BleDeviceBondError, true],
|
|
690
|
+
[HardwareErrorCode.BlePeerRemovedPairingInformation, true],
|
|
691
|
+
[HardwareErrorCode.BleBondInvalid, true],
|
|
692
|
+
[HardwareErrorCode.DeviceNotFound, false],
|
|
693
|
+
] as const)('treats BLE stale-bond error code %s as terminal: %s', (errorCode, expected) => {
|
|
694
|
+
const error = {
|
|
695
|
+
errorCode,
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
expect(isTerminalBleStaleBondError(error)).toBe(expected);
|
|
699
|
+
});
|
|
700
|
+
|
|
684
701
|
test.each([
|
|
685
702
|
[HardwareErrorCode.DeviceCheckDeviceIdError, true],
|
|
686
703
|
[HardwareErrorCode.DeviceNotFound, false],
|
|
@@ -712,48 +729,6 @@ describe('public device lifecycle events', () => {
|
|
|
712
729
|
expect(() => JSON.stringify(disconnectMessage?.payload.device)).not.toThrow();
|
|
713
730
|
});
|
|
714
731
|
|
|
715
|
-
test.each(['V1', 'V2'] as const)(
|
|
716
|
-
'preserves the %s connection identity for a transport disconnect after wipe',
|
|
717
|
-
protocol => {
|
|
718
|
-
jest.spyOn(DataManager, 'getSettings').mockReturnValue('webusb' as never);
|
|
719
|
-
core = initCore();
|
|
720
|
-
initConnector();
|
|
721
|
-
const messages: CoreMessage[] = [];
|
|
722
|
-
core.on(CORE_EVENT, message => messages.push(message));
|
|
723
|
-
const device = createInitializedDevice(protocol);
|
|
724
|
-
const connectId = device.getConnectId();
|
|
725
|
-
DevicePool.devicesCache[connectId] = device;
|
|
726
|
-
|
|
727
|
-
device.invalidateAfterWipe();
|
|
728
|
-
expect(device.toMessageObject()).toBeNull();
|
|
729
|
-
DevicePool.emitter.emit(DEVICE.CONNECT, device);
|
|
730
|
-
expect(messages.some(message => message.type === DEVICE.CONNECT)).toBe(false);
|
|
731
|
-
DevicePool.emitter.emit(TRANSPORT_EVENT.DEVICE_DISCONNECT, { connectId });
|
|
732
|
-
|
|
733
|
-
const disconnects = messages.filter(message => message.type === DEVICE.DISCONNECT);
|
|
734
|
-
expect(disconnects).toHaveLength(1);
|
|
735
|
-
expect(disconnects[0].payload.device).toMatchObject({
|
|
736
|
-
connectId,
|
|
737
|
-
serialNo: 'SERIAL-001',
|
|
738
|
-
deviceId: 'wallet-device-id',
|
|
739
|
-
});
|
|
740
|
-
expect(() => JSON.stringify(disconnects[0].payload.device)).not.toThrow();
|
|
741
|
-
|
|
742
|
-
device.features = {
|
|
743
|
-
...createInitializedDevice(protocol).features,
|
|
744
|
-
deviceId: 'new-device-id',
|
|
745
|
-
initialized: false,
|
|
746
|
-
mode: 'notInitialized',
|
|
747
|
-
} as never;
|
|
748
|
-
DevicePool.emitter.emit(TRANSPORT_EVENT.DEVICE_DISCONNECT, { connectId });
|
|
749
|
-
const nextDisconnects = messages.filter(message => message.type === DEVICE.DISCONNECT);
|
|
750
|
-
expect(nextDisconnects[1].payload.device).toMatchObject({
|
|
751
|
-
deviceId: 'new-device-id',
|
|
752
|
-
mode: 'notInitialized',
|
|
753
|
-
});
|
|
754
|
-
}
|
|
755
|
-
);
|
|
756
|
-
|
|
757
732
|
test('treats an acquired BLE device as reusable until the transport disconnects', async () => {
|
|
758
733
|
jest.spyOn(DataManager, 'getSettings').mockReturnValue('desktop-web-ble' as never);
|
|
759
734
|
const device = createInitializedDevice('V2');
|
|
@@ -1039,28 +1014,6 @@ describe('public device lifecycle events', () => {
|
|
|
1039
1014
|
expect(device.wasInterruptedByUser()).toBe(false);
|
|
1040
1015
|
});
|
|
1041
1016
|
|
|
1042
|
-
test.each(['V1', 'V2'] as const)(
|
|
1043
|
-
'retires a failed USB %s session before the next user attempt',
|
|
1044
|
-
async protocol => {
|
|
1045
|
-
jest.spyOn(DataManager, 'getSettings').mockReturnValue('desktop-webusb' as never);
|
|
1046
|
-
const device = createInitializedDevice(protocol);
|
|
1047
|
-
device.originalDescriptor = { ...device.originalDescriptor, session: device.mainId };
|
|
1048
|
-
const disconnect = jest.fn().mockResolvedValue(undefined);
|
|
1049
|
-
const release = jest.fn().mockResolvedValue(undefined);
|
|
1050
|
-
device.deviceConnector = { disconnect, release } as never;
|
|
1051
|
-
device.commands = { disposed: false, dispose: jest.fn() } as never;
|
|
1052
|
-
(device as unknown as { deviceAcquired: boolean }).deviceAcquired = true;
|
|
1053
|
-
const error = ERRORS.TypedError(HardwareErrorCode.WebUsbDeviceAccessError);
|
|
1054
|
-
await expect(device.run(() => Promise.reject(error))).rejects.toBe(error);
|
|
1055
|
-
await new Promise(resolve => {
|
|
1056
|
-
setImmediate(resolve);
|
|
1057
|
-
});
|
|
1058
|
-
expect(disconnect).toHaveBeenCalledWith(device.mainId);
|
|
1059
|
-
expect(release).toHaveBeenCalledTimes(1);
|
|
1060
|
-
expect(device.hasDeviceAcquire()).toBe(false);
|
|
1061
|
-
}
|
|
1062
|
-
);
|
|
1063
|
-
|
|
1064
1017
|
test('waits for the canceled run to finish releasing before cancellation completes', async () => {
|
|
1065
1018
|
jest.spyOn(DataManager, 'getSettings').mockReturnValue('react-native' as never);
|
|
1066
1019
|
const device = createInitializedDevice('V2');
|
|
@@ -85,56 +85,6 @@ describe('DevicePool state lifecycle', () => {
|
|
|
85
85
|
expect(result.deviceList).toEqual([device]);
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
-
test.each([
|
|
89
|
-
['V1', undefined],
|
|
90
|
-
['V1', 'wipe-path'],
|
|
91
|
-
['V2', undefined],
|
|
92
|
-
['V2', 'wipe-path'],
|
|
93
|
-
] as const)('rediscovers a wiped %s device with connect id %s', async (protocol, connectId) => {
|
|
94
|
-
const descriptor = { path: 'wipe-path', protocolType: protocol } as never;
|
|
95
|
-
const device = Device.fromDescriptor(descriptor);
|
|
96
|
-
device.features = {
|
|
97
|
-
protocol,
|
|
98
|
-
deviceType: protocol === 'V2' ? 'pro2' : 'classic',
|
|
99
|
-
deviceId: 'old-device-id',
|
|
100
|
-
serialNo: 'wipe-path',
|
|
101
|
-
initialized: true,
|
|
102
|
-
mode: 'normal',
|
|
103
|
-
} as never;
|
|
104
|
-
DevicePool.devicesCache = { 'wipe-path': device };
|
|
105
|
-
device.invalidateAfterWipe();
|
|
106
|
-
expect(device.toMessageObject()).toBeNull();
|
|
107
|
-
const connect = jest.spyOn(device, 'connect').mockResolvedValue(true);
|
|
108
|
-
const initialize = jest.spyOn(device, 'initialize').mockImplementation(() => {
|
|
109
|
-
device.features = {
|
|
110
|
-
protocol,
|
|
111
|
-
deviceType: protocol === 'V2' ? 'pro2' : 'classic',
|
|
112
|
-
deviceId: 'new-device-id',
|
|
113
|
-
serialNo: 'wipe-path',
|
|
114
|
-
initialized: false,
|
|
115
|
-
mode: 'notInitialized',
|
|
116
|
-
} as never;
|
|
117
|
-
return Promise.resolve();
|
|
118
|
-
});
|
|
119
|
-
const release = jest.spyOn(device, 'release').mockResolvedValue(undefined);
|
|
120
|
-
|
|
121
|
-
try {
|
|
122
|
-
const result = await DevicePool.getDevices([descriptor], connectId);
|
|
123
|
-
expect(initialize).toHaveBeenCalledTimes(1);
|
|
124
|
-
expect(connect).toHaveBeenCalledTimes(1);
|
|
125
|
-
expect(release).toHaveBeenCalledTimes(1);
|
|
126
|
-
expect(result.deviceList[0].toMessageObject()).toMatchObject({
|
|
127
|
-
deviceId: 'new-device-id',
|
|
128
|
-
mode: 'notInitialized',
|
|
129
|
-
features: { initialized: false },
|
|
130
|
-
});
|
|
131
|
-
await DevicePool.getDevices([descriptor], connectId);
|
|
132
|
-
expect(initialize).toHaveBeenCalledTimes(1);
|
|
133
|
-
} finally {
|
|
134
|
-
jest.restoreAllMocks();
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
|
|
138
88
|
test('keeps a discovered device when its Protocol V2 label cannot be read', async () => {
|
|
139
89
|
const labelError = new Error('settings unavailable');
|
|
140
90
|
const getDeviceState = jest
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { EDeviceType } from '@onekeyfe/hd-shared';
|
|
2
|
-
|
|
3
1
|
import { Device } from '../src/device/Device';
|
|
4
2
|
import { UI_REQUEST } from '../src/events/ui-request';
|
|
5
3
|
|
|
@@ -25,39 +23,12 @@ const createBootloaderDevice = (protocolType: 'V1' | 'V2') => {
|
|
|
25
23
|
};
|
|
26
24
|
|
|
27
25
|
describe('Pro2 bootloader mode', () => {
|
|
28
|
-
test('
|
|
29
|
-
const device = createBootloaderDevice('V2');
|
|
30
|
-
|
|
31
|
-
expect(device.hasUnexpectedMode([], [])).toBe(UI_REQUEST.BOOTLOADER);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('allows Protocol V2 methods that explicitly support bootloader mode', () => {
|
|
26
|
+
test('does not block Protocol V2 methods in bootloader mode', () => {
|
|
35
27
|
const device = createBootloaderDevice('V2');
|
|
36
28
|
|
|
37
|
-
expect(device.hasUnexpectedMode([
|
|
29
|
+
expect(device.hasUnexpectedMode([], [])).toBeNull();
|
|
38
30
|
});
|
|
39
31
|
|
|
40
|
-
test.each([EDeviceType.Pro2, EDeviceType.Neo])(
|
|
41
|
-
'blocks unsupported Protocol V2 methods in %s romloader mode',
|
|
42
|
-
deviceType => {
|
|
43
|
-
const device = createBootloaderDevice('V2');
|
|
44
|
-
device.updateState(
|
|
45
|
-
{
|
|
46
|
-
protocol: 'V2',
|
|
47
|
-
identity: { deviceType },
|
|
48
|
-
status: { mode: 'romloader', initialized: null },
|
|
49
|
-
},
|
|
50
|
-
'initialize'
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
expect(device.hasUnexpectedMode([], [])).toBe(UI_REQUEST.BOOTLOADER);
|
|
54
|
-
expect(device.hasUnexpectedMode([UI_REQUEST.NOT_INITIALIZE], [])).toBe(UI_REQUEST.BOOTLOADER);
|
|
55
|
-
expect(
|
|
56
|
-
device.hasUnexpectedMode([UI_REQUEST.BOOTLOADER, UI_REQUEST.NOT_INITIALIZE], [])
|
|
57
|
-
).toBeNull();
|
|
58
|
-
}
|
|
59
|
-
);
|
|
60
|
-
|
|
61
32
|
test('keeps the Protocol V1 bootloader restriction', () => {
|
|
62
33
|
const device = createBootloaderDevice('V1');
|
|
63
34
|
|
|
@@ -4991,6 +4991,23 @@ describe('Protocol V2 firmware update targets', () => {
|
|
|
4991
4991
|
expect(typedCall.mock.calls.map(call => call[0])).toEqual(['DeviceInfoGet']);
|
|
4992
4992
|
});
|
|
4993
4993
|
|
|
4994
|
+
test('stops V4 bootloader reconnect polling on a stale BLE bond', async () => {
|
|
4995
|
+
const method = new FirmwareUpdateV4({
|
|
4996
|
+
id: 1,
|
|
4997
|
+
payload: {
|
|
4998
|
+
method: 'firmwareUpdateV4',
|
|
4999
|
+
},
|
|
5000
|
+
});
|
|
5001
|
+
const staleBondError = ERRORS.TypedError(HardwareErrorCode.BlePeerRemovedPairingInformation);
|
|
5002
|
+
const reconnectProtocolV2Device = jest.fn().mockRejectedValue(staleBondError);
|
|
5003
|
+
(method as any).reconnectProtocolV2Device = reconnectProtocolV2Device;
|
|
5004
|
+
|
|
5005
|
+
await expect((method as any).waitForProtocolV2BootloaderMode(60_000, 0)).rejects.toBe(
|
|
5006
|
+
staleBondError
|
|
5007
|
+
);
|
|
5008
|
+
expect(reconnectProtocolV2Device).toHaveBeenCalledTimes(1);
|
|
5009
|
+
});
|
|
5010
|
+
|
|
4994
5011
|
test('polls again when Protocol V2 bootloader serial is temporarily unavailable', async () => {
|
|
4995
5012
|
const method = new FirmwareUpdateV4({
|
|
4996
5013
|
id: 1,
|
|
@@ -8992,6 +9009,36 @@ describe('Protocol V2 firmware update targets', () => {
|
|
|
8992
9009
|
);
|
|
8993
9010
|
});
|
|
8994
9011
|
|
|
9012
|
+
test('does not retry or wrap a stale BLE bond during a V4 file transfer', async () => {
|
|
9013
|
+
const method = new FirmwareUpdateV4({
|
|
9014
|
+
id: 1,
|
|
9015
|
+
payload: {
|
|
9016
|
+
method: 'firmwareUpdateV4',
|
|
9017
|
+
},
|
|
9018
|
+
});
|
|
9019
|
+
const staleBondError = ERRORS.TypedError(HardwareErrorCode.BlePeerRemovedPairingInformation);
|
|
9020
|
+
(method as any).fileWriteChunk = jest.fn().mockRejectedValue(staleBondError);
|
|
9021
|
+
const recoverProtocolV2FileTransfer = jest.fn();
|
|
9022
|
+
(method as any).recoverProtocolV2FileTransfer = recoverProtocolV2FileTransfer;
|
|
9023
|
+
const source = await openFirmwareByteSource({
|
|
9024
|
+
binary: new Uint8Array([1]).buffer,
|
|
9025
|
+
});
|
|
9026
|
+
|
|
9027
|
+
try {
|
|
9028
|
+
await expect(
|
|
9029
|
+
(method as any).protocolV2SourceUpdateProcess({
|
|
9030
|
+
source,
|
|
9031
|
+
filePath: 'vol1:firmware.bin',
|
|
9032
|
+
processedSize: 0,
|
|
9033
|
+
totalSize: 1,
|
|
9034
|
+
})
|
|
9035
|
+
).rejects.toBe(staleBondError);
|
|
9036
|
+
} finally {
|
|
9037
|
+
await source?.close();
|
|
9038
|
+
}
|
|
9039
|
+
expect(recoverProtocolV2FileTransfer).not.toHaveBeenCalled();
|
|
9040
|
+
});
|
|
9041
|
+
|
|
8995
9042
|
test('does not recover or wrap a cancelled V4 file transfer', async () => {
|
|
8996
9043
|
const method = new FirmwareUpdateV4({
|
|
8997
9044
|
id: 1,
|