@meshconnect/uwc-injected-connector 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/injected-connector.d.ts +18 -1
  2. package/dist/injected-connector.d.ts.map +1 -1
  3. package/dist/injected-connector.js +45 -4
  4. package/dist/injected-connector.js.map +1 -1
  5. package/dist/services/connection-manager.d.ts +67 -1
  6. package/dist/services/connection-manager.d.ts.map +1 -1
  7. package/dist/services/connection-manager.js +130 -18
  8. package/dist/services/connection-manager.js.map +1 -1
  9. package/dist/services/ethereum/ethereum-transaction-builder.d.ts.map +1 -1
  10. package/dist/services/ethereum/ethereum-transaction-builder.js +3 -2
  11. package/dist/services/ethereum/ethereum-transaction-builder.js.map +1 -1
  12. package/dist/services/ethereum/ethereum-wallet-service.d.ts +10 -3
  13. package/dist/services/ethereum/ethereum-wallet-service.d.ts.map +1 -1
  14. package/dist/services/ethereum/ethereum-wallet-service.js +15 -7
  15. package/dist/services/ethereum/ethereum-wallet-service.js.map +1 -1
  16. package/dist/utils/step-reporter.d.ts +33 -0
  17. package/dist/utils/step-reporter.d.ts.map +1 -0
  18. package/dist/utils/step-reporter.js +54 -0
  19. package/dist/utils/step-reporter.js.map +1 -0
  20. package/dist/utils/with-deadline.d.ts +30 -0
  21. package/dist/utils/with-deadline.d.ts.map +1 -0
  22. package/dist/utils/with-deadline.js +67 -0
  23. package/dist/utils/with-deadline.js.map +1 -0
  24. package/package.json +5 -5
  25. package/src/injected-connector.test.ts +97 -0
  26. package/src/injected-connector.ts +68 -5
  27. package/src/services/connection-manager.test.ts +234 -0
  28. package/src/services/connection-manager.ts +187 -28
  29. package/src/services/ethereum/ethereum-transaction-builder.test.ts +97 -0
  30. package/src/services/ethereum/ethereum-transaction-builder.ts +3 -2
  31. package/src/services/ethereum/ethereum-wallet-service.ts +28 -8
  32. package/src/utils/step-reporter.test.ts +56 -0
  33. package/src/utils/step-reporter.ts +83 -0
  34. package/src/utils/with-deadline.test.ts +80 -0
  35. package/src/utils/with-deadline.ts +67 -0
@@ -22,7 +22,8 @@ import type {
22
22
  WalletMetadata,
23
23
  TonConnectConfig,
24
24
  WalletLifecycleEvent,
25
- WalletLifecycleContext
25
+ WalletLifecycleContext,
26
+ WalletStepInfo
26
27
  } from '@meshconnect/uwc-types'
27
28
  import { assertSameMessage } from '@meshconnect/uwc-types'
