@meshconnect/uwc-injected-connector 0.20.3 → 0.22.0

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 (62) hide show
  1. package/dist/eip6963-discovery.d.ts +13 -0
  2. package/dist/eip6963-discovery.d.ts.map +1 -1
  3. package/dist/eip6963-discovery.js +12 -3
  4. package/dist/eip6963-discovery.js.map +1 -1
  5. package/dist/injected-connector.d.ts +39 -2
  6. package/dist/injected-connector.d.ts.map +1 -1
  7. package/dist/injected-connector.js +170 -19
  8. package/dist/injected-connector.js.map +1 -1
  9. package/dist/services/connection-manager.d.ts +84 -5
  10. package/dist/services/connection-manager.d.ts.map +1 -1
  11. package/dist/services/connection-manager.js +172 -30
  12. package/dist/services/connection-manager.js.map +1 -1
  13. package/dist/services/ethereum/ethereum-transaction-builder.d.ts.map +1 -1
  14. package/dist/services/ethereum/ethereum-transaction-builder.js +3 -2
  15. package/dist/services/ethereum/ethereum-transaction-builder.js.map +1 -1
  16. package/dist/services/ethereum/ethereum-wallet-service.d.ts +86 -7
  17. package/dist/services/ethereum/ethereum-wallet-service.d.ts.map +1 -1
  18. package/dist/services/ethereum/ethereum-wallet-service.js +201 -28
  19. package/dist/services/ethereum/ethereum-wallet-service.js.map +1 -1
  20. package/dist/services/solana/solana-wallet-service.d.ts +57 -0
  21. package/dist/services/solana/solana-wallet-service.d.ts.map +1 -1
  22. package/dist/services/solana/solana-wallet-service.js +118 -0
  23. package/dist/services/solana/solana-wallet-service.js.map +1 -1
  24. package/dist/services/ton/ton-wallet-service.d.ts +9 -0
  25. package/dist/services/ton/ton-wallet-service.d.ts.map +1 -1
  26. package/dist/services/ton/ton-wallet-service.js +15 -0
  27. package/dist/services/ton/ton-wallet-service.js.map +1 -1
  28. package/dist/services/tron/tron-wallet-service.d.ts +11 -0
  29. package/dist/services/tron/tron-wallet-service.d.ts.map +1 -1
  30. package/dist/services/tron/tron-wallet-service.js +21 -0
  31. package/dist/services/tron/tron-wallet-service.js.map +1 -1
  32. package/dist/utils/step-reporter.d.ts +33 -0
  33. package/dist/utils/step-reporter.d.ts.map +1 -0
  34. package/dist/utils/step-reporter.js +54 -0
  35. package/dist/utils/step-reporter.js.map +1 -0
  36. package/dist/utils/with-deadline.d.ts +30 -0
  37. package/dist/utils/with-deadline.d.ts.map +1 -0
  38. package/dist/utils/with-deadline.js +67 -0
  39. package/dist/utils/with-deadline.js.map +1 -0
  40. package/package.json +6 -6
  41. package/src/eip6963-discovery.test.ts +111 -2
  42. package/src/eip6963-discovery.ts +28 -4
  43. package/src/injected-connector.test.ts +574 -16
  44. package/src/injected-connector.ts +210 -23
  45. package/src/services/connection-manager.test.ts +338 -1
  46. package/src/services/connection-manager.ts +236 -41
  47. package/src/services/ethereum/ethereum-transaction-builder.test.ts +97 -0
  48. package/src/services/ethereum/ethereum-transaction-builder.ts +3 -2
  49. package/src/services/ethereum/ethereum-wallet-service.test.ts +495 -17
  50. package/src/services/ethereum/ethereum-wallet-service.ts +248 -32
  51. package/src/services/solana/solana-wallet-service.lifecycle.test.ts +156 -6
  52. package/src/services/solana/solana-wallet-service.reconnect.test.ts +175 -0
  53. package/src/services/solana/solana-wallet-service.ts +132 -0
  54. package/src/services/ton/ton-wallet-service.test.ts +25 -0
  55. package/src/services/ton/ton-wallet-service.ts +15 -0
  56. package/src/services/tron/tron-wallet-service.test.ts +45 -0
  57. package/src/services/tron/tron-wallet-service.ts +20 -0
  58. package/src/signSolanaTransactionBytes.test.ts +1 -1
  59. package/src/utils/step-reporter.test.ts +56 -0
  60. package/src/utils/step-reporter.ts +83 -0
  61. package/src/utils/with-deadline.test.ts +80 -0
  62. package/src/utils/with-deadline.ts +67 -0
