@meshconnect/uwc-injected-connector 0.28.3 → 0.29.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/injected-connector.d.ts.map +1 -1
- package/dist/injected-connector.js +27 -2
- package/dist/injected-connector.js.map +1 -1
- package/dist/services/connection-manager.d.ts +16 -1
- package/dist/services/connection-manager.d.ts.map +1 -1
- package/dist/services/connection-manager.js +49 -5
- package/dist/services/connection-manager.js.map +1 -1
- package/dist/utils/error-utils.d.ts +19 -0
- package/dist/utils/error-utils.d.ts.map +1 -1
- package/dist/utils/error-utils.js +34 -0
- package/dist/utils/error-utils.js.map +1 -1
- package/package.json +6 -6
- package/src/injected-connector.test.ts +293 -1
- package/src/injected-connector.ts +40 -9
- package/src/services/connection-manager.test.ts +146 -0
- package/src/services/connection-manager.ts +56 -6
- package/src/utils/error-utils.test.ts +96 -1
- package/src/utils/error-utils.ts +34 -0
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
2
2
|
import { InjectedConnector } from './injected-connector'
|
|
3
3
|
import type {
|
|
4
|
+
NamespaceFallback,
|
|
4
5
|
Network,
|
|
5
6
|
ExtensionInjectedProvider,
|
|
6
7
|
TransactionRequest,
|
|
7
8
|
EIP712TypedData
|
|
8
9
|
} from '@meshconnect/uwc-types'
|
|
9
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
WalletConnectorError,
|
|
12
|
+
WalletNotAuthorizedError
|
|
13
|
+
} from '@meshconnect/uwc-types'
|
|
10
14
|
|
|
11
15
|
// Mock all service dependencies
|
|
12
16
|
vi.mock('./services/storage-service')
|
|
@@ -1206,6 +1210,294 @@ describe('InjectedConnector', () => {
|
|
|
1206
1210
|
})
|
|
1207
1211
|
})
|
|
1208
1212
|
|
|
1213
|
+
describe('connect: Solana fallback for a Solana-only account (CUS-173)', () => {
|
|
1214
|
+
const PHANTOM_REFUSAL = Object.assign(
|
|
1215
|
+
new Error('Requested resource not available.'),
|
|
1216
|
+
{ code: -32002 }
|
|
1217
|
+
)
|
|
1218
|
+
const solanaNetwork = {
|
|
1219
|
+
id: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
1220
|
+
internalId: 2,
|
|
1221
|
+
name: 'Solana',
|
|
1222
|
+
namespace: 'solana',
|
|
1223
|
+
networkType: 'solana',
|
|
1224
|
+
nativeCurrency: { name: 'SOL', symbol: 'SOL', decimals: 9 }
|
|
1225
|
+
} as Network
|
|
1226
|
+
const phantom = () =>
|
|
1227
|
+
({
|
|
1228
|
+
supportedNetworkIds: ['eip155:1', solanaNetwork.id],
|
|
1229
|
+
namespaceMetaData: {
|
|
1230
|
+
eip155: { detectedWallet: { uuid: 'phantom-eip155' } },
|
|
1231
|
+
solana: { detectedWallet: { uuid: 'phantom-solana' } }
|
|
1232
|
+
}
|
|
1233
|
+
}) as unknown as ExtensionInjectedProvider
|
|
1234
|
+
const fallbackResult = {
|
|
1235
|
+
networkId: solanaNetwork.id,
|
|
1236
|
+
address: 'SolAddr1',
|
|
1237
|
+
availableAddresses: [{ address: 'SolAddr1', networkId: solanaNetwork.id }]
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
const manager = async () => {
|
|
1241
|
+
const { ConnectionManager } = await import(
|
|
1242
|
+
'./services/connection-manager'
|
|
1243
|
+
)
|
|
1244
|
+
const m = vi.mocked(ConnectionManager).mock.results[0].value
|
|
1245
|
+
m.checkExistingConnections.mockResolvedValue({
|
|
1246
|
+
address: null,
|
|
1247
|
+
availableAddresses: []
|
|
1248
|
+
})
|
|
1249
|
+
m.beginAttempt.mockReturnValue({ generation: 1 })
|
|
1250
|
+
m.isAttemptLive.mockReturnValue(true)
|
|
1251
|
+
m.connectEthereum.mockRejectedValue(PHANTOM_REFUSAL)
|
|
1252
|
+
m.connectSolanaFallback.mockResolvedValue(fallbackResult)
|
|
1253
|
+
return m
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
// The core ↔ connector fallback contract, as core builds it.
|
|
1257
|
+
const fallbackTo = (
|
|
1258
|
+
overrides: Partial<NamespaceFallback> = {}
|
|
1259
|
+
): { fallback: NamespaceFallback } => ({
|
|
1260
|
+
fallback: {
|
|
1261
|
+
network: solanaNetwork,
|
|
1262
|
+
onFallback: vi.fn(),
|
|
1263
|
+
signal: new AbortController().signal,
|
|
1264
|
+
...overrides
|
|
1265
|
+
}
|
|
1266
|
+
})
|
|
1267
|
+
|
|
1268
|
+
it('connects the same wallet on Solana when the EVM target is refused', async () => {
|
|
1269
|
+
const m = await manager()
|
|
1270
|
+
const provider = phantom()
|
|
1271
|
+
|
|
1272
|
+
const onFallback = vi.fn()
|
|
1273
|
+
const result = await connector.connect(
|
|
1274
|
+
mockNetwork,
|
|
1275
|
+
provider,
|
|
1276
|
+
fallbackTo({ onFallback })
|
|
1277
|
+
)
|
|
1278
|
+
|
|
1279
|
+
expect(onFallback).toHaveBeenCalledTimes(1)
|
|
1280
|
+
expect(result).toMatchObject({
|
|
1281
|
+
networkId: solanaNetwork.id,
|
|
1282
|
+
address: 'SolAddr1'
|
|
1283
|
+
})
|
|
1284
|
+
expect(m.connectSolanaFallback).toHaveBeenCalledWith(
|
|
1285
|
+
solanaNetwork,
|
|
1286
|
+
provider,
|
|
1287
|
+
0,
|
|
1288
|
+
expect.any(Function)
|
|
1289
|
+
)
|
|
1290
|
+
})
|
|
1291
|
+
|
|
1292
|
+
it('falls back on the refusal without a code (a BridgeParent < 1.7.4 drops it)', async () => {
|
|
1293
|
+
const m = await manager()
|
|
1294
|
+
m.connectEthereum.mockRejectedValue(
|
|
1295
|
+
new Error('Requested resource not available.')
|
|
1296
|
+
)
|
|
1297
|
+
|
|
1298
|
+
await expect(
|
|
1299
|
+
connector.connect(mockNetwork, phantom(), fallbackTo())
|
|
1300
|
+
).resolves.toMatchObject({ networkId: solanaNetwork.id })
|
|
1301
|
+
})
|
|
1302
|
+
|
|
1303
|
+
it('falls back on the refusal as connectEthereum surfaces it (a WalletConnectorError)', async () => {
|
|
1304
|
+
const m = await manager()
|
|
1305
|
+
const { parseError } = await import('./utils/error-utils')
|
|
1306
|
+
let wrapped: unknown
|
|
1307
|
+
try {
|
|
1308
|
+
parseError(PHANTOM_REFUSAL)
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
wrapped = error
|
|
1311
|
+
}
|
|
1312
|
+
expect(wrapped).toBeInstanceOf(WalletConnectorError)
|
|
1313
|
+
m.connectEthereum.mockRejectedValue(wrapped)
|
|
1314
|
+
|
|
1315
|
+
await expect(
|
|
1316
|
+
connector.connect(mockNetwork, phantom(), fallbackTo())
|
|
1317
|
+
).resolves.toMatchObject({ networkId: solanaNetwork.id })
|
|
1318
|
+
})
|
|
1319
|
+
|
|
1320
|
+
it('propagates the fallback connect failing (the user declines Solana)', async () => {
|
|
1321
|
+
const m = await manager()
|
|
1322
|
+
const declined = Object.assign(new Error('User rejected the request.'), {
|
|
1323
|
+
code: 4001
|
|
1324
|
+
})
|
|
1325
|
+
m.connectSolanaFallback.mockRejectedValue(declined)
|
|
1326
|
+
|
|
1327
|
+
await expect(
|
|
1328
|
+
connector.connect(mockNetwork, phantom(), fallbackTo())
|
|
1329
|
+
).rejects.toBe(declined)
|
|
1330
|
+
})
|
|
1331
|
+
|
|
1332
|
+
it.each([
|
|
1333
|
+
['no fallback network is given', {}, () => {}],
|
|
1334
|
+
[
|
|
1335
|
+
'the fallback is not Solana',
|
|
1336
|
+
fallbackTo({
|
|
1337
|
+
network: { ...solanaNetwork, namespace: 'tron' } as Network
|
|
1338
|
+
}),
|
|
1339
|
+
() => {}
|
|
1340
|
+
],
|
|
1341
|
+
[
|
|
1342
|
+
'the attempt was superseded or disconnected',
|
|
1343
|
+
fallbackTo(),
|
|
1344
|
+
(m: { isAttemptLive: ReturnType<typeof vi.fn> }) =>
|
|
1345
|
+
m.isAttemptLive.mockReturnValue(false)
|
|
1346
|
+
],
|
|
1347
|
+
[
|
|
1348
|
+
'the connect was cancelled (its signal fired)',
|
|
1349
|
+
fallbackTo({ signal: AbortSignal.abort() }),
|
|
1350
|
+
() => {}
|
|
1351
|
+
],
|
|
1352
|
+
[
|
|
1353
|
+
'the refusal is "already pending"',
|
|
1354
|
+
fallbackTo(),
|
|
1355
|
+
(m: { connectEthereum: ReturnType<typeof vi.fn> }) =>
|
|
1356
|
+
m.connectEthereum.mockRejectedValue(
|
|
1357
|
+
Object.assign(
|
|
1358
|
+
new Error(
|
|
1359
|
+
"Request of type 'wallet_requestPermissions' already pending for origin https://link.example. Please wait."
|
|
1360
|
+
),
|
|
1361
|
+
{ code: -32002 }
|
|
1362
|
+
)
|
|
1363
|
+
)
|
|
1364
|
+
],
|
|
1365
|
+
[
|
|
1366
|
+
'the user rejected EVM',
|
|
1367
|
+
fallbackTo(),
|
|
1368
|
+
(m: { connectEthereum: ReturnType<typeof vi.fn> }) =>
|
|
1369
|
+
m.connectEthereum.mockRejectedValue(
|
|
1370
|
+
Object.assign(new Error('User rejected the request.'), {
|
|
1371
|
+
code: 4001
|
|
1372
|
+
})
|
|
1373
|
+
)
|
|
1374
|
+
]
|
|
1375
|
+
] as const)('rethrows unchanged when %s', async (_, options, arrange) => {
|
|
1376
|
+
const m = await manager()
|
|
1377
|
+
;(arrange as (m: unknown) => void)(m)
|
|
1378
|
+
const thrown = await connector
|
|
1379
|
+
.connect(mockNetwork, phantom(), options)
|
|
1380
|
+
.catch(e => e)
|
|
1381
|
+
|
|
1382
|
+
expect(thrown).toBe(
|
|
1383
|
+
await m.connectEthereum.mock.results[0].value.catch((e: unknown) => e)
|
|
1384
|
+
)
|
|
1385
|
+
expect(m.connectSolanaFallback).not.toHaveBeenCalled()
|
|
1386
|
+
})
|
|
1387
|
+
|
|
1388
|
+
it('waits the prompt gap after the refusal, then reports the fallback before the Solana prompt', async () => {
|
|
1389
|
+
vi.useFakeTimers()
|
|
1390
|
+
try {
|
|
1391
|
+
const m = await manager()
|
|
1392
|
+
const order: string[] = []
|
|
1393
|
+
const onFallback = vi.fn(() => order.push('fallback'))
|
|
1394
|
+
m.connectSolanaFallback.mockImplementation(async () => {
|
|
1395
|
+
order.push('solana')
|
|
1396
|
+
return fallbackResult
|
|
1397
|
+
})
|
|
1398
|
+
|
|
1399
|
+
const pending = connector.connect(mockNetwork, phantom(), {
|
|
1400
|
+
...fallbackTo({ onFallback }),
|
|
1401
|
+
secondaryPromptGapMs: 500
|
|
1402
|
+
})
|
|
1403
|
+
await vi.advanceTimersByTimeAsync(499)
|
|
1404
|
+
expect(order).toEqual([])
|
|
1405
|
+
await vi.advanceTimersByTimeAsync(1)
|
|
1406
|
+
|
|
1407
|
+
await expect(pending).resolves.toMatchObject({
|
|
1408
|
+
networkId: solanaNetwork.id
|
|
1409
|
+
})
|
|
1410
|
+
expect(order).toEqual(['fallback', 'solana'])
|
|
1411
|
+
expect(m.connectSolanaFallback).toHaveBeenCalledWith(
|
|
1412
|
+
solanaNetwork,
|
|
1413
|
+
expect.anything(),
|
|
1414
|
+
500,
|
|
1415
|
+
expect.any(Function)
|
|
1416
|
+
)
|
|
1417
|
+
} finally {
|
|
1418
|
+
vi.useRealTimers()
|
|
1419
|
+
}
|
|
1420
|
+
})
|
|
1421
|
+
|
|
1422
|
+
it('hands the Solana fallback a liveness check that ends with the connect (cancel or newer connect)', async () => {
|
|
1423
|
+
const m = await manager()
|
|
1424
|
+
const controller = new AbortController()
|
|
1425
|
+
await connector.connect(
|
|
1426
|
+
mockNetwork,
|
|
1427
|
+
phantom(),
|
|
1428
|
+
fallbackTo({ signal: controller.signal })
|
|
1429
|
+
)
|
|
1430
|
+
const isLive = m.connectSolanaFallback.mock.calls[0][3] as () => boolean
|
|
1431
|
+
|
|
1432
|
+
expect(isLive()).toBe(true)
|
|
1433
|
+
controller.abort()
|
|
1434
|
+
expect(isLive()).toBe(false)
|
|
1435
|
+
m.isAttemptLive.mockReturnValue(false)
|
|
1436
|
+
expect(isLive()).toBe(false)
|
|
1437
|
+
})
|
|
1438
|
+
|
|
1439
|
+
it('rethrows unchanged when the attempt is superseded during the prompt gap', async () => {
|
|
1440
|
+
const m = await manager()
|
|
1441
|
+
m.isAttemptLive.mockReturnValueOnce(true).mockReturnValue(false)
|
|
1442
|
+
const onFallback = vi.fn()
|
|
1443
|
+
|
|
1444
|
+
await expect(
|
|
1445
|
+
connector.connect(mockNetwork, phantom(), fallbackTo({ onFallback }))
|
|
1446
|
+
).rejects.toBe(PHANTOM_REFUSAL)
|
|
1447
|
+
expect(onFallback).not.toHaveBeenCalled()
|
|
1448
|
+
expect(m.connectSolanaFallback).not.toHaveBeenCalled()
|
|
1449
|
+
})
|
|
1450
|
+
|
|
1451
|
+
it('rethrows unchanged when the connect is cancelled during the prompt gap', async () => {
|
|
1452
|
+
vi.useFakeTimers()
|
|
1453
|
+
try {
|
|
1454
|
+
const m = await manager()
|
|
1455
|
+
const controller = new AbortController()
|
|
1456
|
+
const pending = connector
|
|
1457
|
+
.connect(mockNetwork, phantom(), {
|
|
1458
|
+
...fallbackTo({ signal: controller.signal }),
|
|
1459
|
+
secondaryPromptGapMs: 500
|
|
1460
|
+
})
|
|
1461
|
+
.catch(e => e)
|
|
1462
|
+
await vi.advanceTimersByTimeAsync(100)
|
|
1463
|
+
controller.abort()
|
|
1464
|
+
await vi.advanceTimersByTimeAsync(1_000)
|
|
1465
|
+
|
|
1466
|
+
expect(await pending).toBe(PHANTOM_REFUSAL)
|
|
1467
|
+
expect(m.connectSolanaFallback).not.toHaveBeenCalled()
|
|
1468
|
+
} finally {
|
|
1469
|
+
vi.useRealTimers()
|
|
1470
|
+
}
|
|
1471
|
+
})
|
|
1472
|
+
|
|
1473
|
+
it('rethrows unchanged when the wallet has no detected Solana provider', async () => {
|
|
1474
|
+
const m = await manager()
|
|
1475
|
+
const evmOnly = {
|
|
1476
|
+
supportedNetworkIds: ['eip155:1', solanaNetwork.id],
|
|
1477
|
+
namespaceMetaData: {
|
|
1478
|
+
eip155: { detectedWallet: { uuid: 'phantom-eip155' } },
|
|
1479
|
+
solana: { walletStandardName: 'Phantom' }
|
|
1480
|
+
}
|
|
1481
|
+
} as unknown as ExtensionInjectedProvider
|
|
1482
|
+
|
|
1483
|
+
await expect(
|
|
1484
|
+
connector.connect(mockNetwork, evmOnly, fallbackTo())
|
|
1485
|
+
).rejects.toBe(PHANTOM_REFUSAL)
|
|
1486
|
+
expect(m.connectSolanaFallback).not.toHaveBeenCalled()
|
|
1487
|
+
})
|
|
1488
|
+
|
|
1489
|
+
it('never falls back from a non-EVM target', async () => {
|
|
1490
|
+
const m = await manager()
|
|
1491
|
+
const refusal = new Error('Requested resource not available.')
|
|
1492
|
+
m.connectSolana.mockRejectedValue(refusal)
|
|
1493
|
+
|
|
1494
|
+
await expect(
|
|
1495
|
+
connector.connect(solanaNetwork, phantom(), fallbackTo())
|
|
1496
|
+
).rejects.toBe(refusal)
|
|
1497
|
+
expect(m.connectSolanaFallback).not.toHaveBeenCalled()
|
|
1498
|
+
})
|
|
1499
|
+
})
|
|
1500
|
+
|
|
1209
1501
|
describe('switchNetwork', () => {
|
|
1210
1502
|
it('should delegate to connection manager', async () => {
|
|
1211
1503
|
const { ConnectionManager } = await import(
|
|
@@ -72,7 +72,7 @@ import type {
|
|
|
72
72
|
ExtendedStandardWalletAdapter,
|
|
73
73
|
LegacyInjectedSolanaDiscoveryFailure
|
|
74
74
|
} from './wallet-standard-discovery'
|
|
75
|
-
import { parseError } from './utils/error-utils'
|
|
75
|
+
import { isUnsupportedAccountError, parseError } from './utils/error-utils'
|
|
76
76
|
import { resolveSigningAccount } from './utils/solana-account-utils'
|
|
77
77
|
|
|
78
78
|
/**
|
|
@@ -454,14 +454,45 @@ export class InjectedConnector implements Connector {
|
|
|
454
454
|
await waitBeforeSecondaryPrompt(promptGapMs)
|
|
455
455
|
}
|
|
456
456
|
// Not connected, need to establish a new connection.
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
457
|
+
try {
|
|
458
|
+
result = await this.connectToNamespace(
|
|
459
|
+
targetNamespace,
|
|
460
|
+
network,
|
|
461
|
+
provider,
|
|
462
|
+
requiredAddresses,
|
|
463
|
+
promptGapMs,
|
|
464
|
+
attempt
|
|
465
|
+
)
|
|
466
|
+
} catch (error) {
|
|
467
|
+
// The wallet's active account cannot hold the auto-picked EVM network
|
|
468
|
+
// (Phantom, Solana-only account): connect the same wallet on Solana.
|
|
469
|
+
const fallback = options?.fallback
|
|
470
|
+
// Superseded, disconnected or cancelled: nobody awaits a new prompt.
|
|
471
|
+
const isLive = () =>
|
|
472
|
+
this.connectionManager.isAttemptLive(attempt) &&
|
|
473
|
+
!fallback?.signal.aborted
|
|
474
|
+
if (
|
|
475
|
+
!fallback ||
|
|
476
|
+
targetNamespace !== 'eip155' ||
|
|
477
|
+
fallback.network.namespace !== 'solana' ||
|
|
478
|
+
!provider.namespaceMetaData.solana?.detectedWallet?.uuid ||
|
|
479
|
+
!isLive() ||
|
|
480
|
+
!isUnsupportedAccountError(error)
|
|
481
|
+
) {
|
|
482
|
+
throw error
|
|
483
|
+
}
|
|
484
|
+
// The "Unsupported account" window just closed: space the Solana
|
|
485
|
+
// prompt from it, as any prompt that follows another.
|
|
486
|
+
await waitBeforeSecondaryPrompt(promptGapMs)
|
|
487
|
+
if (!isLive()) throw error
|
|
488
|
+
fallback.onFallback()
|
|
489
|
+
result = await this.connectionManager.connectSolanaFallback(
|
|
490
|
+
fallback.network,
|
|
491
|
+
provider,
|
|
492
|
+
promptGapMs,
|
|
493
|
+
isLive
|
|
494
|
+
)
|
|
495
|
+
}
|
|
465
496
|
}
|
|
466
497
|
|
|
467
498
|
// Cross-namespace Tron auto-connect. When the Tron connector owns the Tron
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
2
2
|
import type { ExtensionInjectedProvider, Network } from '@meshconnect/uwc-types'
|
|
3
3
|
import { ConnectionManager } from './connection-manager'
|
|
4
|
+
import { isUnsupportedAccountError } from '../utils/error-utils'
|
|
4
5
|
|
|
5
6
|
function makeProvider(
|
|
6
7
|
namespaces: Partial<
|
|
@@ -3358,6 +3359,9 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
|
|
|
3358
3359
|
await outcome
|
|
3359
3360
|
expect(ethService.connect).toHaveBeenCalledTimes(2)
|
|
3360
3361
|
expect(stepsOf('awaitOpenPrompt')).toEqual([])
|
|
3362
|
+
// What the connect surfaces is still Phantom's refusal, so InjectedConnector
|
|
3363
|
+
// can move an auto-picked connect to Solana (CUS-173).
|
|
3364
|
+
expect(isUnsupportedAccountError(await connecting.catch(e => e))).toBe(true)
|
|
3361
3365
|
|
|
3362
3366
|
earlier.answer('0xABC')
|
|
3363
3367
|
await vi.advanceTimersByTimeAsync(0)
|
|
@@ -3483,3 +3487,145 @@ describe('ConnectionManager — EVM connect behind an open wallet window (ONC-23
|
|
|
3483
3487
|
expect(stepsOf('awaitOpenPrompt')).toEqual([])
|
|
3484
3488
|
})
|
|
3485
3489
|
})
|
|
3490
|
+
|
|
3491
|
+
describe('ConnectionManager — Solana fallback for a Solana-only account (CUS-173)', () => {
|
|
3492
|
+
// Phantom with a Solana-only active account refuses EVM; the connect moves to
|
|
3493
|
+
// Solana and must not ask for EVM again in its gather, which would reopen
|
|
3494
|
+
// Phantom's "Unsupported account" window.
|
|
3495
|
+
let ethService: ReturnType<typeof makeEthService>
|
|
3496
|
+
let solanaService: ReturnType<typeof makeSolanaService>
|
|
3497
|
+
let onConnectStep: ReturnType<typeof vi.fn>
|
|
3498
|
+
const solana = makeNetwork(
|
|
3499
|
+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
3500
|
+
'solana'
|
|
3501
|
+
)
|
|
3502
|
+
const provider = () =>
|
|
3503
|
+
makeProvider({
|
|
3504
|
+
eip155: { detectedWallet: { uuid: 'phantom-eip155' } },
|
|
3505
|
+
solana: { detectedWallet: { uuid: 'phantom-solana' } }
|
|
3506
|
+
})
|
|
3507
|
+
|
|
3508
|
+
function build() {
|
|
3509
|
+
return new ConnectionManager(
|
|
3510
|
+
ethService as any,
|
|
3511
|
+
solanaService as any,
|
|
3512
|
+
makeTronService() as any,
|
|
3513
|
+
makeTonService() as any,
|
|
3514
|
+
makeBitcoinService() as any,
|
|
3515
|
+
{ onConnectStep, secondaryGatherTimeoutMs: 1000 }
|
|
3516
|
+
)
|
|
3517
|
+
}
|
|
3518
|
+
|
|
3519
|
+
function stepsOf(step: string) {
|
|
3520
|
+
return onConnectStep.mock.calls
|
|
3521
|
+
.map(([info]) => info)
|
|
3522
|
+
.filter(info => info.step === step)
|
|
3523
|
+
}
|
|
3524
|
+
|
|
3525
|
+
beforeEach(() => {
|
|
3526
|
+
ethService = makeEthService()
|
|
3527
|
+
ethService.findWalletByUuid.mockReturnValue({ provider: {} })
|
|
3528
|
+
solanaService = makeSolanaService()
|
|
3529
|
+
solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
|
|
3530
|
+
onConnectStep = vi.fn()
|
|
3531
|
+
})
|
|
3532
|
+
|
|
3533
|
+
it('connects on Solana without gathering EVM, as a namespaceFallback step', async () => {
|
|
3534
|
+
const result = await build().connectSolanaFallback(solana, provider())
|
|
3535
|
+
|
|
3536
|
+
expect(result).toMatchObject({
|
|
3537
|
+
networkId: solana.id,
|
|
3538
|
+
address: 'SolAddr1'
|
|
3539
|
+
})
|
|
3540
|
+
expect(solanaService.connect).toHaveBeenCalledTimes(1)
|
|
3541
|
+
expect(ethService.connect).not.toHaveBeenCalled()
|
|
3542
|
+
expect(ethService.checkExistingConnection).not.toHaveBeenCalled()
|
|
3543
|
+
expect(
|
|
3544
|
+
stepsOf('secondaryGather').filter(s => s.namespace === 'eip155')
|
|
3545
|
+
).toEqual([])
|
|
3546
|
+
expect(stepsOf('namespaceFallback')).toEqual([
|
|
3547
|
+
expect.objectContaining({ outcome: 'ok', namespace: 'solana' })
|
|
3548
|
+
])
|
|
3549
|
+
})
|
|
3550
|
+
|
|
3551
|
+
it('still gathers EVM on a plain Solana connect', async () => {
|
|
3552
|
+
await build().connectSolana(solana, provider())
|
|
3553
|
+
|
|
3554
|
+
expect(
|
|
3555
|
+
stepsOf('secondaryGather').filter(s => s.namespace === 'eip155')
|
|
3556
|
+
).toHaveLength(1)
|
|
3557
|
+
})
|
|
3558
|
+
|
|
3559
|
+
it('reports a refused Solana prompt as a failed namespaceFallback step and rethrows', async () => {
|
|
3560
|
+
solanaService.connect.mockRejectedValueOnce(
|
|
3561
|
+
Object.assign(new Error('User rejected the request.'), { code: 4001 })
|
|
3562
|
+
)
|
|
3563
|
+
|
|
3564
|
+
await expect(
|
|
3565
|
+
build().connectSolanaFallback(solana, provider())
|
|
3566
|
+
).rejects.toThrow(/rejected/)
|
|
3567
|
+
expect(stepsOf('namespaceFallback')).toEqual([
|
|
3568
|
+
expect.objectContaining({ outcome: 'failed', namespace: 'solana' })
|
|
3569
|
+
])
|
|
3570
|
+
})
|
|
3571
|
+
|
|
3572
|
+
it.each([
|
|
3573
|
+
['settles', 'ok'],
|
|
3574
|
+
['is refused', 'failed']
|
|
3575
|
+
] as const)(
|
|
3576
|
+
'drops the namespaceFallback record when a newer connect started before the prompt %s',
|
|
3577
|
+
async (_, outcome) => {
|
|
3578
|
+
let settle: {
|
|
3579
|
+
resolve: (v: unknown) => void
|
|
3580
|
+
reject: (e: unknown) => void
|
|
3581
|
+
} = { resolve: () => {}, reject: () => {} }
|
|
3582
|
+
solanaService.connect.mockImplementationOnce(
|
|
3583
|
+
() =>
|
|
3584
|
+
new Promise((resolve, reject) => {
|
|
3585
|
+
settle = { resolve, reject }
|
|
3586
|
+
})
|
|
3587
|
+
)
|
|
3588
|
+
const manager = build()
|
|
3589
|
+
const first = manager.beginAttempt()
|
|
3590
|
+
const pending = manager
|
|
3591
|
+
.connectSolanaFallback(solana, provider(), 0, () =>
|
|
3592
|
+
manager.isAttemptLive(first)
|
|
3593
|
+
)
|
|
3594
|
+
.catch(e => e)
|
|
3595
|
+
await vi.waitFor(() => expect(solanaService.connect).toHaveBeenCalled())
|
|
3596
|
+
|
|
3597
|
+
manager.beginAttempt()
|
|
3598
|
+
if (outcome === 'ok') settle.resolve('SolAddr1')
|
|
3599
|
+
else settle.reject(new Error('User rejected the request.'))
|
|
3600
|
+
await pending
|
|
3601
|
+
|
|
3602
|
+
expect(stepsOf('namespaceFallback')).toEqual([])
|
|
3603
|
+
}
|
|
3604
|
+
)
|
|
3605
|
+
|
|
3606
|
+
it('still reports the namespaceFallback record of a live attempt', async () => {
|
|
3607
|
+
const manager = build()
|
|
3608
|
+
const attempt = manager.beginAttempt()
|
|
3609
|
+
|
|
3610
|
+
await manager.connectSolanaFallback(solana, provider(), 0, () =>
|
|
3611
|
+
manager.isAttemptLive(attempt)
|
|
3612
|
+
)
|
|
3613
|
+
|
|
3614
|
+
expect(stepsOf('namespaceFallback')).toEqual([
|
|
3615
|
+
expect.objectContaining({ outcome: 'ok', namespace: 'solana' })
|
|
3616
|
+
])
|
|
3617
|
+
})
|
|
3618
|
+
|
|
3619
|
+
it('tells a live attempt from a superseded or disconnected one', async () => {
|
|
3620
|
+
const manager = build()
|
|
3621
|
+
const first = manager.beginAttempt()
|
|
3622
|
+
expect(manager.isAttemptLive(first)).toBe(true)
|
|
3623
|
+
|
|
3624
|
+
const second = manager.beginAttempt()
|
|
3625
|
+
expect(manager.isAttemptLive(first)).toBe(false)
|
|
3626
|
+
expect(manager.isAttemptLive(second)).toBe(true)
|
|
3627
|
+
|
|
3628
|
+
await manager.disconnect()
|
|
3629
|
+
expect(manager.isAttemptLive(second)).toBe(false)
|
|
3630
|
+
})
|
|
3631
|
+
})
|
|
@@ -1123,7 +1123,10 @@ export class ConnectionManager {
|
|
|
1123
1123
|
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider,
|
|
1124
1124
|
availableAddresses: AvailableAddress[],
|
|
1125
1125
|
silentOnly = false,
|
|
1126
|
-
promptGapMs = 0
|
|
1126
|
+
promptGapMs = 0,
|
|
1127
|
+
// Namespaces this connect already knows the wallet cannot serve, so no
|
|
1128
|
+
// gather asks for them (a fallback's refused target).
|
|
1129
|
+
skipGather: ReadonlySet<Namespace> = new Set()
|
|
1127
1130
|
): Promise<boolean> {
|
|
1128
1131
|
// A silent gather never opens a window, so it is not tracked.
|
|
1129
1132
|
const promptKey = silentOnly
|
|
@@ -1132,7 +1135,7 @@ export class ConnectionManager {
|
|
|
1132
1135
|
// Every leg runs; `prompted` is whether any of them sent a request that can
|
|
1133
1136
|
// prompt.
|
|
1134
1137
|
let prompted = false
|
|
1135
|
-
if (connectedNamespace !== 'eip155') {
|
|
1138
|
+
if (connectedNamespace !== 'eip155' && !skipGather.has('eip155')) {
|
|
1136
1139
|
const legPrompted = await this.gatherNamespace(
|
|
1137
1140
|
'eip155',
|
|
1138
1141
|
leg =>
|
|
@@ -1332,6 +1335,52 @@ export class ConnectionManager {
|
|
|
1332
1335
|
return { generation: this.attemptGeneration }
|
|
1333
1336
|
}
|
|
1334
1337
|
|
|
1338
|
+
/** Whether `attempt` is still the latest connect, not superseded or disconnected. */
|
|
1339
|
+
isAttemptLive(attempt: ConnectAttempt): boolean {
|
|
1340
|
+
return attempt.generation === this.attemptGeneration
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
/**
|
|
1344
|
+
* Connects on Solana after the wallet refused the EVM target because its
|
|
1345
|
+
* active account holds no EVM address (`isUnsupportedAccountError`). Skips
|
|
1346
|
+
* the EVM gather: asking for EVM accounts again would only reopen the
|
|
1347
|
+
* wallet's "Unsupported account" window.
|
|
1348
|
+
*
|
|
1349
|
+
* The `namespaceFallback` step wraps a wallet prompt, so it can settle after
|
|
1350
|
+
* a newer connect started; the step sink files a record under whichever
|
|
1351
|
+
* handoff is current then. A record from a connect that is no longer live
|
|
1352
|
+
* (`isLive`: superseded in any mode, cancelled or disconnected) is therefore
|
|
1353
|
+
* dropped rather than filed under the newer one.
|
|
1354
|
+
*/
|
|
1355
|
+
async connectSolanaFallback(
|
|
1356
|
+
network: Network,
|
|
1357
|
+
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider,
|
|
1358
|
+
promptGapMs = 0,
|
|
1359
|
+
isLive: () => boolean = () => true
|
|
1360
|
+
): Promise<ConnectorResult> {
|
|
1361
|
+
const connect = () =>
|
|
1362
|
+
this.connectSolana(network, provider, promptGapMs, new Set(['eip155']))
|
|
1363
|
+
if (!this.steps.enabled) return await connect()
|
|
1364
|
+
const start = Date.now()
|
|
1365
|
+
const report = (outcome: 'ok' | 'failed') => {
|
|
1366
|
+
if (!isLive()) return
|
|
1367
|
+
this.steps.report({
|
|
1368
|
+
step: 'namespaceFallback',
|
|
1369
|
+
outcome,
|
|
1370
|
+
durationMs: Date.now() - start,
|
|
1371
|
+
namespace: 'solana'
|
|
1372
|
+
})
|
|
1373
|
+
}
|
|
1374
|
+
try {
|
|
1375
|
+
const result = await connect()
|
|
1376
|
+
report('ok')
|
|
1377
|
+
return result
|
|
1378
|
+
} catch (error) {
|
|
1379
|
+
report('failed')
|
|
1380
|
+
throw error
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1335
1384
|
/** Track `request` as open in the wallet until it settles; returns it. */
|
|
1336
1385
|
private trackPrompt<T>(
|
|
1337
1386
|
key: string | undefined,
|
|
@@ -1372,8 +1421,7 @@ export class ConnectionManager {
|
|
|
1372
1421
|
wallet: { uuid: string; provider: EthereumProvider },
|
|
1373
1422
|
attempt: ConnectAttempt | undefined
|
|
1374
1423
|
): Promise<string> {
|
|
1375
|
-
const isLive = () =>
|
|
1376
|
-
attempt === undefined || attempt.generation === this.attemptGeneration
|
|
1424
|
+
const isLive = () => attempt === undefined || this.isAttemptLive(attempt)
|
|
1377
1425
|
// Disconnected or superseded before its primary request: sending it
|
|
1378
1426
|
// would open a prompt nobody waits for, or one that refuses the newer
|
|
1379
1427
|
// attempt's.
|
|
@@ -1489,7 +1537,8 @@ export class ConnectionManager {
|
|
|
1489
1537
|
async connectSolana(
|
|
1490
1538
|
network: Network,
|
|
1491
1539
|
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider,
|
|
1492
|
-
promptGapMs = 0
|
|
1540
|
+
promptGapMs = 0,
|
|
1541
|
+
skipGather: ReadonlySet<Namespace> = new Set()
|
|
1493
1542
|
): Promise<ConnectorResult> {
|
|
1494
1543
|
try {
|
|
1495
1544
|
const walletUuid =
|
|
@@ -1525,7 +1574,8 @@ export class ConnectionManager {
|
|
|
1525
1574
|
provider,
|
|
1526
1575
|
availableAddresses,
|
|
1527
1576
|
false,
|
|
1528
|
-
promptGapMs
|
|
1577
|
+
promptGapMs,
|
|
1578
|
+
skipGather
|
|
1529
1579
|
)
|
|
1530
1580
|
|
|
1531
1581
|
return {
|