28
29
  import {
@@ -38,11 +39,16 @@ import { SolanaWalletService } from './services/solana/solana-wallet-service'
38
39
  import { TronWalletService } from './services/tron/tron-wallet-service'
39
40
  import { TonWalletService } from './services/ton/ton-wallet-service'
40
41
  import { StorageService } from './services/storage-service'
41
- import { ConnectionManager } from './services/connection-manager'
42
+ import {
43
+ ConnectionManager,
44
+ DEFAULT_SECONDARY_GATHER_TIMEOUT_MS
45
+ } from './services/connection-manager'
42
46
  import type {
43
47
  SecondarySolanaConnectSkippedInfo,
44
48
  SecondarySolanaConnectGatheredInfo
45
49
  } from './services/connection-manager'
50
+ import { withDeadline } from './utils/with-deadline'
51
+ import { createStepReporter, type StepReporter } from './utils/step-reporter'
46
52
  import { SignatureService } from './services/signature-service'
47
53
  import { TransactionService } from './services/transaction-service'
48
54
  import type {
@@ -105,6 +111,19 @@ export interface InjectedConnectorOptions {
105
111
  onWalletConnectionsReleased?:
106
112
  | ((info: { namespaces: Namespace[] }) => void)
107
113
  | undefined
114
+ /**
115
+ * Step-level telemetry sink (ONC-183): one call per named phase of a connect
116
+ * — provider detection, the authorizing wallet call, the chain read, and each
117
+ * best-effort secondary-namespace gather. Core forwards these as `walletStep`
118
+ * events, stamped with the enclosing handoff id.
119
+ */
120
+ onConnectStep?: ((info: WalletStepInfo) => void) | undefined
121
+ /**
122
+ * Per-namespace deadline (ms) for a best-effort SECONDARY address gather.
123
+ * `0` disables the bound. Defaults to
124
+ * {@link DEFAULT_SECONDARY_GATHER_TIMEOUT_MS}.
125
+ */
126
+ secondaryGatherTimeoutMs?: number | undefined
108
127
  }
109
128
 
110
129
  export class InjectedConnector implements Connector {
@@ -140,6 +159,10 @@ export class InjectedConnector implements Connector {
140
159
  /** injectedIds already reported this session — dedupes the emit across the
141
160
  * repeated Solana detection cycles (mirrors the bridgeError source dedup). */
142
161
  private readonly reportedLegacyFailures = new Set<string>()
162
+ /** Step-level telemetry reporter (ONC-183); see `InjectedConnectorOptions`. */
163
+ private readonly steps: StepReporter
164
+ /** Deadline for the cross-namespace Tron gather (see `enrichWithTron`). */
165
+ private readonly secondaryGatherTimeoutMs: number
143
166
 
144
167
  /** Forward a legacy Solana discovery failure to the sink, once per injectedId. */
145
168
  private reportLegacyDiscoveryFailure: LegacyInjectedSolanaDiscoveryFailure =
@@ -161,7 +184,9 @@ export class InjectedConnector implements Connector {
161
184
  onLegacyDiscoveryFailure,
162
185
  onSecondarySolanaConnectSkipped,
163
186
  onSecondarySolanaConnectGathered,
164
- onWalletConnectionsReleased
187
+ onWalletConnectionsReleased,
188
+ onConnectStep,
189
+ secondaryGatherTimeoutMs
165
190
  } = options
166
191
 
167
192
  this.networkRpcMap = networkRpcMap
@@ -190,9 +215,14 @@ export class InjectedConnector implements Connector {
190
215
  usingIntegratedBrowser,
191
216
  onSecondarySolanaConnectSkipped,
192
217
  onSecondarySolanaConnectGathered,
193
- onWalletConnectionsReleased
218
+ onWalletConnectionsReleased,
219
+ onConnectStep,
220
+ secondaryGatherTimeoutMs
194
221
  }
195
222
  )
223
+ this.steps = createStepReporter(onConnectStep)
224
+ this.secondaryGatherTimeoutMs =
225
+ secondaryGatherTimeoutMs ?? DEFAULT_SECONDARY_GATHER_TIMEOUT_MS
196
226
  this.signatureService = new SignatureService()
197
227
  this.transactionService = new TransactionService(this.networkRpcMap)
198
228
 
@@ -402,18 +432,51 @@ export class InjectedConnector implements Connector {
402
432
  )
403
433
  if (!tronNetwork) return
404
434
 
435
+ const start = Date.now()
436
+ let timedOut = false
405
437
  try {
406
438
  // Pass the provider — the Tron connector reads its tron.injectedId.
407
- const result = await this.tronConnector.connect(tronNetwork, provider)
439
+ // Deadline-bounded like the ConnectionManager gathers (ONC-183): this runs
440
+ // after the target namespace is already authorized, so a Tron prompt the
441
+ // user never answers must cost the Tron address, not the whole connect.
442
+ const result = await withDeadline(
443
+ () => this.tronConnector!.connect(tronNetwork, provider),
444
+ this.secondaryGatherTimeoutMs,
445
+ () => {
446
+ timedOut = true
447
+ this.steps.report({
448
+ step: 'secondaryGather',
449
+ outcome: 'timedOut',
450
+ durationMs: Date.now() - start,
451
+ namespace: 'tron',
452
+ timeoutMs: this.secondaryGatherTimeoutMs
453
+ })
454
+ }
455
+ )
456
+ if (!result) return
408
457
  for (const addr of result.availableAddresses) {
409
458
  const dup = availableAddresses.some(
410
459
  a => a.networkId === addr.networkId && a.address === addr.address
411
460
  )
412
461
  if (!dup) availableAddresses.push(addr)
413
462
  }
463
+ this.steps.report({
464
+ step: 'secondaryGather',
465
+ outcome: 'ok',
466
+ durationMs: Date.now() - start,
467
+ namespace: 'tron'
468
+ })
414
469
  } catch {
415
470
  // Tron unavailable or the user declined the prompt — continue without it,
416
471
  // same as the Solana/Tron/TON gathers in ConnectionManager.
472
+ if (!timedOut) {
473
+ this.steps.report({
474
+ step: 'secondaryGather',
475
+ outcome: 'failed',
476
+ durationMs: Date.now() - start,
477
+ namespace: 'tron'
478
+ })
479
+ }
417
480
  }
418
481
  }
419
482
 
@@ -1313,3 +1313,237 @@ describe('ConnectionManager — switchNetwork returns LIVE addresses (not connec
1313
1313
  ])
1314
1314
  })
