@meshconnect/uwc-injected-connector 0.20.3 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/eip6963-discovery.d.ts +13 -0
  2. package/dist/eip6963-discovery.d.ts.map +1 -1
  3. package/dist/eip6963-discovery.js +12 -3
  4. package/dist/eip6963-discovery.js.map +1 -1
  5. package/dist/injected-connector.d.ts +39 -2
  6. package/dist/injected-connector.d.ts.map +1 -1
  7. package/dist/injected-connector.js +170 -19
  8. package/dist/injected-connector.js.map +1 -1
  9. package/dist/services/connection-manager.d.ts +84 -5
  10. package/dist/services/connection-manager.d.ts.map +1 -1
  11. package/dist/services/connection-manager.js +172 -30
  12. package/dist/services/connection-manager.js.map +1 -1
  13. package/dist/services/ethereum/ethereum-transaction-builder.d.ts.map +1 -1
  14. package/dist/services/ethereum/ethereum-transaction-builder.js +3 -2
  15. package/dist/services/ethereum/ethereum-transaction-builder.js.map +1 -1
  16. package/dist/services/ethereum/ethereum-wallet-service.d.ts +86 -7
  17. package/dist/services/ethereum/ethereum-wallet-service.d.ts.map +1 -1
  18. package/dist/services/ethereum/ethereum-wallet-service.js +201 -28
  19. package/dist/services/ethereum/ethereum-wallet-service.js.map +1 -1
  20. package/dist/services/solana/solana-wallet-service.d.ts +57 -0
  21. package/dist/services/solana/solana-wallet-service.d.ts.map +1 -1
  22. package/dist/services/solana/solana-wallet-service.js +118 -0
  23. package/dist/services/solana/solana-wallet-service.js.map +1 -1
  24. package/dist/services/ton/ton-wallet-service.d.ts +9 -0
  25. package/dist/services/ton/ton-wallet-service.d.ts.map +1 -1
  26. package/dist/services/ton/ton-wallet-service.js +15 -0
  27. package/dist/services/ton/ton-wallet-service.js.map +1 -1
  28. package/dist/services/tron/tron-wallet-service.d.ts +11 -0
  29. package/dist/services/tron/tron-wallet-service.d.ts.map +1 -1
  30. package/dist/services/tron/tron-wallet-service.js +21 -0
  31. package/dist/services/tron/tron-wallet-service.js.map +1 -1
  32. package/dist/utils/step-reporter.d.ts +33 -0
  33. package/dist/utils/step-reporter.d.ts.map +1 -0
  34. package/dist/utils/step-reporter.js +54 -0
  35. package/dist/utils/step-reporter.js.map +1 -0
  36. package/dist/utils/with-deadline.d.ts +30 -0
  37. package/dist/utils/with-deadline.d.ts.map +1 -0
  38. package/dist/utils/with-deadline.js +67 -0
  39. package/dist/utils/with-deadline.js.map +1 -0
  40. package/package.json +6 -6
  41. package/src/eip6963-discovery.test.ts +111 -2
  42. package/src/eip6963-discovery.ts +28 -4
  43. package/src/injected-connector.test.ts +574 -16
  44. package/src/injected-connector.ts +210 -23
  45. package/src/services/connection-manager.test.ts +338 -1
  46. package/src/services/connection-manager.ts +236 -41
  47. package/src/services/ethereum/ethereum-transaction-builder.test.ts +97 -0
  48. package/src/services/ethereum/ethereum-transaction-builder.ts +3 -2
  49. package/src/services/ethereum/ethereum-wallet-service.test.ts +495 -17
  50. package/src/services/ethereum/ethereum-wallet-service.ts +248 -32
  51. package/src/services/solana/solana-wallet-service.lifecycle.test.ts +156 -6
  52. package/src/services/solana/solana-wallet-service.reconnect.test.ts +175 -0
  53. package/src/services/solana/solana-wallet-service.ts +132 -0
  54. package/src/services/ton/ton-wallet-service.test.ts +25 -0
  55. package/src/services/ton/ton-wallet-service.ts +15 -0
  56. package/src/services/tron/tron-wallet-service.test.ts +45 -0
  57. package/src/services/tron/tron-wallet-service.ts +20 -0
  58. package/src/signSolanaTransactionBytes.test.ts +1 -1
  59. package/src/utils/step-reporter.test.ts +56 -0
  60. package/src/utils/step-reporter.ts +83 -0
  61. package/src/utils/with-deadline.test.ts +80 -0
  62. package/src/utils/with-deadline.ts +67 -0
@@ -17,12 +17,13 @@ import type {
17
17
  TonNativeTransferRequest,
18
18
  NetworkRpcMap,
19
19
  EVMCapabilities,
20
- SignatureType,
20
+ SignatureResult,
21
21
  EIP712TypedData,
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
 
@@ -478,13 +541,69 @@ export class InjectedConnector implements Connector {
478
541
  }
479
542
  }
480
543
 