@@ -8,6 +8,20 @@ vi.mock('../../eip6963-discovery', () => ({
8
8
  getAvailableWallets: vi.fn()
9
9
  }))
10
10
 
11
+ function providerWithEvents() {
12
+ const handlers: Record<string, (arg?: unknown) => void> = {}
13
+ const provider = {
14
+ request: vi.fn(),
15
+ on: (event: string, h: (arg?: unknown) => void) => {
16
+ handlers[event] = h
17
+ },
18
+ removeListener: vi.fn((event: string) => {
19
+ delete handlers[event]
20
+ })
21
+ } as unknown as EthereumProvider
22
+ return { provider, handlers }
23
+ }
24
+
11
25
  describe('EthereumWalletService', () => {
12
26
  let service: EthereumWalletService
13
27
  let mockProvider: EthereumProvider
@@ -132,6 +146,27 @@ describe('EthereumWalletService', () => {
132
146
  'No accounts returned from wallet'
133
147
  )
134
148
  })
149
+
150
+ it("captures the connected wallet's source/rdns from detectedWallets at connect time", async () => {
151
+ service['detectedWallets'] = [
152
+ {
153
+ uuid: 'uuid-1',
154
+ name: 'MetaMask',
155
+ icon: '',
156
+ rdns: 'io.metamask',
157
+ provider: mockProvider,
158
+ source: 'local'
159
+ }
160
+ ]
161
+ vi.mocked(mockProvider.request)
162
+ .mockResolvedValueOnce(['0xabc'])
163
+ .mockResolvedValueOnce('0x1')
164
+
165
+ await service.connect(mockProvider, 'uuid-1')
166
+
167
+ expect(service['connectedWalletSource']).toBe('local')
168
+ expect(service['connectedWalletRdns']).toBe('io.metamask')
169
+ })
135
170
  })
136
171
 
137
172
  describe('switchNetwork', () => {
@@ -267,6 +302,42 @@ describe('EthereumWalletService', () => {
267
302
  })
268
303
  })
269
304
 
