@meshconnect/uwc-injected-connector 0.20.3 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/eip6963-discovery.d.ts +13 -0
- package/dist/eip6963-discovery.d.ts.map +1 -1
- package/dist/eip6963-discovery.js +12 -3
- package/dist/eip6963-discovery.js.map +1 -1
- package/dist/injected-connector.d.ts +39 -2
- package/dist/injected-connector.d.ts.map +1 -1
- package/dist/injected-connector.js +170 -19
- package/dist/injected-connector.js.map +1 -1
- package/dist/services/connection-manager.d.ts +84 -5
- package/dist/services/connection-manager.d.ts.map +1 -1
- package/dist/services/connection-manager.js +172 -30
- package/dist/services/connection-manager.js.map +1 -1
- package/dist/services/ethereum/ethereum-transaction-builder.d.ts.map +1 -1
- package/dist/services/ethereum/ethereum-transaction-builder.js +3 -2
- package/dist/services/ethereum/ethereum-transaction-builder.js.map +1 -1
- package/dist/services/ethereum/ethereum-wallet-service.d.ts +86 -7
- package/dist/services/ethereum/ethereum-wallet-service.d.ts.map +1 -1
- package/dist/services/ethereum/ethereum-wallet-service.js +201 -28
- package/dist/services/ethereum/ethereum-wallet-service.js.map +1 -1
- package/dist/services/solana/solana-wallet-service.d.ts +57 -0
- package/dist/services/solana/solana-wallet-service.d.ts.map +1 -1
- package/dist/services/solana/solana-wallet-service.js +118 -0
- package/dist/services/solana/solana-wallet-service.js.map +1 -1
- package/dist/services/ton/ton-wallet-service.d.ts +9 -0
- package/dist/services/ton/ton-wallet-service.d.ts.map +1 -1
- package/dist/services/ton/ton-wallet-service.js +15 -0
- package/dist/services/ton/ton-wallet-service.js.map +1 -1
- package/dist/services/tron/tron-wallet-service.d.ts +11 -0
- package/dist/services/tron/tron-wallet-service.d.ts.map +1 -1
- package/dist/services/tron/tron-wallet-service.js +21 -0
- package/dist/services/tron/tron-wallet-service.js.map +1 -1
- package/dist/utils/step-reporter.d.ts +33 -0
- package/dist/utils/step-reporter.d.ts.map +1 -0
- package/dist/utils/step-reporter.js +54 -0
- package/dist/utils/step-reporter.js.map +1 -0
- package/dist/utils/with-deadline.d.ts +30 -0
- package/dist/utils/with-deadline.d.ts.map +1 -0
- package/dist/utils/with-deadline.js +67 -0
- package/dist/utils/with-deadline.js.map +1 -0
- package/package.json +6 -6
- package/src/eip6963-discovery.test.ts +111 -2
- package/src/eip6963-discovery.ts +28 -4
- package/src/injected-connector.test.ts +574 -16
- package/src/injected-connector.ts +210 -23
- package/src/services/connection-manager.test.ts +338 -1
- package/src/services/connection-manager.ts +236 -41
- package/src/services/ethereum/ethereum-transaction-builder.test.ts +97 -0
- package/src/services/ethereum/ethereum-transaction-builder.ts +3 -2
- package/src/services/ethereum/ethereum-wallet-service.test.ts +495 -17
- package/src/services/ethereum/ethereum-wallet-service.ts +248 -32
- package/src/services/solana/solana-wallet-service.lifecycle.test.ts +156 -6
- package/src/services/solana/solana-wallet-service.reconnect.test.ts +175 -0
- package/src/services/solana/solana-wallet-service.ts +132 -0
- package/src/services/ton/ton-wallet-service.test.ts +25 -0
- package/src/services/ton/ton-wallet-service.ts +15 -0
- package/src/services/tron/tron-wallet-service.test.ts +45 -0
- package/src/services/tron/tron-wallet-service.ts +20 -0
- package/src/signSolanaTransactionBytes.test.ts +1 -1
- package/src/utils/step-reporter.test.ts +56 -0
- package/src/utils/step-reporter.ts +83 -0
- package/src/utils/with-deadline.test.ts +80 -0
- package/src/utils/with-deadline.ts +67 -0
|
@@ -5,14 +5,18 @@ import type {
|
|
|
5
5
|
EVMCapabilityStatus,
|
|
6
6
|
Network,
|
|
7
7
|
WalletMetadata,
|
|
8
|
-
WalletLifecycleEvent
|
|
8
|
+
WalletLifecycleEvent,
|
|
9
|
+
WalletStepInfo,
|
|
10
|
+
WalletStepName
|
|
9
11
|
} from '@meshconnect/uwc-types'
|
|
10
12
|
import type {
|
|
11
13
|
EthereumProvider,
|
|
14
|
+
EIP1193Provider,
|
|
12
15
|
DetectedWallet as EIP6963DetectedWallet
|
|
13
16
|
} from '../../eip6963-discovery'
|
|
14
17
|
import { getAvailableWallets as getEIP6963Wallets } from '../../eip6963-discovery'
|
|
15
18
|
import { supportedNetworks } from '@meshconnect/uwc-constants'
|
|
19
|
+
import { createStepReporter } from '../../utils/step-reporter'
|
|
16
20
|
|
|
17
21
|
/**
|
|
18
22
|
* Normalize an EIP-1193 `chainChanged` payload (hex `'0x1'`, decimal string, or a
|
|
@@ -46,6 +50,18 @@ function toCaipChainId(raw: unknown): NetworkId | undefined {
|
|
|
46
50
|
export class EthereumWalletService {
|
|
47
51
|
private detectedWallets: EIP6963DetectedWallet[] = []
|
|
48
52
|
private connectedProvider: EthereumProvider | null = null
|
|
53
|
+
private connectedWalletUuid: string | null = null
|
|
54
|
+
/**
|
|
55
|
+
* Provenance of the CONNECTED wallet's provider, captured once at
|
|
56
|
+
* connect()/setConnectionState() time (when `findWalletByUuid` is
|
|
57
|
+
* guaranteed to still resolve it) rather than re-derived later — see
|
|
58
|
+
* `attachBridgedListeners` for why this can't be a live lookup by uuid.
|
|
59
|
+
* `null` means unknown (e.g. state set without a uuid) and is treated as
|
|
60
|
+
* bridge-sourced, preserving the pre-existing bridge-first behavior.
|
|
61
|
+
*/
|
|
62
|
+
private connectedWalletSource: 'bridge' | 'local' | null = null
|
|
63
|
+
/** Wallet-stable rdns of the connected wallet, captured alongside source above. */
|
|
64
|
+
private connectedWalletRdns: string | undefined = undefined
|
|
49
65
|
private account: string | null = null
|
|
50
66
|
|
|
51
67
|
/**
|
|
@@ -88,11 +104,28 @@ export class EthereumWalletService {
|
|
|
88
104
|
|
|
89
105
|
/**
|
|
90
106
|
* Connect to wallet
|
|
107
|
+
*
|
|
108
|
+
* `onStep` (optional) reports the two wallet round-trips separately (ONC-183)
|
|
109
|
+
* — `eth_requestAccounts` is the user-facing approval, `eth_chainId` is a
|
|
110
|
+
* follow-on read against an already-granted permission, and they fail for
|
|
111
|
+
* different reasons (an abandoned prompt vs. a provider that granted accounts
|
|
112
|
+
* then went unresponsive, the bridge-mode case). Rolled together they were
|
|
113
|
+
* one opaque duration.
|
|
91
114
|
*/
|
|
92
|
-
async connect(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
115
|
+
async connect(
|
|
116
|
+
provider: EthereumProvider,
|
|
117
|
+
uuid?: string,
|
|
118
|
+
onStep?: (info: WalletStepInfo) => void
|
|
119
|
+
): Promise<string> {
|
|
120
|
+
// Shared reporter (ONC-183 review) rather than a fourth hand-rolled copy of
|
|
121
|
+
// the time-and-report shape — see `createStepReporter`.
|
|
122
|
+
const steps = createStepReporter(onStep)
|
|
123
|
+
const track = <T>(step: WalletStepName, request: () => Promise<T>) =>
|
|
124
|
+
steps.track(step, 'eip155', request)
|
|
125
|
+
|
|
126
|
+
const accounts = await track('primaryConnect', () =>
|
|
127
|
+
provider.request({ method: 'eth_requestAccounts' })
|
|
128
|
+
)
|
|
96
129
|
|
|
97
130
|
if (!accounts || accounts.length === 0) {
|
|
98
131
|
throw new Error('No accounts returned from wallet')
|
|
@@ -100,12 +133,14 @@ export class EthereumWalletService {
|
|
|
100
133
|
|
|
101
134
|
const address = accounts[0]
|
|
102
135
|
this.connectedProvider = provider
|
|
136
|
+
this.connectedWalletUuid = uuid ?? null
|
|
137
|
+
this.captureConnectedWalletProvenance(uuid)
|
|
103
138
|
this.account = address
|
|
104
139
|
|
|
105
140
|
// Request chain ID to ensure we're connected
|
|
106
|
-
await
|
|
107
|
-
method: 'eth_chainId'
|
|
108
|
-
|
|
141
|
+
await track('chainIdRead', () =>
|
|
142
|
+
provider.request({ method: 'eth_chainId' })
|
|
143
|
+
)
|
|
109
144
|
|
|
110
145
|
return address
|
|
111
146
|
}
|
|
@@ -217,12 +252,29 @@ export class EthereumWalletService {
|
|
|
217
252
|
*/
|
|
218
253
|
setConnectionState(
|
|
219
254
|
provider: EthereumProvider | null,
|
|
220
|
-
account: string | null
|
|
255
|
+
account: string | null,
|
|
256
|
+
uuid?: string | null
|
|
221
257
|
): void {
|
|
222
258
|
this.connectedProvider = provider
|
|
259
|
+
this.connectedWalletUuid = uuid ?? null
|
|
260
|
+
this.captureConnectedWalletProvenance(uuid ?? undefined)
|
|
223
261
|
this.account = account
|
|
224
262
|
}
|
|
225
263
|
|
|
264
|
+
/**
|
|
265
|
+
* Snapshot the connected wallet's `source`/`rdns` from `detectedWallets` at
|
|
266
|
+
* connect time. Deliberately NOT re-derived later: `detectedWallets` is
|
|
267
|
+
* replaced wholesale by a later `initializeDiscovery()` call (e.g. a
|
|
268
|
+
* refresh), which can drop the entry this uuid resolved to even though the
|
|
269
|
+
* wallet is still connected — a live re-lookup at subscribe time would then
|
|
270
|
+
* silently lose both fields.
|
|
271
|
+
*/
|
|
272
|
+
private captureConnectedWalletProvenance(uuid: string | undefined): void {
|
|
273
|
+
const wallet = uuid ? this.findWalletByUuid(uuid) : undefined
|
|
274
|
+
this.connectedWalletSource = wallet?.source ?? null
|
|
275
|
+
this.connectedWalletRdns = wallet?.rdns
|
|
276
|
+
}
|
|
277
|
+
|
|
226
278
|
/**
|
|
227
279
|
* Get current account
|
|
228
280
|
*/
|
|
@@ -230,6 +282,28 @@ export class EthereumWalletService {
|
|
|
230
282
|
return this.account
|
|
231
283
|
}
|
|
232
284
|
|
|
285
|
+
/**
|
|
286
|
+
* The LIVE selected account of the connected EVM provider, read at call time
|
|
287
|
+
* via `eth_accounts` (non-prompting). Unlike `getAccount()`'s connect-time
|
|
288
|
+
* cache, this reflects an in-wallet account switch, so the signMessage flow
|
|
289
|
+
* can claim the account that ACTUALLY signs — a stale claim surfaces as a
|
|
290
|
+
* backend "Signature is invalid" after a switch. Returns null when there is
|
|
291
|
+
* no connected provider, the wallet exposes no accounts, or the request
|
|
292
|
+
* fails; the caller decides whether to fall back to the cache or throw.
|
|
293
|
+
*/
|
|
294
|
+
async getLiveAccount(): Promise<string | null> {
|
|
295
|
+
const provider = this.connectedProvider
|
|
296
|
+
if (!provider) return null
|
|
297
|
+
try {
|
|
298
|
+
const accounts = await provider.request({ method: 'eth_accounts' })
|
|
299
|
+
return Array.isArray(accounts) && accounts.length > 0
|
|
300
|
+
? (accounts[0] as string)
|
|
301
|
+
: null
|
|
302
|
+
} catch {
|
|
303
|
+
return null
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
233
307
|
/**
|
|
234
308
|
* Get connected provider
|
|
235
309
|
*/
|
|
@@ -238,42 +312,146 @@ export class EthereumWalletService {
|
|
|
238
312
|
}
|
|
239
313
|
|
|
240
314
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* PII boundary: the new address from `accountsChanged` is NEVER forwarded — an
|
|
248
|
-
* empty account list maps to `disconnect`, a non-empty one to a content-free
|
|
249
|
-
* `accountChanged`. `chainChanged` carries only the (non-PII) CAIP-2 chain id.
|
|
315
|
+
* Live-provider guard shared by `onLifecycleEvent` and `onAccountsChanged`:
|
|
316
|
+
* both need a connected EIP-1193 provider with `.on`/`.removeListener`
|
|
317
|
+
* (EIP-1193 lists `removeListener` as SHOULD — attaching listeners we could
|
|
318
|
+
* never remove would BE the zombie-listener class this layer exists to
|
|
319
|
+
* prevent), and neither can attach across the production iframe's Comlink
|
|
320
|
+
* proxy (an un-proxied handler can't be cloned over postMessage).
|
|
250
321
|
*/
|
|
251
|
-
|
|
252
|
-
listener: (event: WalletLifecycleEvent) => void
|
|
253
|
-
): (() => void) | undefined {
|
|
322
|
+
private getObservableProvider(): EIP1193Provider | null {
|
|
254
323
|
const provider = this.connectedProvider
|
|
255
324
|
if (
|
|
256
325
|
!provider ||
|
|
257
326
|
typeof provider.on !== 'function' ||
|
|
258
327
|
typeof provider.removeListener !== 'function'
|
|
259
328
|
) {
|
|
260
|
-
return
|
|
329
|
+
return null
|
|
261
330
|
}
|
|
331
|
+
// Narrowed above: `.on`/`.removeListener` are confirmed present, matching
|
|
332
|
+
// EIP1193Provider's (non-optional) shape — `EthereumProvider` alone leaves
|
|
333
|
+
// both optional, which loses that narrowing once returned from this method.
|
|
334
|
+
return provider as EIP1193Provider
|
|
335
|
+
}
|
|
262
336
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
// and `on`/`removeListener` can't satisfy teardown across the bridge (callback
|
|
266
|
-
// identity). Subscribing here would throw or leak a listener. Until the bridge
|
|
267
|
-
// exposes a forwarding wrapper (P3), report lifecycle as unavailable over the
|
|
268
|
-
// bridge (the core emits `lifecycleTelemetryUnavailable`) rather than
|
|
269
|
-
// attempting a broken subscription.
|
|
270
|
-
if (
|
|
337
|
+
private isBridgeMode(): boolean {
|
|
338
|
+
return (
|
|
271
339
|
typeof window !== 'undefined' &&
|
|
272
340
|
(window as unknown as { UWCBridgeChildInitialized?: boolean })
|
|
273
341
|
.UWCBridgeChildInitialized === true
|
|
274
|
-
)
|
|
275
|
-
|
|
342
|
+
)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Whether the CONNECTED wallet's own provider lives in the top frame (so
|
|
347
|
+
* lifecycle events must be relayed across the bridge) as opposed to this
|
|
348
|
+
* frame's own injection context (so `provider.on` can attach directly).
|
|
349
|
+
* Bridge mode alone isn't sufficient: `eip6963-discovery`'s bridge poll can
|
|
350
|
+
* come back empty and fall back to local discovery even while embedded, in
|
|
351
|
+
* which case the wallet this session actually connected to is a real,
|
|
352
|
+
* non-proxied object right here — attaching via the bridge would register a
|
|
353
|
+
* listener on the TOP frame's copy of that wallet, which never received the
|
|
354
|
+
* connection permission (wallets scope `accountsChanged` per origin) and so
|
|
355
|
+
* never fires. `connectedWalletSource === null` (uuid-less connection
|
|
356
|
+
* state) is treated as bridged, preserving the pre-existing behavior.
|
|
357
|
+
*/
|
|
358
|
+
private shouldUseBridgedListeners(): boolean {
|
|
359
|
+
return this.isBridgeMode() && this.connectedWalletSource !== 'local'
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Attach one or more bridge-forwarded provider events via the global
|
|
364
|
+
* `window.__uwcSubscribeEip6963ProviderEvent` (installed by
|
|
365
|
+
* `@meshconnect/uwc-bridge-child`, only present in bridge mode). Returns
|
|
366
|
+
* `undefined` when there's no connected wallet uuid or the bridge global
|
|
367
|
+
* isn't installed (an older bridge-parent that predates this capability),
|
|
368
|
+
* matching this method's existing "unsupported returns undefined"
|
|
369
|
+
* contract elsewhere in this file.
|
|
370
|
+
*/
|
|
371
|
+
private attachBridgedListeners(
|
|
372
|
+
subscriptions: Array<{
|
|
373
|
+
eventName: 'accountsChanged' | 'chainChanged' | 'disconnect'
|
|
374
|
+
handler: (payload?: unknown) => void
|
|
375
|
+
}>
|
|
376
|
+
): (() => void) | undefined {
|
|
377
|
+
const uuid = this.connectedWalletUuid
|
|
378
|
+
const bridgeSubscribe = (
|
|
379
|
+
window as unknown as {
|
|
380
|
+
__uwcSubscribeEip6963ProviderEvent?: (
|
|
381
|
+
uuid: string,
|
|
382
|
+
eventName: string,
|
|
383
|
+
callback: (payload?: unknown) => void,
|
|
384
|
+
rdns?: string
|
|
385
|
+
) => () => void
|
|
386
|
+
}
|
|
387
|
+
).__uwcSubscribeEip6963ProviderEvent
|
|
388
|
+
if (!uuid || !bridgeSubscribe) return undefined
|
|
389
|
+
|
|
390
|
+
// The wallet's rdns (e.g. "io.metamask") is stable across independent
|
|
391
|
+
// EIP-6963 injection contexts, unlike its uuid (freshly minted per
|
|
392
|
+
// injection per spec). Threading it through lets BridgeParent fall back
|
|
393
|
+
// to matching by rdns when the uuid this session discovered locally
|
|
394
|
+
// doesn't match what the parent's own top-frame discovery saw — see
|
|
395
|
+
// BridgeParent.ts's subscribeEip6963ProviderEvent for the other half.
|
|
396
|
+
// Uses the rdns CAPTURED at connect time (`connectedWalletRdns`), not a
|
|
397
|
+
// live `findWalletByUuid` lookup: `detectedWallets` can be replaced
|
|
398
|
+
// wholesale by a later `initializeDiscovery()` refresh that drops this
|
|
399
|
+
// uuid's entry even though the wallet is still connected, which would
|
|
400
|
+
// silently turn this into an unqualified (fallback-less) uuid lookup.
|
|
401
|
+
const rdns = this.connectedWalletRdns
|
|
402
|
+
|
|
403
|
+
const teardowns = subscriptions.map(({ eventName, handler }) =>
|
|
404
|
+
bridgeSubscribe(uuid, eventName, handler, rdns)
|
|
405
|
+
)
|
|
406
|
+
return () => {
|
|
407
|
+
for (const teardown of teardowns) teardown()
|
|
276
408
|
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Subscribe to the live EIP-1193 provider's lifecycle events (account/chain
|
|
413
|
+
* switched in the wallet, disconnect). Returns a teardown, or `undefined`
|
|
414
|
+
* when there is no connected provider, it lacks `.on`/`.removeListener`, or
|
|
415
|
+
* this is the production iframe (see `getObservableProvider`).
|
|
416
|
+
*
|
|
417
|
+
* PII boundary: the new address from `accountsChanged` is NEVER forwarded — an
|
|
418
|
+
* empty account list maps to `disconnect`, a non-empty one to a content-free
|
|
419
|
+
* `accountChanged`. `chainChanged` carries only the (non-PII) CAIP-2 chain id.
|
|
420
|
+
*/
|
|
421
|
+
onLifecycleEvent(
|
|
422
|
+
listener: (event: WalletLifecycleEvent) => void
|
|
423
|
+
): (() => void) | undefined {
|
|
424
|
+
if (this.shouldUseBridgedListeners()) {
|
|
425
|
+
return this.attachBridgedListeners(
|
|
426
|
+
(['accountsChanged', 'chainChanged', 'disconnect'] as const).map(
|
|
427
|
+
eventName => ({
|
|
428
|
+
eventName,
|
|
429
|
+
handler: (payload?: unknown) => {
|
|
430
|
+
if (eventName === 'accountsChanged') {
|
|
431
|
+
const list = Array.isArray(payload) ? payload : []
|
|
432
|
+
listener(
|
|
433
|
+
list.length === 0
|
|
434
|
+
? { type: 'disconnect' }
|
|
435
|
+
: { type: 'accountChanged' }
|
|
436
|
+
)
|
|
437
|
+
} else if (eventName === 'chainChanged') {
|
|
438
|
+
const caip = toCaipChainId(payload)
|
|
439
|
+
listener(
|
|
440
|
+
caip !== undefined
|
|
441
|
+
? { type: 'chainChanged', chainId: caip }
|
|
442
|
+
: { type: 'chainChanged' }
|
|
443
|
+
)
|
|
444
|
+
} else {
|
|
445
|
+
listener({ type: 'disconnect' })
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
})
|
|
449
|
+
)
|
|
450
|
+
)
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const provider = this.getObservableProvider()
|
|
454
|
+
if (!provider) return undefined
|
|
277
455
|
|
|
278
456
|
const onAccounts = (accounts: unknown) => {
|
|
279
457
|
const list = Array.isArray(accounts) ? accounts : []
|
|
@@ -302,11 +480,49 @@ export class EthereumWalletService {
|
|
|
302
480
|
}
|
|
303
481
|
}
|
|
304
482
|
|
|
483
|
+
/**
|
|
484
|
+
* Subscribe to the live provider's REAL account list, for session-sync
|
|
485
|
+
* (NOT telemetry — see `onLifecycleEvent`'s PII boundary above, which this
|
|
486
|
+
* deliberately does not share). Forwards the raw `accountsChanged` payload
|
|
487
|
+
* as-is, including an empty array; the caller (core) interprets an empty
|
|
488
|
+
* list as a disconnect signal.
|
|
489
|
+
*/
|
|
490
|
+
onAccountsChanged(
|
|
491
|
+
listener: (accounts: string[]) => void
|
|
492
|
+
): (() => void) | undefined {
|
|
493
|
+
if (this.shouldUseBridgedListeners()) {
|
|
494
|
+
return this.attachBridgedListeners([
|
|
495
|
+
{
|
|
496
|
+
eventName: 'accountsChanged',
|
|
497
|
+
handler: (payload?: unknown) => {
|
|
498
|
+
const list = Array.isArray(payload) ? (payload as string[]) : []
|
|
499
|
+
this.account = (list[0] as string) ?? null
|
|
500
|
+
listener(list)
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
])
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const provider = this.getObservableProvider()
|
|
507
|
+
if (!provider) return undefined
|
|
508
|
+
|
|
509
|
+
const onAccounts = (accounts: unknown) => {
|
|
510
|
+
const list = Array.isArray(accounts) ? (accounts as string[]) : []
|
|
511
|
+
this.account = (list[0] as string) ?? null
|
|
512
|
+
listener(list)
|
|
513
|
+
}
|
|
514
|
+
provider.on('accountsChanged', onAccounts)
|
|
515
|
+
return () => provider.removeListener?.('accountsChanged', onAccounts)
|
|
516
|
+
}
|
|
517
|
+
|
|
305
518
|
/**
|
|
306
519
|
* Disconnect (clear state only)
|
|
307
520
|
*/
|
|
308
521
|
disconnect(): void {
|
|
309
522
|
this.connectedProvider = null
|
|
523
|
+
this.connectedWalletUuid = null
|
|
524
|
+
this.connectedWalletSource = null
|
|
525
|
+
this.connectedWalletRdns = undefined
|
|
310
526
|
this.account = null
|
|
311
527
|
}
|
|
312
528
|
|
|
@@ -4,15 +4,17 @@ import { SolanaWalletService } from './solana-wallet-service'
|
|
|
4
4
|
import { StorageService } from '../storage-service'
|
|
5
5
|
|
|
6
6
|
function mockAdapter() {
|
|
7
|
-
const handlers: Record<string, () => void> = {}
|
|
7
|
+
const handlers: Record<string, ((arg?: unknown) => void)[]> = {}
|
|
8
8
|
return {
|
|
9
|
-
on: (e: string, h: () => void) => {
|
|
10
|
-
handlers[e] = h
|
|
9
|
+
on: (e: string, h: (arg?: unknown) => void) => {
|
|
10
|
+
handlers[e] = [...(handlers[e] ?? []), h]
|
|
11
11
|
},
|
|
12
|
-
off: vi.fn((e: string) => {
|
|
13
|
-
|
|
12
|
+
off: vi.fn((e: string, h: (arg?: unknown) => void) => {
|
|
13
|
+
handlers[e] = (handlers[e] ?? []).filter(fn => fn !== h)
|
|
14
14
|
}),
|
|
15
|
-
fire: (e: string) =>
|
|
15
|
+
fire: (e: string, arg?: unknown) => {
|
|
16
|
+
handlers[e]?.forEach(h => h(arg))
|
|
17
|
+
}
|
|
16
18
|
}
|
|
17
19
|
}
|
|
18
20
|
|
|
@@ -55,3 +57,151 @@ describe('SolanaWalletService.onLifecycleEvent', () => {
|
|
|
55
57
|
).toBeUndefined()
|
|
56
58
|
})
|
|
57
59
|
})
|
|
60
|
+
|
|
61
|
+
describe('SolanaWalletService.onAccountsChanged', () => {
|
|
62
|
+
afterEach(() => {
|
|
63
|
+
delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
|
|
64
|
+
.UWCBridgeChildInitialized
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('returns undefined with no connected adapter', () => {
|
|
68
|
+
expect(serviceWith(null).onAccountsChanged(() => {})).toBeUndefined()
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('forwards [address] on connect and [] on disconnect, tearing down both listeners via .off', () => {
|
|
72
|
+
const adapter = mockAdapter()
|
|
73
|
+
const accounts: string[][] = []
|
|
74
|
+
const stop = serviceWith(adapter).onAccountsChanged(a => accounts.push(a))
|
|
75
|
+
expect(typeof stop).toBe('function')
|
|
76
|
+
|
|
77
|
+
adapter.fire('connect', { toBase58: () => 'SoLaNaAddr_NEW' })
|
|
78
|
+
expect(accounts).toEqual([['SoLaNaAddr_NEW']])
|
|
79
|
+
|
|
80
|
+
adapter.fire('disconnect')
|
|
81
|
+
expect(accounts).toEqual([['SoLaNaAddr_NEW'], []])
|
|
82
|
+
|
|
83
|
+
stop!()
|
|
84
|
+
expect(adapter.off).toHaveBeenCalledWith('connect', expect.any(Function))
|
|
85
|
+
expect(adapter.off).toHaveBeenCalledWith('disconnect', expect.any(Function))
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('returns undefined in bridge mode when there is no bridge global or connected wallet id', () => {
|
|
89
|
+
;(
|
|
90
|
+
window as unknown as { UWCBridgeChildInitialized?: boolean }
|
|
91
|
+
).UWCBridgeChildInitialized = true
|
|
92
|
+
expect(
|
|
93
|
+
serviceWith(mockAdapter()).onAccountsChanged(() => {})
|
|
94
|
+
).toBeUndefined()
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('SolanaWalletService.onAccountsChanged — bridge relay (Task 3.2)', () => {
|
|
99
|
+
afterEach(() => {
|
|
100
|
+
delete (window as unknown as { UWCBridgeChildInitialized?: boolean })
|
|
101
|
+
.UWCBridgeChildInitialized
|
|
102
|
+
delete (window as unknown as { __uwcSubscribeSolanaWalletEvent?: unknown })
|
|
103
|
+
.__uwcSubscribeSolanaWalletEvent
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
function serviceWithConnectedWalletUuid(walletUuid: string) {
|
|
107
|
+
const service = new SolanaWalletService(new StorageService())
|
|
108
|
+
;(
|
|
109
|
+
service as unknown as { connectedWalletUuid: string | null }
|
|
110
|
+
).connectedWalletUuid = walletUuid
|
|
111
|
+
return service
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
it('subscribes via window.__uwcSubscribeSolanaWalletEvent using connectedWalletUuid as the walletId', () => {
|
|
115
|
+
;(
|
|
116
|
+
window as unknown as { UWCBridgeChildInitialized?: boolean }
|
|
117
|
+
).UWCBridgeChildInitialized = true
|
|
118
|
+
|
|
119
|
+
const bridgeTeardown = vi.fn()
|
|
120
|
+
const bridgeSubscribe = vi.fn().mockReturnValue(bridgeTeardown)
|
|
121
|
+
;(
|
|
122
|
+
window as unknown as {
|
|
123
|
+
__uwcSubscribeSolanaWalletEvent?: typeof bridgeSubscribe
|
|
124
|
+
}
|
|
125
|
+
).__uwcSubscribeSolanaWalletEvent = bridgeSubscribe
|
|
126
|
+
|
|
127
|
+
const service = serviceWithConnectedWalletUuid('phantom-solana:mainnet')
|
|
128
|
+
const accounts: string[][] = []
|
|
129
|
+
const stop = service.onAccountsChanged(a => accounts.push(a))
|
|
130
|
+
|
|
131
|
+
expect(bridgeSubscribe).toHaveBeenCalledWith(
|
|
132
|
+
'phantom-solana:mainnet',
|
|
133
|
+
expect.any(Function)
|
|
134
|
+
)
|
|
135
|
+
expect(typeof stop).toBe('function')
|
|
136
|
+
|
|
137
|
+
// The bridge global forwards a plain string | null — map it to
|
|
138
|
+
// [address] / [] the same way the direct-mode branch does.
|
|
139
|
+
const forwardedCallback = bridgeSubscribe.mock.calls[0]?.[1]
|
|
140
|
+
forwardedCallback('SoLaNaAddr_NEW')
|
|
141
|
+
expect(accounts).toEqual([['SoLaNaAddr_NEW']])
|
|
142
|
+
|
|
143
|
+
forwardedCallback(null)
|
|
144
|
+
expect(accounts).toEqual([['SoLaNaAddr_NEW'], []])
|
|
145
|
+
|
|
146
|
+
stop!()
|
|
147
|
+
expect(bridgeTeardown).toHaveBeenCalled()
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('returns undefined when the bridge global is installed but there is no connectedWalletUuid', () => {
|
|
151
|
+
;(
|
|
152
|
+
window as unknown as { UWCBridgeChildInitialized?: boolean }
|
|
153
|
+
).UWCBridgeChildInitialized = true
|
|
154
|
+
;(
|
|
155
|
+
window as unknown as { __uwcSubscribeSolanaWalletEvent?: unknown }
|
|
156
|
+
).__uwcSubscribeSolanaWalletEvent = vi.fn()
|
|
157
|
+
|
|
158
|
+
const service = new SolanaWalletService(new StorageService())
|
|
159
|
+
expect(service.onAccountsChanged(() => {})).toBeUndefined()
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('returns undefined when there is a connectedWalletUuid but no bridge global (older bridge-parent)', () => {
|
|
163
|
+
;(
|
|
164
|
+
window as unknown as { UWCBridgeChildInitialized?: boolean }
|
|
165
|
+
).UWCBridgeChildInitialized = true
|
|
166
|
+
|
|
167
|
+
const service = serviceWithConnectedWalletUuid('phantom-solana:mainnet')
|
|
168
|
+
expect(service.onAccountsChanged(() => {})).toBeUndefined()
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
describe('SolanaWalletService.getLiveAccount — live signer address', () => {
|
|
173
|
+
it('returns the LIVE adapter publicKey after an in-wallet switch, not the connect-time cache', async () => {
|
|
174
|
+
// Adapter whose publicKey.toBase58() reflects the CURRENT selected account
|
|
175
|
+
// (bridged mode returns it async; awaiting works for both).
|
|
176
|
+
const adapter = {
|
|
177
|
+
publicKey: { toBase58: async () => 'SoLaNaAddr_B_NEW' }
|
|
178
|
+
}
|
|
179
|
+
const service = serviceWith(adapter)
|
|
180
|
+
// Seed a stale connect-time cache B (getAccount returns this).
|
|
181
|
+
;(service as unknown as { account: string | null }).account =
|
|
182
|
+
'SoLaNaAddr_A_OLD'
|
|
183
|
+
|
|
184
|
+
expect(service.getAccount()).toBe('SoLaNaAddr_A_OLD')
|
|
185
|
+
expect(await service.getLiveAccount()).toBe('SoLaNaAddr_B_NEW')
|
|
186
|
+
expect(await service.getLiveAccount()).not.toBe('SoLaNaAddr_A_OLD')
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
it('returns null when there is no connected adapter', async () => {
|
|
190
|
+
expect(await serviceWith(null).getLiveAccount()).toBeNull()
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('returns null when the adapter exposes no publicKey', async () => {
|
|
194
|
+
expect(await serviceWith({ publicKey: null }).getLiveAccount()).toBeNull()
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('returns null when toBase58 throws', async () => {
|
|
198
|
+
const adapter = {
|
|
199
|
+
publicKey: {
|
|
200
|
+
toBase58: async () => {
|
|
201
|
+
throw new Error('disconnected')
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
expect(await serviceWith(adapter).getLiveAccount()).toBeNull()
|
|
206
|
+
})
|
|
207
|
+
})
|