@meshconnect/uwc-injected-connector 0.20.2 → 0.21.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 +22 -2
- package/dist/injected-connector.d.ts.map +1 -1
- package/dist/injected-connector.js +125 -15
- package/dist/injected-connector.js.map +1 -1
- package/dist/services/connection-manager.d.ts +18 -5
- package/dist/services/connection-manager.d.ts.map +1 -1
- package/dist/services/connection-manager.js +43 -13
- package/dist/services/connection-manager.js.map +1 -1
- package/dist/services/ethereum/ethereum-wallet-service.d.ts +78 -6
- package/dist/services/ethereum/ethereum-wallet-service.d.ts.map +1 -1
- package/dist/services/ethereum/ethereum-wallet-service.js +187 -22
- 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/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 +477 -16
- package/src/injected-connector.ts +142 -18
- package/src/services/connection-manager.test.ts +104 -1
- package/src/services/connection-manager.ts +50 -14
- package/src/services/ethereum/ethereum-wallet-service.test.ts +495 -17
- package/src/services/ethereum/ethereum-wallet-service.ts +221 -25
- 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
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
} from '@meshconnect/uwc-types'
|
|
10
10
|
import type {
|
|
11
11
|
EthereumProvider,
|
|
12
|
+
EIP1193Provider,
|
|
12
13
|
DetectedWallet as EIP6963DetectedWallet
|
|
13
14
|
} from '../../eip6963-discovery'
|
|
14
15
|
import { getAvailableWallets as getEIP6963Wallets } from '../../eip6963-discovery'
|
|
@@ -46,6 +47,18 @@ function toCaipChainId(raw: unknown): NetworkId | undefined {
|
|
|
46
47
|
export class EthereumWalletService {
|
|
47
48
|
private detectedWallets: EIP6963DetectedWallet[] = []
|
|
48
49
|
private connectedProvider: EthereumProvider | null = null
|
|
50
|
+
private connectedWalletUuid: string | null = null
|
|
51
|
+
/**
|
|
52
|
+
* Provenance of the CONNECTED wallet's provider, captured once at
|
|
53
|
+
* connect()/setConnectionState() time (when `findWalletByUuid` is
|
|
54
|
+
* guaranteed to still resolve it) rather than re-derived later — see
|
|
55
|
+
* `attachBridgedListeners` for why this can't be a live lookup by uuid.
|
|
56
|
+
* `null` means unknown (e.g. state set without a uuid) and is treated as
|
|
57
|
+
* bridge-sourced, preserving the pre-existing bridge-first behavior.
|
|
58
|
+
*/
|
|
59
|
+
private connectedWalletSource: 'bridge' | 'local' | null = null
|
|
60
|
+
/** Wallet-stable rdns of the connected wallet, captured alongside source above. */
|
|
61
|
+
private connectedWalletRdns: string | undefined = undefined
|
|
49
62
|
private account: string | null = null
|
|
50
63
|
|
|
51
64
|
/**
|
|
@@ -89,7 +102,7 @@ export class EthereumWalletService {
|
|
|
89
102
|
/**
|
|
90
103
|
* Connect to wallet
|
|
91
104
|
*/
|
|
92
|
-
async connect(provider: EthereumProvider): Promise<string> {
|
|
105
|
+
async connect(provider: EthereumProvider, uuid?: string): Promise<string> {
|
|
93
106
|
const accounts = await provider.request({
|
|
94
107
|
method: 'eth_requestAccounts'
|
|
95
108
|
})
|
|
@@ -100,6 +113,8 @@ export class EthereumWalletService {
|
|
|
100
113
|
|
|
101
114
|
const address = accounts[0]
|
|
102
115
|
this.connectedProvider = provider
|
|
116
|
+
this.connectedWalletUuid = uuid ?? null
|
|
117
|
+
this.captureConnectedWalletProvenance(uuid)
|
|
103
118
|
this.account = address
|
|
104
119
|
|
|
105
120
|
// Request chain ID to ensure we're connected
|
|
@@ -217,12 +232,29 @@ export class EthereumWalletService {
|
|
|
217
232
|
*/
|
|
218
233
|
setConnectionState(
|
|
219
234
|
provider: EthereumProvider | null,
|
|
220
|
-
account: string | null
|
|
235
|
+
account: string | null,
|
|
236
|
+
uuid?: string | null
|
|
221
237
|
): void {
|
|
222
238
|
this.connectedProvider = provider
|
|
239
|
+
this.connectedWalletUuid = uuid ?? null
|
|
240
|
+
this.captureConnectedWalletProvenance(uuid ?? undefined)
|
|
223
241
|
this.account = account
|
|
224
242
|
}
|
|
225
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Snapshot the connected wallet's `source`/`rdns` from `detectedWallets` at
|
|
246
|
+
* connect time. Deliberately NOT re-derived later: `detectedWallets` is
|
|
247
|
+
* replaced wholesale by a later `initializeDiscovery()` call (e.g. a
|
|
248
|
+
* refresh), which can drop the entry this uuid resolved to even though the
|
|
249
|
+
* wallet is still connected — a live re-lookup at subscribe time would then
|
|
250
|
+
* silently lose both fields.
|
|
251
|
+
*/
|
|
252
|
+
private captureConnectedWalletProvenance(uuid: string | undefined): void {
|
|
253
|
+
const wallet = uuid ? this.findWalletByUuid(uuid) : undefined
|
|
254
|
+
this.connectedWalletSource = wallet?.source ?? null
|
|
255
|
+
this.connectedWalletRdns = wallet?.rdns
|
|
256
|
+
}
|
|
257
|
+
|
|
226
258
|
/**
|
|
227
259
|
* Get current account
|
|
228
260
|
*/
|
|
@@ -230,6 +262,28 @@ export class EthereumWalletService {
|
|
|
230
262
|
return this.account
|
|
231
263
|
}
|
|
232
264
|
|
|
265
|
+
/**
|
|
266
|
+
* The LIVE selected account of the connected EVM provider, read at call time
|
|
267
|
+
* via `eth_accounts` (non-prompting). Unlike `getAccount()`'s connect-time
|
|
268
|
+
* cache, this reflects an in-wallet account switch, so the signMessage flow
|
|
269
|
+
* can claim the account that ACTUALLY signs — a stale claim surfaces as a
|
|
270
|
+
* backend "Signature is invalid" after a switch. Returns null when there is
|
|
271
|
+
* no connected provider, the wallet exposes no accounts, or the request
|
|
272
|
+
* fails; the caller decides whether to fall back to the cache or throw.
|
|
273
|
+
*/
|
|
274
|
+
async getLiveAccount(): Promise<string | null> {
|
|
275
|
+
const provider = this.connectedProvider
|
|
276
|
+
if (!provider) return null
|
|
277
|
+
try {
|
|
278
|
+
const accounts = await provider.request({ method: 'eth_accounts' })
|
|
279
|
+
return Array.isArray(accounts) && accounts.length > 0
|
|
280
|
+
? (accounts[0] as string)
|
|
281
|
+
: null
|
|
282
|
+
} catch {
|
|
283
|
+
return null
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
233
287
|
/**
|
|
234
288
|
* Get connected provider
|
|
235
289
|
*/
|
|
@@ -238,43 +292,147 @@ export class EthereumWalletService {
|
|
|
238
292
|
}
|
|
239
293
|
|
|
240
294
|
/**
|
|
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.
|
|
295
|
+
* Live-provider guard shared by `onLifecycleEvent` and `onAccountsChanged`:
|
|
296
|
+
* both need a connected EIP-1193 provider with `.on`/`.removeListener`
|
|
297
|
+
* (EIP-1193 lists `removeListener` as SHOULD — attaching listeners we could
|
|
298
|
+
* never remove would BE the zombie-listener class this layer exists to
|
|
299
|
+
* prevent), and neither can attach across the production iframe's Comlink
|
|
300
|
+
* proxy (an un-proxied handler can't be cloned over postMessage).
|
|
250
301
|
*/
|
|
251
|
-
|
|
252
|
-
listener: (event: WalletLifecycleEvent) => void
|
|
253
|
-
): (() => void) | undefined {
|
|
302
|
+
private getObservableProvider(): EIP1193Provider | null {
|
|
254
303
|
const provider = this.connectedProvider
|
|
255
304
|
if (
|
|
256
305
|
!provider ||
|
|
257
306
|
typeof provider.on !== 'function' ||
|
|
258
307
|
typeof provider.removeListener !== 'function'
|
|
259
308
|
) {
|
|
260
|
-
return
|
|
309
|
+
return null
|
|
261
310
|
}
|
|
311
|
+
// Narrowed above: `.on`/`.removeListener` are confirmed present, matching
|
|
312
|
+
// EIP1193Provider's (non-optional) shape — `EthereumProvider` alone leaves
|
|
313
|
+
// both optional, which loses that narrowing once returned from this method.
|
|
314
|
+
return provider as EIP1193Provider
|
|
315
|
+
}
|
|
262
316
|
|
|
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 (
|
|
317
|
+
private isBridgeMode(): boolean {
|
|
318
|
+
return (
|
|
271
319
|
typeof window !== 'undefined' &&
|
|
272
320
|
(window as unknown as { UWCBridgeChildInitialized?: boolean })
|
|
273
321
|
.UWCBridgeChildInitialized === true
|
|
274
|
-
)
|
|
275
|
-
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Whether the CONNECTED wallet's own provider lives in the top frame (so
|
|
327
|
+
* lifecycle events must be relayed across the bridge) as opposed to this
|
|
328
|
+
* frame's own injection context (so `provider.on` can attach directly).
|
|
329
|
+
* Bridge mode alone isn't sufficient: `eip6963-discovery`'s bridge poll can
|
|
330
|
+
* come back empty and fall back to local discovery even while embedded, in
|
|
331
|
+
* which case the wallet this session actually connected to is a real,
|
|
332
|
+
* non-proxied object right here — attaching via the bridge would register a
|
|
333
|
+
* listener on the TOP frame's copy of that wallet, which never received the
|
|
334
|
+
* connection permission (wallets scope `accountsChanged` per origin) and so
|
|
335
|
+
* never fires. `connectedWalletSource === null` (uuid-less connection
|
|
336
|
+
* state) is treated as bridged, preserving the pre-existing behavior.
|
|
337
|
+
*/
|
|
338
|
+
private shouldUseBridgedListeners(): boolean {
|
|
339
|
+
return this.isBridgeMode() && this.connectedWalletSource !== 'local'
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Attach one or more bridge-forwarded provider events via the global
|
|
344
|
+
* `window.__uwcSubscribeEip6963ProviderEvent` (installed by
|
|
345
|
+
* `@meshconnect/uwc-bridge-child`, only present in bridge mode). Returns
|
|
346
|
+
* `undefined` when there's no connected wallet uuid or the bridge global
|
|
347
|
+
* isn't installed (an older bridge-parent that predates this capability),
|
|
348
|
+
* matching this method's existing "unsupported returns undefined"
|
|
349
|
+
* contract elsewhere in this file.
|
|
350
|
+
*/
|
|
351
|
+
private attachBridgedListeners(
|
|
352
|
+
subscriptions: Array<{
|
|
353
|
+
eventName: 'accountsChanged' | 'chainChanged' | 'disconnect'
|
|
354
|
+
handler: (payload?: unknown) => void
|
|
355
|
+
}>
|
|
356
|
+
): (() => void) | undefined {
|
|
357
|
+
const uuid = this.connectedWalletUuid
|
|
358
|
+
const bridgeSubscribe = (
|
|
359
|
+
window as unknown as {
|
|
360
|
+
__uwcSubscribeEip6963ProviderEvent?: (
|
|
361
|
+
uuid: string,
|
|
362
|
+
eventName: string,
|
|
363
|
+
callback: (payload?: unknown) => void,
|
|
364
|
+
rdns?: string
|
|
365
|
+
) => () => void
|
|
366
|
+
}
|
|
367
|
+
).__uwcSubscribeEip6963ProviderEvent
|
|
368
|
+
if (!uuid || !bridgeSubscribe) return undefined
|
|
369
|
+
|
|
370
|
+
// The wallet's rdns (e.g. "io.metamask") is stable across independent
|
|
371
|
+
// EIP-6963 injection contexts, unlike its uuid (freshly minted per
|
|
372
|
+
// injection per spec). Threading it through lets BridgeParent fall back
|
|
373
|
+
// to matching by rdns when the uuid this session discovered locally
|
|
374
|
+
// doesn't match what the parent's own top-frame discovery saw — see
|
|
375
|
+
// BridgeParent.ts's subscribeEip6963ProviderEvent for the other half.
|
|
376
|
+
// Uses the rdns CAPTURED at connect time (`connectedWalletRdns`), not a
|
|
377
|
+
// live `findWalletByUuid` lookup: `detectedWallets` can be replaced
|
|
378
|
+
// wholesale by a later `initializeDiscovery()` refresh that drops this
|
|
379
|
+
// uuid's entry even though the wallet is still connected, which would
|
|
380
|
+
// silently turn this into an unqualified (fallback-less) uuid lookup.
|
|
381
|
+
const rdns = this.connectedWalletRdns
|
|
382
|
+
|
|
383
|
+
const teardowns = subscriptions.map(({ eventName, handler }) =>
|
|
384
|
+
bridgeSubscribe(uuid, eventName, handler, rdns)
|
|
385
|
+
)
|
|
386
|
+
return () => {
|
|
387
|
+
for (const teardown of teardowns) teardown()
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Subscribe to the live EIP-1193 provider's lifecycle events (account/chain
|
|
393
|
+
* switched in the wallet, disconnect). Returns a teardown, or `undefined`
|
|
394
|
+
* when there is no connected provider, it lacks `.on`/`.removeListener`, or
|
|
395
|
+
* this is the production iframe (see `getObservableProvider`).
|
|
396
|
+
*
|
|
397
|
+
* PII boundary: the new address from `accountsChanged` is NEVER forwarded — an
|
|
398
|
+
* empty account list maps to `disconnect`, a non-empty one to a content-free
|
|
399
|
+
* `accountChanged`. `chainChanged` carries only the (non-PII) CAIP-2 chain id.
|
|
400
|
+
*/
|
|
401
|
+
onLifecycleEvent(
|
|
402
|
+
listener: (event: WalletLifecycleEvent) => void
|
|
403
|
+
): (() => void) | undefined {
|
|
404
|
+
if (this.shouldUseBridgedListeners()) {
|
|
405
|
+
return this.attachBridgedListeners(
|
|
406
|
+
(['accountsChanged', 'chainChanged', 'disconnect'] as const).map(
|
|
407
|
+
eventName => ({
|
|
408
|
+
eventName,
|
|
409
|
+
handler: (payload?: unknown) => {
|
|
410
|
+
if (eventName === 'accountsChanged') {
|
|
411
|
+
const list = Array.isArray(payload) ? payload : []
|
|
412
|
+
listener(
|
|
413
|
+
list.length === 0
|
|
414
|
+
? { type: 'disconnect' }
|
|
415
|
+
: { type: 'accountChanged' }
|
|
416
|
+
)
|
|
417
|
+
} else if (eventName === 'chainChanged') {
|
|
418
|
+
const caip = toCaipChainId(payload)
|
|
419
|
+
listener(
|
|
420
|
+
caip !== undefined
|
|
421
|
+
? { type: 'chainChanged', chainId: caip }
|
|
422
|
+
: { type: 'chainChanged' }
|
|
423
|
+
)
|
|
424
|
+
} else {
|
|
425
|
+
listener({ type: 'disconnect' })
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
})
|
|
429
|
+
)
|
|
430
|
+
)
|
|
276
431
|
}
|
|
277
432
|
|
|
433
|
+
const provider = this.getObservableProvider()
|
|
434
|
+
if (!provider) return undefined
|
|
435
|
+
|
|
278
436
|
const onAccounts = (accounts: unknown) => {
|
|
279
437
|
const list = Array.isArray(accounts) ? accounts : []
|
|
280
438
|
listener(
|
|
@@ -302,11 +460,49 @@ export class EthereumWalletService {
|
|
|
302
460
|
}
|
|
303
461
|
}
|
|
304
462
|
|
|
463
|
+
/**
|
|
464
|
+
* Subscribe to the live provider's REAL account list, for session-sync
|
|
465
|
+
* (NOT telemetry — see `onLifecycleEvent`'s PII boundary above, which this
|
|
466
|
+
* deliberately does not share). Forwards the raw `accountsChanged` payload
|
|
467
|
+
* as-is, including an empty array; the caller (core) interprets an empty
|
|
468
|
+
* list as a disconnect signal.
|
|
469
|
+
*/
|
|
470
|
+
onAccountsChanged(
|
|
471
|
+
listener: (accounts: string[]) => void
|
|
472
|
+
): (() => void) | undefined {
|
|
473
|
+
if (this.shouldUseBridgedListeners()) {
|
|
474
|
+
return this.attachBridgedListeners([
|
|
475
|
+
{
|
|
476
|
+
eventName: 'accountsChanged',
|
|
477
|
+
handler: (payload?: unknown) => {
|
|
478
|
+
const list = Array.isArray(payload) ? (payload as string[]) : []
|
|
479
|
+
this.account = (list[0] as string) ?? null
|
|
480
|
+
listener(list)
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
])
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const provider = this.getObservableProvider()
|
|
487
|
+
if (!provider) return undefined
|
|
488
|
+
|
|
489
|
+
const onAccounts = (accounts: unknown) => {
|
|
490
|
+
const list = Array.isArray(accounts) ? (accounts as string[]) : []
|
|
491
|
+
this.account = (list[0] as string) ?? null
|
|
492
|
+
listener(list)
|
|
493
|
+
}
|
|
494
|
+
provider.on('accountsChanged', onAccounts)
|
|
495
|
+
return () => provider.removeListener?.('accountsChanged', onAccounts)
|
|
496
|
+
}
|
|
497
|
+
|
|
305
498
|
/**
|
|
306
499
|
* Disconnect (clear state only)
|
|
307
500
|
*/
|
|
308
501
|
disconnect(): void {
|
|
309
502
|
this.connectedProvider = null
|
|
503
|
+
this.connectedWalletUuid = null
|
|
504
|
+
this.connectedWalletSource = null
|
|
505
|
+
this.connectedWalletRdns = undefined
|
|
310
506
|
this.account = null
|
|
311
507
|
}
|
|
312
508
|
|
|
@@ -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
|
+
})
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
+
import { SolanaWalletService } from './solana-wallet-service'
|
|
3
|
+
import { StorageService } from '../storage-service'
|
|
4
|
+
import type { WalletStandardInfo } from '../../wallet-standard-discovery'
|
|
5
|
+
|
|
6
|
+
function serviceWithPrivateState(overrides: {
|
|
7
|
+
connectedAdapter?: unknown
|
|
8
|
+
connectedWalletUuid?: string | null
|
|
9
|
+
}) {
|
|
10
|
+
const service = new SolanaWalletService(new StorageService())
|
|
11
|
+
const priv = service as unknown as {
|
|
12
|
+
connectedAdapter: unknown
|
|
13
|
+
connectedWalletUuid: string | null
|
|
14
|
+
}
|
|
15
|
+
if ('connectedAdapter' in overrides) {
|
|
16
|
+
priv.connectedAdapter = overrides.connectedAdapter
|
|
17
|
+
}
|
|
18
|
+
if ('connectedWalletUuid' in overrides) {
|
|
19
|
+
priv.connectedWalletUuid = overrides.connectedWalletUuid ?? null
|
|
20
|
+
}
|
|
21
|
+
return service
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('SolanaWalletService.ensureConnectedAdapter', () => {
|
|
25
|
+
it('returns true (NOT the adapter) without touching discovery when already connected (happy path, no-op)', async () => {
|
|
26
|
+
// Regression guard: ensureConnectedAdapter must return a BOOLEAN, never the
|
|
27
|
+
// adapter — in bridge mode the adapter is a Comlink proxy, and `await`ing it
|
|
28
|
+
// forces Comlink to clone the (unserializable) adapter. Callers read it
|
|
29
|
+
// synchronously via getConnectedAdapter() instead.
|
|
30
|
+
const adapter = { publicKey: { toBase58: async () => 'addr' } }
|
|
31
|
+
const service = serviceWithPrivateState({
|
|
32
|
+
connectedAdapter: adapter,
|
|
33
|
+
connectedWalletUuid: 'uuid-1'
|
|
34
|
+
})
|
|
35
|
+
const discoverySpy = vi.spyOn(service, 'initializeDiscovery')
|
|
36
|
+
|
|
37
|
+
const result = await service.ensureConnectedAdapter()
|
|
38
|
+
|
|
39
|
+
expect(result).toBe(true)
|
|
40
|
+
expect(service.getConnectedAdapter()).toBe(adapter)
|
|
41
|
+
expect(discoverySpy).not.toHaveBeenCalled()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('returns false when there is no connected adapter and no remembered wallet uuid', async () => {
|
|
45
|
+
const service = serviceWithPrivateState({
|
|
46
|
+
connectedAdapter: null,
|
|
47
|
+
connectedWalletUuid: null
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
expect(await service.ensureConnectedAdapter()).toBe(false)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('rebuilds via discovery + findWalletByUuid + checkExistingConnection when the adapter was cleared but the wallet uuid is remembered', async () => {
|
|
54
|
+
const service = serviceWithPrivateState({
|
|
55
|
+
connectedAdapter: null,
|
|
56
|
+
connectedWalletUuid: 'uuid-1'
|
|
57
|
+
})
|
|
58
|
+
const rebuiltAdapter = { publicKey: { toBase58: async () => 'addr' } }
|
|
59
|
+
const walletInfo = {
|
|
60
|
+
uuid: 'uuid-1',
|
|
61
|
+
adapter: rebuiltAdapter
|
|
62
|
+
} as unknown as WalletStandardInfo
|
|
63
|
+
|
|
64
|
+
vi.spyOn(service, 'initializeDiscovery').mockResolvedValue(undefined)
|
|
65
|
+
vi.spyOn(service, 'findWalletByUuid').mockReturnValue(walletInfo)
|
|
66
|
+
const checkSpy = vi
|
|
67
|
+
.spyOn(service, 'checkExistingConnection')
|
|
68
|
+
.mockResolvedValue('SoLaNaAddr_NEW')
|
|
69
|
+
|
|
70
|
+
const result = await service.ensureConnectedAdapter()
|
|
71
|
+
|
|
72
|
+
expect(checkSpy).toHaveBeenCalledWith(rebuiltAdapter, 'uuid-1')
|
|
73
|
+
expect(result).toBe(true)
|
|
74
|
+
expect(service.getConnectedAdapter()).toBe(rebuiltAdapter)
|
|
75
|
+
expect(service.getAccount()).toBe('SoLaNaAddr_NEW')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('returns false when findWalletByUuid finds nothing', async () => {
|
|
79
|
+
const service = serviceWithPrivateState({
|
|
80
|
+
connectedAdapter: null,
|
|
81
|
+
connectedWalletUuid: 'uuid-1'
|
|
82
|
+
})
|
|
83
|
+
vi.spyOn(service, 'initializeDiscovery').mockResolvedValue(undefined)
|
|
84
|
+
vi.spyOn(service, 'findWalletByUuid').mockReturnValue(undefined)
|
|
85
|
+
|
|
86
|
+
expect(await service.ensureConnectedAdapter()).toBe(false)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('returns false when checkExistingConnection yields no address (not previously authorized / silent reconnect unsupported)', async () => {
|
|
90
|
+
const service = serviceWithPrivateState({
|
|
91
|
+
connectedAdapter: null,
|
|
92
|
+
connectedWalletUuid: 'uuid-1'
|
|
93
|
+
})
|
|
94
|
+
const walletInfo = {
|
|
95
|
+
uuid: 'uuid-1',
|
|
96
|
+
adapter: { publicKey: null }
|
|
97
|
+
} as unknown as WalletStandardInfo
|
|
98
|
+
vi.spyOn(service, 'initializeDiscovery').mockResolvedValue(undefined)
|
|
99
|
+
vi.spyOn(service, 'findWalletByUuid').mockReturnValue(walletInfo)
|
|
100
|
+
vi.spyOn(service, 'checkExistingConnection').mockResolvedValue(null)
|
|
101
|
+
|
|
102
|
+
expect(await service.ensureConnectedAdapter()).toBe(false)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('never throws: returns false when discovery/rebuild helpers throw', async () => {
|
|
106
|
+
const service = serviceWithPrivateState({
|
|
107
|
+
connectedAdapter: null,
|
|
108
|
+
connectedWalletUuid: 'uuid-1'
|
|
109
|
+
})
|
|
110
|
+
vi.spyOn(service, 'initializeDiscovery').mockRejectedValue(
|
|
111
|
+
new Error('discovery failed')
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
await expect(service.ensureConnectedAdapter()).resolves.toBe(false)
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
describe('SolanaWalletService — connectedWalletUuid bookkeeping', () => {
|
|
119
|
+
it('is set on connect()', async () => {
|
|
120
|
+
const service = new SolanaWalletService(new StorageService())
|
|
121
|
+
const adapter = {
|
|
122
|
+
connect: vi.fn().mockResolvedValue(undefined),
|
|
123
|
+
publicKey: { toBase58: async () => 'addr' }
|
|
124
|
+
} as any
|
|
125
|
+
|
|
126
|
+
await service.connect(adapter, 'uuid-42')
|
|
127
|
+
|
|
128
|
+
expect(
|
|
129
|
+
(service as unknown as { connectedWalletUuid: string | null })
|
|
130
|
+
.connectedWalletUuid
|
|
131
|
+
).toBe('uuid-42')
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('is set on setConnectionState(...) when adapter+account+walletUuid are all provided', () => {
|
|
135
|
+
const service = new SolanaWalletService(new StorageService())
|
|
136
|
+
const adapter = { publicKey: { toBase58: async () => 'addr' } } as any
|
|
137
|
+
|
|
138
|
+
service.setConnectionState(adapter, 'addr', 'uuid-7')
|
|
139
|
+
|
|
140
|
+
expect(
|
|
141
|
+
(service as unknown as { connectedWalletUuid: string | null })
|
|
142
|
+
.connectedWalletUuid
|
|
143
|
+
).toBe('uuid-7')
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('is not set on setConnectionState(...) when walletUuid is omitted', () => {
|
|
147
|
+
const service = new SolanaWalletService(new StorageService())
|
|
148
|
+
const adapter = { publicKey: { toBase58: async () => 'addr' } } as any
|
|
149
|
+
|
|
150
|
+
service.setConnectionState(adapter, 'addr')
|
|
151
|
+
|
|
152
|
+
expect(
|
|
153
|
+
(service as unknown as { connectedWalletUuid: string | null })
|
|
154
|
+
.connectedWalletUuid
|
|
155
|
+
).toBeNull()
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('is cleared on disconnect()', async () => {
|
|
159
|
+
const service = new SolanaWalletService(new StorageService())
|
|
160
|
+
const adapter = {
|
|
161
|
+
connect: vi.fn().mockResolvedValue(undefined),
|
|
162
|
+
publicKey: { toBase58: async () => 'addr' }
|
|
163
|
+
} as any
|
|
164
|
+
await service.connect(adapter, 'uuid-42')
|
|
165
|
+
|
|
166
|
+
service.disconnect()
|
|
167
|
+
|
|
168
|
+
expect(
|
|
169
|
+
(service as unknown as { connectedWalletUuid: string | null })
|
|
170
|
+
.connectedWalletUuid
|
|
171
|
+
).toBeNull()
|
|
172
|
+
expect(service.getConnectedAdapter()).toBeNull()
|
|
173
|
+
expect(service.getAccount()).toBeNull()
|
|
174
|
+
})
|
|
175
|
+
})
|