305
+ describe('getLiveAccount — live signer address', () => {
306
+ it('returns the LIVE eth_accounts[0] after an in-wallet switch, not the connect-time cache', async () => {
307
+ // Connect with account A (connect calls eth_requestAccounts then eth_chainId).
308
+ vi.mocked(mockProvider.request)
309
+ .mockResolvedValueOnce(['0xOLD_A'])
310
+ .mockResolvedValueOnce('0x1')
311
+ await service.connect(mockProvider)
312
+ expect(service.getAccount()).toBe('0xOLD_A')
313
+
314
+ // Simulate an in-wallet account switch: eth_accounts now reports B.
315
+ vi.mocked(mockProvider.request).mockResolvedValueOnce(['0xNEW_B'])
316
+
317
+ expect(await service.getLiveAccount()).toBe('0xNEW_B')
318
+ expect(await service.getLiveAccount()).not.toBe('0xOLD_A')
319
+ expect(mockProvider.request).toHaveBeenLastCalledWith({
320
+ method: 'eth_accounts'
321
+ })
322
+ })
323
+
324
+ it('returns null when there is no connected provider', async () => {
325
+ expect(await service.getLiveAccount()).toBeNull()
326
+ })
327
+
328
+ it('returns null when the wallet exposes no accounts', async () => {
329
+ service.setConnectionState(mockProvider, '0xabc')
330
+ vi.mocked(mockProvider.request).mockResolvedValueOnce([])
331
+ expect(await service.getLiveAccount()).toBeNull()
332
+ })
333
+
334
+ it('returns null when the eth_accounts request throws', async () => {
335
+ service.setConnectionState(mockProvider, '0xabc')
336
+ vi.mocked(mockProvider.request).mockRejectedValueOnce(new Error('locked'))
337
+ expect(await service.getLiveAccount()).toBeNull()
338
+ })
339
+ })
340
+
270
341
  describe('disconnect', () => {
271
342
  it('should clear connection state', async () => {
272
343
  // Connect first
@@ -280,6 +351,28 @@ describe('EthereumWalletService', () => {
280
351
  expect(service.getConnectedProvider()).toBeNull()
281
352
  expect(service.getAccount()).toBeNull()
282
353
  })
354
+
355
+ it('should clear the captured wallet source/rdns', async () => {
356
+ service['detectedWallets'] = [
357
+ {
358
+ uuid: 'uuid-1',
359
+ name: 'MetaMask',
360
+ icon: '',
361
+ rdns: 'io.metamask',
362
+ provider: mockProvider,
363
+ source: 'local'
364
+ }
365
+ ]
366
+ vi.mocked(mockProvider.request)
367
+ .mockResolvedValueOnce(['0x123'])
368
+ .mockResolvedValueOnce('0x1')
369
+ await service.connect(mockProvider, 'uuid-1')
370
+
371
+ service.disconnect()
372
+
373
+ expect(service['connectedWalletSource']).toBeNull()
374
+ expect(service['connectedWalletRdns']).toBeUndefined()
375
+ })
283
376
  })
284
377
 
285
378
  describe('getCapabilities', () => {
@@ -476,20 +569,6 @@ describe('EthereumWalletService', () => {
476
569
  })
477
570
 
478
571
  describe('onLifecycleEvent', () => {
479
- function providerWithEvents() {
480
- const handlers: Record<string, (arg?: unknown) => void> = {}
481
- const provider = {
482
- request: vi.fn(),
483
- on: (event: string, h: (arg?: unknown) => void) => {
484
- handlers[event] = h
485
- },
486
- removeListener: vi.fn((event: string) => {
487
- delete handlers[event]
488
- })
489
- } as unknown as EthereumProvider
490
- return { provider, handlers }
491
- }
492
-
493
572
  it('returns undefined when there is no connected provider', () => {
494
573
  expect(service.onLifecycleEvent(() => {})).toBeUndefined()
495
574
  })
@@ -514,11 +593,192 @@ describe('EthereumWalletService', () => {
514
593
  expect(on).not.toHaveBeenCalled()
515
594
  })
516
595
 
517
- it('returns undefined in iframe bridge mode (the provider is a Comlink proxy)', () => {
596
+ it('uses the bridge subscription in iframe mode instead of bailing out', () => {
597
+ const { provider } = providerWithEvents()
598
+ service['connectedProvider'] = provider
599
+ service['connectedWalletUuid'] = 'uuid-1'
600
+ const bridgeUnsub = vi.fn()
601
+ const bridgeSubscribe = vi.fn().mockReturnValue(bridgeUnsub)
602
+ ;(
603
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
604
+ ).UWCBridgeChildInitialized = true
605
+ ;(
606
+ window as unknown as {
607
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
608
+ }
609
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
610
+
611
+ try {
612
+ const events: WalletLifecycleEvent[] = []
613
+ const stop = service.onLifecycleEvent(e => events.push(e))
614
+ expect(typeof stop).toBe('function')
615
+
616
+ // 3 bridge subscriptions: accountsChanged, chainChanged, disconnect.
617
+ expect(bridgeSubscribe).toHaveBeenCalledTimes(3)
618
+ expect(bridgeSubscribe.mock.calls.map(c => c[1]).sort()).toEqual([
619
+ 'accountsChanged',
620
+ 'chainChanged',
621
+ 'disconnect'
622
+ ])
623
+
624
+ // Fire one of the forwarded callbacks the same way the parent would.
625
+ const accountsCall = bridgeSubscribe.mock.calls.find(
626
+ c => c[1] === 'accountsChanged'
627
+ )!
628
+ accountsCall[2](['0xNEW'])
629
+ expect(events).toEqual([{ type: 'accountChanged' }])
630
+
631
+ stop!()
632
+ expect(bridgeUnsub).toHaveBeenCalledTimes(3)
633
+ } finally {
634
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
635
+ .UWCBridgeChildInitialized
636
+ delete (
637
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
638
+ ).__uwcSubscribeEip6963ProviderEvent
639
+ }
640
+ })
641
+
642
+ it("threads the connected wallet's rdns through the bridge subscription call", () => {
643
+ const { provider } = providerWithEvents()
644
+ service['detectedWallets'] = [
645
+ {
646
+ uuid: 'uuid-1',
647
+ name: 'MetaMask',
648
+ icon: '',
649
+ rdns: 'io.metamask',
650
+ provider,
651
+ source: 'bridge'
652
+ }
653
+ ]
654
+ // setConnectionState (not poking private fields directly) so the rdns
655
+ // capture at connect time actually runs.
656
+ service.setConnectionState(provider, '0xabc', 'uuid-1')
657
+ const bridgeSubscribe = vi.fn().mockReturnValue(vi.fn())
658
+ ;(
659
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
660
+ ).UWCBridgeChildInitialized = true
661
+ ;(
662
+ window as unknown as {
663
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
664
+ }
665
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
666
+
667
+ try {
668
+ service.onLifecycleEvent(() => {})
669
+
670
+ expect(bridgeSubscribe).toHaveBeenCalledTimes(3)
671
+ for (const call of bridgeSubscribe.mock.calls) {
672
+ expect(call[3]).toBe('io.metamask')
673
+ }
674
+ } finally {
675
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
676
+ .UWCBridgeChildInitialized
677
+ delete (
678
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
679
+ ).__uwcSubscribeEip6963ProviderEvent
680
+ }
681
+ })
682
+
683
+ it('keeps the rdns captured at connect time even if a later discovery refresh drops the entry', () => {
684
+ // Regression: rdns used to be re-derived via a LIVE findWalletByUuid
685
+ // lookup at subscribe time. initializeDiscovery() replaces
686
+ // detectedWallets wholesale, so a refresh between connect and subscribe
687
+ // used to silently blank the rdns and disable the fallback match on
688
+ // the parent side.
689
+ const { provider } = providerWithEvents()
690
+ service['detectedWallets'] = [
691
+ {
692
+ uuid: 'uuid-1',
693
+ name: 'MetaMask',
694
+ icon: '',
695
+ rdns: 'io.metamask',
696
+ provider,
697
+ source: 'bridge'
698
+ }
699
+ ]
700
+ service.setConnectionState(provider, '0xabc', 'uuid-1')
701
+ // Simulate a discovery refresh that no longer contains this uuid.
702
+ service['detectedWallets'] = []
703
+
704
+ const bridgeSubscribe = vi.fn().mockReturnValue(vi.fn())
705
+ ;(
706
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
707
+ ).UWCBridgeChildInitialized = true
708
+ ;(
709
+ window as unknown as {
710
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
711
+ }
712
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
713
+
714
+ try {
715
+ service.onLifecycleEvent(() => {})
716
+
717
+ expect(bridgeSubscribe).toHaveBeenCalledTimes(3)
718
+ for (const call of bridgeSubscribe.mock.calls) {
719
+ expect(call[3]).toBe('io.metamask')
720
+ }
721
+ } finally {
722
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
723
+ .UWCBridgeChildInitialized
724
+ delete (
725
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
726
+ ).__uwcSubscribeEip6963ProviderEvent
727
+ }
728
+ })
729
+
730
+ it('attaches directly to the provider (bypassing the bridge) when the connected wallet was discovered LOCALLY, even in bridge mode', () => {
731
+ // The connected wallet's provider is local to THIS frame — it holds
732
+ // the connection permission this frame's own eth_requestAccounts
733
+ // obtained. Routing through the bridge would attach to the TOP
734
+ // frame's copy of the wallet instead, which never got that
735
+ // permission and so never fires accountsChanged.
736
+ const { provider, handlers } = providerWithEvents()
737
+ service['detectedWallets'] = [
738
+ {
739
+ uuid: 'uuid-local',
740
+ name: 'MetaMask',
741
+ icon: '',
742
+ rdns: 'io.metamask',
743
+ provider,
744
+ source: 'local'
745
+ }
746
+ ]
747
+ service.setConnectionState(provider, '0xabc', 'uuid-local')
748
+
749
+ const bridgeSubscribe = vi.fn()
750
+ ;(
751
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
752
+ ).UWCBridgeChildInitialized = true
753
+ ;(
754
+ window as unknown as {
755
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
756
+ }
757
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
758
+
759
+ try {
760
+ const events: WalletLifecycleEvent[] = []
761
+ const stop = service.onLifecycleEvent(e => events.push(e))
762
+ expect(typeof stop).toBe('function')
763
+ expect(bridgeSubscribe).not.toHaveBeenCalled()
764
+
765
+ handlers.accountsChanged!(['0xNEW'])
766
+ expect(events).toEqual([{ type: 'accountChanged' }])
767
+
768
+ stop!()
769
+ expect(provider.removeListener).toHaveBeenCalledTimes(3)
770
+ } finally {
771
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
772
+ .UWCBridgeChildInitialized
773
+ delete (
774
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
775
+ ).__uwcSubscribeEip6963ProviderEvent
776
+ }
777
+ })
778
+
779
+ it('returns undefined in iframe mode when the bridge subscription global is not installed (older bridge-parent)', () => {
518
780
  const { provider } = providerWithEvents()
519
781
  service['connectedProvider'] = provider
520
- // The bridge child sets this flag when UWC runs inside the Link iframe; a
521
- // raw EIP-1193 handler can't ride the bridge, so we must NOT subscribe.
522
782
  ;(
523
783
  window as unknown as { UWCBridgeChildInitialized?: boolean }
524
784
  ).UWCBridgeChildInitialized = true
@@ -586,4 +846,222 @@ describe('EthereumWalletService', () => {
586
846
  ])
587
847
  })
588
848
  })
849
+
850
+ describe('onAccountsChanged', () => {
851
+ it('returns undefined with no connected provider', () => {
852
+ expect(service.onAccountsChanged(() => {})).toBeUndefined()
853
+ })
854
+
855
+ it('returns undefined in iframe bridge mode (the provider is a Comlink proxy)', () => {
856
+ const { provider } = providerWithEvents()
857
+ service['connectedProvider'] = provider
858
+ ;(
859
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
860
+ ).UWCBridgeChildInitialized = true
861
+ try {
862
+ expect(service.onAccountsChanged(() => {})).toBeUndefined()
863
+ } finally {
864
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
865
+ .UWCBridgeChildInitialized
866
+ }
867
+ })
868
+
869
+ it('uses the bridge subscription in iframe mode instead of bailing out', () => {
870
+ const { provider } = providerWithEvents()
871
+ service['connectedProvider'] = provider
872
+ service['connectedWalletUuid'] = 'uuid-1'
873
+ const bridgeUnsub = vi.fn()
874
+ const bridgeSubscribe = vi.fn().mockReturnValue(bridgeUnsub)
875
+ ;(
876
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
877
+ ).UWCBridgeChildInitialized = true
878
+ ;(
879
+ window as unknown as {
880
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
881
+ }
882
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
883
+
884
+ try {
885
+ const events: string[][] = []
886
+ const stop = service.onAccountsChanged(accounts =>
887
+ events.push(accounts)
888
+ )
889
+ expect(typeof stop).toBe('function')
890
+ expect(bridgeSubscribe).toHaveBeenCalledWith(
891
+ 'uuid-1',
892
+ 'accountsChanged',
893
+ expect.any(Function),
894
+ undefined
895
+ )
896
+
897
+ const [, , handler] = bridgeSubscribe.mock.calls[0]!
898
+ handler(['0xNEW'])
899
+ expect(events).toEqual([['0xNEW']])
900
+
901
+ stop!()
902
+ expect(bridgeUnsub).toHaveBeenCalledTimes(1)
903
+ } finally {
904
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
905
+ .UWCBridgeChildInitialized
906
+ delete (
907
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
908
+ ).__uwcSubscribeEip6963ProviderEvent
909
+ }
910
+ })
911
+
912
+ it('attaches directly to the provider (bypassing the bridge) when the connected wallet was discovered LOCALLY, even in bridge mode', () => {
913
+ const { provider, handlers } = providerWithEvents()
914
+ service['detectedWallets'] = [
915
+ {
916
+ uuid: 'uuid-local',
917
+ name: 'MetaMask',
918
+ icon: '',
919
+ rdns: 'io.metamask',
920
+ provider,
921
+ source: 'local'
922
+ }
923
+ ]
924
+ service.setConnectionState(provider, '0xabc', 'uuid-local')
925
+
926
+ const bridgeSubscribe = vi.fn()
927
+ ;(
928
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
929
+ ).UWCBridgeChildInitialized = true
930
+ ;(
931
+ window as unknown as {
932
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
933
+ }
934
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
935
+
936
+ try {
937
+ const events: string[][] = []
938
+ const stop = service.onAccountsChanged(accounts =>
939
+ events.push(accounts)
940
+ )
941
+ expect(typeof stop).toBe('function')
942
+ expect(bridgeSubscribe).not.toHaveBeenCalled()
943
+
944
+ handlers.accountsChanged!(['0xNEW'])
945
+ expect(events).toEqual([['0xNEW']])
946
+
947
+ stop!()
948
+ expect(provider.removeListener).toHaveBeenCalledTimes(1)
949
+ } finally {
950
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
951
+ .UWCBridgeChildInitialized
952
+ delete (
953
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
954
+ ).__uwcSubscribeEip6963ProviderEvent
955
+ }
956
+ })
957
+
958
+ it('forwards the real accounts array on accountsChanged (not classified, unlike onLifecycleEvent)', () => {
959
+ const { provider, handlers } = providerWithEvents()
960
+ service['connectedProvider'] = provider
961
+
962
+ const events: string[][] = []
963
+ const stop = service.onAccountsChanged(accounts => events.push(accounts))
964
+ expect(typeof stop).toBe('function')
965
+
966
+ handlers.accountsChanged!(['0xNEW', '0xSECOND'])
967
+ expect(events).toEqual([['0xNEW', '0xSECOND']])
968
+ // The full address IS forwarded here — unlike onLifecycleEvent, this
969
+ // stream exists specifically to carry it.
970
+ expect(JSON.stringify(events)).toContain('0xNEW')
971
+
972
+ stop!()
973
+ expect(provider.removeListener).toHaveBeenCalledTimes(1)
974
+ })
975
+
976
+ it('forwards an empty array as-is (disconnect signal, left for the caller to interpret)', () => {
977
+ const { provider, handlers } = providerWithEvents()
978
+ service['connectedProvider'] = provider
979
+
980
+ const events: string[][] = []
981
+ service.onAccountsChanged(accounts => events.push(accounts))
982
+ handlers.accountsChanged!([])
983
+
984
+ expect(events).toEqual([[]])
985
+ })
986
+
987
+ it('refreshes the account cache (direct branch) so getAccount() tracks an in-wallet switch', () => {
988
+ const { provider, handlers } = providerWithEvents()
989
+ service.setConnectionState(provider, '0xOLD')
990
+
991
+ service.onAccountsChanged(() => {})
992
+ handlers.accountsChanged!(['0xNEW'])
993
+
994
+ expect(service.getAccount()).toBe('0xNEW')
995
+ })
996
+
997
+ it('clears the account cache to null (direct branch) when accountsChanged fires with an empty list', () => {
998
+ const { provider, handlers } = providerWithEvents()
999
+ service.setConnectionState(provider, '0xOLD')
1000
+
1001
+ service.onAccountsChanged(() => {})
1002
+ handlers.accountsChanged!([])
1003
+
1004
+ expect(service.getAccount()).toBeNull()
1005
+ })
1006
+
1007
+ it('refreshes the account cache (bridged branch) so getAccount() tracks an in-wallet switch', () => {
1008
+ const { provider } = providerWithEvents()
1009
+ service['connectedProvider'] = provider
1010
+ service['connectedWalletUuid'] = 'uuid-1'
1011
+ service.setConnectionState(provider, '0xOLD', 'uuid-1')
1012
+ const bridgeSubscribe = vi.fn().mockReturnValue(() => {})
1013
+ ;(
1014
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
1015
+ ).UWCBridgeChildInitialized = true
1016
+ ;(
1017
+ window as unknown as {
1018
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
1019
+ }
1020
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
1021
+
1022
+ try {
1023
+ service.onAccountsChanged(() => {})
1024
+ const [, , handler] = bridgeSubscribe.mock.calls[0]!
1025
+ handler(['0xNEW'])
1026
+
1027
+ expect(service.getAccount()).toBe('0xNEW')
1028
+ } finally {
1029
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
1030
+ .UWCBridgeChildInitialized
1031
+ delete (
1032
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
1033
+ ).__uwcSubscribeEip6963ProviderEvent
1034
+ }
1035
+ })
1036
+
1037
+ it('clears the account cache to null (bridged branch) when accountsChanged fires with an empty list', () => {
1038
+ const { provider } = providerWithEvents()
1039
+ service['connectedProvider'] = provider
1040
+ service['connectedWalletUuid'] = 'uuid-1'
1041
+ service.setConnectionState(provider, '0xOLD', 'uuid-1')
1042
+ const bridgeSubscribe = vi.fn().mockReturnValue(() => {})
1043
+ ;(
1044
+ window as unknown as { UWCBridgeChildInitialized?: boolean }
1045
+ ).UWCBridgeChildInitialized = true
1046
+ ;(
1047
+ window as unknown as {
1048
+ __uwcSubscribeEip6963ProviderEvent?: typeof bridgeSubscribe
1049
+ }
1050
+ ).__uwcSubscribeEip6963ProviderEvent = bridgeSubscribe
1051
+
1052
+ try {
1053
+ service.onAccountsChanged(() => {})
1054
+ const [, , handler] = bridgeSubscribe.mock.calls[0]!
1055
+ handler([])
1056
+
1057
+ expect(service.getAccount()).toBeNull()
1058
+ } finally {
1059
+ delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
1060
+ .UWCBridgeChildInitialized
1061
+ delete (
1062
+ window as unknown as { __uwcSubscribeEip6963ProviderEvent?: unknown }
1063
+ ).__uwcSubscribeEip6963ProviderEvent
1064
+ }
1065
+ })
1066
+ })
589
1067
  })