@meshconnect/uwc-injected-connector 0.22.1 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/injected-connector.d.ts +23 -7
- package/dist/injected-connector.d.ts.map +1 -1
- package/dist/injected-connector.js +54 -9
- package/dist/injected-connector.js.map +1 -1
- package/dist/services/bitcoin/bitcoin-wallet-service.d.ts +136 -0
- package/dist/services/bitcoin/bitcoin-wallet-service.d.ts.map +1 -0
- package/dist/services/bitcoin/bitcoin-wallet-service.js +385 -0
- package/dist/services/bitcoin/bitcoin-wallet-service.js.map +1 -0
- package/dist/services/connection-manager.d.ts +17 -2
- package/dist/services/connection-manager.d.ts.map +1 -1
- package/dist/services/connection-manager.js +129 -3
- package/dist/services/connection-manager.js.map +1 -1
- package/dist/wallet-standard-discovery.d.ts +45 -1
- package/dist/wallet-standard-discovery.d.ts.map +1 -1
- package/dist/wallet-standard-discovery.js +221 -1
- package/dist/wallet-standard-discovery.js.map +1 -1
- package/package.json +13 -12
- package/src/injected-connector.test.ts +1 -1
- package/src/injected-connector.ts +78 -8
- package/src/services/bitcoin/bitcoin-wallet-service.test.ts +646 -0
- package/src/services/bitcoin/bitcoin-wallet-service.ts +493 -0
- package/src/services/connection-manager.test.ts +214 -5
- package/src/services/connection-manager.ts +169 -4
- package/src/wallet-standard-discovery.test.ts +215 -1
- package/src/wallet-standard-discovery.ts +325 -2
|
@@ -43,7 +43,10 @@ vi.mock('./legacy-injected-solana-adapter', () => ({
|
|
|
43
43
|
LegacyInjectedSolanaWalletAdapter: MockLegacyAdapter
|
|
44
44
|
}))
|
|
45
45
|
|
|
46
|
-
import {
|
|
46
|
+
import {
|
|
47
|
+
getSolanaWallets,
|
|
48
|
+
getBitcoinWallets
|
|
49
|
+
} from './wallet-standard-discovery'
|
|
47
50
|
|
|
48
51
|
// Derive from the shared constant (discovery uses `solanaNetwork.id`) so the
|
|
49
52
|
// test can't drift if the canonical Solana CAIP-2 id ever changes.
|
|
@@ -350,3 +353,214 @@ describe('getSolanaWallets — metadata-driven legacy injected fallback (MFS-805
|
|
|
350
353
|
expect(getInjectedSolanaProvider).not.toHaveBeenCalled()
|
|
351
354
|
})
|
|
352
355
|
})
|
|
356
|
+
|
|
357
|
+
function bitcoinStandardWallet(
|
|
358
|
+
name: string,
|
|
359
|
+
chains: string[] = ['bitcoin:mainnet'],
|
|
360
|
+
features: Record<string, unknown> = { 'bitcoin:connect': {} }
|
|
361
|
+
): any {
|
|
362
|
+
return { name, chains, features, accounts: [] }
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
describe('getBitcoinWallets — bip122 discovery + bridge', () => {
|
|
366
|
+
beforeEach(() => {
|
|
367
|
+
registryWallets = []
|
|
368
|
+
resolveInjectedProperty.mockReset()
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
afterEach(() => {
|
|
372
|
+
delete (window as any).UWCBridgeChildInitialized
|
|
373
|
+
delete (window as any).walletStandardBitcoinWallets
|
|
374
|
+
})
|
|
375
|
+
|
|
376
|
+
// Minimal WalletMetadata carrying a bip122 injectedId + walletStandardName.
|
|
377
|
+
function okxExpected(): WalletMetadata[] {
|
|
378
|
+
return [
|
|
379
|
+
{
|
|
380
|
+
id: 'okx',
|
|
381
|
+
extensionInjectedProvider: {
|
|
382
|
+
namespaceMetaData: {
|
|
383
|
+
bip122: {
|
|
384
|
+
walletStandardName: 'OKX Wallet',
|
|
385
|
+
injectedId: 'okxwallet.bitcoin'
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
] as unknown as WalletMetadata[]
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// A fully-shaped OKX provider (the probe validates the whole surface).
|
|
394
|
+
function fullOkxProvider(overrides: Record<string, unknown> = {}) {
|
|
395
|
+
return {
|
|
396
|
+
requestAccounts: vi.fn().mockResolvedValue([]),
|
|
397
|
+
getAccounts: vi.fn().mockResolvedValue([]),
|
|
398
|
+
getPublicKey: vi.fn().mockResolvedValue(''),
|
|
399
|
+
signMessage: vi.fn(),
|
|
400
|
+
signPsbt: vi.fn(),
|
|
401
|
+
on: vi.fn(),
|
|
402
|
+
removeListener: vi.fn(),
|
|
403
|
+
...overrides
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
it('discovers OKX via the legacy-injected probe (injectedId), tagging legacyInjectedId', async () => {
|
|
408
|
+
resolveInjectedProperty.mockImplementation((id: string) =>
|
|
409
|
+
id === 'okxwallet.bitcoin' ? fullOkxProvider() : null
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
const wallets = await getBitcoinWallets(okxExpected())
|
|
413
|
+
|
|
414
|
+
expect(wallets).toHaveLength(1)
|
|
415
|
+
expect(wallets[0]!.name).toBe('OKX Wallet')
|
|
416
|
+
expect(wallets[0]!.legacyInjectedId).toBe('okxwallet.bitcoin')
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
it('rejects a provider that only partially matches the OKX surface', async () => {
|
|
420
|
+
// Only `getAccounts` — missing signMessage/signPsbt/on/etc. Must NOT be
|
|
421
|
+
// accepted (would otherwise fail mid-connect).
|
|
422
|
+
resolveInjectedProperty.mockImplementation((id: string) =>
|
|
423
|
+
id === 'okxwallet.bitcoin'
|
|
424
|
+
? { getAccounts: vi.fn().mockResolvedValue([]) }
|
|
425
|
+
: null
|
|
426
|
+
)
|
|
427
|
+
const wallets = await getBitcoinWallets(okxExpected())
|
|
428
|
+
expect(wallets).toHaveLength(0)
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
it('seeds the OKX handle accounts from a non-prompting getAccounts (silent restore)', async () => {
|
|
432
|
+
resolveInjectedProperty.mockImplementation((id: string) =>
|
|
433
|
+
id === 'okxwallet.bitcoin'
|
|
434
|
+
? fullOkxProvider({
|
|
435
|
+
getAccounts: vi.fn().mockResolvedValue(['bc1qokx']),
|
|
436
|
+
getPublicKey: vi.fn().mockResolvedValue('02aabb')
|
|
437
|
+
})
|
|
438
|
+
: null
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
const wallets = await getBitcoinWallets(okxExpected())
|
|
442
|
+
const seeded = wallets[0]!.wallet.accounts as { address: string }[]
|
|
443
|
+
expect(seeded[0]!.address).toBe('bc1qokx')
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
it('skips OKX when its injected provider is absent (not installed)', async () => {
|
|
447
|
+
resolveInjectedProperty.mockReturnValue(null)
|
|
448
|
+
const wallets = await getBitcoinWallets(okxExpected())
|
|
449
|
+
expect(wallets).toHaveLength(0)
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
it('lets a registry wallet of the same name win over the legacy entry', async () => {
|
|
453
|
+
registryWallets = [bitcoinStandardWallet('OKX Wallet')]
|
|
454
|
+
resolveInjectedProperty.mockImplementation((id: string) =>
|
|
455
|
+
id === 'okxwallet.bitcoin' ? fullOkxProvider() : null
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
const wallets = await getBitcoinWallets(okxExpected())
|
|
459
|
+
// Only the registry (Wallet Standard) entry — no duplicate legacy one.
|
|
460
|
+
expect(wallets).toHaveLength(1)
|
|
461
|
+
expect(wallets[0]!.legacyInjectedId).toBeUndefined()
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
it('discovers bitcoin:* wallets from the local registry', async () => {
|
|
465
|
+
registryWallets = [bitcoinStandardWallet('MetaMask')]
|
|
466
|
+
const wallets = await getBitcoinWallets()
|
|
467
|
+
|
|
468
|
+
expect(wallets).toHaveLength(1)
|
|
469
|
+
expect(wallets[0]!.name).toBe('MetaMask')
|
|
470
|
+
expect(wallets[0]!.features).toEqual(['bitcoin:connect'])
|
|
471
|
+
})
|
|
472
|
+
|
|
473
|
+
it('ignores registry wallets that do not declare a bitcoin: chain', async () => {
|
|
474
|
+
registryWallets = [standardWallet('Phantom')] // solana-only wallet
|
|
475
|
+
const wallets = await getBitcoinWallets()
|
|
476
|
+
expect(wallets).toHaveLength(0)
|
|
477
|
+
})
|
|
478
|
+
|
|
479
|
+
it('uses the bridge list when framed and skips the local registry', async () => {
|
|
480
|
+
;(window as any).UWCBridgeChildInitialized = true
|
|
481
|
+
;(window as any).walletStandardBitcoinWallets = [
|
|
482
|
+
{
|
|
483
|
+
uuid: 'bridge-btc',
|
|
484
|
+
name: 'Bridged MetaMask',
|
|
485
|
+
features: ['bitcoin:connect'],
|
|
486
|
+
wallet: { name: 'Bridged MetaMask', accounts: [], features: {} }
|
|
487
|
+
}
|
|
488
|
+
]
|
|
489
|
+
registryWallets = [bitcoinStandardWallet('LocalWallet')]
|
|
490
|
+
|
|
491
|
+
const wallets = await getBitcoinWallets()
|
|
492
|
+
expect(wallets.map(w => w.name)).toEqual(['Bridged MetaMask'])
|
|
493
|
+
})
|
|
494
|
+
|
|
495
|
+
it('falls through to local registry discovery when the bridge returns nothing', async () => {
|
|
496
|
+
;(window as any).UWCBridgeChildInitialized = true
|
|
497
|
+
;(window as any).walletStandardBitcoinWallets = []
|
|
498
|
+
registryWallets = [bitcoinStandardWallet('LocalWallet')]
|
|
499
|
+
|
|
500
|
+
const wallets = await getBitcoinWallets()
|
|
501
|
+
expect(wallets.map(w => w.name)).toEqual(['LocalWallet'])
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
it('applies the enabledWallets allow-list to bridge-discovered wallets too', async () => {
|
|
505
|
+
;(window as any).UWCBridgeChildInitialized = true
|
|
506
|
+
;(window as any).walletStandardBitcoinWallets = [
|
|
507
|
+
{
|
|
508
|
+
uuid: 'bridge-mm',
|
|
509
|
+
name: 'MetaMask',
|
|
510
|
+
features: [],
|
|
511
|
+
wallet: { name: 'MetaMask', accounts: [], features: {} }
|
|
512
|
+
},
|
|
513
|
+
{
|
|
514
|
+
uuid: 'bridge-other',
|
|
515
|
+
name: 'OtherWallet',
|
|
516
|
+
features: [],
|
|
517
|
+
wallet: { name: 'OtherWallet', accounts: [], features: {} }
|
|
518
|
+
}
|
|
519
|
+
]
|
|
520
|
+
|
|
521
|
+
const expected = [
|
|
522
|
+
{
|
|
523
|
+
id: 'metamask',
|
|
524
|
+
extensionInjectedProvider: {
|
|
525
|
+
namespaceMetaData: {
|
|
526
|
+
bip122: { walletStandardName: 'MetaMask', injectedId: 'metamask' }
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
] as unknown as WalletMetadata[]
|
|
531
|
+
|
|
532
|
+
const wallets = await getBitcoinWallets(expected, false, ['metamask'])
|
|
533
|
+
expect(wallets.map(w => w.name)).toEqual(['MetaMask'])
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
it('applies the enabledWallets allow-list to the local registry', async () => {
|
|
537
|
+
registryWallets = [
|
|
538
|
+
bitcoinStandardWallet('MetaMask'),
|
|
539
|
+
bitcoinStandardWallet('OtherWallet')
|
|
540
|
+
]
|
|
541
|
+
|
|
542
|
+
const expected = [
|
|
543
|
+
{
|
|
544
|
+
id: 'metamask',
|
|
545
|
+
extensionInjectedProvider: {
|
|
546
|
+
namespaceMetaData: {
|
|
547
|
+
bip122: { walletStandardName: 'MetaMask', injectedId: 'metamask' }
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
] as unknown as WalletMetadata[]
|
|
552
|
+
|
|
553
|
+
const wallets = await getBitcoinWallets(expected, false, ['metamask'])
|
|
554
|
+
expect(wallets.map(w => w.name)).toEqual(['MetaMask'])
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
it('returns [] outside a browser (no window)', async () => {
|
|
558
|
+
vi.stubGlobal('window', undefined)
|
|
559
|
+
try {
|
|
560
|
+
const wallets = await getBitcoinWallets()
|
|
561
|
+
expect(wallets).toEqual([])
|
|
562
|
+
} finally {
|
|
563
|
+
vi.unstubAllGlobals()
|
|
564
|
+
}
|
|
565
|
+
})
|
|
566
|
+
})
|
|
@@ -9,8 +9,9 @@ import { getWallets } from '@wallet-standard/app'
|
|
|
9
9
|
import { isWalletAdapterCompatibleStandardWallet } from '@solana/wallet-adapter-base'
|
|
10
10
|
import { StandardWalletAdapter } from '@solana/wallet-standard-wallet-adapter-base'
|
|
11
11
|
import type { Transaction } from '@solana/web3.js'
|
|
12
|
-
import type { WalletMetadata } from '@meshconnect/uwc-types'
|
|
13
|
-
import { solanaNetwork } from '@meshconnect/uwc-constants'
|
|
12
|
+
import type { BitcoinConfig, WalletMetadata } from '@meshconnect/uwc-types'
|
|
13
|
+
import { solanaNetwork, bitcoinNetwork } from '@meshconnect/uwc-constants'
|
|
14
|
+
import { hexToBytes } from '@meshconnect/uwc-bitcoin'
|
|
14
15
|
import { BRIDGE_WALLET_POLL_MS } from './bridge-poll'
|
|
15
16
|
import {
|
|
16
17
|
LegacyInjectedSolanaWalletAdapter,
|
|
@@ -232,6 +233,328 @@ export async function getSolanaWallets(
|
|
|
232
233
|
return solanaWallets
|
|
233
234
|
}
|
|
234
235
|
|
|
236
|
+
/**
|
|
237
|
+
* The structural subset of a `bitcoin:*` wallet the injected adapter actually
|
|
238
|
+
* reads (`name`, `accounts`, `features`). Both a real Wallet Standard registry
|
|
239
|
+
* `Wallet` (local discovery) and the bridged wire wallet (framed discovery via
|
|
240
|
+
* `window.walletStandardBitcoinWallets`) satisfy it — unlike `Wallet`, whose
|
|
241
|
+
* extra fields (`chains`/`version`/`icon`) DON'T exist on the bridged shape, so
|
|
242
|
+
* typing `wallet` as `Wallet` would be a lie for framed clients.
|
|
243
|
+
*/
|
|
244
|
+
export interface DetectedBitcoinWalletHandle {
|
|
245
|
+
name: string
|
|
246
|
+
accounts: readonly unknown[]
|
|
247
|
+
features: Record<string, unknown>
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* A discovered `bitcoin:*` Wallet Standard wallet, keyed the same way as
|
|
252
|
+
* `WalletStandardInfo` above (`uuid` from name + primary chain) so
|
|
253
|
+
* `BitcoinWalletService.findWalletByUuid` can resolve the SPECIFIC wallet a
|
|
254
|
+
* session was matched to, instead of guessing.
|
|
255
|
+
*/
|
|
256
|
+
export interface DetectedBitcoinWallet {
|
|
257
|
+
uuid: string
|
|
258
|
+
name: string
|
|
259
|
+
/** `bitcoin:*` feature names the wallet advertises. */
|
|
260
|
+
features: string[]
|
|
261
|
+
/** The wallet handle; the Bitcoin namespace adapter reads its features directly. */
|
|
262
|
+
wallet: DetectedBitcoinWalletHandle
|
|
263
|
+
/**
|
|
264
|
+
* Set for a LEGACY-INJECTED bitcoin wallet (e.g. OKX at
|
|
265
|
+
* `window.okxwallet.bitcoin`) that isn't a Wallet Standard `bitcoin:*`
|
|
266
|
+
* wallet — the window path to re-resolve its provider. When present, the
|
|
267
|
+
* service builds an `OkxBitcoinAdapter` instead of `WalletStandardBitcoinAdapter`.
|
|
268
|
+
*/
|
|
269
|
+
legacyInjectedId?: string
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The subset of a legacy-injected bitcoin provider (e.g. OKX) the discovery
|
|
274
|
+
* probe touches — only the NON-prompting reads used to seed the detected
|
|
275
|
+
* handle. `OkxBitcoinAdapter` (uwc-bitcoin) owns the full surface.
|
|
276
|
+
*/
|
|
277
|
+
interface LegacyInjectedBitcoinProvider {
|
|
278
|
+
getAccounts: () => Promise<string[]>
|
|
279
|
+
getPublicKey: () => Promise<string>
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* The methods `OkxBitcoinAdapter` actually calls. We validate the WHOLE surface
|
|
284
|
+
* at discovery (not just `getAccounts`) — mirroring `isLegacyInjectedSolanaProvider`
|
|
285
|
+
* — so a wrong-shape global at the configured `injectedId` is rejected rather
|
|
286
|
+
* than accepted and then failing mid-connect.
|
|
287
|
+
*/
|
|
288
|
+
const LEGACY_BITCOIN_PROVIDER_METHODS = [
|
|
289
|
+
'requestAccounts',
|
|
290
|
+
'getAccounts',
|
|
291
|
+
'getPublicKey',
|
|
292
|
+
'signMessage',
|
|
293
|
+
'signPsbt',
|
|
294
|
+
'on',
|
|
295
|
+
'removeListener'
|
|
296
|
+
] as const
|
|
297
|
+
|
|
298
|
+
function isLegacyInjectedBitcoinProvider(
|
|
299
|
+
value: unknown
|
|
300
|
+
): value is LegacyInjectedBitcoinProvider {
|
|
301
|
+
if (!value || typeof value !== 'object') return false
|
|
302
|
+
const p = value as Record<string, unknown>
|
|
303
|
+
return LEGACY_BITCOIN_PROVIDER_METHODS.every(
|
|
304
|
+
method => typeof p[method] === 'function'
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Upper bound on the non-prompting legacy-provider `getAccounts()` seed read
|
|
309
|
+
* during discovery, so a misbehaving provider can't stall wallet-listing. */
|
|
310
|
+
const LEGACY_PROBE_TIMEOUT_MS = 1000
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Resolve the `enabledWallets` allow-list (case-insensitive `WalletMetadata.id`
|
|
314
|
+
* or `namespaceMetaData.bip122.injectedId`) to the set of registry
|
|
315
|
+
* `walletStandardName`s it permits. `null` = no gate configured (every
|
|
316
|
+
* bip122-capable wallet the metadata declares is enabled); an empty
|
|
317
|
+
* `enabledWallets` resolves to an empty allow-set (enables nothing) — mirrors
|
|
318
|
+
* `TronConnector`'s `enabledKeys` gate.
|
|
319
|
+
*/
|
|
320
|
+
function resolveEnabledBitcoinWalletNames(
|
|
321
|
+
expectedWallets: WalletMetadata[],
|
|
322
|
+
usingIntegratedBrowser: boolean,
|
|
323
|
+
enabledWallets?: BitcoinConfig['enabledWallets']
|
|
324
|
+
): Set<string> | null {
|
|
325
|
+
if (!enabledWallets) return null
|
|
326
|
+
|
|
327
|
+
const enabledKeys = new Set(
|
|
328
|
+
Array.from(enabledWallets, key => key.toLowerCase())
|
|
329
|
+
)
|
|
330
|
+
const allowedNames = new Set<string>()
|
|
331
|
+
|
|
332
|
+
for (const wallet of expectedWallets) {
|
|
333
|
+
const provider = usingIntegratedBrowser
|
|
334
|
+
? wallet.integratedBrowserInjectedProvider
|
|
335
|
+
: wallet.extensionInjectedProvider
|
|
336
|
+
const bip122Meta = provider?.namespaceMetaData?.bip122
|
|
337
|
+
const name = bip122Meta?.walletStandardName
|
|
338
|
+
if (!name) continue
|
|
339
|
+
|
|
340
|
+
const matches =
|
|
341
|
+
enabledKeys.has(wallet.id.toLowerCase()) ||
|
|
342
|
+
(bip122Meta.injectedId != null &&
|
|
343
|
+
enabledKeys.has(bip122Meta.injectedId.toLowerCase()))
|
|
344
|
+
if (matches) {
|
|
345
|
+
allowedNames.add(name.toLowerCase())
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return allowedNames
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Polls for bridge Bitcoin wallets when UWCBridgeChildInitialized is true.
|
|
354
|
+
* Bitcoin-specific twin of `pollForBridgeWallets` above (kept as a separate
|
|
355
|
+
* function, not a parameterized generic, to match the rest of this codebase's
|
|
356
|
+
* one-poller-per-namespace convention — see tron-discovery.ts/ton-discovery.ts).
|
|
357
|
+
*/
|
|
358
|
+
async function pollForBridgeBitcoinWallets(): Promise<DetectedBitcoinWallet[]> {
|
|
359
|
+
const pollInterval = 50
|
|
360
|
+
const maxPollTime = BRIDGE_WALLET_POLL_MS
|
|
361
|
+
const startTime = Date.now()
|
|
362
|
+
|
|
363
|
+
return new Promise(resolve => {
|
|
364
|
+
const poll = () => {
|
|
365
|
+
if (Date.now() - startTime > maxPollTime) {
|
|
366
|
+
resolve([])
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (
|
|
371
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
372
|
+
// @ts-ignore - window.walletStandardBitcoinWallets is set by the bridge
|
|
373
|
+
window.walletStandardBitcoinWallets &&
|
|
374
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
375
|
+
// @ts-ignore
|
|
376
|
+
Array.isArray(window.walletStandardBitcoinWallets)
|
|
377
|
+
) {
|
|
378
|
+
const bridgeWallets =
|
|
379
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
380
|
+
// @ts-ignore
|
|
381
|
+
window.walletStandardBitcoinWallets as DetectedBitcoinWallet[]
|
|
382
|
+
resolve(bridgeWallets)
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
setTimeout(poll, pollInterval)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
poll()
|
|
390
|
+
})
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Get the Wallet Standard wallets that expose a `bitcoin:*` chain (MetaMask,
|
|
395
|
+
* …), honoring `BitcoinConfig.enabledWallets` when configured.
|
|
396
|
+
*
|
|
397
|
+
* Async and bridge-aware, mirroring `getSolanaWallets`: when framed,
|
|
398
|
+
* `BridgeParent.getBitcoinWallets()`/`BridgeChild.tryMarshalBitcoin` populate
|
|
399
|
+
* `window.walletStandardBitcoinWallets`; poll for it first and fall through to
|
|
400
|
+
* the local registry only if the bridge reports nothing.
|
|
401
|
+
*/
|
|
402
|
+
export async function getBitcoinWallets(
|
|
403
|
+
expectedWallets: WalletMetadata[] = [],
|
|
404
|
+
usingIntegratedBrowser = false,
|
|
405
|
+
enabledWallets?: BitcoinConfig['enabledWallets']
|
|
406
|
+
): Promise<DetectedBitcoinWallet[]> {
|
|
407
|
+
if (typeof window === 'undefined') return []
|
|
408
|
+
|
|
409
|
+
const allowedNames = resolveEnabledBitcoinWalletNames(
|
|
410
|
+
expectedWallets,
|
|
411
|
+
usingIntegratedBrowser,
|
|
412
|
+
enabledWallets
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
416
|
+
// @ts-ignore - window.UWCBridgeChildInitialized is set by the bridge
|
|
417
|
+
if (window.UWCBridgeChildInitialized === true) {
|
|
418
|
+
const bridgeWallets = await pollForBridgeBitcoinWallets()
|
|
419
|
+
|
|
420
|
+
if (bridgeWallets.length > 0) {
|
|
421
|
+
return allowedNames === null
|
|
422
|
+
? bridgeWallets
|
|
423
|
+
: bridgeWallets.filter(wallet =>
|
|
424
|
+
allowedNames.has(wallet.name.toLowerCase())
|
|
425
|
+
)
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const { get } = getWallets()
|
|
430
|
+
const registryWallets = get().filter(wallet =>
|
|
431
|
+
(wallet.chains || []).some(chain => chain.startsWith('bitcoin:'))
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
const filteredWallets =
|
|
435
|
+
allowedNames === null
|
|
436
|
+
? registryWallets
|
|
437
|
+
: registryWallets.filter(wallet =>
|
|
438
|
+
allowedNames.has(wallet.name.toLowerCase())
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
const registryDetected: DetectedBitcoinWallet[] = filteredWallets.map(
|
|
442
|
+
wallet => {
|
|
443
|
+
// Same rationale as Solana's uuid: key on the wallet's first `bitcoin:`
|
|
444
|
+
// chain, not `chains[0]`, so a multi-chain wallet's id doesn't depend on
|
|
445
|
+
// chain ordering.
|
|
446
|
+
const bitcoinChain =
|
|
447
|
+
wallet.chains?.find(chain => chain.startsWith('bitcoin:')) ||
|
|
448
|
+
wallet.chains?.[0] ||
|
|
449
|
+
'unknown'
|
|
450
|
+
return {
|
|
451
|
+
uuid: generateWalletId(wallet.name, bitcoinChain),
|
|
452
|
+
name: wallet.name,
|
|
453
|
+
features: Object.keys(wallet.features),
|
|
454
|
+
wallet
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
// Legacy-injected fallback (OKX): wallets that expose bitcoin via a plain
|
|
460
|
+
// injected provider (`window.okxwallet.bitcoin`), NOT Wallet Standard. A
|
|
461
|
+
// registry wallet always wins over a legacy entry of the same name.
|
|
462
|
+
const legacyDetected = await probeLegacyInjectedBitcoinWallets(
|
|
463
|
+
expectedWallets,
|
|
464
|
+
usingIntegratedBrowser,
|
|
465
|
+
allowedNames,
|
|
466
|
+
new Set(registryDetected.map(w => w.name.toLowerCase()))
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
return [...registryDetected, ...legacyDetected]
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Discover legacy-injected bitcoin wallets (e.g. OKX — providers exposed at a
|
|
474
|
+
* `window.*` path, not Wallet Standard). Mirrors the
|
|
475
|
+
* Solana legacy fallback: iterate the catalog, read each wallet's
|
|
476
|
+
* `bip122.injectedId` + `walletStandardName`, resolve the window path, and — if
|
|
477
|
+
* present — emit a `DetectedBitcoinWallet` tagged with `legacyInjectedId`. The
|
|
478
|
+
* handle's `accounts` are seeded from a NON-prompting `getAccounts()` so the
|
|
479
|
+
* silent existing-connection probe works without popping the wallet.
|
|
480
|
+
*/
|
|
481
|
+
async function probeLegacyInjectedBitcoinWallets(
|
|
482
|
+
expectedWallets: WalletMetadata[],
|
|
483
|
+
usingIntegratedBrowser: boolean,
|
|
484
|
+
allowedNames: Set<string> | null,
|
|
485
|
+
alreadySeen: Set<string>
|
|
486
|
+
): Promise<DetectedBitcoinWallet[]> {
|
|
487
|
+
const detected: DetectedBitcoinWallet[] = []
|
|
488
|
+
const seen = new Set(alreadySeen)
|
|
489
|
+
|
|
490
|
+
for (const expected of expectedWallets) {
|
|
491
|
+
const sessionProvider = usingIntegratedBrowser
|
|
492
|
+
? expected.integratedBrowserInjectedProvider
|
|
493
|
+
: expected.extensionInjectedProvider
|
|
494
|
+
const bip122Meta = sessionProvider?.namespaceMetaData?.bip122
|
|
495
|
+
const injectedId = bip122Meta?.injectedId
|
|
496
|
+
const name = bip122Meta?.walletStandardName
|
|
497
|
+
if (!injectedId || !name) continue
|
|
498
|
+
const key = name.toLowerCase()
|
|
499
|
+
if (seen.has(key)) continue
|
|
500
|
+
if (allowedNames !== null && !allowedNames.has(key)) continue
|
|
501
|
+
|
|
502
|
+
const resolved = resolveInjectedProperty(injectedId)
|
|
503
|
+
if (!isLegacyInjectedBitcoinProvider(resolved)) continue
|
|
504
|
+
const provider = resolved
|
|
505
|
+
|
|
506
|
+
// Non-prompting seed: `getAccounts()` returns already-authorized addresses
|
|
507
|
+
// ([] when not connected — verified against OKX). Lets
|
|
508
|
+
// `checkExistingConnection` restore silently. Bounded so a stalled provider
|
|
509
|
+
// can't hang wallet-listing (discovery runs on every getBitcoinWallets pass).
|
|
510
|
+
let accounts: { address: string; publicKey: Uint8Array }[] = []
|
|
511
|
+
try {
|
|
512
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
513
|
+
const addresses = await Promise.race([
|
|
514
|
+
provider.getAccounts(),
|
|
515
|
+
new Promise<string[]>(resolve => {
|
|
516
|
+
timer = setTimeout(() => resolve([]), LEGACY_PROBE_TIMEOUT_MS)
|
|
517
|
+
})
|
|
518
|
+
// Clear the loser's timer so a fast `getAccounts()` doesn't leave a
|
|
519
|
+
// pending 1s timeout behind on every discovery pass.
|
|
520
|
+
]).finally(() => {
|
|
521
|
+
if (timer) clearTimeout(timer)
|
|
522
|
+
})
|
|
523
|
+
if (addresses?.length) {
|
|
524
|
+
let publicKey: Uint8Array = new Uint8Array()
|
|
525
|
+
try {
|
|
526
|
+
if (typeof provider.getPublicKey === 'function') {
|
|
527
|
+
publicKey = hexToBytes(await provider.getPublicKey())
|
|
528
|
+
}
|
|
529
|
+
} catch {
|
|
530
|
+
publicKey = new Uint8Array()
|
|
531
|
+
}
|
|
532
|
+
accounts = addresses.map((address, i) => ({
|
|
533
|
+
address,
|
|
534
|
+
publicKey: i === 0 ? publicKey : new Uint8Array()
|
|
535
|
+
}))
|
|
536
|
+
}
|
|
537
|
+
} catch {
|
|
538
|
+
accounts = []
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
detected.push({
|
|
542
|
+
uuid: generateWalletId(name, bitcoinNetwork.id),
|
|
543
|
+
name,
|
|
544
|
+
features: [
|
|
545
|
+
'bitcoin:connect',
|
|
546
|
+
'bitcoin:signMessage',
|
|
547
|
+
'bitcoin:signTransaction'
|
|
548
|
+
],
|
|
549
|
+
wallet: { name, accounts, features: {} },
|
|
550
|
+
legacyInjectedId: injectedId
|
|
551
|
+
})
|
|
552
|
+
seen.add(key)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
return detected
|
|
556
|
+
}
|
|
557
|
+
|
|
235
558
|
/**
|
|
236
559
|
* Check if a wallet supports Solana
|
|
237
560
|
*/
|