@onekeyfe/hd-core 1.2.0-alpha.7 → 1.2.0-alpha.9

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.
Files changed (63) hide show
  1. package/__tests__/device-wallet-session-store.test.ts +131 -0
  2. package/__tests__/protocol-v2-firmware-targets.test.ts +19 -0
  3. package/__tests__/protocol-v2.test.ts +742 -536
  4. package/dist/api/ClearSessionCache.d.ts +9 -0
  5. package/dist/api/ClearSessionCache.d.ts.map +1 -0
  6. package/dist/api/FirmwareUpdateV4.d.ts +7 -6
  7. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  8. package/dist/api/firmware/getBinary.d.ts +1 -1
  9. package/dist/api/index.d.ts +3 -0
  10. package/dist/api/index.d.ts.map +1 -1
  11. package/dist/api/protocol-v2/DeviceSessionGet.d.ts +9 -0
  12. package/dist/api/protocol-v2/DeviceSessionGet.d.ts.map +1 -0
  13. package/dist/api/protocol-v2/DeviceStatusGet.d.ts +6 -0
  14. package/dist/api/protocol-v2/DeviceStatusGet.d.ts.map +1 -0
  15. package/dist/api/protocol-v2/FilesystemFormat.d.ts.map +1 -1
  16. package/dist/api/protocol-v2/helpers.d.ts +1 -1
  17. package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
  18. package/dist/device/Device.d.ts +2 -2
  19. package/dist/device/Device.d.ts.map +1 -1
  20. package/dist/device/DeviceWalletSessionStore.d.ts +15 -0
  21. package/dist/device/DeviceWalletSessionStore.d.ts.map +1 -0
  22. package/dist/index.d.ts +32 -19
  23. package/dist/index.js +651 -288
  24. package/dist/inject.d.ts.map +1 -1
  25. package/dist/protocols/protocol-v2/index.d.ts +1 -0
  26. package/dist/protocols/protocol-v2/index.d.ts.map +1 -1
  27. package/dist/protocols/protocol-v2/walletSession.d.ts +13 -0
  28. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -0
  29. package/dist/types/api/firmwareUpdate.d.ts +4 -1
  30. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  31. package/dist/types/api/index.d.ts +6 -1
  32. package/dist/types/api/index.d.ts.map +1 -1
  33. package/dist/types/api/protocolV2.d.ts +6 -2
  34. package/dist/types/api/protocolV2.d.ts.map +1 -1
  35. package/dist/types/api/sessionCache.d.ts +10 -0
  36. package/dist/types/api/sessionCache.d.ts.map +1 -0
  37. package/dist/types/settings.d.ts +6 -14
  38. package/dist/types/settings.d.ts.map +1 -1
  39. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  40. package/dist/utils/patch.d.ts +1 -1
  41. package/dist/utils/patch.d.ts.map +1 -1
  42. package/package.json +4 -4
  43. package/src/api/ClearSessionCache.ts +28 -0
  44. package/src/api/FileRead.ts +2 -2
  45. package/src/api/FirmwareUpdateV4.ts +276 -239
  46. package/src/api/index.ts +3 -0
  47. package/src/api/protocol-v2/DeviceSessionGet.ts +26 -0
  48. package/src/api/protocol-v2/DeviceStatusGet.ts +14 -0
  49. package/src/api/protocol-v2/FilesystemFormat.ts +4 -1
  50. package/src/api/protocol-v2/helpers.ts +17 -14
  51. package/src/data/messages/messages-protocol-v2.json +134 -3
  52. package/src/data/messages/messages.json +8 -0
  53. package/src/device/Device.ts +64 -56
  54. package/src/device/DeviceWalletSessionStore.ts +78 -0
  55. package/src/inject.ts +5 -2
  56. package/src/protocols/protocol-v2/index.ts +1 -0
  57. package/src/protocols/protocol-v2/walletSession.ts +83 -0
  58. package/src/types/api/firmwareUpdate.ts +8 -2
  59. package/src/types/api/index.ts +10 -3
  60. package/src/types/api/protocolV2.ts +16 -2
  61. package/src/types/api/sessionCache.ts +14 -0
  62. package/src/types/settings.ts +14 -16
  63. package/src/utils/deviceFeaturesUtils.ts +22 -32
