@meshconnect/uwc-injected-connector 0.27.5 → 0.28.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 (30) hide show
  1. package/dist/injected-connector.d.ts.map +1 -1
  2. package/dist/injected-connector.js +62 -47
  3. package/dist/injected-connector.js.map +1 -1
  4. package/dist/services/connection-manager.d.ts +17 -13
  5. package/dist/services/connection-manager.d.ts.map +1 -1
  6. package/dist/services/connection-manager.js +93 -65
  7. package/dist/services/connection-manager.js.map +1 -1
  8. package/dist/services/solana/solana-wallet-service.d.ts +2 -0
  9. package/dist/services/solana/solana-wallet-service.d.ts.map +1 -1
  10. package/dist/services/solana/solana-wallet-service.js +1 -0
  11. package/dist/services/solana/solana-wallet-service.js.map +1 -1
  12. package/dist/services/ton/ton-wallet-service.d.ts +7 -1
  13. package/dist/services/ton/ton-wallet-service.d.ts.map +1 -1
  14. package/dist/services/ton/ton-wallet-service.js +21 -1
  15. package/dist/services/ton/ton-wallet-service.js.map +1 -1
  16. package/dist/utils/secondary-prompt-gap.d.ts +47 -0
  17. package/dist/utils/secondary-prompt-gap.d.ts.map +1 -0
  18. package/dist/utils/secondary-prompt-gap.js +65 -0
  19. package/dist/utils/secondary-prompt-gap.js.map +1 -0
  20. package/package.json +6 -6
  21. package/src/injected-connector.test.ts +374 -7
  22. package/src/injected-connector.ts +100 -55
  23. package/src/services/connection-manager.test.ts +841 -171
  24. package/src/services/connection-manager.ts +157 -86
  25. package/src/services/solana/solana-wallet-service.check-existing.test.ts +24 -0
  26. package/src/services/solana/solana-wallet-service.ts +6 -1
  27. package/src/services/ton/ton-wallet-service.test.ts +69 -0
  28. package/src/services/ton/ton-wallet-service.ts +22 -1
  29. package/src/utils/secondary-prompt-gap.test.ts +288 -0
  30. package/src/utils/secondary-prompt-gap.ts +95 -0
