@onekeyfe/hd-core 1.2.0-alpha.35 → 1.2.0-alpha.37

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 (100) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +78 -0
  2. package/__tests__/DeviceEventRegistration.test.ts +6 -0
  3. package/__tests__/deviceUploadNft.test.ts +187 -0
  4. package/__tests__/get-device-state.test.ts +104 -12
  5. package/__tests__/homescreen.test.ts +41 -1
  6. package/__tests__/logBlockEvent.test.ts +1 -1
  7. package/__tests__/open-wallet-session.test.ts +259 -18
  8. package/__tests__/pro2Nft.test.ts +108 -0
  9. package/__tests__/protocol-v2-ui-lifecycle.test.ts +56 -0
  10. package/__tests__/protocol-v2-unlock-policy.test.ts +86 -35
  11. package/__tests__/protocol-v2.test.ts +377 -402
  12. package/__tests__/public-pro2-api-boundary.test.ts +1 -0
  13. package/dist/api/BaseMethod.d.ts +3 -1
  14. package/dist/api/BaseMethod.d.ts.map +1 -1
  15. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  16. package/dist/api/GetPassphraseState.d.ts.map +1 -1
  17. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  18. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  19. package/dist/api/helpers/protocolV2FileWrite.d.ts +2 -0
  20. package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
  21. package/dist/api/index.d.ts +1 -0
  22. package/dist/api/index.d.ts.map +1 -1
  23. package/dist/api/protocol-v2/DeviceUploadNft.d.ts +30 -0
  24. package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +1 -0
  25. package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
  26. package/dist/core/deviceEventRegistration.d.ts +2 -0
  27. package/dist/core/deviceEventRegistration.d.ts.map +1 -1
  28. package/dist/core/index.d.ts.map +1 -1
  29. package/dist/device/Device.d.ts +22 -4
  30. package/dist/device/Device.d.ts.map +1 -1
  31. package/dist/events/device.d.ts +4 -0
  32. package/dist/events/device.d.ts.map +1 -1
  33. package/dist/events/logBlockEvent.d.ts.map +1 -1
  34. package/dist/events/ui-request.d.ts +27 -2
  35. package/dist/events/ui-request.d.ts.map +1 -1
  36. package/dist/index.d.ts +88 -7
  37. package/dist/index.js +937 -604
  38. package/dist/inject.d.ts.map +1 -1
  39. package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
  40. package/dist/protocols/protocol-v2/uiInteraction.d.ts +3 -3
  41. package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
  42. package/dist/protocols/protocol-v2/unlockPolicy.d.ts +1 -3
  43. package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
  44. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +22 -0
  45. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +1 -0
  46. package/dist/protocols/protocol-v2/walletSession.d.ts +4 -1
  47. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  48. package/dist/types/api/index.d.ts +2 -1
  49. package/dist/types/api/index.d.ts.map +1 -1
  50. package/dist/types/api/protocolV2.d.ts +3 -0
  51. package/dist/types/api/protocolV2.d.ts.map +1 -1
  52. package/dist/utils/deviceFeaturesUtils.d.ts +2 -0
  53. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  54. package/dist/utils/homescreen.d.ts +4 -0
  55. package/dist/utils/homescreen.d.ts.map +1 -1
  56. package/dist/utils/index.d.ts +1 -1
  57. package/dist/utils/index.d.ts.map +1 -1
  58. package/dist/utils/patch.d.ts +1 -1
  59. package/dist/utils/patch.d.ts.map +1 -1
  60. package/dist/utils/pro2Nft.d.ts +29 -0
  61. package/dist/utils/pro2Nft.d.ts.map +1 -0
  62. package/dist/utils/pro2Wallpaper.d.ts +10 -0
  63. package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
  64. package/package.json +4 -4
  65. package/src/api/BaseMethod.ts +8 -10
  66. package/src/api/FirmwareUpdateV4.ts +13 -6
  67. package/src/api/GetPassphraseState.ts +7 -1
  68. package/src/api/OpenWalletSession.ts +54 -17
  69. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +32 -11
  70. package/src/api/device/DeviceUnlock.ts +1 -1
  71. package/src/api/helpers/protocolV2FileWrite.ts +11 -4
  72. package/src/api/index.ts +1 -0
  73. package/src/api/protocol-v2/DeviceUploadNft.ts +166 -0
  74. package/src/api/protocol-v2/ProtocolInfoRequest.ts +3 -1
  75. package/src/core/deviceEventRegistration.ts +4 -0
  76. package/src/core/index.ts +111 -74
  77. package/src/data/messages/messages-protocol-v2.json +10 -0
  78. package/src/device/Device.ts +221 -24
  79. package/src/events/device.ts +4 -0
  80. package/src/events/logBlockEvent.ts +1 -0
  81. package/src/events/ui-request.ts +32 -2
  82. package/src/inject.ts +1 -0
  83. package/src/protocols/protocol-v2/features.ts +9 -2
  84. package/src/protocols/protocol-v2/uiInteraction.ts +31 -5
  85. package/src/protocols/protocol-v2/unlockPolicy.ts +1 -105
  86. package/src/protocols/protocol-v2/unlockPolicyRunner.ts +117 -0
  87. package/src/protocols/protocol-v2/walletSession.ts +93 -18
  88. package/src/types/api/index.ts +2 -0
  89. package/src/types/api/protocolV2.ts +13 -0
  90. package/src/utils/deviceFeaturesUtils.ts +4 -0
  91. package/src/utils/homescreen.ts +32 -6
  92. package/src/utils/index.ts +6 -1
  93. package/src/utils/pro2Nft.ts +91 -0
  94. package/src/utils/pro2Wallpaper.ts +35 -8
  95. package/dist/protocols/protocol-v2/lockedError.d.ts +0 -2
  96. package/dist/protocols/protocol-v2/lockedError.d.ts.map +0 -1
  97. package/dist/protocols/protocol-v2/unlockRetry.d.ts +0 -10
  98. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +0 -1
  99. package/src/protocols/protocol-v2/lockedError.ts +0 -10
  100. package/src/protocols/protocol-v2/unlockRetry.ts +0 -109
