@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.
- package/dist/eip6963-discovery.d.ts +13 -0
- package/dist/eip6963-discovery.d.ts.map +1 -1
- package/dist/eip6963-discovery.js +12 -3
- package/dist/eip6963-discovery.js.map +1 -1
- package/dist/injected-connector.d.ts +39 -2
- package/dist/injected-connector.d.ts.map +1 -1
- package/dist/injected-connector.js +170 -19
- package/dist/injected-connector.js.map +1 -1
- package/dist/services/connection-manager.d.ts +84 -5
- package/dist/services/connection-manager.d.ts.map +1 -1
- package/dist/services/connection-manager.js +172 -30
- package/dist/services/connection-manager.js.map +1 -1
- package/dist/services/ethereum/ethereum-transaction-builder.d.ts.map +1 -1
- package/dist/services/ethereum/ethereum-transaction-builder.js +3 -2
- package/dist/services/ethereum/ethereum-transaction-builder.js.map +1 -1
- package/dist/services/ethereum/ethereum-wallet-service.d.ts +86 -7
- package/dist/services/ethereum/ethereum-wallet-service.d.ts.map +1 -1
- package/dist/services/ethereum/ethereum-wallet-service.js +201 -28
- package/dist/services/ethereum/ethereum-wallet-service.js.map +1 -1
- package/dist/services/solana/solana-wallet-service.d.ts +57 -0
- package/dist/services/solana/solana-wallet-service.d.ts.map +1 -1
- package/dist/services/solana/solana-wallet-service.js +118 -0
- package/dist/services/solana/solana-wallet-service.js.map +1 -1
- package/dist/services/ton/ton-wallet-service.d.ts +9 -0
- package/dist/services/ton/ton-wallet-service.d.ts.map +1 -1
- package/dist/services/ton/ton-wallet-service.js +15 -0
- package/dist/services/ton/ton-wallet-service.js.map +1 -1
- package/dist/services/tron/tron-wallet-service.d.ts +11 -0
- package/dist/services/tron/tron-wallet-service.d.ts.map +1 -1
- package/dist/services/tron/tron-wallet-service.js +21 -0
- package/dist/services/tron/tron-wallet-service.js.map +1 -1
- package/dist/utils/step-reporter.d.ts +33 -0
- package/dist/utils/step-reporter.d.ts.map +1 -0
- package/dist/utils/step-reporter.js +54 -0
- package/dist/utils/step-reporter.js.map +1 -0
- package/dist/utils/with-deadline.d.ts +30 -0
- package/dist/utils/with-deadline.d.ts.map +1 -0
- package/dist/utils/with-deadline.js +67 -0
- package/dist/utils/with-deadline.js.map +1 -0
- package/package.json +6 -6
- package/src/eip6963-discovery.test.ts +111 -2
- package/src/eip6963-discovery.ts +28 -4
- package/src/injected-connector.test.ts +574 -16
- package/src/injected-connector.ts +210 -23
- package/src/services/connection-manager.test.ts +338 -1
- package/src/services/connection-manager.ts +236 -41
- package/src/services/ethereum/ethereum-transaction-builder.test.ts +97 -0
- package/src/services/ethereum/ethereum-transaction-builder.ts +3 -2
- package/src/services/ethereum/ethereum-wallet-service.test.ts +495 -17
- package/src/services/ethereum/ethereum-wallet-service.ts +248 -32
- package/src/services/solana/solana-wallet-service.lifecycle.test.ts +156 -6
- package/src/services/solana/solana-wallet-service.reconnect.test.ts +175 -0
- package/src/services/solana/solana-wallet-service.ts +132 -0
- package/src/services/ton/ton-wallet-service.test.ts +25 -0
- package/src/services/ton/ton-wallet-service.ts +15 -0
- package/src/services/tron/tron-wallet-service.test.ts +45 -0
- package/src/services/tron/tron-wallet-service.ts +20 -0
- package/src/signSolanaTransactionBytes.test.ts +1 -1
- package/src/utils/step-reporter.test.ts +56 -0
- package/src/utils/step-reporter.ts +83 -0
- package/src/utils/with-deadline.test.ts +80 -0
- package/src/utils/with-deadline.ts +67 -0
|
@@ -12,6 +12,7 @@ vi.mock('./services/storage-service')
|
|
|
12
12
|
vi.mock('./services/ethereum/ethereum-wallet-service')
|
|
13
13
|
vi.mock('./services/solana/solana-wallet-service')
|
|
14
14
|
vi.mock('./services/tron/tron-wallet-service')
|
|
15
|
+
vi.mock('./services/ton/ton-wallet-service')
|
|
15
16
|
vi.mock('./services/connection-manager')
|
|
16
17
|
vi.mock('./services/signature-service')
|
|
17
18
|
vi.mock('./services/transaction-service')
|
|
@@ -23,6 +24,20 @@ vi.mock('@meshconnect/uwc-bridge-child', async importOriginal => ({
|
|
|
23
24
|
BridgeChild: vi.fn()
|
|
24
25
|
}))
|
|
25
26
|
|
|
27
|
+
// Shared by both the `onLifecycleEvent routing` and `onAccountsChanged
|
|
28
|
+
// routing` describe blocks below — the mocked service classes are the same
|
|
29
|
+
// regardless of which stream is under test.
|
|
30
|
+
const getMocks = async () => {
|
|
31
|
+
const { EthereumWalletService } =
|
|
32
|
+
await import('./services/ethereum/ethereum-wallet-service')
|
|
33
|
+
const { SolanaWalletService } =
|
|
34
|
+
await import('./services/solana/solana-wallet-service')
|
|
35
|
+
return {
|
|
36
|
+
ethService: vi.mocked(EthereumWalletService).mock.results[0].value,
|
|
37
|
+
solService: vi.mocked(SolanaWalletService).mock.results[0].value
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
26
41
|
describe('InjectedConnector', () => {
|
|
27
42
|
let connector: InjectedConnector
|
|
28
43
|
let mockEthProvider: ExtensionInjectedProvider
|
|
@@ -397,6 +412,127 @@ describe('InjectedConnector', () => {
|
|
|
397
412
|
return c
|
|
398
413
|
}
|
|
399
414
|
|
|
415
|
+
// ONC-183 review: this Tron leg is the second, separate gather path (the
|
|
416
|
+
// other four live in ConnectionManager) and had no coverage of its
|
|
417
|
+
// deadline bound at all.
|
|
418
|
+
it('bounds the Tron gather: a never-answering Tron prompt still completes the connect', async () => {
|
|
419
|
+
vi.useFakeTimers()
|
|
420
|
+
try {
|
|
421
|
+
const tronConnect = vi.fn().mockReturnValue(new Promise(() => {}))
|
|
422
|
+
const onConnectStep = vi.fn()
|
|
423
|
+
const { TronConnector } =
|
|
424
|
+
await import('@meshconnect/uwc-tron-connector')
|
|
425
|
+
vi.mocked(TronConnector).mockImplementation(function () {
|
|
426
|
+
return { connect: tronConnect } as never
|
|
427
|
+
} as never)
|
|
428
|
+
const c = new InjectedConnector({
|
|
429
|
+
tronConnectorConfig: { enabledWallets: ['trust'] } as never,
|
|
430
|
+
networks: [mockNetwork, tronNetwork],
|
|
431
|
+
secondaryGatherTimeoutMs: 1000,
|
|
432
|
+
onConnectStep
|
|
433
|
+
})
|
|
434
|
+
const { ConnectionManager } =
|
|
435
|
+
await import('./services/connection-manager')
|
|
436
|
+
const connManager = vi
|
|
437
|
+
.mocked(ConnectionManager)
|
|
438
|
+
.mock.results.at(-1)!.value
|
|
439
|
+
connManager.checkExistingConnections.mockResolvedValue({
|
|
440
|
+
address: null,
|
|
441
|
+
availableAddresses: []
|
|
442
|
+
})
|
|
443
|
+
connManager.connectEthereum.mockResolvedValue({
|
|
444
|
+
networkId: 'eip155:1',
|
|
445
|
+
address: '0xeth',
|
|
446
|
+
availableAddresses: [{ address: '0xeth', networkId: 'eip155:1' }]
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
const pending = c.connect(mockNetwork, multiNsProvider)
|
|
450
|
+
await vi.advanceTimersByTimeAsync(1000)
|
|
451
|
+
const result = await pending
|
|
452
|
+
|
|
453
|
+
// EVM still connects; only the abandoned Tron leg is dropped.
|
|
454
|
+
expect(result.address).toBe('0xeth')
|
|
455
|
+
expect(
|
|
456
|
+
result.availableAddresses.some(a => a.networkId.startsWith('tron:'))
|
|
457
|
+
).toBe(false)
|
|
458
|
+
expect(onConnectStep).toHaveBeenCalledWith(
|
|
459
|
+
expect.objectContaining({
|
|
460
|
+
step: 'secondaryGather',
|
|
461
|
+
outcome: 'timedOut',
|
|
462
|
+
namespace: 'tron',
|
|
463
|
+
timeoutMs: 1000
|
|
464
|
+
})
|
|
465
|
+
)
|
|
466
|
+
} finally {
|
|
467
|
+
vi.useRealTimers()
|
|
468
|
+
}
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
it('reports a REJECTED Tron gather as failed, not ok', async () => {
|
|
472
|
+
const onConnectStep = vi.fn()
|
|
473
|
+
const { TronConnector } =
|
|
474
|
+
await import('@meshconnect/uwc-tron-connector')
|
|
475
|
+
vi.mocked(TronConnector).mockImplementation(function () {
|
|
476
|
+
return {
|
|
477
|
+
connect: vi.fn().mockRejectedValue(new Error('User rejected'))
|
|
478
|
+
} as never
|
|
479
|
+
} as never)
|
|
480
|
+
const c = new InjectedConnector({
|
|
481
|
+
tronConnectorConfig: { enabledWallets: ['trust'] } as never,
|
|
482
|
+
networks: [mockNetwork, tronNetwork],
|
|
483
|
+
onConnectStep
|
|
484
|
+
})
|
|
485
|
+
const { ConnectionManager } =
|
|
486
|
+
await import('./services/connection-manager')
|
|
487
|
+
const connManager = vi
|
|
488
|
+
.mocked(ConnectionManager)
|
|
489
|
+
.mock.results.at(-1)!.value
|
|
490
|
+
connManager.checkExistingConnections.mockResolvedValue({
|
|
491
|
+
address: null,
|
|
492
|
+
availableAddresses: []
|
|
493
|
+
})
|
|
494
|
+
connManager.connectEthereum.mockResolvedValue({
|
|
495
|
+
networkId: 'eip155:1',
|
|
496
|
+
address: '0xeth',
|
|
497
|
+
availableAddresses: [{ address: '0xeth', networkId: 'eip155:1' }]
|
|
498
|
+
})
|
|
499
|
+
|
|
500
|
+
const result = await c.connect(mockNetwork, multiNsProvider)
|
|
501
|
+
|
|
502
|
+
expect(result.address).toBe('0xeth')
|
|
503
|
+
expect(onConnectStep).toHaveBeenCalledWith(
|
|
504
|
+
expect.objectContaining({
|
|
505
|
+
step: 'secondaryGather',
|
|
506
|
+
outcome: 'failed',
|
|
507
|
+
namespace: 'tron'
|
|
508
|
+
})
|
|
509
|
+
)
|
|
510
|
+
})
|
|
511
|
+
|
|
512
|
+
it('routes a tron session to tronConnector.onAccountsChanged', async () => {
|
|
513
|
+
const { TronConnector } =
|
|
514
|
+
await import('@meshconnect/uwc-tron-connector')
|
|
515
|
+
const onAccountsChangedFn = vi.fn().mockReturnValue(vi.fn())
|
|
516
|
+
vi.mocked(TronConnector).mockImplementation(function () {
|
|
517
|
+
return { onAccountsChanged: onAccountsChangedFn } as never
|
|
518
|
+
} as never)
|
|
519
|
+
const c = new InjectedConnector({
|
|
520
|
+
tronConnectorConfig: { enabledWallets: ['trust'] } as never,
|
|
521
|
+
networks: [mockNetwork, tronNetwork]
|
|
522
|
+
})
|
|
523
|
+
|
|
524
|
+
const listener = vi.fn()
|
|
525
|
+
c.onAccountsChanged!(listener, {
|
|
526
|
+
connectionMode: 'injected',
|
|
527
|
+
namespace: 'tron'
|
|
528
|
+
})
|
|
529
|
+
|
|
530
|
+
expect(onAccountsChangedFn).toHaveBeenCalledWith(listener, {
|
|
531
|
+
connectionMode: 'injected',
|
|
532
|
+
namespace: 'tron'
|
|
533
|
+
})
|
|
534
|
+
})
|
|
535
|
+
|
|
400
536
|
it('auto-connects Tron alongside an EVM target and merges the address', async () => {
|
|
401
537
|
const tronConnect = vi.fn().mockResolvedValue({
|
|
402
538
|
networkId: 'tron:0x2b6653dc',
|
|
@@ -626,6 +762,103 @@ describe('InjectedConnector', () => {
|
|
|
626
762
|
})
|
|
627
763
|
})
|
|
628
764
|
|
|
765
|
+
describe('getLiveAccount', () => {
|
|
766
|
+
it('routes eip155 to ethereumService.getLiveAccount()', async () => {
|
|
767
|
+
const { EthereumWalletService } =
|
|
768
|
+
await import('./services/ethereum/ethereum-wallet-service')
|
|
769
|
+
const mockEthService = vi.mocked(EthereumWalletService).mock.results[0]!
|
|
770
|
+
.value
|
|
771
|
+
mockEthService.getLiveAccount.mockResolvedValue('0xLiveEth')
|
|
772
|
+
|
|
773
|
+
const result = await connector.getLiveAccount!('eip155')
|
|
774
|
+
|
|
775
|
+
expect(result).toBe('0xLiveEth')
|
|
776
|
+
})
|
|
777
|
+
|
|
778
|
+
it('routes solana to solanaService.getLiveAccount()', async () => {
|
|
779
|
+
const { SolanaWalletService } =
|
|
780
|
+
await import('./services/solana/solana-wallet-service')
|
|
781
|
+
const mockSolService =
|
|
782
|
+
vi.mocked(SolanaWalletService).mock.results[0]!.value
|
|
783
|
+
mockSolService.getLiveAccount.mockResolvedValue('SoLaNaLiveAddr')
|
|
784
|
+
|
|
785
|
+
const result = await connector.getLiveAccount!('solana')
|
|
786
|
+
|
|
787
|
+
expect(result).toBe('SoLaNaLiveAddr')
|
|
788
|
+
})
|
|
789
|
+
|
|
790
|
+
it('falls back to tronService.getLiveAccount() when no tronConnector is configured', async () => {
|
|
791
|
+
const { TronWalletService } =
|
|
792
|
+
await import('./services/tron/tron-wallet-service')
|
|
793
|
+
const mockTronService =
|
|
794
|
+
vi.mocked(TronWalletService).mock.results[0]!.value
|
|
795
|
+
mockTronService.getLiveAccount.mockResolvedValue('TFallbackTronAddr')
|
|
796
|
+
|
|
797
|
+
// Default `connector` (from the outer beforeEach) has no
|
|
798
|
+
// tronConnectorConfig, so `this.tronConnector` is null and routing must
|
|
799
|
+
// fall through to `tronService.getLiveAccount()`.
|
|
800
|
+
const result = await connector.getLiveAccount!('tron')
|
|
801
|
+
|
|
802
|
+
expect(result).toBe('TFallbackTronAddr')
|
|
803
|
+
})
|
|
804
|
+
|
|
805
|
+
it('prefers tronConnector.getLiveAccount() when a tronConnector is configured', async () => {
|
|
806
|
+
const { TronConnector } = await import('@meshconnect/uwc-tron-connector')
|
|
807
|
+
const tronConnectorLive = vi.fn().mockResolvedValue('TConnectorLiveAddr')
|
|
808
|
+
vi.mocked(TronConnector).mockImplementation(function () {
|
|
809
|
+
return { getLiveAccount: tronConnectorLive } as never
|
|
810
|
+
} as never)
|
|
811
|
+
const c = new InjectedConnector({
|
|
812
|
+
tronConnectorConfig: { enabledWallets: ['trust'] } as never
|
|
813
|
+
})
|
|
814
|
+
|
|
815
|
+
const result = await c.getLiveAccount!('tron')
|
|
816
|
+
|
|
817
|
+
expect(tronConnectorLive).toHaveBeenCalled()
|
|
818
|
+
expect(result).toBe('TConnectorLiveAddr')
|
|
819
|
+
})
|
|
820
|
+
|
|
821
|
+
it('returns tonService.getAccount() for tvm (sync, normalized to null)', async () => {
|
|
822
|
+
const { TonWalletService } =
|
|
823
|
+
await import('./services/ton/ton-wallet-service')
|
|
824
|
+
const mockTonService = vi.mocked(TonWalletService).mock.results[0]!.value
|
|
825
|
+
mockTonService.getAccount.mockReturnValue('EQLiveTonAddr')
|
|
826
|
+
|
|
827
|
+
const result = await connector.getLiveAccount!('tvm')
|
|
828
|
+
|
|
829
|
+
expect(result).toBe('EQLiveTonAddr')
|
|
830
|
+
})
|
|
831
|
+
|
|
832
|
+
it('normalizes an undefined tvm account to null', async () => {
|
|
833
|
+
const { TonWalletService } =
|
|
834
|
+
await import('./services/ton/ton-wallet-service')
|
|
835
|
+
const mockTonService = vi.mocked(TonWalletService).mock.results[0]!.value
|
|
836
|
+
mockTonService.getAccount.mockReturnValue(undefined as never)
|
|
837
|
+
|
|
838
|
+
const result = await connector.getLiveAccount!('tvm')
|
|
839
|
+
|
|
840
|
+
expect(result).toBeNull()
|
|
841
|
+
})
|
|
842
|
+
|
|
843
|
+
it('returns null for an unsupported namespace', async () => {
|
|
844
|
+
const result = await connector.getLiveAccount!('cosmos' as never)
|
|
845
|
+
|
|
846
|
+
expect(result).toBeNull()
|
|
847
|
+
})
|
|
848
|
+
|
|
849
|
+
it('never throws: returns null when the underlying service throws', async () => {
|
|
850
|
+
const { EthereumWalletService } =
|
|
851
|
+
await import('./services/ethereum/ethereum-wallet-service')
|
|
852
|
+
const mockEthService = vi.mocked(EthereumWalletService).mock.results[0]!
|
|
853
|
+
.value
|
|
854
|
+
mockEthService.getLiveAccount.mockRejectedValue(new Error('boom'))
|
|
855
|
+
|
|
856
|
+
const result = await connector.getLiveAccount!('eip155')
|
|
857
|
+
|
|
858
|
+
expect(result).toBeNull()
|
|
859
|
+
})
|
|
860
|
+
})
|
|
861
|
+
|
|
629
862
|
describe('signMessage', () => {
|
|
630
863
|
it('should throw error if provider is not provided', async () => {
|
|
631
864
|
await expect(
|
|
@@ -668,6 +901,7 @@ describe('InjectedConnector', () => {
|
|
|
668
901
|
mockConnManager.getCurrentNetworkId.mockReturnValue('eip155:1')
|
|
669
902
|
mockConnManager.getCurrentAccount.mockReturnValue('0xaddress')
|
|
670
903
|
mockEthService.getConnectedProvider.mockReturnValue(mockEthProvider)
|
|
904
|
+
mockEthService.getLiveAccount.mockResolvedValue('0xaddress')
|
|
671
905
|
mockSigService.signEthereumMessage.mockResolvedValue('0xsignature')
|
|
672
906
|
|
|
673
907
|
const result = await connector.signMessage(
|
|
@@ -681,18 +915,67 @@ describe('InjectedConnector', () => {
|
|
|
681
915
|
mockEthProvider
|
|
682
916
|
)
|
|
683
917
|
expect(result).toEqual({
|
|
684
|
-
|
|
685
|
-
|
|
918
|
+
signature: {
|
|
919
|
+
type: 'standard',
|
|
920
|
+
signature: '0xsignature'
|
|
921
|
+
},
|
|
922
|
+
address: '0xaddress'
|
|
686
923
|
})
|
|
687
924
|
})
|
|
688
925
|
|
|
926
|
+
it('claims the LIVE eip155 signer address at sign time (post-switch), not the connect-time cache', async () => {
|
|
927
|
+
const { ConnectionManager } =
|
|
928
|
+
await import('./services/connection-manager')
|
|
929
|
+
const { EthereumWalletService } =
|
|
930
|
+
await import('./services/ethereum/ethereum-wallet-service')
|
|
931
|
+
const { SignatureService } = await import('./services/signature-service')
|
|
932
|
+
|
|
933
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
934
|
+
const mockEthService = vi.mocked(EthereumWalletService).mock.results[0]
|
|
935
|
+
.value
|
|
936
|
+
const mockSigService = vi.mocked(SignatureService).mock.results[0].value
|
|
937
|
+
|
|
938
|
+
const mockEthProvider = { request: vi.fn() } as any
|
|
939
|
+
|
|
940
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('eip155:1')
|
|
941
|
+
// Connect-time cache still reports the OLD account A.
|
|
942
|
+
mockConnManager.getCurrentAccount.mockReturnValue('0xOLD_A')
|
|
943
|
+
mockEthService.getConnectedProvider.mockReturnValue(mockEthProvider)
|
|
944
|
+
// Simulate an in-wallet account switch: the live provider now reports B.
|
|
945
|
+
mockEthService.getLiveAccount.mockResolvedValue('0xNEW_B')
|
|
946
|
+
mockSigService.signEthereumMessage.mockResolvedValue('0xsignature')
|
|
947
|
+
|
|
948
|
+
const result = await connector.signMessage(
|
|
949
|
+
'test message',
|
|
950
|
+
mockEthProvider as any
|
|
951
|
+
)
|
|
952
|
+
|
|
953
|
+
// The claimed address AND the `from` used to sign must both be the LIVE
|
|
954
|
+
// account B, guaranteeing claim == from == signer.
|
|
955
|
+
expect(mockSigService.signEthereumMessage).toHaveBeenCalledWith(
|
|
956
|
+
'test message',
|
|
957
|
+
'0xNEW_B',
|
|
958
|
+
mockEthProvider
|
|
959
|
+
)
|
|
960
|
+
expect(result.address).toBe('0xNEW_B')
|
|
961
|
+
})
|
|
962
|
+
|
|
689
963
|
it('should throw error if no Ethereum address for signing', async () => {
|
|
690
964
|
const { ConnectionManager } =
|
|
691
965
|
await import('./services/connection-manager')
|
|
966
|
+
const { EthereumWalletService } =
|
|
967
|
+
await import('./services/ethereum/ethereum-wallet-service')
|
|
968
|
+
|
|
692
969
|
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
970
|
+
const mockEthService = vi.mocked(EthereumWalletService).mock.results[0]
|
|
971
|
+
.value
|
|
693
972
|
|
|
694
973
|
mockConnManager.getCurrentNetworkId.mockReturnValue('eip155:1')
|
|
695
974
|
mockConnManager.getCurrentAccount.mockReturnValue(null)
|
|
975
|
+
mockEthService.getConnectedProvider.mockReturnValue({
|
|
976
|
+
request: vi.fn()
|
|
977
|
+
} as any)
|
|
978
|
+
mockEthService.getLiveAccount.mockResolvedValue(null)
|
|
696
979
|
|
|
697
980
|
await expect(
|
|
698
981
|
connector.signMessage('test message', mockEthProvider)
|
|
@@ -733,7 +1016,9 @@ describe('InjectedConnector', () => {
|
|
|
733
1016
|
const mockSolanaAdapter = { signMessage: vi.fn() } as any
|
|
734
1017
|
|
|
735
1018
|
mockConnManager.getCurrentNetworkId.mockReturnValue('solana:mainnet')
|
|
1019
|
+
mockConnManager.getCurrentAccount.mockReturnValue('SoLaNaAddr111')
|
|
736
1020
|
mockSolService.getConnectedAdapter.mockReturnValue(mockSolanaAdapter)
|
|
1021
|
+
mockSolService.getLiveAccount.mockResolvedValue('SoLaNaAddr111')
|
|
737
1022
|
mockSigService.signSolanaMessage.mockResolvedValue('solana-signature')
|
|
738
1023
|
|
|
739
1024
|
const result = await connector.signMessage(
|
|
@@ -746,12 +1031,45 @@ describe('InjectedConnector', () => {
|
|
|
746
1031
|
mockSolanaAdapter
|
|
747
1032
|
)
|
|
748
1033
|
expect(result).toEqual({
|
|
749
|
-
|
|
750
|
-
|
|
1034
|
+
signature: {
|
|
1035
|
+
type: 'standard',
|
|
1036
|
+
signature: 'solana-signature'
|
|
1037
|
+
},
|
|
1038
|
+
address: 'SoLaNaAddr111'
|
|
751
1039
|
})
|
|
752
1040
|
})
|
|
753
1041
|
|
|
754
|
-
it('
|
|
1042
|
+
it('claims the LIVE solana signer address at sign time (post-switch), not the connect-time cache', async () => {
|
|
1043
|
+
const { ConnectionManager } =
|
|
1044
|
+
await import('./services/connection-manager')
|
|
1045
|
+
const { SolanaWalletService } =
|
|
1046
|
+
await import('./services/solana/solana-wallet-service')
|
|
1047
|
+
const { SignatureService } = await import('./services/signature-service')
|
|
1048
|
+
|
|
1049
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1050
|
+
const mockSolService =
|
|
1051
|
+
vi.mocked(SolanaWalletService).mock.results[0].value
|
|
1052
|
+
const mockSigService = vi.mocked(SignatureService).mock.results[0].value
|
|
1053
|
+
|
|
1054
|
+
const mockSolanaAdapter = { signMessage: vi.fn() } as any
|
|
1055
|
+
|
|
1056
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('solana:mainnet')
|
|
1057
|
+
// Connect-time cache still reports OLD account A.
|
|
1058
|
+
mockConnManager.getCurrentAccount.mockReturnValue('SoLaNaAddr_A_OLD')
|
|
1059
|
+
mockSolService.getConnectedAdapter.mockReturnValue(mockSolanaAdapter)
|
|
1060
|
+
// Simulate an in-wallet account switch: the live adapter now reports B.
|
|
1061
|
+
mockSolService.getLiveAccount.mockResolvedValue('SoLaNaAddr_B_NEW')
|
|
1062
|
+
mockSigService.signSolanaMessage.mockResolvedValue('solana-signature')
|
|
1063
|
+
|
|
1064
|
+
const result = await connector.signMessage(
|
|
1065
|
+
'test message',
|
|
1066
|
+
mockEthProvider
|
|
1067
|
+
)
|
|
1068
|
+
|
|
1069
|
+
expect(result.address).toBe('SoLaNaAddr_B_NEW')
|
|
1070
|
+
})
|
|
1071
|
+
|
|
1072
|
+
it('throws for solana when the live adapter exposes no signer address (never claims empty)', async () => {
|
|
755
1073
|
const { ConnectionManager } =
|
|
756
1074
|
await import('./services/connection-manager')
|
|
757
1075
|
const { SolanaWalletService } =
|
|
@@ -762,6 +1080,29 @@ describe('InjectedConnector', () => {
|
|
|
762
1080
|
vi.mocked(SolanaWalletService).mock.results[0].value
|
|
763
1081
|
|
|
764
1082
|
mockConnManager.getCurrentNetworkId.mockReturnValue('solana:mainnet')
|
|
1083
|
+
mockConnManager.getCurrentAccount.mockReturnValue(null)
|
|
1084
|
+
mockSolService.getConnectedAdapter.mockReturnValue({
|
|
1085
|
+
signMessage: vi.fn()
|
|
1086
|
+
} as any)
|
|
1087
|
+
mockSolService.getLiveAccount.mockResolvedValue(null)
|
|
1088
|
+
|
|
1089
|
+
await expect(
|
|
1090
|
+
connector.signMessage('test message', mockEthProvider)
|
|
1091
|
+
).rejects.toThrow('No active wallet address for signing')
|
|
1092
|
+
})
|
|
1093
|
+
|
|
1094
|
+
it('should throw error if no connected Solana adapter and the rebuild fails', async () => {
|
|
1095
|
+
const { ConnectionManager } =
|
|
1096
|
+
await import('./services/connection-manager')
|
|
1097
|
+
const { SolanaWalletService } =
|
|
1098
|
+
await import('./services/solana/solana-wallet-service')
|
|
1099
|
+
|
|
1100
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1101
|
+
const mockSolService =
|
|
1102
|
+
vi.mocked(SolanaWalletService).mock.results[0].value
|
|
1103
|
+
|
|
1104
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('solana:mainnet')
|
|
1105
|
+
mockConnManager.getCurrentAccount.mockReturnValue('SoLaNaAddr111')
|
|
765
1106
|
mockSolService.getConnectedAdapter.mockReturnValue(null)
|
|
766
1107
|
|
|
767
1108
|
await expect(
|
|
@@ -769,6 +1110,189 @@ describe('InjectedConnector', () => {
|
|
|
769
1110
|
).rejects.toThrow('No connected Solana adapter')
|
|
770
1111
|
})
|
|
771
1112
|
|
|
1113
|
+
it('re-binds the Solana adapter at sign time when it was cleared by a re-auth teardown (Phantom account-switch regression)', async () => {
|
|
1114
|
+
const { ConnectionManager } =
|
|
1115
|
+
await import('./services/connection-manager')
|
|
1116
|
+
const { SolanaWalletService } =
|
|
1117
|
+
await import('./services/solana/solana-wallet-service')
|
|
1118
|
+
const { SignatureService } = await import('./services/signature-service')
|
|
1119
|
+
|
|
1120
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1121
|
+
const mockSolService =
|
|
1122
|
+
vi.mocked(SolanaWalletService).mock.results[0].value
|
|
1123
|
+
const mockSigService = vi.mocked(SignatureService).mock.results[0].value
|
|
1124
|
+
|
|
1125
|
+
// `getConnectedAdapter()` is null (cleared by a sibling-namespace
|
|
1126
|
+
// re-auth teardown after a Phantom account switch); `ensureConnectedAdapter`
|
|
1127
|
+
// silently rebinds it from the remembered wallet uuid.
|
|
1128
|
+
const rebuiltAdapter = { signMessage: vi.fn() } as any
|
|
1129
|
+
|
|
1130
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('solana:mainnet')
|
|
1131
|
+
mockConnManager.getCurrentAccount.mockReturnValue('SoLaNaAddr_OLD')
|
|
1132
|
+
mockSolService.getConnectedAdapter.mockReturnValue(rebuiltAdapter)
|
|
1133
|
+
mockSolService.getLiveAccount.mockResolvedValue('SoLaNaAddr_NEW')
|
|
1134
|
+
mockSigService.signSolanaMessage.mockResolvedValue('solana-signature')
|
|
1135
|
+
|
|
1136
|
+
const result = await connector.signMessage(
|
|
1137
|
+
'test message',
|
|
1138
|
+
mockEthProvider
|
|
1139
|
+
)
|
|
1140
|
+
|
|
1141
|
+
expect(mockSigService.signSolanaMessage).toHaveBeenCalledWith(
|
|
1142
|
+
'test message',
|
|
1143
|
+
rebuiltAdapter
|
|
1144
|
+
)
|
|
1145
|
+
expect(result.address).toBe('SoLaNaAddr_NEW')
|
|
1146
|
+
})
|
|
1147
|
+
|
|
1148
|
+
it('claims the LIVE tron signer address as of sign completion, not a pre-sign read (closes the TOCTOU window)', async () => {
|
|
1149
|
+
const { ConnectionManager } =
|
|
1150
|
+
await import('./services/connection-manager')
|
|
1151
|
+
const { TronWalletService } =
|
|
1152
|
+
await import('./services/tron/tron-wallet-service')
|
|
1153
|
+
const { SignatureService } = await import('./services/signature-service')
|
|
1154
|
+
|
|
1155
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1156
|
+
const mockTronService = vi.mocked(TronWalletService).mock.results[0].value
|
|
1157
|
+
const mockSigService = vi.mocked(SignatureService).mock.results[0].value
|
|
1158
|
+
|
|
1159
|
+
const mockTronProvider = { tronWeb: {} } as any
|
|
1160
|
+
|
|
1161
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('tron:0x2b6653dc')
|
|
1162
|
+
// Connect-time cache still reports OLD account A.
|
|
1163
|
+
mockConnManager.getCurrentAccount.mockReturnValue('TTronAddr_A_OLD')
|
|
1164
|
+
mockTronService.getConnectedProvider.mockReturnValue(mockTronProvider)
|
|
1165
|
+
|
|
1166
|
+
// Simulate an account switch landing DURING the signTronMessage await:
|
|
1167
|
+
// the live address is A before signing starts, and B by the time
|
|
1168
|
+
// signing resolves. getLiveAccount is called twice (pre- and
|
|
1169
|
+
// post-sign) — return A on the first call, B on every call after.
|
|
1170
|
+
mockTronService.getLiveAccount
|
|
1171
|
+
.mockResolvedValueOnce('TTronAddr_A_OLD')
|
|
1172
|
+
.mockResolvedValue('TTronAddr_B_NEW')
|
|
1173
|
+
|
|
1174
|
+
// signTronMessage itself is where the switch happens: resolve it only
|
|
1175
|
+
// after the account has "changed", so a pre-sign address read would
|
|
1176
|
+
// still see A while a post-sign read sees B.
|
|
1177
|
+
mockSigService.signTronMessage.mockResolvedValue('tron-signature')
|
|
1178
|
+
|
|
1179
|
+
const result = await connector.signMessage(
|
|
1180
|
+
'test message',
|
|
1181
|
+
mockEthProvider
|
|
1182
|
+
)
|
|
1183
|
+
|
|
1184
|
+
// The account that actually signed (as of sign completion) is B, not
|
|
1185
|
+
// the pre-sign snapshot A.
|
|
1186
|
+
expect(result.address).toBe('TTronAddr_B_NEW')
|
|
1187
|
+
})
|
|
1188
|
+
|
|
1189
|
+
it('throws for tron when the live provider exposes no signer address (never claims empty)', async () => {
|
|
1190
|
+
const { ConnectionManager } =
|
|
1191
|
+
await import('./services/connection-manager')
|
|
1192
|
+
const { TronWalletService } =
|
|
1193
|
+
await import('./services/tron/tron-wallet-service')
|
|
1194
|
+
|
|
1195
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1196
|
+
const mockTronService = vi.mocked(TronWalletService).mock.results[0].value
|
|
1197
|
+
|
|
1198
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('tron:0x2b6653dc')
|
|
1199
|
+
mockConnManager.getCurrentAccount.mockReturnValue(null)
|
|
1200
|
+
mockTronService.getConnectedProvider.mockReturnValue({
|
|
1201
|
+
tronWeb: {}
|
|
1202
|
+
} as any)
|
|
1203
|
+
mockTronService.getLiveAccount.mockResolvedValue(null)
|
|
1204
|
+
|
|
1205
|
+
await expect(
|
|
1206
|
+
connector.signMessage('test message', mockEthProvider)
|
|
1207
|
+
).rejects.toThrow('No active wallet address for signing')
|
|
1208
|
+
})
|
|
1209
|
+
|
|
1210
|
+
it('should sign a TON (tvm) message and return the ton service address', async () => {
|
|
1211
|
+
const { ConnectionManager } =
|
|
1212
|
+
await import('./services/connection-manager')
|
|
1213
|
+
const { TonWalletService } =
|
|
1214
|
+
await import('./services/ton/ton-wallet-service')
|
|
1215
|
+
|
|
1216
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1217
|
+
const mockTonService = vi.mocked(TonWalletService).mock.results[0].value
|
|
1218
|
+
|
|
1219
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('tvm:-239')
|
|
1220
|
+
// For tvm the signer address comes from the TON service, not from the
|
|
1221
|
+
// connection manager's active account.
|
|
1222
|
+
mockTonService.getAccount.mockReturnValue('EQTonAddress123')
|
|
1223
|
+
mockTonService.signMessage.mockResolvedValue({
|
|
1224
|
+
type: 'tvm',
|
|
1225
|
+
signature: 'ton-sig',
|
|
1226
|
+
timestamp: 1234,
|
|
1227
|
+
domain: 'example.com',
|
|
1228
|
+
payload: { type: 'text', text: 'test message' }
|
|
1229
|
+
})
|
|
1230
|
+
|
|
1231
|
+
const result = await connector.signMessage(
|
|
1232
|
+
'test message',
|
|
1233
|
+
mockEthProvider
|
|
1234
|
+
)
|
|
1235
|
+
|
|
1236
|
+
expect(mockTonService.signMessage).toHaveBeenCalledWith('test message')
|
|
1237
|
+
expect(result).toEqual({
|
|
1238
|
+
signature: {
|
|
1239
|
+
type: 'tvm',
|
|
1240
|
+
signature: 'ton-sig',
|
|
1241
|
+
timestamp: 1234,
|
|
1242
|
+
domain: 'example.com',
|
|
1243
|
+
payload: { type: 'text', text: 'test message' }
|
|
1244
|
+
},
|
|
1245
|
+
address: 'EQTonAddress123'
|
|
1246
|
+
})
|
|
1247
|
+
})
|
|
1248
|
+
|
|
1249
|
+
it('claims the LIVE tvm signer address at sign time (post-switch), not a stale one', async () => {
|
|
1250
|
+
const { ConnectionManager } =
|
|
1251
|
+
await import('./services/connection-manager')
|
|
1252
|
+
const { TonWalletService } =
|
|
1253
|
+
await import('./services/ton/ton-wallet-service')
|
|
1254
|
+
|
|
1255
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1256
|
+
const mockTonService = vi.mocked(TonWalletService).mock.results[0].value
|
|
1257
|
+
|
|
1258
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('tvm:-239')
|
|
1259
|
+
// Simulate an in-wallet account switch: getAccount() now reports the NEW
|
|
1260
|
+
// signer address B. The connector must read getAccount() at sign time and
|
|
1261
|
+
// claim B, not a previously-cached A.
|
|
1262
|
+
mockTonService.getAccount.mockReturnValue('EQTonAddress_B_NEW')
|
|
1263
|
+
mockTonService.signMessage.mockResolvedValue({
|
|
1264
|
+
type: 'tvm',
|
|
1265
|
+
signature: 'ton-sig-b',
|
|
1266
|
+
timestamp: 5678,
|
|
1267
|
+
domain: 'example.com',
|
|
1268
|
+
payload: { type: 'text', text: 'test message' }
|
|
1269
|
+
})
|
|
1270
|
+
|
|
1271
|
+
const result = await connector.signMessage(
|
|
1272
|
+
'test message',
|
|
1273
|
+
mockEthProvider
|
|
1274
|
+
)
|
|
1275
|
+
|
|
1276
|
+
expect(result.address).toBe('EQTonAddress_B_NEW')
|
|
1277
|
+
})
|
|
1278
|
+
|
|
1279
|
+
it('throws for tvm when there is no active signer address (never claims empty)', async () => {
|
|
1280
|
+
const { ConnectionManager } =
|
|
1281
|
+
await import('./services/connection-manager')
|
|
1282
|
+
const { TonWalletService } =
|
|
1283
|
+
await import('./services/ton/ton-wallet-service')
|
|
1284
|
+
|
|
1285
|
+
const mockConnManager = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1286
|
+
const mockTonService = vi.mocked(TonWalletService).mock.results[0].value
|
|
1287
|
+
|
|
1288
|
+
mockConnManager.getCurrentNetworkId.mockReturnValue('tvm:-239')
|
|
1289
|
+
mockTonService.getAccount.mockReturnValue(null)
|
|
1290
|
+
|
|
1291
|
+
await expect(
|
|
1292
|
+
connector.signMessage('test message', mockEthProvider)
|
|
1293
|
+
).rejects.toThrow('No active wallet address for signing')
|
|
1294
|
+
})
|
|
1295
|
+
|
|
772
1296
|
it('should throw error for unsupported namespace', async () => {
|
|
773
1297
|
const { ConnectionManager } =
|
|
774
1298
|
await import('./services/connection-manager')
|
|
@@ -1138,17 +1662,6 @@ describe('InjectedConnector', () => {
|
|
|
1138
1662
|
// surface (e.g. a Solana-primary Phantom session with an authorized EVM side
|
|
1139
1663
|
// sets the Ethereum service's connectedProvider too), so lifecycle follows
|
|
1140
1664
|
// context.namespace, never a fixed service preference.
|
|
1141
|
-
const getMocks = async () => {
|
|
1142
|
-
const { EthereumWalletService } =
|
|
1143
|
-
await import('./services/ethereum/ethereum-wallet-service')
|
|
1144
|
-
const { SolanaWalletService } =
|
|
1145
|
-
await import('./services/solana/solana-wallet-service')
|
|
1146
|
-
return {
|
|
1147
|
-
ethService: vi.mocked(EthereumWalletService).mock.results[0].value,
|
|
1148
|
-
solService: vi.mocked(SolanaWalletService).mock.results[0].value
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
1665
|
it('routes a solana session to the Solana service even when the EVM side is live', async () => {
|
|
1153
1666
|
const { ethService, solService } = await getMocks()
|
|
1154
1667
|
const ethUnsub = vi.fn()
|
|
@@ -1223,4 +1736,49 @@ describe('InjectedConnector', () => {
|
|
|
1223
1736
|
expect(solService.onLifecycleEvent).not.toHaveBeenCalled()
|
|
1224
1737
|
})
|
|
1225
1738
|
})
|
|
1739
|
+
|
|
1740
|
+
describe('onAccountsChanged routing (eip155 / solana)', () => {
|
|
1741
|
+
it('routes an eip155 session to ethereumService.onAccountsChanged', async () => {
|
|
1742
|
+
const { ethService, solService } = await getMocks()
|
|
1743
|
+
const ethUnsub = vi.fn()
|
|
1744
|
+
ethService.onAccountsChanged.mockReturnValue(ethUnsub)
|
|
1745
|
+
|
|
1746
|
+
const listener = vi.fn()
|
|
1747
|
+
const teardown = connector.onAccountsChanged!(listener, {
|
|
1748
|
+
connectionMode: 'injected',
|
|
1749
|
+
namespace: 'eip155'
|
|
1750
|
+
})
|
|
1751
|
+
|
|
1752
|
+
expect(ethService.onAccountsChanged).toHaveBeenCalledWith(listener)
|
|
1753
|
+
expect(solService.onAccountsChanged).not.toHaveBeenCalled()
|
|
1754
|
+
expect(teardown).toBe(ethUnsub)
|
|
1755
|
+
})
|
|
1756
|
+
|
|
1757
|
+
it('routes a solana session to solanaService.onAccountsChanged', async () => {
|
|
1758
|
+
const { ethService, solService } = await getMocks()
|
|
1759
|
+
const solUnsub = vi.fn()
|
|
1760
|
+
solService.onAccountsChanged.mockReturnValue(solUnsub)
|
|
1761
|
+
|
|
1762
|
+
const listener = vi.fn()
|
|
1763
|
+
const teardown = connector.onAccountsChanged!(listener, {
|
|
1764
|
+
connectionMode: 'injected',
|
|
1765
|
+
namespace: 'solana'
|
|
1766
|
+
})
|
|
1767
|
+
|
|
1768
|
+
expect(solService.onAccountsChanged).toHaveBeenCalledWith(listener)
|
|
1769
|
+
expect(ethService.onAccountsChanged).not.toHaveBeenCalled()
|
|
1770
|
+
expect(teardown).toBe(solUnsub)
|
|
1771
|
+
})
|
|
1772
|
+
|
|
1773
|
+
it('returns undefined for a tron session when no tronConnectorConfig was supplied', async () => {
|
|
1774
|
+
const { ethService } = await getMocks()
|
|
1775
|
+
expect(
|
|
1776
|
+
connector.onAccountsChanged!(vi.fn(), {
|
|
1777
|
+
connectionMode: 'injected',
|
|
1778
|
+
namespace: 'tron'
|
|
1779
|
+
})
|
|
1780
|
+
).toBeUndefined()
|
|
1781
|
+
expect(ethService.onAccountsChanged).not.toHaveBeenCalled()
|
|
1782
|
+
})
|
|
1783
|
+
})
|
|
1226
1784
|
})
|