544
+ /**
545
+ * Real account-sync counterpart to `onLifecycleEvent` (see its doc for the
546
+ * routing rationale). `eip155` and `solana` (direct mode only — the
547
+ * solana service itself bails in the bridge, deferring to the bridge
548
+ * relay / polling) are wired; TON injected account-sync remains a
549
+ * follow-on, matching `onLifecycleEvent`'s own note (tron IS wired here
550
+ * via `tronConnector`).
551
+ */
552
+ onAccountsChanged(
553
+ listener: (accounts: string[]) => void,
554
+ context: WalletLifecycleContext
555
+ ): (() => void) | undefined {
556
+ switch (context.namespace) {
557
+ case 'eip155':
558
+ return this.ethereumService.onAccountsChanged(listener)
559
+ case 'solana':
560
+ return this.solanaService.onAccountsChanged(listener)
561
+ case 'tron':
562
+ return this.tronConnector?.onAccountsChanged?.(listener, context)
563
+ default:
564
+ return undefined
565
+ }
566
+ }
567
+
568
+ /**
569
+ * Read the wallet's LIVE current address for a given namespace (see
570
+ * `Connector.getLiveAccount`). Routes to the SAME per-namespace live reads
571
+ * `signMessage` already uses (`ethereumService`/`solanaService`/
572
+ * `tronService`.`getLiveAccount()`, `tonService.getAccount()`) so switching
573
+ * an in-wallet account is visible without a fresh connect. For tron, prefer
574
+ * the Mesh-owned `tronConnector` (when configured) over the legacy
575
+ * `tronService`, mirroring how `signMessage`/`onAccountsChanged` already
576
+ * prioritize it. Never throws — swallows any underlying error as `null`.
577
+ */
578
+ async getLiveAccount(namespace: Namespace): Promise<string | null> {
579
+ try {
580
+ switch (namespace) {
581
+ case 'eip155':
582
+ return await this.ethereumService.getLiveAccount()
583
+ case 'solana':
584
+ return await this.solanaService.getLiveAccount()
585
+ case 'tron':
586
+ return (
587
+ (await this.tronConnector?.getLiveAccount?.()) ??
588
+ (await this.tronService.getLiveAccount())
589
+ )
590
+ case 'tvm':
591
+ return this.tonService.getAccount() ?? null
592
+ default:
593
+ return null
594
+ }
595
+ } catch {
596
+ return null
597
+ }
598
+ }
599
+
481
600
  /**
482
601
  * Sign a message with the connected wallet
483
602
  */