@@ -5,6 +5,7 @@ import GetPassphraseState from '../src/api/GetPassphraseState';
5
5
  import OpenWalletSession from '../src/api/OpenWalletSession';
6
6
  import { Device } from '../src/device/Device';
7
7
  import { deviceWalletSessionStore } from '../src/device/DeviceWalletSessionStore';
8
+ import { ensureProtocolV2WalletSessionUnlocked } from '../src/protocols/protocol-v2/walletSession';
8
9
 
9
10
  jest.mock('../src/data/config', () => ({
10
11
  getSDKVersion: jest.fn(() => '1.0.0'),
@@ -26,6 +27,7 @@ const createDevice = ({
26
27
  unlocked: true,
27
28
  passphraseProtection,
28
29
  attachToPinEnabled: false,
30
+ unlockedAttachPin: false,
29
31
  },
30
32
  commands: {
31
33
  typedCall: jest.fn((request: string, ...args: unknown[]) => {
@@ -43,10 +45,21 @@ const createDevice = ({
43
45
  }),
44
46
  promptPassphrase,
45
47
  },
46
- getDeviceState: jest.fn().mockResolvedValue({
47
- identity: { deviceId: 'device-1' },
48
- status: { passphraseProtection },
49
- }),
48
+ ensureProtocolV2RuntimeContext: jest.fn(() =>
49
+ device.commands.typedCall('ProtocolInfoRequest', 'ProtocolInfo', {
50
+ eventless_wallet_session: true,
51
+ })
52
+ ),
53
+ getDeviceState: jest.fn(() =>
54
+ Promise.resolve({
55
+ identity: { deviceId: 'device-1' },
56
+ status: {
57
+ unlocked: device.features.unlocked,
58
+ passphraseProtection: device.features.passphraseProtection,
59
+ unlockedAttachPin: device.features.unlockedAttachPin,
60
+ },
61
+ })
62
+ ),
50
63
  getCurrentDeviceId: () => 'device-1',
51
64
  getCurrentPassphraseProtection: () => passphraseProtection,
52
65
  getInternalState: () =>
@@ -82,6 +95,50 @@ describe('openWalletSession', () => {
82
95
  deviceWalletSessionStore.clear();
83
96
  });
84
97
 
98
+ test('unlocks a locked Protocol V2 device before restoring a wallet session', async () => {
99
+ const device = {
100
+ commands: {
101
+ typedCall: jest.fn().mockResolvedValue({ message: { unlocked: false } }),
102
+ },
103
+ isBootloader: jest.fn().mockReturnValue(false),
104
+ isProtocolV2: jest.fn().mockReturnValue(true),
105
+ isRomloader: jest.fn().mockReturnValue(false),
106
+ state: { status: { unlocked: false } },
107
+ unlockDevice: jest.fn().mockResolvedValue(undefined),
108
+ updateProtocolV2Status: jest.fn(function updateProtocolV2Status(
109
+ this: { state: { status: { unlocked: boolean } } },
110
+ status: { unlocked?: boolean }
111
+ ) {
112
+ this.state.status.unlocked = status.unlocked ?? this.state.status.unlocked;
113
+ }),
114
+ };
115
+
116
+ await expect(ensureProtocolV2WalletSessionUnlocked(device as any)).resolves.toBe(true);
117
+ expect(device.commands.typedCall).toHaveBeenCalledWith('DeviceStatusGet', 'DeviceStatus', {});
118
+ expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.Main, {
119
+ source: 'unlock-coordinator',
120
+ reason: 'device-locked',
121
+ deviceOnly: true,
122
+ });
123
+ });
124
+
125
+ test('does not request PIN when wallet-session recovery finds the device unlocked', async () => {
126
+ const device = {
127
+ commands: {
128
+ typedCall: jest.fn().mockResolvedValue({ message: { unlocked: true } }),
129
+ },
130
+ isBootloader: jest.fn().mockReturnValue(false),
131
+ isProtocolV2: jest.fn().mockReturnValue(true),
132
+ isRomloader: jest.fn().mockReturnValue(false),
133
+ state: { status: { unlocked: true } },
134
+ unlockDevice: jest.fn(),
135
+ updateProtocolV2Status: jest.fn(),
136
+ };
137
+
138
+ await expect(ensureProtocolV2WalletSessionUnlocked(device as any)).resolves.toBe(false);
139
+ expect(device.unlockDevice).not.toHaveBeenCalled();
140
+ });
141
+
85
142
  test.each([{ useEmptyPassphrase: true }, { initSession: true }, {}])(
86
143
  'requires the explicit mode in the new public API: %p',
87
144
  legacyParams => {
@@ -273,10 +330,24 @@ describe('openWalletSession', () => {
273
330
  typedCall,
274
331
  promptPassphrase: jest.fn().mockResolvedValue({ passphraseOnDevice: true }),
275
332
  });
276
- device.getDeviceState = jest.fn().mockResolvedValue({
277
- identity: { deviceId: 'device-1' },
278
- status: { unlocked: false, passphraseProtection: true },
279
- });
333
+ device.getDeviceState = jest
334
+ .fn()
335
+ .mockResolvedValueOnce({
336
+ identity: { deviceId: 'device-1' },
337
+ status: {
338
+ unlocked: false,
339
+ passphraseProtection: null,
340
+ unlockedAttachPin: null,
341
+ },
342
+ })
343
+ .mockResolvedValue({
344
+ identity: { deviceId: 'device-1' },
345
+ status: {
346
+ unlocked: true,
347
+ passphraseProtection: true,
348
+ unlockedAttachPin: false,
349
+ },
350
+ });
280
351
  method.device = device as any;
281
352
 
282
353
  await expect(method.run()).resolves.toMatchObject({
@@ -330,6 +401,12 @@ describe('openWalletSession', () => {
330
401
  status.passphrase_enabled ?? device.features.passphraseProtection;
331
402
  return device.features;
332
403
  });
404
+ device.unlockDevice = jest.fn().mockImplementation(() => {
405
+ device.features.unlocked = true;
406
+ device.features.passphraseProtection = true;
407
+ device.features.unlockedAttachPin = false;
408
+ return Promise.resolve(device.features);
409
+ });
333
410
  method.device = device as any;
334
411
 
335
412
  await expect(method.run()).resolves.toEqual({
@@ -339,10 +416,98 @@ describe('openWalletSession', () => {
339
416
  passphraseState: 'hidden-state-after-unlock',
340
417
  resumed: false,
341
418
  });
342
- expect(device.unlockDevice).not.toHaveBeenCalled();
419
+ expect(device.unlockDevice).toHaveBeenCalledTimes(1);
343
420
  expect(promptPassphrase).toHaveBeenCalledTimes(1);
344
421
  });
345
422
 
423
+ test('unlocks before wallet selection when the Protocol V2 wallet status is unknown', async () => {
424
+ const typedCall = jest
425
+ .fn()
426
+ .mockResolvedValueOnce({ message: { version: 2 } })
427
+ .mockResolvedValueOnce({ message: {} })
428
+ .mockResolvedValueOnce({
429
+ message: {
430
+ btc_test_address: 'hidden-state-after-unknown-status',
431
+ session_id: 'hidden-session-after-unknown-status',
432
+ },
433
+ });
434
+ const promptPassphrase = jest.fn().mockResolvedValue({ passphrase: 'host hidden wallet' });
435
+ const method = new OpenWalletSession({
436
+ payload: { method: 'openWalletSession', connectId: 'connect-id', mode: 'select-hidden' },
437
+ });
438
+ method.init();
439
+ const device = createDevice({ typedCall, promptPassphrase });
440
+ device.getDeviceState = jest
441
+ .fn()
442
+ .mockResolvedValueOnce({
443
+ identity: { deviceId: 'device-1' },
444
+ status: {
445
+ unlocked: undefined,
446
+ passphraseProtection: null,
447
+ unlockedAttachPin: null,
448
+ },
449
+ })
450
+ .mockResolvedValue({
451
+ identity: { deviceId: 'device-1' },
452
+ status: {
453
+ unlocked: true,
454
+ passphraseProtection: true,
455
+ unlockedAttachPin: false,
456
+ },
457
+ });
458
+ device.unlockDevice = jest.fn().mockImplementation(() => {
459
+ device.features.unlocked = true;
460
+ device.features.passphraseProtection = true;
461
+ device.features.unlockedAttachPin = false;
462
+ return Promise.resolve(device.features);
463
+ });
464
+ method.device = device as any;
465
+
466
+ await expect(method.run()).resolves.toMatchObject({
467
+ walletType: 'hidden',
468
+ passphraseState: 'hidden-state-after-unknown-status',
469
+ });
470
+ expect(device.unlockDevice).toHaveBeenCalledTimes(1);
471
+ expect(device.unlockDevice.mock.invocationCallOrder[0]).toBeLessThan(
472
+ promptPassphrase.mock.invocationCallOrder[0]
473
+ );
474
+ });
475
+
476
+ test('rejects wallet selection when the post-unlock Protocol V2 status remains incomplete', async () => {
477
+ const typedCall = jest
478
+ .fn()
479
+ .mockResolvedValueOnce({ message: { version: 2 } })
480
+ .mockResolvedValueOnce({ message: {} })
481
+ .mockResolvedValueOnce({
482
+ message: {
483
+ btc_test_address: 'hidden-state-from-incomplete-status',
484
+ session_id: 'hidden-session-from-incomplete-status',
485
+ },
486
+ });
487
+ const promptPassphrase = jest.fn().mockResolvedValue({ passphrase: 'host hidden wallet' });
488
+ const method = new OpenWalletSession({
489
+ payload: { method: 'openWalletSession', connectId: 'connect-id', mode: 'select-hidden' },
490
+ });
491
+ method.init();
492
+ const device = createDevice({ typedCall, promptPassphrase });
493
+ device.getDeviceState = jest.fn().mockResolvedValue({
494
+ identity: { deviceId: 'device-1' },
495
+ status: {
496
+ unlocked: true,
497
+ passphraseProtection: null,
498
+ unlockedAttachPin: null,
499
+ },
500
+ });
501
+ device.unlockDevice = jest.fn().mockResolvedValue(device.features);
502
+ method.device = device as any;
503
+
504
+ await expect(method.run()).rejects.toMatchObject({
505
+ errorCode: HardwareErrorCode.DeviceInitializeFailed,
506
+ });
507
+ expect(device.unlockDevice).toHaveBeenCalledTimes(1);
508
+ expect(promptPassphrase).not.toHaveBeenCalled();
509
+ });
510
+
346
511
  test('rejects a Pro2 standard-wallet fallback after selecting a hidden wallet', async () => {
347
512
  const typedCall = jest
348
513
  .fn()
@@ -656,7 +821,11 @@ describe('openWalletSession', () => {
656
821
  expect(typedCall).toHaveBeenCalledWith('ProtocolInfoRequest', 'ProtocolInfo', {
657
822
  eventless_wallet_session: true,
658
823
  });
659
- expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.Main);
824
+ expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.Main, {
825
+ source: 'wallet-session-coordinator',
826
+ reason: 'open-wallet',
827
+ deviceOnly: true,
828
+ });
660
829
  expect(typedCall).toHaveBeenCalledWith('DeviceSessionGet', 'DeviceSession', {});
661
830
  expect(promptPassphrase).not.toHaveBeenCalled();
662
831
  expect(device.passphraseState).toBeUndefined();
@@ -686,7 +855,11 @@ describe('openWalletSession', () => {
686
855
  })
687
856
  .mockResolvedValueOnce({
688
857
  identity: { deviceId: 'device-1' },
689
- status: { passphraseProtection: true },
858
+ status: {
859
+ unlocked: true,
860
+ passphraseProtection: true,
861
+ unlockedAttachPin: false,
862
+ },
690
863
  });
691
864
  device.unlockDevice = jest.fn().mockImplementation(() => {
692
865
  device.features.unlocked = true;
@@ -701,7 +874,11 @@ describe('openWalletSession', () => {
701
874
  passphraseState: null,
702
875
  resumed: false,
703
876
  });
704
- expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.Main);
877
+ expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.Main, {
878
+ source: 'wallet-session-coordinator',
879
+ reason: 'open-wallet',
880
+ deviceOnly: true,
881
+ });
705
882
  expect(device.getDeviceState).toHaveBeenCalledTimes(2);
706
883
  });
707
884
 
@@ -721,6 +898,24 @@ describe('openWalletSession', () => {
721
898
  method.init();
722
899
  const device = createDevice({ typedCall });
723
900
  device.features.unlockedAttachPin = true;
901
+ device.getDeviceState = jest
902
+ .fn()
903
+ .mockResolvedValueOnce({
904
+ identity: { deviceId: 'device-1' },
905
+ status: {
906
+ unlocked: true,
907
+ unlockedAttachPin: true,
908
+ passphraseProtection: true,
909
+ },
910
+ })
911
+ .mockResolvedValue({
912
+ identity: { deviceId: 'device-1' },
913
+ status: {
914
+ unlocked: true,
915
+ unlockedAttachPin: false,
916
+ passphraseProtection: true,
917
+ },
918
+ });
724
919
  device.unlockDevice = jest.fn().mockImplementation(() => {
725
920
  device.features.unlockedAttachPin = false;
726
921
  return Promise.resolve(device.features);
@@ -731,7 +926,11 @@ describe('openWalletSession', () => {
731
926
  walletType: 'standard',
732
927
  passphraseState: null,
733
928
  });
734
- expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.Main);
929
+ expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.Main, {
930
+ source: 'wallet-session-coordinator',
931
+ reason: 'open-wallet',
932
+ deviceOnly: true,
933
+ });
735
934
  expect(typedCall).toHaveBeenCalledWith('DeviceSessionGet', 'DeviceSession', {});
736
935
  });
737
936
 
@@ -939,7 +1138,9 @@ describe('openWalletSession', () => {
939
1138
  },
940
1139
  { cancelDeviceOnReject: false }
941
1140
  );
942
- expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.AttachToPin);
1141
+ expect(device.unlockDevice).toHaveBeenCalledWith(DeviceSessionPinType.AttachToPin, {
1142
+ emitUiEvent: false,
1143
+ });
943
1144
  expect(typedCall).toHaveBeenCalledWith('DeviceSessionGet', 'DeviceSession', {});
944
1145
  });
945
1146
 
@@ -1215,7 +1416,11 @@ describe('openWalletSession', () => {
1215
1416
  })
1216
1417
  .mockResolvedValueOnce({
1217
1418
  identity: { deviceId: 'device-1' },
1218
- status: { passphraseProtection: true },
1419
+ status: {
1420
+ unlocked: true,
1421
+ passphraseProtection: true,
1422
+ unlockedAttachPin: false,
1423
+ },
1219
1424
  });
1220
1425
  device.unlockDevice = jest.fn().mockImplementation(() => {
1221
1426
  device.features.unlocked = true;
@@ -1271,7 +1476,11 @@ describe('openWalletSession', () => {
1271
1476
  })
1272
1477
  .mockResolvedValueOnce({
1273
1478
  identity: { deviceId: 'other-device' },
1274
- status: { passphraseProtection: true },
1479
+ status: {
1480
+ unlocked: true,
1481
+ passphraseProtection: true,
1482
+ unlockedAttachPin: false,
1483
+ },
1275
1484
  });
1276
1485
  device.unlockDevice = jest.fn().mockImplementation(() => {
1277
1486
  device.features.unlocked = true;
@@ -1335,7 +1544,6 @@ describe('openWalletSession', () => {
1335
1544
  const typedCall = jest
1336
1545
  .fn()
1337
1546
  .mockResolvedValueOnce({ message: { version: 2 } })
1338
- .mockResolvedValueOnce({ message: {} })
1339
1547
  .mockResolvedValueOnce({
1340
1548
  message: {
1341
1549
  btc_test_address: 'hidden-state',
@@ -1362,7 +1570,40 @@ describe('openWalletSession', () => {
1362
1570
  passphraseState: 'hidden-state',
1363
1571
  resumed: false,
1364
1572
  });
1365
- expect(promptPassphrase).toHaveBeenCalledTimes(1);
1573
+ expect(typedCall).toHaveBeenCalledWith('DeviceSessionGet', 'DeviceSession', {});
1574
+ expect(promptPassphrase).not.toHaveBeenCalled();
1366
1575
  expect(deviceWalletSessionStore.get('device-1', 'hidden-state')).toBe('new-session');
1367
1576
  });
1577
+
1578
+ test.each([false, true])('keeps deriveCardano API compatibility: %s', async deriveCardano => {
1579
+ const typedCall = jest
1580
+ .fn()
1581
+ .mockResolvedValueOnce({ message: { version: 2 } })
1582
+ .mockResolvedValueOnce({ message: {} })
1583
+ .mockResolvedValueOnce({
1584
+ message: {
1585
+ btc_test_address: 'hidden-state',
1586
+ session_id: 'new-session',
1587
+ },
1588
+ });
1589
+ const method = new OpenWalletSession({
1590
+ payload: {
1591
+ method: 'openWalletSession',
1592
+ connectId: 'connect-id',
1593
+ mode: 'select-hidden',
1594
+ deriveCardano,
1595
+ },
1596
+ });
1597
+ method.init();
1598
+ method.device = createDevice({
1599
+ typedCall,
1600
+ promptPassphrase: jest.fn().mockResolvedValue({ passphrase: 'host hidden wallet' }),
1601
+ }) as any;
1602
+
1603
+ await expect(method.run()).resolves.toMatchObject({
1604
+ walletType: 'hidden',
1605
+ passphraseState: 'hidden-state',
1606
+ });
1607
+ expect(typedCall).toHaveBeenCalledWith('DeviceSessionGet', 'DeviceSession', {});
1608
+ });
1368
1609
  });
@@ -0,0 +1,108 @@
1
+ import { blake2s } from '@noble/hashes/blake2s';
2
+ import { bytesToHex } from '@noble/hashes/utils';
3
+
4
+ import {
5
+ PRO2_NFT_IMAGE_HEIGHT,
6
+ PRO2_NFT_IMAGE_WIDTH,
7
+ PRO2_NFT_THUMBNAIL_HEIGHT,
8
+ PRO2_NFT_THUMBNAIL_WIDTH,
9
+ buildPro2NftBundle,
10
+ } from '../src/utils/pro2Nft';
11
+
12
+ const rgba = (width: number, height: number, alpha = 0xff) => {
13
+ const data = new Uint8Array(width * height * 4);
14
+ for (let index = 3; index < data.length; index += 4) data[index] = alpha;
15
+ return data;
16
+ };
17
+
18
+ describe('buildPro2NftBundle', () => {
19
+ test('builds the exact Pro2 RGB565 triplet and basename', () => {
20
+ const bundle = buildPro2NftBundle({
21
+ image: {
22
+ width: PRO2_NFT_IMAGE_WIDTH,
23
+ height: PRO2_NFT_IMAGE_HEIGHT,
24
+ rgba: rgba(PRO2_NFT_IMAGE_WIDTH, PRO2_NFT_IMAGE_HEIGHT),
25
+ },
26
+ thumbnail: {
27
+ width: PRO2_NFT_THUMBNAIL_WIDTH,
28
+ height: PRO2_NFT_THUMBNAIL_HEIGHT,
29
+ rgba: rgba(PRO2_NFT_THUMBNAIL_WIDTH, PRO2_NFT_THUMBNAIL_HEIGHT),
30
+ },
31
+ title: 'CryptoPunk #3100',
32
+ subtitle: 'CryptoPunks',
33
+ timestampMs: 1_760_000_000_000,
34
+ });
35
+
36
+ expect(bundle.image.byteLength).toBe(583_212);
37
+ expect(bundle.thumbnail.byteLength).toBe(138_876);
38
+ expect(Array.from(bundle.image.slice(0, 12))).toEqual([
39
+ 0x19, 0x12, 0, 0, 28, 2, 28, 2, 56, 4, 0, 0,
40
+ ]);
41
+ expect(Array.from(bundle.thumbnail.slice(0, 12))).toEqual([
42
+ 0x19, 0x12, 0, 0, 7, 1, 7, 1, 16, 2, 0, 0,
43
+ ]);
44
+ expect(new TextDecoder().decode(bundle.metadata)).toBe(
45
+ '{"title":"CryptoPunk #3100","subtitle":"CryptoPunks"}'
46
+ );
47
+ expect(bytesToHex(blake2s(bundle.image))).toBe(
48
+ 'af4b5f8fe216cfd0ef4a9162a42b5c66a73466f2491066f66f756c4e28cc869e'
49
+ );
50
+ expect(bytesToHex(blake2s(bundle.thumbnail))).toBe(
51
+ '44591afae1163cc14651d77644501c6a110f96506889b825cab0351ff4de857d'
52
+ );
53
+ expect(bytesToHex(blake2s(bundle.metadata))).toBe(
54
+ 'e30d4dc2cddd441c9f1438c783e05a623ccf76f2f6219d4281769dda1b3635b5'
55
+ );
56
+ expect(bundle.basename).toBe('nft-af4b5f8f-1760000000000');
57
+ });
58
+
59
+ test('composites transparent pixels onto black without an alpha plane', () => {
60
+ const image = rgba(PRO2_NFT_IMAGE_WIDTH, PRO2_NFT_IMAGE_HEIGHT);
61
+ image.set([255, 255, 255, 0], 0);
62
+ const bundle = buildPro2NftBundle({
63
+ image: { width: PRO2_NFT_IMAGE_WIDTH, height: PRO2_NFT_IMAGE_HEIGHT, rgba: image },
64
+ thumbnail: {
65
+ width: PRO2_NFT_THUMBNAIL_WIDTH,
66
+ height: PRO2_NFT_THUMBNAIL_HEIGHT,
67
+ rgba: rgba(PRO2_NFT_THUMBNAIL_WIDTH, PRO2_NFT_THUMBNAIL_HEIGHT, 0),
68
+ },
69
+ title: 'NFT',
70
+ subtitle: '',
71
+ timestampMs: 1,
72
+ });
73
+
74
+ expect(bundle.image[1]).toBe(0x12);
75
+ expect(Array.from(bundle.image.slice(12, 14))).toEqual([0, 0]);
76
+ expect(bundle.thumbnail.byteLength).toBe(138_876);
77
+ });
78
+
79
+ test('rejects invalid dimensions and metadata byte lengths', () => {
80
+ const validThumbnail = {
81
+ width: PRO2_NFT_THUMBNAIL_WIDTH,
82
+ height: PRO2_NFT_THUMBNAIL_HEIGHT,
83
+ rgba: rgba(PRO2_NFT_THUMBNAIL_WIDTH, PRO2_NFT_THUMBNAIL_HEIGHT),
84
+ };
85
+ expect(() =>
86
+ buildPro2NftBundle({
87
+ image: { width: 1, height: 1, rgba: new Uint8Array(4) },
88
+ thumbnail: validThumbnail,
89
+ title: 'NFT',
90
+ subtitle: '',
91
+ timestampMs: 1,
92
+ })
93
+ ).toThrow('540x540');
94
+ expect(() =>
95
+ buildPro2NftBundle({
96
+ image: {
97
+ width: PRO2_NFT_IMAGE_WIDTH,
98
+ height: PRO2_NFT_IMAGE_HEIGHT,
99
+ rgba: rgba(PRO2_NFT_IMAGE_WIDTH, PRO2_NFT_IMAGE_HEIGHT),
100
+ },
101
+ thumbnail: validThumbnail,
102
+ title: 'a'.repeat(64),
103
+ subtitle: '',
104
+ timestampMs: 1,
105
+ })
106
+ ).toThrow('1 to 63');
107
+ });
108
+ });
@@ -0,0 +1,56 @@
1
+ import { DeviceSessionPinType } from '@onekeyfe/hd-transport';
2
+
3
+ import { DEVICE } from '../src/events';
4
+ import { Device } from '../src/device/Device';
5
+
6
+ jest.mock('../src/data/config', () => ({
7
+ getSDKVersion: jest.fn(() => '1.0.0-test'),
8
+ DEFAULT_DOMAIN: 'https://example.com/',
9
+ }));
10
+
11
+ describe('Protocol V2 UI interaction lifecycle', () => {
12
+ test('emits a matching PIN phase completion after DeviceSessionAskPin succeeds', async () => {
13
+ const device = new Device({ id: 'connect-1' } as any);
14
+ device.isProtocolV2 = jest.fn(() => true);
15
+ device.updateProtocolV2Status = jest.fn(() => undefined as any);
16
+ device.commands = {
17
+ typedCall: jest.fn().mockResolvedValue({
18
+ message: { unlocked: true },
19
+ }),
20
+ } as any;
21
+ device.beginProtocolV2UiInteraction();
22
+
23
+ const starts: unknown[] = [];
24
+ const completions: unknown[] = [];
25
+ device.on(DEVICE.PIN_ON_DEVICE, (...args) => starts.push(args));
26
+ device.on(DEVICE.PIN_ON_DEVICE_COMPLETE, (...args) => completions.push(args));
27
+
28
+ await device.unlockDevice(DeviceSessionPinType.Main, {
29
+ source: 'unlock-coordinator',
30
+ reason: 'device-locked',
31
+ deviceOnly: true,
32
+ });
33
+
34
+ const startMetadata = (starts[0] as unknown[])[2] as Record<string, unknown>;
35
+ const completionMetadata = (completions[0] as unknown[])[1] as Record<string, unknown>;
36
+ expect(startMetadata.interaction).toMatchObject({
37
+ phase: 'pin',
38
+ transition: 'start',
39
+ protocol: 'V2',
40
+ });
41
+ expect(completionMetadata).toMatchObject({
42
+ interactionId: (startMetadata.interaction as { interactionId: string }).interactionId,
43
+ phaseId: (startMetadata.interaction as { phaseId: string }).phaseId,
44
+ phase: 'pin',
45
+ transition: 'complete',
46
+ outcome: 'succeeded',
47
+ protocol: 'V2',
48
+ });
49
+ expect(completionMetadata.sequence).toBeGreaterThan(
50
+ (startMetadata.interaction as { sequence: number }).sequence
51
+ );
52
+ expect(completionMetadata.phaseId).toBe(
53
+ (startMetadata.interaction as { phaseId: string }).phaseId
54
+ );
55
+ });
56
+ });
@@ -1,45 +1,96 @@
1
- import * as publicMethods from '../src/api';
2
- import {
3
- PROTOCOL_V2_RETRY_ON_LOCKED_METHODS,
4
- getProtocolV2UnlockPolicy,
5
- } from '../src/protocols/protocol-v2/unlockPolicy';
1
+ import ConfluxSignMessageCIP23 from '../src/api/conflux/ConfluxSignMessageCIP23';
2
+ import DeviceLock from '../src/api/device/DeviceLock';
3
+ import { runMethodWithUnlockPolicy } from '../src/protocols/protocol-v2/unlockPolicyRunner';
6
4
 
7
5
  jest.mock('../src/data/config', () => ({
8
6
  DEFAULT_DOMAIN: 'https://example.com/',
9
7
  getSDKVersion: () => '0.0.0-test',
10
8
  }));
11
9
 
12
- describe('Protocol V2 public method unlock policy', () => {
13
- test('classifies the complete public method table with an explicit replay allowlist', () => {
14
- const publicMethodNames = Object.keys(publicMethods);
15
- const retryMethodNames = new Set<string>(PROTOCOL_V2_RETRY_ON_LOCKED_METHODS);
16
-
17
- expect(publicMethodNames.length).toBeGreaterThan(100);
18
- expect(new Set(PROTOCOL_V2_RETRY_ON_LOCKED_METHODS).size).toBe(
19
- PROTOCOL_V2_RETRY_ON_LOCKED_METHODS.length
20
- );
21
-
22
- for (const methodName of PROTOCOL_V2_RETRY_ON_LOCKED_METHODS) {
23
- expect(publicMethods).toHaveProperty(methodName);
24
- }
25
-
26
- for (const methodName of publicMethodNames) {
27
- expect(getProtocolV2UnlockPolicy(methodName)).toBe(
28
- retryMethodNames.has(methodName) ? 'retry-on-locked' : 'none'
29
- );
30
- }
10
+ describe('Protocol V2 unlock semantics', () => {
11
+ test('wallet business methods inherit the wallet-session unlock requirement', () => {
12
+ const method = new ConfluxSignMessageCIP23({
13
+ id: 1,
14
+ payload: { method: 'confluxSignMessageCIP23' },
15
+ });
16
+
17
+ expect(method.useDevicePassphraseState).toBe(true);
18
+ expect(method.unlockPolicy).toBe('none');
19
+ expect(method.getSupportedProtocols()).toContain('V2');
20
+ });
21
+
22
+ test('lock-free Protocol V2 controls explicitly opt out of wallet-session handling', () => {
23
+ const method = new DeviceLock({
24
+ id: 1,
25
+ payload: { method: 'deviceLock' },
26
+ });
27
+
28
+ method.init();
29
+
30
+ expect(method.useDevicePassphraseState).toBe(false);
31
+ expect(method.unlockPolicy).toBe('none');
31
32
  });
32
33
 
33
- test.each([
34
- 'deviceUnlock',
35
- 'deviceLock',
36
- 'deviceWipe',
37
- 'deviceChangePin',
38
- 'deviceSettings',
39
- 'firmwareUpdateV4',
40
- 'deviceUploadWallpaper',
41
- 'uploadPortfolio',
42
- ])('does not make the state-changing method %s replayable by default', methodName => {
43
- expect(getProtocolV2UnlockPolicy(methodName)).toBe('none');
34
+ test('pre-unlocks an unregistered wallet business method', async () => {
35
+ const features = { unlocked: false };
36
+ const method = {
37
+ name: 'newWalletBusinessMethod',
38
+ unlockPolicy: 'none',
39
+ useDevicePassphraseState: true,
40
+ run: jest.fn().mockResolvedValue({ message: 'ok' }),
41
+ };
42
+ const device = {
43
+ features,
44
+ commands: {
45
+ typedCall: jest.fn().mockResolvedValue({ message: { unlocked: false } }),
46
+ },
47
+ isProtocolV2: () => true,
48
+ isBootloader: () => false,
49
+ isRomloader: () => false,
50
+ updateProtocolV2Status: jest.fn(() => features),
51
+ unlockDevice: jest.fn().mockImplementation(() => {
52
+ features.unlocked = true;
53
+ return Promise.resolve(features);
54
+ }),
55
+ };
56
+
57
+ await expect(runMethodWithUnlockPolicy(method as any, device as any)).resolves.toEqual({
58
+ message: 'ok',
59
+ });
60
+ expect(device.unlockDevice).toHaveBeenCalledTimes(1);
61
+ expect(method.run).toHaveBeenCalledTimes(1);
62
+ });
63
+
64
+ test('runs fresh-status validation before starting unlock', async () => {
65
+ const identityError = new Error('Unexpected device');
66
+ const method = {
67
+ name: 'walletBusinessMethod',
68
+ unlockPolicy: 'none',
69
+ useDevicePassphraseState: true,
70
+ run: jest.fn(),
71
+ };
72
+ const features = { unlocked: false };
73
+ const device = {
74
+ features,
75
+ commands: {
76
+ typedCall: jest.fn().mockResolvedValue({ message: { unlocked: false } }),
77
+ },
78
+ isProtocolV2: () => true,
79
+ isBootloader: () => false,
80
+ isRomloader: () => false,
81
+ updateProtocolV2Status: jest.fn(() => features),
82
+ unlockDevice: jest.fn(),
83
+ };
84
+
85
+ await expect(
86
+ runMethodWithUnlockPolicy(method as any, device as any, {
87
+ afterStatusBeforeUnlock: () => {
88
+ throw identityError;
89
+ },
90
+ })
91
+ ).rejects.toBe(identityError);
92
+
93
+ expect(device.unlockDevice).not.toHaveBeenCalled();
94
+ expect(method.run).not.toHaveBeenCalled();
44
95
  });
45
96
  });