@@ -0,0 +1,131 @@
1
+ import ClearSessionCache from '../src/api/ClearSessionCache';
2
+ import {
3
+ DeviceWalletSessionStore,
4
+ deviceWalletSessionStore,
5
+ } from '../src/device/DeviceWalletSessionStore';
6
+ import { createCoreApi } from '../src/inject';
7
+
8
+ jest.mock('../src/data/config', () => ({
9
+ getSDKVersion: jest.fn(() => '1.0.0'),
10
+ DEFAULT_DOMAIN: 'https://jssdk.onekey.so/1.0.0/',
11
+ }));
12
+
13
+ describe('DeviceWalletSessionStore', () => {
14
+ test('requires passphraseState for wallet session lookup', () => {
15
+ const store = new DeviceWalletSessionStore();
16
+ store.set('device-1', 'hidden-a', 'session-a');
17
+
18
+ expect(store.get('device-1', undefined)).toBeUndefined();
19
+ expect(store.get('device-1', 'hidden-a')).toBe('session-a');
20
+ });
21
+
22
+ test('isolates wallets and devices', () => {
23
+ const store = new DeviceWalletSessionStore();
24
+ store.set('device-1', 'hidden-a', 'session-a');
25
+ store.set('device-1', 'hidden-b', 'session-b');
26
+ store.set('device-2', 'hidden-a', 'session-c');
27
+
28
+ expect(store.get('device-1', 'hidden-a')).toBe('session-a');
29
+ expect(store.get('device-1', 'hidden-b')).toBe('session-b');
30
+ expect(store.get('device-2', 'hidden-a')).toBe('session-c');
31
+ });
32
+
33
+ test('keeps pending sessions unreadable until wallet binding', () => {
34
+ const store = new DeviceWalletSessionStore();
35
+ store.setPending('device-1', 'pending-session');
36
+
37
+ expect(store.get('device-1', undefined)).toBeUndefined();
38
+ expect(store.getPending('device-1')).toBe('pending-session');
39
+ });
40
+
41
+ test('migrates descriptor keys to stable device ids', () => {
42
+ const store = new DeviceWalletSessionStore();
43
+ store.set('ble-path', 'hidden-a', 'session-a');
44
+ store.setPending('ble-path', 'pending-session');
45
+
46
+ store.migrateDeviceKey('ble-path', 'stable-device-id');
47
+
48
+ expect(store.get('ble-path', 'hidden-a')).toBeUndefined();
49
+ expect(store.get('stable-device-id', 'hidden-a')).toBe('session-a');
50
+ expect(store.getPending('ble-path')).toBeUndefined();
51
+ expect(store.getPending('stable-device-id')).toBe('pending-session');
52
+ });
53
+
54
+ test('clears one wallet, one device, or all sessions', () => {
55
+ const store = new DeviceWalletSessionStore();
56
+ store.set('device-1', 'hidden-a', 'session-a');
57
+ store.set('device-1', 'hidden-b', 'session-b');
58
+ store.set('device-2', 'hidden-a', 'session-c');
59
+
60
+ store.delete('device-1', 'hidden-a');
61
+ expect(store.get('device-1', 'hidden-a')).toBeUndefined();
62
+ expect(store.get('device-1', 'hidden-b')).toBe('session-b');
63
+
64
+ store.deleteDevice('device-1');
65
+ expect(store.get('device-1', 'hidden-b')).toBeUndefined();
66
+ expect(store.get('device-2', 'hidden-a')).toBe('session-c');
67
+
68
+ store.clear();
69
+ expect(store.get('device-2', 'hidden-a')).toBeUndefined();
70
+ });
71
+ });
72
+
73
+ describe('ClearSessionCache', () => {
74
+ beforeEach(() => {
75
+ deviceWalletSessionStore.clear();
76
+ });
77
+
78
+ test('clears one wallet without using a device', async () => {
79
+ deviceWalletSessionStore.set('device-1', 'hidden-a', 'session-a');
80
+ deviceWalletSessionStore.set('device-1', 'hidden-b', 'session-b');
81
+ const method = new ClearSessionCache({
82
+ payload: {
83
+ method: 'clearSessionCache',
84
+ deviceId: 'device-1',
85
+ passphraseState: 'hidden-a',
86
+ },
87
+ });
88
+
89
+ method.init();
90
+
91
+ expect(method.useDevice).toBe(false);
92
+ await expect(method.run()).resolves.toEqual({ cleared: true });
93
+ expect(deviceWalletSessionStore.get('device-1', 'hidden-a')).toBeUndefined();
94
+ expect(deviceWalletSessionStore.get('device-1', 'hidden-b')).toBe('session-b');
95
+ });
96
+
97
+ test('clears one device or the full runtime store', async () => {
98
+ deviceWalletSessionStore.set('device-1', 'hidden-a', 'session-a');
99
+ deviceWalletSessionStore.set('device-2', 'hidden-a', 'session-b');
100
+
101
+ const clearDevice = new ClearSessionCache({
102
+ payload: { method: 'clearSessionCache', deviceId: 'device-1' },
103
+ });
104
+ clearDevice.init();
105
+ await clearDevice.run();
106
+
107
+ expect(deviceWalletSessionStore.get('device-1', 'hidden-a')).toBeUndefined();
108
+ expect(deviceWalletSessionStore.get('device-2', 'hidden-a')).toBe('session-b');
109
+
110
+ const clearAll = new ClearSessionCache({
111
+ payload: { method: 'clearSessionCache' },
112
+ });
113
+ clearAll.init();
114
+ await clearAll.run();
115
+
116
+ expect(deviceWalletSessionStore.get('device-2', 'hidden-a')).toBeUndefined();
117
+ });
118
+
119
+ test('routes cache clearing through the active CoreApi call channel', async () => {
120
+ const call = jest.fn().mockResolvedValue({ success: true, payload: { cleared: true } });
121
+ const api = createCoreApi(call as any);
122
+
123
+ await api.clearSessionCache({ deviceId: 'device-1', passphraseState: 'hidden-a' });
124
+
125
+ expect(call).toHaveBeenCalledWith({
126
+ method: 'clearSessionCache',
127
+ deviceId: 'device-1',
128
+ passphraseState: 'hidden-a',
129
+ });
130
+ });
131
+ });
@@ -0,0 +1,19 @@
1
+ import { ProtocolV2FirmwareTargetType } from '../src/protocols/protocol-v2/firmware';
2
+
3
+ describe('Protocol V2 firmware target contract', () => {
4
+ test('matches the current firmware-pro2 target ids', () => {
5
+ expect(ProtocolV2FirmwareTargetType).toEqual({
6
+ FW_MGMT_TARGET_INVALID: 0,
7
+ FW_MGMT_TARGET_CRATE: 1,
8
+ FW_MGMT_TARGET_ROMLOADER: 2,
9
+ FW_MGMT_TARGET_BOOTLOADER: 3,
10
+ FW_MGMT_TARGET_APPLICATION_P1: 4,
11
+ FW_MGMT_TARGET_APPLICATION_P2: 5,
12
+ FW_MGMT_TARGET_COPROCESSOR: 6,
13
+ FW_MGMT_TARGET_SE01: 7,
14
+ FW_MGMT_TARGET_SE02: 8,
15
+ FW_MGMT_TARGET_SE03: 9,
16
+ FW_MGMT_TARGET_SE04: 10,
17
+ });
18
+ });
19
+ });