1315
1315
  })
1316
+
1317
+ describe('ConnectionManager — bounded secondary gathers + step telemetry (ONC-183)', () => {
1318
+ let ethService: ReturnType<typeof makeEthService>
1319
+ let solanaService: ReturnType<typeof makeSolanaService>
1320
+ let tronService: ReturnType<typeof makeTronService>
1321
+ let tonService: ReturnType<typeof makeTonService>
1322
+ let onConnectStep: ReturnType<typeof vi.fn>
1323
+
1324
+ function build(secondaryGatherTimeoutMs?: number) {
1325
+ return new ConnectionManager(
1326
+ ethService as any,
1327
+ solanaService as any,
1328
+ tronService as any,
1329
+ tonService as any,
1330
+ {
1331
+ onConnectStep,
1332
+ ...(secondaryGatherTimeoutMs !== undefined && {
1333
+ secondaryGatherTimeoutMs
1334
+ })
1335
+ }
1336
+ )
1337
+ }
1338
+
1339
+ /** Steps of one kind, in emit order. */
1340
+ function stepsOf(step: string) {
1341
+ return onConnectStep.mock.calls
1342
+ .map(([info]) => info)
1343
+ .filter(info => info.step === step)
1344
+ }
1345
+
1346
+ beforeEach(() => {
1347
+ vi.useFakeTimers()
1348
+ ethService = makeEthService()
1349
+ solanaService = makeSolanaService()
1350
+ tronService = makeTronService()
1351
+ tonService = makeTonService()
1352
+ onConnectStep = vi.fn()
1353
+ })
1354
+
1355
+ afterEach(() => {
1356
+ vi.useRealTimers()
1357
+ })
1358
+
1359
+ it('completes the connect when a secondary Solana gather never answers', async () => {
1360
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
1361
+ // The motivating hang: MetaMask's Solana Snap prompt is opened and never
1362
+ // answered, AFTER the user already approved EVM.
1363
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
1364
+ solanaService.connect.mockReturnValue(new Promise(() => {}))
1365
+
1366
+ const manager = build(1000)
1367
+ const provider = makeProvider({
1368
+ eip155: { detectedWallet: { uuid: 'mm-eip155' } },
1369
+ solana: { detectedWallet: { uuid: 'mm-solana' } }
1370
+ })
1371
+
1372
+ const pending = manager.connectEthereum(
1373
+ makeNetwork('eip155:1', 'eip155'),
1374
+ provider
1375
+ )
1376
+ await vi.advanceTimersByTimeAsync(1000)
1377
+ const result = await pending
1378
+
1379
+ // The namespace the user asked for still connects; only the abandoned
1380
+ // secondary leg is dropped.
1381
+ expect(result.address).toBe('0xABC')
1382
+ expect(result.availableAddresses).toEqual([
1383
+ { address: '0xABC', networkId: 'eip155:1' }
1384
+ ])
1385
+ })
1386
+
1387
+ it('reports the abandoned gather as a timedOut secondaryGather step', async () => {
1388
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
1389
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
1390
+ solanaService.connect.mockReturnValue(new Promise(() => {}))
1391
+
1392
+ const manager = build(1000)
1393
+ const pending = manager.connectEthereum(
1394
+ makeNetwork('eip155:1', 'eip155'),
1395
+ makeProvider({
1396
+ eip155: { detectedWallet: { uuid: 'mm-eip155' } },
1397
+ solana: { detectedWallet: { uuid: 'mm-solana' } }
1398
+ })
1399
+ )
1400
+ await vi.advanceTimersByTimeAsync(1000)
1401
+ await pending
1402
+
1403
+ const solanaGather = stepsOf('secondaryGather').find(
1404
+ s => s.namespace === 'solana'
1405
+ )
1406
+ expect(solanaGather).toMatchObject({
1407
+ outcome: 'timedOut',
1408
+ namespace: 'solana',
1409
+ timeoutMs: 1000
1410
+ })
1411
+ })
1412
+
1413
+ it('gives each namespace its own budget — a hung leg does not starve later ones', async () => {
1414
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
1415
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
1416
+ solanaService.connect.mockReturnValue(new Promise(() => {}))
1417
+ // Tron is gathered AFTER the hung Solana leg and must still be reached.
1418
+ tronService.findWalletByUuid.mockReturnValue({ provider: {} })
1419
+ tronService.checkExistingConnection.mockResolvedValue('TronAddr1')
1420
+
1421
+ const manager = build(1000)
1422
+ const pending = manager.connectEthereum(
1423
+ makeNetwork('eip155:1', 'eip155'),
1424
+ makeProvider({
1425
+ eip155: { detectedWallet: { uuid: 'mm-eip155' } },
1426
+ solana: { detectedWallet: { uuid: 'mm-solana' } },
1427
+ tron: { detectedWallet: { uuid: 'mm-tron' } }
1428
+ })
1429
+ )
1430
+ await vi.advanceTimersByTimeAsync(1000)
1431
+ const result = await pending
1432
+
1433
+ expect(
1434
+ result.availableAddresses.some(a => a.networkId.startsWith('tron:'))
1435
+ ).toBe(true)
1436
+ })
1437
+
1438
+ it('reports providerDetection as failed when the wallet was never detected', async () => {
1439
+ // No detectedWallet uuid — the retryable "provider was not detected" class.
1440
+ const manager = build()
1441
+ await expect(
1442
+ manager.connectEthereum(
1443
+ makeNetwork('eip155:1', 'eip155'),
1444
+ makeProvider({ eip155: { injectedId: 'isMetaMask' } })
1445
+ )
1446
+ ).rejects.toThrow()
1447
+
1448
+ expect(stepsOf('providerDetection')).toEqual([
1449
+ expect.objectContaining({ outcome: 'failed', namespace: 'eip155' })
1450
+ ])
1451
+ // The wallet was never reached, so no authorizing call was attempted.
1452
+ expect(stepsOf('primaryConnect')).toEqual([])
1453
+ })
1454
+
1455
+ it('does not let a throwing telemetry sink break a connect', async () => {
1456
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
1457
+ onConnectStep.mockImplementation(() => {
1458
+ throw new Error('sink exploded')
1459
+ })
1460
+
1461
+ const manager = build()
1462
+ const result = await manager.connectEthereum(
1463
+ makeNetwork('eip155:1', 'eip155'),
1464
+ makeProvider({ eip155: { detectedWallet: { uuid: 'mm-eip155' } } })
1465
+ )
1466
+ expect(result.address).toBe('0xABC')
1467
+ })
1468
+ })
1469
+
1470
+ describe('ConnectionManager — secondary gather outcome fidelity (ONC-183 review)', () => {
1471
+ let ethService: ReturnType<typeof makeEthService>
1472
+ let solanaService: ReturnType<typeof makeSolanaService>
1473
+ let tronService: ReturnType<typeof makeTronService>
1474
+ let tonService: ReturnType<typeof makeTonService>
1475
+ let onConnectStep: ReturnType<typeof vi.fn>
1476
+
1477
+ function build() {
1478
+ return new ConnectionManager(
1479
+ ethService as any,
1480
+ solanaService as any,
1481
+ tronService as any,
1482
+ tonService as any,
1483
+ { onConnectStep }
1484
+ )
1485
+ }
1486
+
1487
+ function gatherStep(namespace: string) {
1488
+ return onConnectStep.mock.calls
1489
+ .map(([info]) => info)
1490
+ .find(i => i.step === 'secondaryGather' && i.namespace === namespace)
1491
+ }
1492
+
1493
+ beforeEach(() => {
1494
+ ethService = makeEthService()
1495
+ solanaService = makeSolanaService()
1496
+ tronService = makeTronService()
1497
+ tonService = makeTonService()
1498
+ onConnectStep = vi.fn()
1499
+ })
1500
+
1501
+ it('reports a DECLINED secondary Solana prompt as failed, not ok', async () => {
1502
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
1503
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
1504
+ solanaService.checkExistingConnection.mockResolvedValue(null)
1505
+ // The helper swallows this internally — it must NOT surface as `ok`.
1506
+ solanaService.connect.mockRejectedValue(
1507
+ new Error('User rejected the request')
1508
+ )
1509
+
1510
+ const result = await build().connectEthereum(
1511
+ makeNetwork('eip155:1', 'eip155'),
1512
+ makeProvider({
1513
+ eip155: { detectedWallet: { uuid: 'mm-eip155' } },
1514
+ solana: { detectedWallet: { uuid: 'mm-solana' } }
1515
+ })
1516
+ )
1517
+
1518
+ // Connect still succeeds — the swallow keeps best-effort behaviour intact.
1519
+ expect(result.address).toBe('0xABC')
1520
+ expect(gatherStep('solana')).toMatchObject({ outcome: 'failed' })
1521
+ })
1522
+
1523
+ it('still reports ok for a clean gather', async () => {
1524
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
1525
+ solanaService.findWalletByUuid.mockReturnValue({ adapter: {} })
1526
+ solanaService.checkExistingConnection.mockResolvedValue('SolAddr1')
1527
+
1528
+ await build().connectEthereum(
1529
+ makeNetwork('eip155:1', 'eip155'),
1530
+ makeProvider({
1531
+ eip155: { detectedWallet: { uuid: 'mm-eip155' } },
1532
+ solana: { detectedWallet: { uuid: 'mm-solana' } }
1533
+ })
1534
+ )
1535
+
1536
+ expect(gatherStep('solana')).toMatchObject({ outcome: 'ok' })
1537
+ })
1538
+
1539
+ it('reports ok for a namespace with nothing to gather (no wallet detected)', async () => {
1540
+ ethService.findWalletByUuid.mockReturnValue({ provider: {} })
1541
+ // No solana detectedWallet → the helper is a clean no-op, not a failure.
1542
+ await build().connectEthereum(
1543
+ makeNetwork('eip155:1', 'eip155'),
1544
+ makeProvider({ eip155: { detectedWallet: { uuid: 'mm-eip155' } } })
1545
+ )
1546
+
1547
+ expect(gatherStep('solana')).toMatchObject({ outcome: 'ok' })
1548
+ })
1549
+ })