484
603
  async signMessage(
485
604
  message: string,
486
605
  provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider
487
- ): Promise<SignatureType> {
606
+ ): Promise<SignatureResult> {
488
607
  if (!provider) {
489
608
  throw new Error('Provider is required for message signing')
490
609
  }
@@ -510,44 +629,71 @@ export class InjectedConnector implements Connector {
510
629
  const currentAddress = this.connectionManager.getCurrentAccount()
511
630
 
512
631
  if (namespace === 'eip155') {
513
- if (!currentAddress) {
514
- throw new Error('No active wallet address for signing')
515
- }
516
-
517
632
  // Get the actual Ethereum provider
518
633
  const ethProvider = this.ethereumService.getConnectedProvider()
519
634
  if (!ethProvider) {
520
635
  throw new Error('No connected Ethereum provider')
521
636
  }
522
637
 
638
+ // `SignatureResult.address` MUST be the account that ACTUALLY signs — a
639
+ // downstream verify flow claims it to the backend. Read the LIVE selected
640
+ // account (reflects an in-wallet account switch) and use the SAME address
641
+ // as the `from` for personal_sign AND as the returned address, so
642
+ // claim == from == signer. Fall back to the connect-time cache only if the
643
+ // live read is unavailable; never claim an empty address.
644
+ const signerAddress =
645
+ (await this.ethereumService.getLiveAccount()) ?? currentAddress
646
+ if (!signerAddress) {
647
+ throw new Error('No active wallet address for signing')
648
+ }
649
+
523
650
  const signature = await this.signatureService.signEthereumMessage(
524
651
  message,
525
- currentAddress,
652
+ signerAddress,
526
653
  ethProvider
527
654
  )
528
655
  return {
529
- type: 'standard',
530
- signature: signature
656
+ signature: {
657
+ type: 'standard',
658
+ signature: signature
659
+ },
660
+ address: signerAddress
531
661
  }
532
662
  } else if (namespace === 'solana') {
533
- // Get the actual Solana adapter
663
+ // Silently re-bind the adapter if a prior re-auth teardown cleared it, then
664
+ // read it SYNCHRONOUSLY — never `await` the adapter itself: in bridge mode
665
+ // it's a Comlink proxy, and awaiting that proxy forces Comlink to clone the
666
+ // (unserializable) adapter → "Unserializable return value".
667
+ await this.solanaService.ensureConnectedAdapter()
534
668
  const solanaAdapter = this.solanaService.getConnectedAdapter()
535
669
  if (!solanaAdapter) {
536
670
  throw new Error('No connected Solana adapter')
537
671
  }
538
672
 
673
+ // Claim the LIVE adapter public key (reflects an in-wallet account
674
+ // switch) rather than the connect-time cache, so the returned address is
675
+ // the account that ACTUALLY signed.
676
+ const signerAddress =
677
+ (await this.solanaService.getLiveAccount()) ?? currentAddress
678
+ if (!signerAddress) {
679
+ throw new Error('No active wallet address for signing')
680
+ }
681
+
539
682
  const signature = await this.signatureService.signSolanaMessage(
540
683
  message,
541
684
  solanaAdapter
542
685
  )
543
686
  return {
544
- type: 'standard',
545
- signature: signature
687
+ signature: {
688
+ type: 'standard',
689
+ signature: signature
690
+ },
691
+ address: signerAddress
546
692
  }
547
693
  } else if (namespace === 'tron') {
548
694
  if (this.tronConnector) {
549
- // TronConnector.signMessage already returns a SignatureType
550
- // ({ type: 'standard', signature }) — return it as-is, no re-wrap.
695
+ // TronConnector.signMessage already returns a SignatureResult
696
+ // ({ signature, address }) — return it as-is, no re-wrap.
551
697
  return await this.tronConnector.signMessage(message)
552
698
  }
553
699
  const tronProvider = this.tronService.getConnectedProvider()
@@ -555,16 +701,50 @@ export class InjectedConnector implements Connector {
555
701
  throw new Error('No connected Tron provider')
556
702
  }
557
703
 
704
+ // Claim the LIVE default address (reflects an in-wallet account switch);
705
+ // signMessageV2 signs with the wallet's current default address, so the
706
+ // returned address must match it, not the connect-time cache. Read it
707
+ // once before signing as a fallback for the throw-on-empty check below.
708
+ const preSignAddress =
709
+ (await this.tronService.getLiveAccount()) ?? currentAddress
710
+
558
711
  const signature = await this.signatureService.signTronMessage(
559
712
  message,
560
713
  tronProvider
561
714
  )
715
+
716
+ // signMessageV2 takes no address param — it implicitly signs with
717
+ // tronWeb.defaultAddress at execution time. Re-read the live address
718
+ // AFTER signing resolves (rather than reusing the pre-sign read) so
719
+ // the returned address reflects the account active at sign
720
+ // completion, not a stale snapshot from before the `await`. This
721
+ // closes the same account-switch TOCTOU window that eip155/solana
722
+ // avoid by pinning an explicit signer address.
723
+ const signerAddress =
724
+ (await this.tronService.getLiveAccount()) ?? preSignAddress
725
+ if (!signerAddress) {
726
+ throw new Error('No active wallet address for signing')
727
+ }
728
+
562
729
  return {
563
- type: 'standard',
564
- signature: signature
730
+ signature: {
731
+ type: 'standard',
732
+ signature: signature
733
+ },
734
+ address: signerAddress
565
735
  }
566
736
  } else if (namespace === 'tvm') {
567
- return await this.tonService.signMessage(message)
737
+ // For TON the signer address lives on the TON service (its own bridge
738
+ // account), not the connection manager's active account.
739
+ const tonAddress = this.tonService.getAccount()
740
+ if (!tonAddress) {
741
+ throw new Error('No active wallet address for signing')
742
+ }
743
+ const signature = await this.tonService.signMessage(message)
744
+ return {
745
+ signature,
746
+ address: tonAddress
747
+ }
568
748
  } else {
569
749
  throw new Error(`Signing not supported for namespace: ${namespace}`)
570
750
  }
@@ -646,7 +826,10 @@ export class InjectedConnector implements Connector {
646
826
  ethProvider
647
827
  )
648
828
  } else if (namespace === 'solana') {
649
- // Get the actual Solana adapter
829
+ // Silently re-bind if cleared, then read the adapter SYNCHRONOUSLY — never
830
+ // `await` the adapter proxy (bridge-mode Comlink clone → "Unserializable
831
+ // return value").
832
+ await this.solanaService.ensureConnectedAdapter()
650
833
  const solanaAdapter = this.solanaService.getConnectedAdapter()
651
834
  if (!solanaAdapter) {
652
835
  throw new Error('No connected Solana adapter')
@@ -715,6 +898,10 @@ export class InjectedConnector implements Connector {
715
898
  )
716
899
  }
717
900
 
901
+ // Silently re-bind if cleared, then read the adapter SYNCHRONOUSLY — never
902
+ // `await` the adapter proxy (bridge-mode Comlink clone → "Unserializable
903
+ // return value").
904
+ await this.solanaService.ensureConnectedAdapter()
718
905
  const solanaAdapter = this.solanaService.getConnectedAdapter()
719
906
  if (!solanaAdapter) {
720
907
  throw new Error('No connected Solana adapter')