@@ -60,6 +60,7 @@ function makeSolanaService() {
60
60
  getLiveAccount: vi.fn().mockResolvedValue(null),
61
61
  findWalletByUuid: vi.fn().mockReturnValue(null),
62
62
  checkExistingConnection: vi.fn().mockResolvedValue(null),
63
+ checkExistingConnectionSilently: vi.fn().mockResolvedValue(null),
63
64
  connect: vi.fn().mockResolvedValue('SolAddr1'),
64
65
  setConnectionState: vi.fn(),
65
66
  buildAvailableAddresses: vi
@@ -857,7 +858,7 @@ describe('ConnectionManager — legacy-injectedId Solana gather gate (ONC-3536 +
857
858
  expect(solanaService.checkExistingConnection).toHaveBeenCalledWith(
858
859
  expect.anything(),
859
860
  'cb-solana',
860
- { allowSilentReconnect: true }
861
+ expect.objectContaining({ allowSilentReconnect: true })
861
862
  )
862
863
  expect(solanaService.connect).toHaveBeenCalled()
863
864
  const namespaces = result.availableAddresses.map(
@@ -911,7 +912,7 @@ describe('ConnectionManager — legacy-injectedId Solana gather gate (ONC-3536 +
911
912
  expect(solanaService.checkExistingConnection).toHaveBeenCalledWith(
912
913
  expect.anything(),
913
914
  'cb-solana',
914
- { allowSilentReconnect: false }
915
+ expect.objectContaining({ allowSilentReconnect: false })
915
916
  )
916
917
  expect(onGathered).not.toHaveBeenCalled()
917
918
  expect(onSkip).toHaveBeenCalledWith({
@@ -981,7 +982,7 @@ describe('ConnectionManager — legacy-injectedId Solana gather gate (ONC-3536 +
981
982
  expect(solanaService.checkExistingConnection).toHaveBeenCalledWith(
982
983
  expect.anything(),
983
984
  'ph-solana',
984
- { allowSilentReconnect: true }
985
+ expect.objectContaining({ allowSilentReconnect: true })
985
986
  )
986
987
  expect(
987
988
  result.availableAddresses.map(a => a.networkId.split(':')[0])
@@ -1004,7 +1005,7 @@ describe('ConnectionManager — legacy-injectedId Solana gather gate (ONC-3536 +
1004
1005
  expect(solanaService.checkExistingConnection).toHaveBeenCalledWith(
1005
1006
  expect.anything(),
1006
1007
  'cb-solana',
1007
- { allowSilentReconnect: true }
1008
+ expect.objectContaining({ allowSilentReconnect: true })
1008
1009
  )
1009
1010
  expect(onSkip).not.toHaveBeenCalled()
1010
1011
  expect(onGathered).toHaveBeenCalledWith({
@@ -2108,6 +2109,678 @@ describe('ConnectionManager.checkExistingConnections — silentOnly', () => {
2108
2109
  })
2109
2110
  })
2110
2111
 
2112
+ // A not-yet-authorized target goes straight to its prompting connect. Gathering
2113
+ // first held the target's wallet prompt behind up to one
2114
+ // `secondaryGatherTimeoutMs` per namespace (a 15s TON gather on Trust iOS).
2115
+ describe('ConnectionManager.checkExistingConnections — target not yet authorized', () => {
2116
+ let ethService: ReturnType<typeof makeEthService>
2117
+ let solanaService: ReturnType<typeof makeSolanaService>
2118
+ let tronService: ReturnType<typeof makeTronService>
2119
+ let tonService: ReturnType<typeof makeTonService>
2120
+ let bitcoinService: ReturnType<typeof makeBitcoinService>
2121
+ let manager: ConnectionManager
2122
+
2123
+ beforeEach(() => {
2124
+ ethService = makeEthService()
2125
+ solanaService = makeSolanaService()
2126
+ tronService = makeTronService()
2127
+ tonService = makeTonService()
2128
+ bitcoinService = makeBitcoinService()
2129
+ manager = new ConnectionManager(
2130
+ ethService as any,
2131
+ solanaService as any,
2132
+ tronService as any,
2133
+ tonService as any,
2134
+ bitcoinService as any
2135
+ )
2136
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
2137
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
2138
+ tronService.findWalletByUuid.mockReturnValue({ provider: {} })
2139
+ })
2140
+
2141
+ const multiChainProvider = () =>
2142
+ makeProvider({
2143
+ eip155: { detectedWallet: { uuid: 'mm-eip155' } },
2144
+ solana: { detectedWallet: { uuid: 'mm-solana' } },
2145
+ tron: { detectedWallet: { uuid: 'mm-tron' } },
2146
+ tvm: { detectedWallet: { jsBridgeKey: 'mmton' } }
2147
+ })
2148
+
2149
+ it('skips every secondary gather when the EVM target has no existing connection', async () => {
2150
+ ethService.checkExistingConnection.mockResolvedValue([])
2151
+
2152
+ const result = await manager.checkExistingConnections(
2153
+ 'eip155',
2154
+ multiChainProvider()
2155
+ )
2156
+
2157
+ expect(result.address).toBeNull()
2158
+ expect(result.availableAddresses).toEqual([])
2159
+ expect(ethService.checkExistingConnection).toHaveBeenCalledTimes(1)
2160
+ expect(solanaService.checkExistingConnection).not.toHaveBeenCalled()
2161
+ expect(solanaService.connect).not.toHaveBeenCalled()
2162
+ expect(tronService.checkExistingConnection).not.toHaveBeenCalled()
2163
+ expect(tonService.checkExistingConnection).not.toHaveBeenCalled()
2164
+ expect(tonService.connect).not.toHaveBeenCalled()
2165
+ expect(bitcoinService.checkExistingConnection).not.toHaveBeenCalled()
2166
+ })
2167
+
2168
+ // MetaMask grants every chain with its EVM approval, and only Bitcoin with a
2169
+ // Bitcoin one: gathering after a Bitcoin-first connect would ask twice.
2170
+ it('keeps the gathers before a non-EVM target that has no existing connection', async () => {
2171
+ solanaService.checkExistingConnection.mockResolvedValue(null)
2172
+ ethService.checkExistingConnection.mockResolvedValue([])
2173
+
2174
+ const result = await manager.checkExistingConnections(
2175
+ 'solana',
2176
+ multiChainProvider()
2177
+ )
2178
+
2179
+ expect(result.address).toBeNull()
2180
+ expect(ethService.connect).toHaveBeenCalledTimes(1)
2181
+ expect(tonService.checkExistingConnection).toHaveBeenCalled()
2182
+ })
2183
+
2184
+ it('still gathers the other namespaces once the target is authorized', async () => {
2185
+ ethService.checkExistingConnection.mockResolvedValue(['0xABC'])
2186
+ solanaService.checkExistingConnection.mockResolvedValue('SolAddr9')
2187
+
2188
+ const result = await manager.checkExistingConnections(
2189
+ 'eip155',
2190
+ multiChainProvider()
2191
+ )
2192
+
2193
+ expect(result.address).toBe('0xABC')
2194
+ expect(result.availableAddresses).toContainEqual({
2195
+ address: 'SolAddr9',
2196
+ networkId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
2197
+ })
2198
+ expect(tronService.checkExistingConnection).toHaveBeenCalled()
2199
+ expect(tonService.checkExistingConnection).toHaveBeenCalled()
2200
+ })
2201
+ })
2202
+
2203
+ // `secondaryPrompted` tells the injected connector whether a non-EVM target's
2204
+ // own prompt follows a gather prompt (and so waits the gap) or only reads of
2205
+ // an existing authorization (and so goes at once).
2206
+ describe('ConnectionManager.checkExistingConnections — secondaryPrompted', () => {
2207
+ let ethService: ReturnType<typeof makeEthService>
2208
+ let solanaService: ReturnType<typeof makeSolanaService>
2209
+ let tronService: ReturnType<typeof makeTronService>
2210
+ let tonService: ReturnType<typeof makeTonService>
2211
+ let bitcoinService: ReturnType<typeof makeBitcoinService>
2212
+ let manager: ConnectionManager
2213
+
2214
+ const build = (options?: { secondaryGatherTimeoutMs?: number }) =>
2215
+ new ConnectionManager(
2216
+ ethService as any,
2217
+ solanaService as any,
2218
+ tronService as any,
2219
+ tonService as any,
2220
+ bitcoinService as any,
2221
+ options
2222
+ )
2223
+
2224
+ beforeEach(() => {
2225
+ ethService = makeEthService()
2226
+ solanaService = makeSolanaService()
2227
+ tronService = makeTronService()
2228
+ tonService = makeTonService()
2229
+ bitcoinService = makeBitcoinService()
2230
+ manager = build()
2231
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
2232
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
2233
+ tronService.findWalletByUuid.mockReturnValue({ provider: {} })
2234
+ // As the service does with no stored session: the hook, then the
2235
+ // handshake (the request that can prompt).
2236
+ tonService.connect.mockImplementation(
2237
+ async (_jsBridgeKey: string, beforeHandshake?: () => Promise<void>) => {
2238
+ await beforeHandshake?.()
2239
+ return { address: 'TonAddr1' }
2240
+ }
2241
+ )
2242
+ })
2243
+
2244
+ afterEach(() => {
2245
+ vi.useRealTimers()
2246
+ })
2247
+
2248
+ const provider = () =>
2249
+ makeProvider({
2250
+ eip155: { detectedWallet: { uuid: 'w-eip155' } },
2251
+ solana: { detectedWallet: { uuid: 'w-solana' } },
2252
+ tron: { detectedWallet: { uuid: 'w-tron' } },
2253
+ tvm: { detectedWallet: { jsBridgeKey: 'wTon' } }
2254
+ })
2255
+
2256
+ /** Every secondary namespace already authorized: each leg only reads. */
2257
+ const authorizeEverySecondary = () => {
2258
+ ethService.checkExistingConnection.mockResolvedValue(['0xABC'])
2259
+ tronService.checkExistingConnection.mockResolvedValue('TronAddr9')
2260
+ tonService.checkExistingConnection.mockResolvedValue('TonAddr9')
2261
+ }
2262
+
2263
+ it('is true when a gather before a Solana target sent a prompting request', async () => {
2264
+ authorizeEverySecondary()
2265
+ ethService.checkExistingConnection.mockResolvedValue(null)
2266
+
2267
+ const result = await manager.checkExistingConnections('solana', provider())
2268
+
2269
+ expect(ethService.connect).toHaveBeenCalledTimes(1)
2270
+ expect(result.secondaryPrompted).toBe(true)
2271
+ })
2272
+
2273
+ it('is false when every gather only read an existing authorization', async () => {
2274
+ authorizeEverySecondary()
2275
+
2276
+ const result = await manager.checkExistingConnections('solana', provider())
2277
+
2278
+ expect(ethService.connect).not.toHaveBeenCalled()
2279
+ expect(tronService.connect).not.toHaveBeenCalled()
2280
+ expect(tonService.connect).not.toHaveBeenCalled()
2281
+ expect(result.secondaryPrompted).toBe(false)
2282
+ expect(result.availableAddresses.map(a => a.networkId)).toEqual([
2283
+ 'eip155:1',
2284
+ 'tron:0x2b6653dc',
2285
+ 'tvm:-239'
2286
+ ])
2287
+ })
2288
+
2289
+ it('is false for a wallet with no other namespace to gather', async () => {
2290
+ const result = await manager.checkExistingConnections(
2291
+ 'solana',
2292
+ makeProvider({ solana: { detectedWallet: { uuid: 'w-solana' } } })
2293
+ )
2294
+
2295
+ expect(result.secondaryPrompted).toBe(false)
2296
+ })
2297
+
2298
+ it('stays true when the legs after the prompting one only read', async () => {
2299
+ authorizeEverySecondary()
2300
+ ethService.checkExistingConnection.mockResolvedValue(null)
2301
+
2302
+ const result = await manager.checkExistingConnections('solana', provider())
2303
+
2304
+ // EVM prompted first; Tron and TON read afterwards.
2305
+ expect(tronService.connect).not.toHaveBeenCalled()
2306
+ expect(tonService.connect).not.toHaveBeenCalled()
2307
+ expect(result.secondaryPrompted).toBe(true)
2308
+ })
2309
+
2310
+ it('is true when only the last leg prompted', async () => {
2311
+ authorizeEverySecondary()
2312
+ tonService.checkExistingConnection.mockResolvedValue(null)
2313
+
2314
+ const result = await manager.checkExistingConnections('solana', provider())
2315
+
2316
+ expect(ethService.connect).not.toHaveBeenCalled()
2317
+ expect(tonService.connect).toHaveBeenCalledTimes(1)
2318
+ expect(result.secondaryPrompted).toBe(true)
2319
+ })
2320
+
2321
+ it('is true when the prompted leg was declined', async () => {
2322
+ authorizeEverySecondary()
2323
+ ethService.checkExistingConnection.mockResolvedValue(null)
2324
+ ethService.connect.mockRejectedValue(
2325
+ Object.assign(new Error('User rejected the request.'), { code: 4001 })
2326
+ )
2327
+
2328
+ const result = await manager.checkExistingConnections('solana', provider())
2329
+
2330
+ expect(result.secondaryPrompted).toBe(true)
2331
+ expect(result.availableAddresses.map(a => a.networkId)).not.toContain(
2332
+ 'eip155:1'
2333
+ )
2334
+ })
2335
+
2336
+ it('is true when the prompted leg was never answered before its deadline', async () => {
2337
+ vi.useFakeTimers()
2338
+ manager = build({ secondaryGatherTimeoutMs: 1000 })
2339
+ authorizeEverySecondary()
2340
+ ethService.checkExistingConnection.mockResolvedValue(null)
2341
+ ethService.connect.mockReturnValue(new Promise(() => {}))
2342
+
2343
+ const pending = manager.checkExistingConnections('solana', provider())
2344
+ await vi.advanceTimersByTimeAsync(1000)
2345
+
2346
+ await expect(pending).resolves.toMatchObject({ secondaryPrompted: true })
2347
+ })
2348
+
2349
+ it('is false when the deadline passed while the leg was still waiting out its gap', async () => {
2350
+ vi.useFakeTimers()
2351
+ manager = build({ secondaryGatherTimeoutMs: 1000 })
2352
+ ethService.checkExistingConnection.mockResolvedValue(null)
2353
+
2354
+ const pending = manager.checkExistingConnections(
2355
+ 'solana',
2356
+ makeProvider({
2357
+ eip155: { detectedWallet: { uuid: 'w-eip155' } },
2358
+ solana: { detectedWallet: { uuid: 'w-solana' } }
2359
+ }),
2360
+ undefined,
2361
+ { secondaryPromptGapMs: 1500 }
2362
+ )
2363
+ await vi.advanceTimersByTimeAsync(5000)
2364
+
2365
+ await expect(pending).resolves.toMatchObject({ secondaryPrompted: false })
2366
+ expect(ethService.connect).not.toHaveBeenCalled()
2367
+ })
2368
+
2369
+ it('does not depend on the gap: true at a gap of 0', async () => {
2370
+ authorizeEverySecondary()
2371
+ ethService.checkExistingConnection.mockResolvedValue(null)
2372
+
2373
+ const result = await manager.checkExistingConnections(
2374
+ 'solana',
2375
+ provider(),
2376
+ undefined,
2377
+ { secondaryPromptGapMs: 0 }
2378
+ )
2379
+
2380
+ expect(result.secondaryPrompted).toBe(true)
2381
+ })
2382
+
2383
+ it('is false for an EVM target with no existing connection (its gathers are skipped)', async () => {
2384
+ const result = await manager.checkExistingConnections('eip155', provider())
2385
+
2386
+ expect(solanaService.connect).not.toHaveBeenCalled()
2387
+ expect(result.secondaryPrompted).toBe(false)
2388
+ })
2389
+
2390
+ it('is false under silentOnly, which never prompts', async () => {
2391
+ solanaService.checkExistingConnectionSilently.mockResolvedValue('SolAddr1')
2392
+
2393
+ const result = await manager.checkExistingConnections(
2394
+ 'solana',
2395
+ provider(),
2396
+ undefined,
2397
+ { silentOnly: true }
2398
+ )
2399
+
2400
+ expect(result.address).toBe('SolAddr1')
2401
+ expect(ethService.connect).not.toHaveBeenCalled()
2402
+ expect(result.secondaryPrompted).toBe(false)
2403
+ })
2404
+
2405
+ it.each(['tron', 'tvm', 'bip122'] as const)(
2406
+ 'is true for a %s target when the EVM gather prompted',
2407
+ async target => {
2408
+ ethService.checkExistingConnection.mockResolvedValue(null)
2409
+
2410
+ const result = await manager.checkExistingConnections(
2411
+ target,
2412
+ makeProvider({
2413
+ eip155: { detectedWallet: { uuid: 'w-eip155' } },
2414
+ tron: { detectedWallet: { uuid: 'w-tron' } },
2415
+ tvm: { detectedWallet: { jsBridgeKey: 'wTon' } },
2416
+ bip122: { detectedWallet: { uuid: 'w-bip122' } }
2417
+ })
2418
+ )
2419
+
2420
+ expect(ethService.connect).toHaveBeenCalledTimes(1)
2421
+ expect(result.address).toBeNull()
2422
+ expect(result.secondaryPrompted).toBe(true)
2423
+ }
2424
+ )
2425
+ })
2426
+
2427
+ // secondaryPromptGapMs: every secondary namespace still prompts, each after a
2428
+ // pause. Trust iOS left a prompt unanswered when it arrived while the previous
2429
+ // one was still closing, and answered it a second later.
2430
+ describe('ConnectionManager — secondaryPromptGapMs', () => {
2431
+ let ethService: ReturnType<typeof makeEthService>
2432
+ let solanaService: ReturnType<typeof makeSolanaService>
2433
+ let tronService: ReturnType<typeof makeTronService>
2434
+ let tonService: ReturnType<typeof makeTonService>
2435
+ let bitcoinService: ReturnType<typeof makeBitcoinService>
2436
+ let manager: ConnectionManager
2437
+ // The TON service's fresh handshake (the request that can prompt).
2438
+ let tonHandshake: ReturnType<typeof vi.fn>
2439
+
2440
+ beforeEach(() => {
2441
+ vi.useFakeTimers()
2442
+ ethService = makeEthService()
2443
+ solanaService = makeSolanaService()
2444
+ tronService = makeTronService()
2445
+ tonService = makeTonService()
2446
+ bitcoinService = makeBitcoinService()
2447
+ manager = new ConnectionManager(
2448
+ ethService as any,
2449
+ solanaService as any,
2450
+ tronService as any,
2451
+ tonService as any,
2452
+ bitcoinService as any
2453
+ )
2454
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
2455
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
2456
+ tronService.findWalletByUuid.mockReturnValue({ provider: {} })
2457
+ tonHandshake = vi.fn().mockResolvedValue({ address: 'TonAddr1' })
2458
+ // As the service does with no stored session: the caller's hook, then
2459
+ // the handshake.
2460
+ tonService.connect.mockImplementation(
2461
+ async (_jsBridgeKey: string, beforeHandshake?: () => Promise<void>) => {
2462
+ await beforeHandshake?.()
2463
+ return tonHandshake()
2464
+ }
2465
+ )
2466
+ })
2467
+
2468
+ afterEach(() => {
2469
+ vi.useRealTimers()
2470
+ })
2471
+
2472
+ const provider = () =>
2473
+ makeProvider({
2474
+ eip155: { detectedWallet: { uuid: 'trust-eip155' } },
2475
+ solana: { detectedWallet: { uuid: 'trust-solana' } },
2476
+ tron: { detectedWallet: { uuid: 'trust-tron' } },
2477
+ tvm: { detectedWallet: { jsBridgeKey: 'trustwalletTon' } }
2478
+ })
2479
+
2480
+ it('waits the gap before each secondary prompt, and gathers every namespace', async () => {
2481
+ const pending = manager.connectEthereum(
2482
+ makeNetwork('eip155:1', 'eip155'),
2483
+ provider(),
2484
+ 1500
2485
+ )
2486
+
2487
+ // The existing-grant read goes out at once; the prompt waits.
2488
+ await vi.advanceTimersByTimeAsync(0)
2489
+ expect(solanaService.checkExistingConnection).toHaveBeenCalledTimes(1)
2490
+ expect(solanaService.connect).not.toHaveBeenCalled()
2491
+ await vi.advanceTimersByTimeAsync(1499)
2492
+ expect(solanaService.connect).not.toHaveBeenCalled()
2493
+ await vi.advanceTimersByTimeAsync(1)
2494
+ expect(solanaService.connect).toHaveBeenCalledTimes(1)
2495
+
2496
+ // The next prompt waits its own gap after the previous answer.
2497
+ expect(tronService.connect).not.toHaveBeenCalled()
2498
+ await vi.advanceTimersByTimeAsync(1500)
2499
+ expect(tronService.connect).toHaveBeenCalledTimes(1)
2500
+ await vi.advanceTimersByTimeAsync(1499)
2501
+ expect(tonHandshake).not.toHaveBeenCalled()
2502
+ await vi.advanceTimersByTimeAsync(1)
2503
+ expect(tonHandshake).toHaveBeenCalledTimes(1)
2504
+
2505
+ const result = await pending
2506
+ expect(result.availableAddresses.map(a => a.networkId)).toEqual([
2507
+ 'eip155:1',
2508
+ 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
2509
+ 'tron:0x2b6653dc',
2510
+ 'tvm:-239'
2511
+ ])
2512
+ })
2513
+
2514
+ it('reconnects a previously connected Solana wallet through the gapped connect', async () => {
2515
+ // A stored "connected before" flag made the read reconnect at once; when
2516
+ // the wallet had revoked the site, that reconnect opened a prompt right
2517
+ // after the EVM approval and Trust iOS never answered it.
2518
+ const pending = manager.connectEthereum(
2519
+ makeNetwork('eip155:1', 'eip155'),
2520
+ provider(),
2521
+ 1500
2522
+ )
2523
+ await vi.advanceTimersByTimeAsync(0)
2524
+
2525
+ expect(solanaService.checkExistingConnection).toHaveBeenCalledWith(
2526
+ {},
2527
+ 'trust-solana',
2528
+ expect.objectContaining({ allowSilentReconnect: false })
2529
+ )
2530
+ expect(solanaService.connect).not.toHaveBeenCalled()
2531
+ await vi.advanceTimersByTimeAsync(1500)
2532
+ expect(solanaService.connect).toHaveBeenCalledTimes(1)
2533
+
2534
+ await vi.advanceTimersByTimeAsync(3000)
2535
+ await pending
2536
+ })
2537
+
2538
+ it('reports whether each leg sent a prompting request', async () => {
2539
+ const onConnectStep = vi.fn()
2540
+ manager = new ConnectionManager(
2541
+ ethService as any,
2542
+ solanaService as any,
2543
+ tronService as any,
2544
+ tonService as any,
2545
+ bitcoinService as any,
2546
+ { onConnectStep }
2547
+ )
2548
+ // Solana and TON prompt; Tron is already authorized (a read).
2549
+ solanaService.connect.mockResolvedValue('SolAddr1')
2550
+ tronService.checkExistingConnection.mockResolvedValue('TronAddr9')
2551
+
2552
+ const pending = manager.connectEthereum(
2553
+ makeNetwork('eip155:1', 'eip155'),
2554
+ provider(),
2555
+ 1500
2556
+ )
2557
+ await vi.runAllTimersAsync()
2558
+ await pending
2559
+
2560
+ const leg = (namespace: string) =>
2561
+ onConnectStep.mock.calls
2562
+ .map(([info]) => info)
2563
+ .find(
2564
+ info =>
2565
+ info.step === 'secondaryGather' && info.namespace === namespace
2566
+ )
2567
+ expect(leg('solana')).toMatchObject({ outcome: 'ok', prompted: true })
2568
+ expect(leg('tvm')).toMatchObject({ outcome: 'ok', prompted: true })
2569
+ expect(leg('tron')).toMatchObject({ outcome: 'ok', prompted: false })
2570
+ })
2571
+
2572
+ it('reports a remembered Solana wallet reconnect as prompted, with no gap', async () => {
2573
+ const onConnectStep = vi.fn()
2574
+ manager = new ConnectionManager(
2575
+ ethService as any,
2576
+ solanaService as any,
2577
+ tronService as any,
2578
+ tonService as any,
2579
+ bitcoinService as any,
2580
+ { onConnectStep, secondaryGatherTimeoutMs: 5000 }
2581
+ )
2582
+ // The read's reconnect (a stored "connected before" flag) never returns.
2583
+ solanaService.checkExistingConnection.mockImplementation(
2584
+ (
2585
+ _adapter: unknown,
2586
+ _uuid: string,
2587
+ options?: { onReconnect?: () => void }
2588
+ ) => {
2589
+ options?.onReconnect?.()
2590
+ return new Promise(() => {})
2591
+ }
2592
+ )
2593
+
2594
+ const pending = manager.connectEthereum(
2595
+ makeNetwork('eip155:1', 'eip155'),
2596
+ provider()
2597
+ )
2598
+ await vi.advanceTimersByTimeAsync(30_000)
2599
+ await pending
2600
+
2601
+ expect(onConnectStep).toHaveBeenCalledWith(
2602
+ expect.objectContaining({
2603
+ step: 'secondaryGather',
2604
+ namespace: 'solana',
2605
+ outcome: 'timedOut',
2606
+ prompted: true
2607
+ })
2608
+ )
2609
+ })
2610
+
2611
+ it('reports a leg that timed out during the gap as not prompted', async () => {
2612
+ const onConnectStep = vi.fn()
2613
+ manager = new ConnectionManager(
2614
+ ethService as any,
2615
+ solanaService as any,
2616
+ tronService as any,
2617
+ tonService as any,
2618
+ bitcoinService as any,
2619
+ { onConnectStep, secondaryGatherTimeoutMs: 1000 }
2620
+ )
2621
+
2622
+ const pending = manager.connectEthereum(
2623
+ makeNetwork('eip155:1', 'eip155'),
2624
+ provider(),
2625
+ 1500
2626
+ )
2627
+ await vi.advanceTimersByTimeAsync(10_000)
2628
+ await pending
2629
+
2630
+ expect(onConnectStep).toHaveBeenCalledWith(
2631
+ expect.objectContaining({
2632
+ step: 'secondaryGather',
2633
+ namespace: 'solana',
2634
+ outcome: 'timedOut',
2635
+ prompted: false
2636
+ })
2637
+ )
2638
+ })
2639
+
2640
+ it('reports a leg whose prompt went unanswered as timed out and prompted', async () => {
2641
+ const onConnectStep = vi.fn()
2642
+ manager = new ConnectionManager(
2643
+ ethService as any,
2644
+ solanaService as any,
2645
+ tronService as any,
2646
+ tonService as any,
2647
+ bitcoinService as any,
2648
+ { onConnectStep, secondaryGatherTimeoutMs: 5000 }
2649
+ )
2650
+ solanaService.connect.mockReturnValue(new Promise(() => {}))
2651
+
2652
+ const pending = manager.connectEthereum(
2653
+ makeNetwork('eip155:1', 'eip155'),
2654
+ provider(),
2655
+ 1500
2656
+ )
2657
+ await vi.advanceTimersByTimeAsync(30_000)
2658
+ await pending
2659
+
2660
+ expect(onConnectStep).toHaveBeenCalledWith(
2661
+ expect.objectContaining({
2662
+ step: 'secondaryGather',
2663
+ namespace: 'solana',
2664
+ outcome: 'timedOut',
2665
+ prompted: true
2666
+ })
2667
+ )
2668
+ })
2669
+
2670
+ it('sends no request from a leg whose deadline passed during the gap', async () => {
2671
+ const onConnectStep = vi.fn()
2672
+ manager = new ConnectionManager(
2673
+ ethService as any,
2674
+ solanaService as any,
2675
+ tronService as any,
2676
+ tonService as any,
2677
+ bitcoinService as any,
2678
+ { onConnectStep, secondaryGatherTimeoutMs: 1000 }
2679
+ )
2680
+
2681
+ const pending = manager.connectEthereum(
2682
+ makeNetwork('eip155:1', 'eip155'),
2683
+ provider(),
2684
+ 1500
2685
+ )
2686
+ await vi.advanceTimersByTimeAsync(10_000)
2687
+ await pending
2688
+
2689
+ expect(solanaService.connect).not.toHaveBeenCalled()
2690
+ expect(tronService.connect).not.toHaveBeenCalled()
2691
+ expect(tonHandshake).not.toHaveBeenCalled()
2692
+ expect(onConnectStep).toHaveBeenCalledWith(
2693
+ expect.objectContaining({
2694
+ step: 'secondaryGather',
2695
+ namespace: 'solana',
2696
+ outcome: 'timedOut'
2697
+ })
2698
+ )
2699
+ })
2700
+
2701
+ it('does not delay or mark a TON session the service restores', async () => {
2702
+ const onConnectStep = vi.fn()
2703
+ manager = new ConnectionManager(
2704
+ ethService as any,
2705
+ solanaService as any,
2706
+ tronService as any,
2707
+ tonService as any,
2708
+ bitcoinService as any,
2709
+ { onConnectStep }
2710
+ )
2711
+ solanaService.checkExistingConnection.mockResolvedValue('SolAddr9')
2712
+ tronService.checkExistingConnection.mockResolvedValue('TronAddr9')
2713
+ // A stored session resumes without the handshake or its hook.
2714
+ tonService.connect.mockResolvedValue({ address: 'TonAddr9' })
2715
+
2716
+ const pending = manager.connectEthereum(
2717
+ makeNetwork('eip155:1', 'eip155'),
2718
+ provider(),
2719
+ 1500
2720
+ )
2721
+ await vi.advanceTimersByTimeAsync(0)
2722
+ const result = await pending
2723
+
2724
+ expect(result.availableAddresses).toHaveLength(4)
2725
+ expect(onConnectStep).toHaveBeenCalledWith(
2726
+ expect.objectContaining({
2727
+ step: 'secondaryGather',
2728
+ namespace: 'tvm',
2729
+ outcome: 'ok',
2730
+ prompted: false
2731
+ })
2732
+ )
2733
+ })
2734
+
2735
+ it('sends no TON handshake once its restore outlasted the deadline', async () => {
2736
+ manager = new ConnectionManager(
2737
+ ethService as any,
2738
+ solanaService as any,
2739
+ tronService as any,
2740
+ tonService as any,
2741
+ bitcoinService as any,
2742
+ { secondaryGatherTimeoutMs: 1000 }
2743
+ )
2744
+ solanaService.checkExistingConnection.mockResolvedValue('SolAddr9')
2745
+ tronService.checkExistingConnection.mockResolvedValue('TronAddr9')
2746
+ // The restore takes longer than the leg's deadline, then falls through.
2747
+ tonService.connect.mockImplementation(
2748
+ async (_jsBridgeKey: string, beforeHandshake?: () => Promise<void>) => {
2749
+ await new Promise(resolve => setTimeout(resolve, 2000))
2750
+ await beforeHandshake?.()
2751
+ return tonHandshake()
2752
+ }
2753
+ )
2754
+
2755
+ const pending = manager.connectEthereum(
2756
+ makeNetwork('eip155:1', 'eip155'),
2757
+ provider()
2758
+ )
2759
+ await vi.advanceTimersByTimeAsync(5000)
2760
+ await pending
2761
+
2762
+ expect(tonService.connect).toHaveBeenCalledTimes(1)
2763
+ expect(tonHandshake).not.toHaveBeenCalled()
2764
+ })
2765
+
2766
+ it('does not delay a namespace that is already authorized', async () => {
2767
+ solanaService.checkExistingConnection.mockResolvedValue('SolAddr9')
2768
+ tronService.checkExistingConnection.mockResolvedValue('TronAddr9')
2769
+ tonService.checkExistingConnection.mockResolvedValue('TonAddr9')
2770
+
2771
+ const pending = manager.connectEthereum(
2772
+ makeNetwork('eip155:1', 'eip155'),
2773
+ provider(),
2774
+ 1500
2775
+ )
2776
+ await vi.advanceTimersByTimeAsync(0)
2777
+ const result = await pending
2778
+
2779
+ expect(result.availableAddresses).toHaveLength(4)
2780
+ expect(solanaService.connect).not.toHaveBeenCalled()
2781
+ })
2782
+ })
2783
+
2111
2784
  // ONC-1975: silent-only per target namespace (Tron, TON, Bitcoin), and while
2112
2785
  // framed, where the secondary gathers have extra eager connects of their own.
2113
2786
  describe('ConnectionManager.checkExistingConnections — silentOnly, other targets and framed', () => {
@@ -2374,11 +3047,12 @@ describe('ConnectionManager.checkExistingConnections — silentOnly, other targe
2374
3047
  })
2375
3048
 
2376
3049
  describe('ConnectionManager — EVM connect behind an open wallet window (ONC-2390)', () => {
2377
- // Production's MetaMask config: the pre-connect Solana gather opens
2378
- // MetaMask's permission window before EVM is asked. If the user takes longer
2379
- // than the gather deadline, UWC used to ask for EVM straight away and
2380
- // MetaMask refused it with -32002 because the window was still open
2381
- // (ONC-2254). Now the EVM connect waits for that window, then asks again.
3050
+ // MetaMask refuses every request with -32002 "already pending" while one of
3051
+ // its windows is open, whichever connect opened it. A cold EVM connect asks
3052
+ // for EVM before anything else, so the window it meets is one an earlier
3053
+ // connect left open (Try Again, Link's return-user "Open MetaMask"): that
3054
+ // connect's EVM request, or a gather it ran after its approval. The EVM
3055
+ // connect waits for that window, then asks again.
2382
3056
  let ethService: ReturnType<typeof makeEthService>
2383
3057
  let solanaService: ReturnType<typeof makeSolanaService>
2384
3058
  let onConnectStep: ReturnType<typeof vi.fn>
@@ -2396,15 +3070,12 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2396
3070
  })
2397
3071
  const network = makeNetwork('eip155:1', 'eip155')
2398
3072
 
2399
- function build(
2400
- bitcoinService = makeBitcoinService(),
2401
- tonService = makeTonService()
2402
- ) {
3073
+ function build(bitcoinService = makeBitcoinService()) {
2403
3074
  return new ConnectionManager(
2404
3075
  ethService as any,
2405
3076
  solanaService as any,
2406
3077
  makeTronService() as any,
2407
- tonService as any,
3078
+ makeTonService() as any,
2408
3079
  bitcoinService as any,
2409
3080
  { onConnectStep, secondaryGatherTimeoutMs: 1000 }
2410
3081
  )
@@ -2415,49 +3086,59 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2415
3086
  return manager.checkExistingConnections('eip155', withProvider)
2416
3087
  }
2417
3088
 
3089
+ /** A wallet request that stays open until answered. */
3090
+ function heldRequest() {
3091
+ let answer!: (address: string | Error) => void
3092
+ const request = new Promise<string>((resolve, reject) => {
3093
+ answer = value =>
3094
+ value instanceof Error ? reject(value) : resolve(value)
3095
+ })
3096
+ return { request, answer: (value: string | Error) => answer(value) }
3097
+ }
3098
+
2418
3099
  /**
2419
- * Starts an attempt whose Solana window opens and stays open, unanswered.
2420
- * Returns the attempt and a way to answer the window.
3100
+ * Starts an earlier connect whose EVM window opens and stays open,
3101
+ * unanswered. Returns its attempt and a way to answer the window.
2421
3102
  */
2422
- function openSolanaPrompt(
3103
+ async function openEvmPrompt(
2423
3104
  manager: ConnectionManager,
2424
3105
  withProvider = provider()
2425
3106
  ) {
2426
- let answer!: (address: string | Error) => void
2427
- solanaService.connect.mockImplementationOnce(
2428
- () =>
2429
- new Promise((resolve, reject) => {
2430
- answer = value =>
2431
- value instanceof Error ? reject(value) : resolve(value)
2432
- })
2433
- )
3107
+ const { request, answer } = heldRequest()
3108
+ ethService.connect.mockReturnValueOnce(request)
2434
3109
  const attempt = manager.beginAttempt()
2435
- const checking = check(manager, withProvider)
2436
- return {
2437
- attempt,
2438
- checking,
2439
- answer: (value: string | Error) => answer(value)
2440
- }
3110
+ await check(manager, withProvider)
3111
+ // Its own outcome is not under test: it is superseded or disconnected.
3112
+ manager
3113
+ .connectEthereum(network, withProvider, 0, attempt)
3114
+ .catch(() => undefined)
3115
+ await vi.advanceTimersByTimeAsync(0)
3116
+ expect(ethService.connect).toHaveBeenCalledTimes(1)
3117
+ return { attempt, answer }
2441
3118
  }
2442
3119
 
2443
3120
  /**
2444
- * Starts an attempt whose Solana window stays open past its gather
2445
- * deadline. Returns the attempt and a way to answer the window.
3121
+ * Starts an earlier connect whose EVM request is approved at once and
3122
+ * whose Solana gather then opens a window that stays open, unanswered.
2446
3123
  */
2447
- async function timeOutSolanaPrompt(manager: ConnectionManager) {
2448
- let answer!: (address: string | Error) => void
2449
- solanaService.connect.mockImplementationOnce(
2450
- () =>
2451
- new Promise((resolve, reject) => {
2452
- answer = value =>
2453
- value instanceof Error ? reject(value) : resolve(value)
2454
- })
2455
- )
3124
+ async function openSolanaPromptAfterApproval(manager: ConnectionManager) {
3125
+ const { request, answer } = heldRequest()
3126
+ solanaService.connect.mockReturnValueOnce(request)
2456
3127
  const attempt = manager.beginAttempt()
2457
- const checking = check(manager)
2458
- await vi.advanceTimersByTimeAsync(1000)
2459
- expect(await checking).toEqual({ address: null, availableAddresses: [] })
2460
- return { attempt, answer: (value: string | Error) => answer(value) }
3128
+ await check(manager)
3129
+ manager
3130
+ .connectEthereum(network, provider(), 0, attempt)
3131
+ .catch(() => undefined)
3132
+ await vi.advanceTimersByTimeAsync(0)
3133
+ expect(solanaService.connect).toHaveBeenCalledTimes(1)
3134
+ return { attempt, answer }
3135
+ }
3136
+
3137
+ /** The next connect, up to its EVM request. */
3138
+ async function nextConnect(manager: ConnectionManager) {
3139
+ const attempt = manager.beginAttempt()
3140
+ await check(manager)
3141
+ return attempt
2461
3142
  }
2462
3143
 
2463
3144
  function stepsOf(step: string) {
@@ -2472,10 +3153,6 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2472
3153
  ethService.findWalletByUuid.mockReturnValue({ provider: {} })
2473
3154
  solanaService = makeSolanaService()
2474
3155
  solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
2475
- // A gathered Solana account is read back through getAccount().
2476
- solanaService.setConnectionState.mockImplementation((_a, address) =>
2477
- solanaService.getAccount.mockReturnValue(address)
2478
- )
2479
3156
  onConnectStep = vi.fn()
2480
3157
  })
2481
3158
 
@@ -2483,26 +3160,41 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2483
3160
  vi.useRealTimers()
2484
3161
  })
2485
3162
 
3163
+ it('a cold EVM connect sends nothing before its own EVM request', async () => {
3164
+ const manager = build()
3165
+ await check(manager)
3166
+
3167
+ expect(solanaService.checkExistingConnection).not.toHaveBeenCalled()
3168
+ expect(solanaService.connect).not.toHaveBeenCalled()
3169
+ expect(stepsOf('secondaryGather')).toEqual([])
3170
+ })
3171
+
2486
3172
  it.each([
2487
3173
  ['direct', ALREADY_PENDING],
2488
3174
  // Embedded (bridge) mode keeps only the message.
2489
3175
  ['bridged', new Error(ALREADY_PENDING.message)]
2490
3176
  ])(
2491
- 'waits for the open Solana window, then connects EVM with the Solana address (%s)',
3177
+ 'waits for the EVM window an earlier connect left open, then connects (%s)',
2492
3178
  async (_mode, refusal) => {
2493
3179
  const manager = build()
2494
- const { attempt, answer } = await timeOutSolanaPrompt(manager)
3180
+ const earlier = await openEvmPrompt(manager)
3181
+ const attempt = await nextConnect(manager)
2495
3182
  ethService.connect.mockRejectedValueOnce(refusal)
2496
3183
 
2497
- const connecting = manager.connectEthereum(network, provider(), attempt)
3184
+ const connecting = manager.connectEthereum(
3185
+ network,
3186
+ provider(),
3187
+ 0,
3188
+ attempt
3189
+ )
2498
3190
  await vi.advanceTimersByTimeAsync(5000)
2499
- // Still waiting on the user: no second EVM request while the window is open.
2500
- expect(ethService.connect).toHaveBeenCalledTimes(1)
3191
+ // Still waiting on the user: no second request while the window is open.
3192
+ expect(ethService.connect).toHaveBeenCalledTimes(2)
2501
3193
 
2502
- answer('SolAddr1')
3194
+ earlier.answer('0xABC')
2503
3195
  const result = await connecting
2504
3196
 
2505
- expect(ethService.connect).toHaveBeenCalledTimes(2)
3197
+ expect(ethService.connect).toHaveBeenCalledTimes(3)
2506
3198
  expect(result.address).toBe('0xABC')
2507
3199
  expect(result.availableAddresses).toEqual(
2508
3200
  expect.arrayContaining([
@@ -2514,21 +3206,43 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2514
3206
  ])
2515
3207
  )
2516
3208
  expect(stepsOf('awaitOpenPrompt')).toEqual([
2517
- expect.objectContaining({ outcome: 'ok', namespace: 'solana' })
3209
+ expect.objectContaining({ outcome: 'ok', namespace: 'eip155' })
2518
3210
  ])
2519
3211
  }
2520
3212
  )
2521
3213
 
2522
- it('asks for EVM again when the user declines the late Solana window', async () => {
3214
+ it('asks for EVM again when the user declines the earlier window', async () => {
2523
3215
  const manager = build()
2524
- const { attempt, answer } = await timeOutSolanaPrompt(manager)
3216
+ const earlier = await openEvmPrompt(manager)
3217
+ const attempt = await nextConnect(manager)
2525
3218
  ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2526
3219
 
2527
- const connecting = manager.connectEthereum(network, provider(), attempt)
2528
- answer(new Error('User rejected the request.'))
3220
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
3221
+ earlier.answer(new Error('User rejected the request.'))
2529
3222
  const result = await connecting
2530
3223
 
3224
+ expect(ethService.connect).toHaveBeenCalledTimes(3)
3225
+ expect(result.address).toBe('0xABC')
3226
+ expect(stepsOf('awaitOpenPrompt')).toEqual([
3227
+ expect.objectContaining({ outcome: 'ok', namespace: 'eip155' })
3228
+ ])
3229
+ })
3230
+
3231
+ it('waits for a Solana window an earlier connect opened after its approval', async () => {
3232
+ const manager = build()
3233
+ const earlier = await openSolanaPromptAfterApproval(manager)
3234
+ const attempt = await nextConnect(manager)
3235
+ ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
3236
+
3237
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
3238
+ // Past the earlier gather's deadline: its window is still open.
3239
+ await vi.advanceTimersByTimeAsync(5000)
2531
3240
  expect(ethService.connect).toHaveBeenCalledTimes(2)
3241
+
3242
+ earlier.answer('SolAddr1')
3243
+ const result = await connecting
3244
+
3245
+ expect(ethService.connect).toHaveBeenCalledTimes(3)
2532
3246
  expect(result.address).toBe('0xABC')
2533
3247
  expect(stepsOf('awaitOpenPrompt')).toEqual([
2534
3248
  expect.objectContaining({ outcome: 'ok', namespace: 'solana' })
@@ -2537,19 +3251,21 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2537
3251
 
2538
3252
  it('retries only once', async () => {
2539
3253
  const manager = build()
2540
- const { attempt, answer } = await timeOutSolanaPrompt(manager)
3254
+ const earlier = await openEvmPrompt(manager)
3255
+ const attempt = await nextConnect(manager)
2541
3256
  ethService.connect.mockRejectedValue(ALREADY_PENDING)
2542
3257
 
2543
- const connecting = manager.connectEthereum(network, provider(), attempt)
3258
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
2544
3259
  const outcome = expect(connecting).rejects.toThrow()
2545
- answer('SolAddr1')
3260
+ earlier.answer('0xABC')
2546
3261
  await outcome
2547
- expect(ethService.connect).toHaveBeenCalledTimes(2)
3262
+ expect(ethService.connect).toHaveBeenCalledTimes(3)
2548
3263
  })
2549
3264
 
2550
3265
  it("reports the wallet's own steps for both EVM requests", async () => {
2551
3266
  const manager = build()
2552
- const { attempt, answer } = await timeOutSolanaPrompt(manager)
3267
+ const earlier = await openEvmPrompt(manager)
3268
+ const attempt = await nextConnect(manager)
2553
3269
  type OnStep = (info: Record<string, unknown>) => void
2554
3270
  ethService.connect
2555
3271
  .mockImplementationOnce(
@@ -2575,8 +3291,8 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2575
3291
  }
2576
3292
  )
2577
3293
 
2578
- const connecting = manager.connectEthereum(network, provider(), attempt)
2579
- answer('SolAddr1')
3294
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
3295
+ earlier.answer('0xABC')
2580
3296
  await connecting
2581
3297
 
2582
3298
  expect(stepsOf('primaryConnect').map(({ outcome }) => outcome)).toEqual([
@@ -2587,12 +3303,11 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2587
3303
 
2588
3304
  it('does not wait when no window is open', async () => {
2589
3305
  const manager = build()
2590
- const attempt = manager.beginAttempt()
2591
- await check(manager)
3306
+ const attempt = await nextConnect(manager)
2592
3307
  ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2593
3308
 
2594
3309
  await expect(
2595
- manager.connectEthereum(network, provider(), attempt)
3310
+ manager.connectEthereum(network, provider(), 0, attempt)
2596
3311
  ).rejects.toThrow(/already pending/)
2597
3312
  expect(ethService.connect).toHaveBeenCalledTimes(1)
2598
3313
  expect(stepsOf('awaitOpenPrompt')).toEqual([])
@@ -2600,183 +3315,143 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2600
3315
 
2601
3316
  it('does not wait without an attempt', async () => {
2602
3317
  const manager = build()
2603
- await timeOutSolanaPrompt(manager)
3318
+ await openEvmPrompt(manager)
2604
3319
  ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2605
3320
 
2606
3321
  await expect(manager.connectEthereum(network, provider())).rejects.toThrow(
2607
3322
  /already pending/
2608
3323
  )
2609
- expect(ethService.connect).toHaveBeenCalledTimes(1)
3324
+ expect(ethService.connect).toHaveBeenCalledTimes(2)
2610
3325
  })
2611
3326
 
2612
3327
  it('does not wait on any other EVM error', async () => {
2613
3328
  const manager = build()
2614
- const { attempt } = await timeOutSolanaPrompt(manager)
3329
+ await openEvmPrompt(manager)
3330
+ const attempt = await nextConnect(manager)
2615
3331
  ethService.connect.mockRejectedValueOnce(
2616
3332
  Object.assign(new Error('User rejected the request.'), { code: 4001 })
2617
3333
  )
2618
3334
 
2619
3335
  await expect(
2620
- manager.connectEthereum(network, provider(), attempt)
3336
+ manager.connectEthereum(network, provider(), 0, attempt)
2621
3337
  ).rejects.toThrow(/rejected/)
2622
- expect(ethService.connect).toHaveBeenCalledTimes(1)
3338
+ expect(ethService.connect).toHaveBeenCalledTimes(2)
2623
3339
  })
2624
3340
 
2625
3341
  it("does not wait on Phantom's -32002 resource error, which is not an open window", async () => {
2626
3342
  const manager = build()
2627
- const { attempt, answer } = await timeOutSolanaPrompt(manager)
3343
+ const earlier = await openEvmPrompt(manager)
3344
+ const attempt = await nextConnect(manager)
2628
3345
  ethService.connect.mockRejectedValueOnce(
2629
3346
  Object.assign(new Error('Requested resource not available.'), {
2630
3347
  code: -32002
2631
3348
  })
2632
3349
  )
2633
3350
 
2634
- const connecting = manager.connectEthereum(network, provider(), attempt)
3351
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
2635
3352
  const outcome = expect(connecting).rejects.toThrow(
2636
3353
  /Requested resource not available/
2637
3354
  )
2638
- // Fails at once, as before this change: it does not wait for the Solana
2639
- // window still open, which may never be answered.
3355
+ // Fails at once: it does not wait for the window still open, which may
3356
+ // never be answered.
2640
3357
  await vi.advanceTimersByTimeAsync(0)
2641
3358
  await outcome
2642
- expect(ethService.connect).toHaveBeenCalledTimes(1)
3359
+ expect(ethService.connect).toHaveBeenCalledTimes(2)
2643
3360
  expect(stepsOf('awaitOpenPrompt')).toEqual([])
2644
3361
 
2645
- answer('SolAddr1')
3362
+ earlier.answer('0xABC')
2646
3363
  await vi.advanceTimersByTimeAsync(0)
2647
- expect(ethService.connect).toHaveBeenCalledTimes(1)
3364
+ expect(ethService.connect).toHaveBeenCalledTimes(2)
2648
3365
  })
2649
3366
 
2650
3367
  it('does not ask again once the attempt was disconnected while waiting', async () => {
2651
3368
  const manager = build()
2652
- const { attempt, answer } = await timeOutSolanaPrompt(manager)
3369
+ const earlier = await openEvmPrompt(manager)
3370
+ const attempt = await nextConnect(manager)
2653
3371
  ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2654
3372
 
2655
- const connecting = manager.connectEthereum(network, provider(), attempt)
3373
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
2656
3374
  const outcome = expect(connecting).rejects.toThrow(/already pending/)
2657
3375
  // Refused, now waiting on the open window.
2658
3376
  await vi.advanceTimersByTimeAsync(5000)
2659
3377
  await manager.disconnect()
2660
3378
  // A late answer must not open a new EVM prompt nobody waits for.
2661
- answer(new Error('User rejected the request.'))
3379
+ earlier.answer(new Error('User rejected the request.'))
2662
3380
  await outcome
2663
3381
 
2664
- expect(ethService.connect).toHaveBeenCalledTimes(1)
2665
- expect(stepsOf('awaitOpenPrompt')).toEqual([
2666
- expect.objectContaining({ outcome: 'skipped', namespace: 'solana' })
2667
- ])
2668
- })
2669
-
2670
- it('waits for a window an earlier connect left open, then connects (Try Again)', async () => {
2671
- const manager = build()
2672
- // Connect A opens the Solana window; the user has not answered it and its
2673
- // deadline has not passed when connect B starts (Link's return-user
2674
- // "Open MetaMask", or Try Again).
2675
- const a = openSolanaPrompt(manager)
2676
- await vi.advanceTimersByTimeAsync(500)
2677
- // MetaMask refuses B's own requests while A's window is open.
2678
- solanaService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2679
- const b = manager.beginAttempt()
2680
- await check(manager)
2681
- ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2682
-
2683
- const connecting = manager.connectEthereum(network, provider(), b)
2684
- await vi.advanceTimersByTimeAsync(5000)
2685
- expect(ethService.connect).toHaveBeenCalledTimes(1)
2686
-
2687
- a.answer('SolAddr1')
2688
- const result = await connecting
2689
-
2690
3382
  expect(ethService.connect).toHaveBeenCalledTimes(2)
2691
- expect(result.address).toBe('0xABC')
2692
- expect(result.availableAddresses).toEqual(
2693
- expect.arrayContaining([
2694
- {
2695
- address: 'SolAddr1',
2696
- networkId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
2697
- }
2698
- ])
2699
- )
2700
3383
  expect(stepsOf('awaitOpenPrompt')).toEqual([
2701
- expect.objectContaining({ outcome: 'ok', namespace: 'solana' })
3384
+ expect.objectContaining({ outcome: 'skipped', namespace: 'eip155' })
2702
3385
  ])
2703
3386
  })
2704
3387
 
2705
3388
  it('still asks again when the refusal arrives after the window was answered', async () => {
2706
3389
  const manager = build()
2707
- const a = openSolanaPrompt(manager)
2708
- await vi.advanceTimersByTimeAsync(500)
2709
- solanaService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2710
- const b = manager.beginAttempt()
2711
- await check(manager)
2712
- // The refusal was sent while A's window was open, but reaches UWC only
2713
- // after the user answered it (a slow bridge).
3390
+ const earlier = await openEvmPrompt(manager)
3391
+ const attempt = await nextConnect(manager)
3392
+ // The refusal was sent while the earlier window was open, but reaches UWC
3393
+ // only after the user answered it (a slow bridge).
2714
3394
  let refuse!: () => void
2715
3395
  ethService.connect.mockImplementationOnce(
2716
3396
  () => new Promise((_, reject) => (refuse = () => reject(ALREADY_PENDING)))
2717
3397
  )
2718
3398
 
2719
- const connecting = manager.connectEthereum(network, provider(), b)
2720
- a.answer('SolAddr1')
3399
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
3400
+ earlier.answer('0xABC')
2721
3401
  await vi.advanceTimersByTimeAsync(0)
2722
3402
  refuse()
2723
3403
 
2724
3404
  expect((await connecting).address).toBe('0xABC')
2725
- expect(ethService.connect).toHaveBeenCalledTimes(2)
3405
+ expect(ethService.connect).toHaveBeenCalledTimes(3)
2726
3406
  })
2727
3407
 
2728
3408
  it('after a disconnect, the next connect waits for the window left open', async () => {
2729
3409
  const manager = build()
2730
- const a = openSolanaPrompt(manager)
2731
- await vi.advanceTimersByTimeAsync(500)
3410
+ const earlier = await openEvmPrompt(manager)
2732
3411
  await manager.disconnect()
2733
- solanaService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2734
- const b = manager.beginAttempt()
2735
- await check(manager)
3412
+ const attempt = await nextConnect(manager)
2736
3413
  ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2737
3414
 
2738
- const connecting = manager.connectEthereum(network, provider(), b)
2739
- a.answer('SolAddr1')
3415
+ const connecting = manager.connectEthereum(network, provider(), 0, attempt)
3416
+ earlier.answer('0xABC')
2740
3417
 
2741
3418
  expect((await connecting).address).toBe('0xABC')
2742
- expect(ethService.connect).toHaveBeenCalledTimes(2)
3419
+ expect(ethService.connect).toHaveBeenCalledTimes(3)
2743
3420
  })
2744
3421
 
2745
3422
  it("does not wait on another wallet's open window", async () => {
2746
3423
  const manager = build()
2747
- // Another wallet's connect left its Solana window open.
2748
- openSolanaPrompt(
3424
+ await openEvmPrompt(
2749
3425
  manager,
2750
3426
  makeProvider({
2751
3427
  eip155: { detectedWallet: { uuid: 'phantom-eip155' } },
2752
3428
  solana: { detectedWallet: { uuid: 'phantom-solana' } }
2753
3429
  })
2754
3430
  )
2755
- await vi.advanceTimersByTimeAsync(500)
2756
- const b = manager.beginAttempt()
2757
- await check(manager)
3431
+ const attempt = await nextConnect(manager)
2758
3432
  ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2759
3433
 
2760
3434
  await expect(
2761
- manager.connectEthereum(network, provider(), b)
3435
+ manager.connectEthereum(network, provider(), 0, attempt)
2762
3436
  ).rejects.toThrow(/already pending/)
2763
- expect(ethService.connect).toHaveBeenCalledTimes(1)
3437
+ expect(ethService.connect).toHaveBeenCalledTimes(2)
2764
3438
  expect(stepsOf('awaitOpenPrompt')).toEqual([])
2765
3439
  })
2766
3440
 
2767
3441
  it('a superseded connect does not send its EVM request', async () => {
2768
3442
  const manager = build()
2769
- const a = await timeOutSolanaPrompt(manager)
2770
- // Connect B supersedes A before A reaches its EVM request: a request from
2771
- // A now would open a prompt no one waits for, or collide with B's.
3443
+ const superseded = await nextConnect(manager)
3444
+ // A newer connect starts before this one reaches its EVM request: a
3445
+ // request now would open a prompt no one waits for, or collide with it.
2772
3446
  manager.beginAttempt()
2773
3447
 
2774
3448
  await expect(
2775
- manager.connectEthereum(network, provider(), a.attempt)
3449
+ manager.connectEthereum(network, provider(), 0, superseded)
2776
3450
  ).rejects.toThrow()
2777
3451
  expect(ethService.connect).not.toHaveBeenCalled()
2778
3452
  })
2779
- it('does not wait on a timed-out passive gather that opens no window', async () => {
3453
+
3454
+ it('does not wait on a passive gather that opens no window', async () => {
2780
3455
  const bitcoinService = makeBitcoinService()
2781
3456
  bitcoinService.findWalletByUuid.mockReturnValue({})
2782
3457
  // Bitcoin's gather only reads; here the read never answers.
@@ -2790,26 +3465,21 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
2790
3465
  solana: { detectedWallet: { uuid: 'mm-solana' } },
2791
3466
  bip122: { detectedWallet: { uuid: 'mm-bip122' } }
2792
3467
  })
2793
- let answer!: (address: string) => void
2794
- solanaService.connect.mockImplementationOnce(
2795
- () => new Promise(resolve => (answer = resolve))
2796
- )
2797
- const attempt = manager.beginAttempt()
2798
- const checking = check(manager, withBitcoin())
2799
- await vi.advanceTimersByTimeAsync(2000)
2800
- await checking
3468
+ // An earlier connect, approved, is still in its hanging Bitcoin read.
3469
+ const earlier = manager.beginAttempt()
3470
+ await check(manager, withBitcoin())
3471
+ manager
3472
+ .connectEthereum(network, withBitcoin(), 0, earlier)
3473
+ .catch(() => undefined)
3474
+ await vi.advanceTimersByTimeAsync(0)
3475
+ expect(bitcoinService.checkExistingConnection).toHaveBeenCalledTimes(1)
3476
+ const attempt = await nextConnect(manager)
2801
3477
  ethService.connect.mockRejectedValueOnce(ALREADY_PENDING)
2802
3478
 
2803
- const connecting = manager.connectEthereum(network, withBitcoin(), attempt)
2804
- answer('SolAddr1')
2805
- // The post-connect Bitcoin gather hangs too, bounded by its own deadline.
2806
- await vi.advanceTimersByTimeAsync(1000)
2807
- const result = await connecting
2808
-
3479
+ await expect(
3480
+ manager.connectEthereum(network, withBitcoin(), 0, attempt)
3481
+ ).rejects.toThrow(/already pending/)
2809
3482
  expect(ethService.connect).toHaveBeenCalledTimes(2)
2810
- expect(result.address).toBe('0xABC')
2811
- expect(stepsOf('awaitOpenPrompt')).toEqual([
2812
- expect.objectContaining({ namespace: 'solana' })
2813
- ])
3483
+ expect(stepsOf('awaitOpenPrompt')).toEqual([])
2814
3484
  })
2815
3485
  })