@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
|
@@ -5,7 +5,7 @@ import { ConnectionManager } from './connection-manager'
|
|
|
5
5
|
function makeProvider(
|
|
6
6
|
namespaces: Partial<
|
|
7
7
|
Record<
|
|
8
|
-
'eip155' | 'solana' | 'tron' | 'tvm',
|
|
8
|
+
'eip155' | 'solana' | 'tron' | 'tvm' | 'bip122',
|
|
9
9
|
{
|
|
10
10
|
detectedWallet?: {
|
|
11
11
|
uuid?: string
|
|
@@ -22,7 +22,8 @@ function makeProvider(
|
|
|
22
22
|
...(namespaces.eip155 ? ['eip155:1'] : []),
|
|
23
23
|
...(namespaces.solana ? ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'] : []),
|
|
24
24
|
...(namespaces.tron ? ['tron:0x2b6653dc'] : []),
|
|
25
|
-
...(namespaces.tvm ? ['tvm:-239'] : [])
|
|
25
|
+
...(namespaces.tvm ? ['tvm:-239'] : []),
|
|
26
|
+
...(namespaces.bip122 ? ['bip122:000000000019d6689c085ae165831e93'] : [])
|
|
26
27
|
],
|
|
27
28
|
namespaceMetaData: namespaces
|
|
28
29
|
} as unknown as ExtensionInjectedProvider
|
|
@@ -112,11 +113,38 @@ function makeTonService() {
|
|
|
112
113
|
}
|
|
113
114
|
}
|
|
114
115
|
|
|
116
|
+
function makeBitcoinService() {
|
|
117
|
+
return {
|
|
118
|
+
getAccount: vi.fn().mockReturnValue(null),
|
|
119
|
+
getLiveAccount: vi.fn().mockResolvedValue(null),
|
|
120
|
+
initializeDiscovery: vi.fn().mockResolvedValue(undefined),
|
|
121
|
+
findWalletByUuid: vi.fn().mockReturnValue(null),
|
|
122
|
+
checkExistingConnection: vi.fn().mockResolvedValue(null),
|
|
123
|
+
connect: vi.fn().mockResolvedValue({
|
|
124
|
+
networkId: 'bip122:000000000019d6689c085ae165831e93',
|
|
125
|
+
address: 'bc1qBtcAddr1',
|
|
126
|
+
availableAddresses: []
|
|
127
|
+
}),
|
|
128
|
+
setConnectionState: vi.fn(),
|
|
129
|
+
buildAvailableAddresses: vi
|
|
130
|
+
.fn()
|
|
131
|
+
.mockImplementation((ids, addr) =>
|
|
132
|
+
ids
|
|
133
|
+
.filter((id: string) => id.startsWith('bip122'))
|
|
134
|
+
.map((id: string) => ({ address: addr, networkId: id }))
|
|
135
|
+
),
|
|
136
|
+
isConnected: vi.fn().mockReturnValue(false),
|
|
137
|
+
getConnectedWalletUuid: vi.fn().mockReturnValue(null),
|
|
138
|
+
disconnect: vi.fn()
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
115
142
|
describe('ConnectionManager — cross-namespace error resilience (ONC-2696)', () => {
|
|
116
143
|
let ethService: ReturnType<typeof makeEthService>
|
|
117
144
|
let solanaService: ReturnType<typeof makeSolanaService>
|
|
118
145
|
let tronService: ReturnType<typeof makeTronService>
|
|
119
146
|
let tonService: ReturnType<typeof makeTonService>
|
|
147
|
+
let bitcoinService: ReturnType<typeof makeBitcoinService>
|
|
120
148
|
let manager: ConnectionManager
|
|
121
149
|
|
|
122
150
|
beforeEach(() => {
|
|
@@ -124,11 +152,13 @@ describe('ConnectionManager — cross-namespace error resilience (ONC-2696)', ()
|
|
|
124
152
|
solanaService = makeSolanaService()
|
|
125
153
|
tronService = makeTronService()
|
|
126
154
|
tonService = makeTonService()
|
|
155
|
+
bitcoinService = makeBitcoinService()
|
|
127
156
|
manager = new ConnectionManager(
|
|
128
157
|
ethService as any,
|
|
129
158
|
solanaService as any,
|
|
130
159
|
tronService as any,
|
|
131
|
-
tonService as any
|
|
160
|
+
tonService as any,
|
|
161
|
+
bitcoinService as any
|
|
132
162
|
)
|
|
133
163
|
})
|
|
134
164
|
|
|
@@ -430,6 +460,30 @@ describe('ConnectionManager — cross-namespace error resilience (ONC-2696)', ()
|
|
|
430
460
|
expect(namespaces).not.toContain('solana')
|
|
431
461
|
})
|
|
432
462
|
|
|
463
|
+
it('connectEthereum gathers an already-connected Bitcoin address (bip122 in the cross-namespace gather)', async () => {
|
|
464
|
+
ethService.findWalletByUuid.mockReturnValue({ provider: {} })
|
|
465
|
+
// Bitcoin already has a live session.
|
|
466
|
+
bitcoinService.getLiveAccount.mockResolvedValue('bc1qBTC')
|
|
467
|
+
|
|
468
|
+
const provider = makeProvider({
|
|
469
|
+
eip155: { detectedWallet: { uuid: 'mm-eip155' } },
|
|
470
|
+
bip122: { detectedWallet: { uuid: 'mm-bip122' } }
|
|
471
|
+
})
|
|
472
|
+
|
|
473
|
+
const result = await manager.connectEthereum(
|
|
474
|
+
makeNetwork('eip155:1', 'eip155'),
|
|
475
|
+
provider
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
const namespaces = result.availableAddresses.map(
|
|
479
|
+
a => a.networkId.split(':')[0]
|
|
480
|
+
)
|
|
481
|
+
expect(namespaces).toContain('bip122')
|
|
482
|
+
expect(result.availableAddresses.some(a => a.address === 'bc1qBTC')).toBe(
|
|
483
|
+
true
|
|
484
|
+
)
|
|
485
|
+
})
|
|
486
|
+
|
|
433
487
|
// ONC-3429: the EVM cross-namespace gather must fall back to an explicit
|
|
434
488
|
// connect (eth_requestAccounts) when the silent eth_accounts returns nothing,
|
|
435
489
|
// matching Solana/Tron/TON. MetaMask v13.32.0 stopped pre-permitting EVM on a
|
|
@@ -504,6 +558,7 @@ describe('ConnectionManager — iframe-safe cross-namespace gather (ONC-3383)',
|
|
|
504
558
|
let solanaService: ReturnType<typeof makeSolanaService>
|
|
505
559
|
let tronService: ReturnType<typeof makeTronService>
|
|
506
560
|
let tonService: ReturnType<typeof makeTonService>
|
|
561
|
+
let bitcoinService: ReturnType<typeof makeBitcoinService>
|
|
507
562
|
let manager: ConnectionManager
|
|
508
563
|
|
|
509
564
|
beforeEach(() => {
|
|
@@ -511,11 +566,13 @@ describe('ConnectionManager — iframe-safe cross-namespace gather (ONC-3383)',
|
|
|
511
566
|
solanaService = makeSolanaService()
|
|
512
567
|
tronService = makeTronService()
|
|
513
568
|
tonService = makeTonService()
|
|
569
|
+
bitcoinService = makeBitcoinService()
|
|
514
570
|
manager = new ConnectionManager(
|
|
515
571
|
ethService as any,
|
|
516
572
|
solanaService as any,
|
|
517
573
|
tronService as any,
|
|
518
|
-
tonService as any
|
|
574
|
+
tonService as any,
|
|
575
|
+
bitcoinService as any
|
|
519
576
|
)
|
|
520
577
|
// Simulate running inside an iframe: window.top !== window.self
|
|
521
578
|
Object.defineProperty(window, 'top', { value: {}, configurable: true })
|
|
@@ -735,6 +792,7 @@ describe('ConnectionManager — legacy-injectedId Solana gather gate (ONC-3536 +
|
|
|
735
792
|
let solanaService: ReturnType<typeof makeSolanaService>
|
|
736
793
|
let tronService: ReturnType<typeof makeTronService>
|
|
737
794
|
let tonService: ReturnType<typeof makeTonService>
|
|
795
|
+
let bitcoinService: ReturnType<typeof makeBitcoinService>
|
|
738
796
|
let onSkip: ReturnType<typeof vi.fn>
|
|
739
797
|
let onGathered: ReturnType<typeof vi.fn>
|
|
740
798
|
|
|
@@ -744,6 +802,7 @@ describe('ConnectionManager — legacy-injectedId Solana gather gate (ONC-3536 +
|
|
|
744
802
|
solanaService as any,
|
|
745
803
|
tronService as any,
|
|
746
804
|
tonService as any,
|
|
805
|
+
bitcoinService as any,
|
|
747
806
|
{
|
|
748
807
|
usingIntegratedBrowser,
|
|
749
808
|
onSecondarySolanaConnectSkipped: onSkip,
|
|
@@ -756,6 +815,7 @@ describe('ConnectionManager — legacy-injectedId Solana gather gate (ONC-3536 +
|
|
|
756
815
|
solanaService = makeSolanaService()
|
|
757
816
|
tronService = makeTronService()
|
|
758
817
|
tonService = makeTonService()
|
|
818
|
+
bitcoinService = makeBitcoinService()
|
|
759
819
|
onSkip = vi.fn()
|
|
760
820
|
onGathered = vi.fn()
|
|
761
821
|
// Default: running inside an iframe (window.top !== window.self) — the
|
|
@@ -1071,6 +1131,7 @@ describe('ConnectionManager — wallet switch releases the other wallet’s conn
|
|
|
1071
1131
|
let solanaService: ReturnType<typeof makeSolanaService>
|
|
1072
1132
|
let tronService: ReturnType<typeof makeTronService>
|
|
1073
1133
|
let tonService: ReturnType<typeof makeTonService>
|
|
1134
|
+
let bitcoinService: ReturnType<typeof makeBitcoinService>
|
|
1074
1135
|
let onWalletConnectionsReleased: ReturnType<typeof vi.fn>
|
|
1075
1136
|
let manager: ConnectionManager
|
|
1076
1137
|
|
|
@@ -1079,12 +1140,14 @@ describe('ConnectionManager — wallet switch releases the other wallet’s conn
|
|
|
1079
1140
|
solanaService = makeSolanaService()
|
|
1080
1141
|
tronService = makeTronService()
|
|
1081
1142
|
tonService = makeTonService()
|
|
1143
|
+
bitcoinService = makeBitcoinService()
|
|
1082
1144
|
onWalletConnectionsReleased = vi.fn()
|
|
1083
1145
|
manager = new ConnectionManager(
|
|
1084
1146
|
ethService as any,
|
|
1085
1147
|
solanaService as any,
|
|
1086
1148
|
tronService as any,
|
|
1087
1149
|
tonService as any,
|
|
1150
|
+
bitcoinService as any,
|
|
1088
1151
|
{ onWalletConnectionsReleased }
|
|
1089
1152
|
)
|
|
1090
1153
|
})
|
|
@@ -1218,6 +1281,147 @@ describe('ConnectionManager — wallet switch releases the other wallet’s conn
|
|
|
1218
1281
|
})
|
|
1219
1282
|
})
|
|
1220
1283
|
|
|
1284
|
+
describe('ConnectionManager — bip122 flows through the shared plumbing', () => {
|
|
1285
|
+
let ethService: ReturnType<typeof makeEthService>
|
|
1286
|
+
let solanaService: ReturnType<typeof makeSolanaService>
|
|
1287
|
+
let tronService: ReturnType<typeof makeTronService>
|
|
1288
|
+
let tonService: ReturnType<typeof makeTonService>
|
|
1289
|
+
let bitcoinService: ReturnType<typeof makeBitcoinService>
|
|
1290
|
+
let manager: ConnectionManager
|
|
1291
|
+
|
|
1292
|
+
beforeEach(() => {
|
|
1293
|
+
ethService = makeEthService()
|
|
1294
|
+
solanaService = makeSolanaService()
|
|
1295
|
+
tronService = makeTronService()
|
|
1296
|
+
tonService = makeTonService()
|
|
1297
|
+
bitcoinService = makeBitcoinService()
|
|
1298
|
+
manager = new ConnectionManager(
|
|
1299
|
+
ethService as any,
|
|
1300
|
+
solanaService as any,
|
|
1301
|
+
tronService as any,
|
|
1302
|
+
tonService as any,
|
|
1303
|
+
bitcoinService as any
|
|
1304
|
+
)
|
|
1305
|
+
})
|
|
1306
|
+
|
|
1307
|
+
it('checkExistingConnections silently restores an already-authorized bip122 wallet', async () => {
|
|
1308
|
+
bitcoinService.findWalletByUuid.mockReturnValue({
|
|
1309
|
+
uuid: 'metamask-uuid',
|
|
1310
|
+
wallet: {}
|
|
1311
|
+
})
|
|
1312
|
+
bitcoinService.checkExistingConnection.mockResolvedValue('bc1qexisting')
|
|
1313
|
+
|
|
1314
|
+
const result = await manager.checkExistingConnections(
|
|
1315
|
+
'bip122',
|
|
1316
|
+
makeProvider({ bip122: { detectedWallet: { uuid: 'metamask-uuid' } } })
|
|
1317
|
+
)
|
|
1318
|
+
|
|
1319
|
+
expect(result.address).toBe('bc1qexisting')
|
|
1320
|
+
expect(bitcoinService.setConnectionState).toHaveBeenCalledWith(
|
|
1321
|
+
{ uuid: 'metamask-uuid', wallet: {} },
|
|
1322
|
+
'bc1qexisting'
|
|
1323
|
+
)
|
|
1324
|
+
expect(
|
|
1325
|
+
result.availableAddresses.some(
|
|
1326
|
+
a => a.address === 'bc1qexisting' && a.networkId.startsWith('bip122')
|
|
1327
|
+
)
|
|
1328
|
+
).toBe(true)
|
|
1329
|
+
})
|
|
1330
|
+
|
|
1331
|
+
it('checkExistingConnections does not prompt/connect when no existing bip122 authorization is found', async () => {
|
|
1332
|
+
bitcoinService.findWalletByUuid.mockReturnValue({
|
|
1333
|
+
uuid: 'metamask-uuid',
|
|
1334
|
+
wallet: {}
|
|
1335
|
+
})
|
|
1336
|
+
bitcoinService.checkExistingConnection.mockResolvedValue(null)
|
|
1337
|
+
|
|
1338
|
+
const result = await manager.checkExistingConnections(
|
|
1339
|
+
'bip122',
|
|
1340
|
+
makeProvider({ bip122: { detectedWallet: { uuid: 'metamask-uuid' } } })
|
|
1341
|
+
)
|
|
1342
|
+
|
|
1343
|
+
expect(result.address).toBeNull()
|
|
1344
|
+
expect(bitcoinService.setConnectionState).not.toHaveBeenCalled()
|
|
1345
|
+
expect(bitcoinService.connect).not.toHaveBeenCalled()
|
|
1346
|
+
})
|
|
1347
|
+
|
|
1348
|
+
it('connectBitcoin returns the connect result merged with gathered cross-namespace addresses', async () => {
|
|
1349
|
+
bitcoinService.connect.mockResolvedValue({
|
|
1350
|
+
networkId: 'bip122:000000000019d6689c085ae165831e93',
|
|
1351
|
+
address: 'bc1qnew',
|
|
1352
|
+
availableAddresses: [
|
|
1353
|
+
{
|
|
1354
|
+
address: 'bc1qnew',
|
|
1355
|
+
networkId: 'bip122:000000000019d6689c085ae165831e93'
|
|
1356
|
+
}
|
|
1357
|
+
]
|
|
1358
|
+
})
|
|
1359
|
+
|
|
1360
|
+
const result = await manager.connectBitcoin(
|
|
1361
|
+
makeNetwork('bip122:000000000019d6689c085ae165831e93', 'bip122'),
|
|
1362
|
+
makeProvider({ bip122: { detectedWallet: { uuid: 'metamask-uuid' } } })
|
|
1363
|
+
)
|
|
1364
|
+
|
|
1365
|
+
expect(result.address).toBe('bc1qnew')
|
|
1366
|
+
expect(result.availableAddresses.some(a => a.address === 'bc1qnew')).toBe(
|
|
1367
|
+
true
|
|
1368
|
+
)
|
|
1369
|
+
})
|
|
1370
|
+
|
|
1371
|
+
it('switchNetwork connects bip122 when not yet connected', async () => {
|
|
1372
|
+
bitcoinService.isConnected.mockReturnValue(false)
|
|
1373
|
+
bitcoinService.connect.mockResolvedValue({
|
|
1374
|
+
networkId: 'bip122:000000000019d6689c085ae165831e93',
|
|
1375
|
+
address: 'bc1qswitched',
|
|
1376
|
+
availableAddresses: [
|
|
1377
|
+
{
|
|
1378
|
+
address: 'bc1qswitched',
|
|
1379
|
+
networkId: 'bip122:000000000019d6689c085ae165831e93'
|
|
1380
|
+
}
|
|
1381
|
+
]
|
|
1382
|
+
})
|
|
1383
|
+
|
|
1384
|
+
const result = await manager.switchNetwork(
|
|
1385
|
+
makeNetwork('bip122:000000000019d6689c085ae165831e93', 'bip122'),
|
|
1386
|
+
makeProvider({ bip122: { detectedWallet: { uuid: 'metamask-uuid' } } })
|
|
1387
|
+
)
|
|
1388
|
+
|
|
1389
|
+
expect(result.address).toBe('bc1qswitched')
|
|
1390
|
+
expect(bitcoinService.connect).toHaveBeenCalledTimes(1)
|
|
1391
|
+
})
|
|
1392
|
+
|
|
1393
|
+
it('switchNetwork rejects a non-Bitcoin-mainnet bip122 chain even when already connected (no chain stamping)', async () => {
|
|
1394
|
+
// Already connected to Bitcoin.
|
|
1395
|
+
bitcoinService.isConnected.mockReturnValue(true)
|
|
1396
|
+
bitcoinService.getLiveAccount.mockResolvedValue('bc1qbitcoin')
|
|
1397
|
+
|
|
1398
|
+
// Switching to Dogecoin (a bip122 network, different genesis/chain id) must
|
|
1399
|
+
// throw rather than fall through and stamp the Bitcoin address onto it.
|
|
1400
|
+
await expect(
|
|
1401
|
+
manager.switchNetwork(
|
|
1402
|
+
makeNetwork('bip122:1a91e3dace36e2be3bf030a65679fe82', 'bip122'),
|
|
1403
|
+
makeProvider({ bip122: { detectedWallet: { uuid: 'metamask-uuid' } } })
|
|
1404
|
+
)
|
|
1405
|
+
).rejects.toThrow()
|
|
1406
|
+
// Must NOT have quietly connected/stamped.
|
|
1407
|
+
expect(bitcoinService.connect).not.toHaveBeenCalled()
|
|
1408
|
+
})
|
|
1409
|
+
|
|
1410
|
+
it('switchNetwork to Bitcoin mainnet when already connected returns the LIVE bitcoin address', async () => {
|
|
1411
|
+
bitcoinService.isConnected.mockReturnValue(true)
|
|
1412
|
+
bitcoinService.getLiveAccount.mockResolvedValue('bc1qLIVE')
|
|
1413
|
+
|
|
1414
|
+
const result = await manager.switchNetwork(
|
|
1415
|
+
makeNetwork('bip122:000000000019d6689c085ae165831e93', 'bip122'),
|
|
1416
|
+
makeProvider({ bip122: { detectedWallet: { uuid: 'metamask-uuid' } } })
|
|
1417
|
+
)
|
|
1418
|
+
|
|
1419
|
+
// getLiveCurrentAccount now has a bip122 branch — no empty-string address.
|
|
1420
|
+
expect(result.address).toBe('bc1qLIVE')
|
|
1421
|
+
expect(bitcoinService.connect).not.toHaveBeenCalled()
|
|
1422
|
+
})
|
|
1423
|
+
})
|
|
1424
|
+
|
|
1221
1425
|
// Root cause: on an in-wallet account switch, switchNetwork's return used to
|
|
1222
1426
|
// read the frozen connect-time cache (service.getAccount()) for both `address`
|
|
1223
1427
|
// and `availableAddresses`, so a post-switch verify/re-auth got bound to the
|
|
@@ -1228,6 +1432,7 @@ describe('ConnectionManager — switchNetwork returns LIVE addresses (not connec
|
|
|
1228
1432
|
let solanaService: ReturnType<typeof makeSolanaService>
|
|
1229
1433
|
let tronService: ReturnType<typeof makeTronService>
|
|
1230
1434
|
let tonService: ReturnType<typeof makeTonService>
|
|
1435
|
+
let bitcoinService: ReturnType<typeof makeBitcoinService>
|
|
1231
1436
|
let manager: ConnectionManager
|
|
1232
1437
|
|
|
1233
1438
|
beforeEach(() => {
|
|
@@ -1235,11 +1440,13 @@ describe('ConnectionManager — switchNetwork returns LIVE addresses (not connec
|
|
|
1235
1440
|
solanaService = makeSolanaService()
|
|
1236
1441
|
tronService = makeTronService()
|
|
1237
1442
|
tonService = makeTonService()
|
|
1443
|
+
bitcoinService = makeBitcoinService()
|
|
1238
1444
|
manager = new ConnectionManager(
|
|
1239
1445
|
ethService as any,
|
|
1240
1446
|
solanaService as any,
|
|
1241
1447
|
tronService as any,
|
|
1242
|
-
tonService as any
|
|
1448
|
+
tonService as any,
|
|
1449
|
+
bitcoinService as any
|
|
1243
1450
|
)
|
|
1244
1451
|
})
|
|
1245
1452
|
|
|
@@ -1327,6 +1534,7 @@ describe('ConnectionManager — bounded secondary gathers + step telemetry (ONC-
|
|
|
1327
1534
|
solanaService as any,
|
|
1328
1535
|
tronService as any,
|
|
1329
1536
|
tonService as any,
|
|
1537
|
+
makeBitcoinService() as any,
|
|
1330
1538
|
{
|
|
1331
1539
|
onConnectStep,
|
|
1332
1540
|
...(secondaryGatherTimeoutMs !== undefined && {
|
|
@@ -1480,6 +1688,7 @@ describe('ConnectionManager — secondary gather outcome fidelity (ONC-183 revie
|
|
|
1480
1688
|
solanaService as any,
|
|
1481
1689
|
tronService as any,
|
|
1482
1690
|
tonService as any,
|
|
1691
|
+
makeBitcoinService() as any,
|
|
1483
1692
|
{ onConnectStep }
|
|
1484
1693
|
)
|
|
1485
1694
|
}
|
|
@@ -15,6 +15,8 @@ import { EthereumWalletService } from './ethereum/ethereum-wallet-service'
|
|
|
15
15
|
import { SolanaWalletService } from './solana/solana-wallet-service'
|
|
16
16
|
import { TronWalletService } from './tron/tron-wallet-service'
|
|
17
17
|
import { TonWalletService } from './ton/ton-wallet-service'
|
|
18
|
+
import { BitcoinWalletService } from './bitcoin/bitcoin-wallet-service'
|
|
19
|
+
import { assertBitcoinNetwork } from '@meshconnect/uwc-bitcoin'
|
|
18
20
|
import { parseError } from '../utils/error-utils'
|
|
19
21
|
|
|
20
22
|
/**
|
|
@@ -304,6 +306,7 @@ export class ConnectionManager {
|
|
|
304
306
|
private solanaService: SolanaWalletService,
|
|
305
307
|
private tronService: TronWalletService,
|
|
306
308
|
private tonService: TonWalletService,
|
|
309
|
+
private bitcoinService: BitcoinWalletService,
|
|
307
310
|
options: ConnectionManagerOptions = {}
|
|
308
311
|
) {
|
|
309
312
|
this.usingIntegratedBrowser = options.usingIntegratedBrowser ?? false
|
|
@@ -433,6 +436,14 @@ export class ConnectionManager {
|
|
|
433
436
|
}
|
|
434
437
|
}
|
|
435
438
|
|
|
439
|
+
if (this.bitcoinService.isConnected()) {
|
|
440
|
+
const uuid = provider.namespaceMetaData?.bip122?.detectedWallet?.uuid
|
|
441
|
+
if (!uuid || this.bitcoinService.getConnectedWalletUuid() !== uuid) {
|
|
442
|
+
await this.bitcoinService.disconnect()
|
|
443
|
+
released.push('bip122')
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
436
447
|
// Surface the release (observability only — it already happened). An
|
|
437
448
|
// unexpected emit, e.g. during a same-wallet reconnect, is the prod
|
|
438
449
|
// signal that a namespace's identity comparison broke.
|
|
@@ -445,7 +456,7 @@ export class ConnectionManager {
|
|
|
445
456
|
* Check and add existing connections from other namespaces
|
|
446
457
|
*/
|
|
447
458
|
async checkExistingConnections(
|
|
448
|
-
targetNamespace:
|
|
459
|
+
targetNamespace: Namespace,
|
|
449
460
|
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider
|
|
450
461
|
): Promise<{
|
|
451
462
|
address: string | null
|
|
@@ -607,6 +618,39 @@ export class ConnectionManager {
|
|
|
607
618
|
provider,
|
|
608
619
|
availableAddresses
|
|
609
620
|
)
|
|
621
|
+
} else if (targetNamespace === 'bip122') {
|
|
622
|
+
const bitcoinAccount = this.bitcoinService.getAccount()
|
|
623
|
+
if (bitcoinAccount) {
|
|
624
|
+
currentAddress = bitcoinAccount
|
|
625
|
+
} else {
|
|
626
|
+
const walletUuid =
|
|
627
|
+
provider.namespaceMetaData?.bip122?.detectedWallet?.uuid
|
|
628
|
+
if (walletUuid) {
|
|
629
|
+
const wallet = this.bitcoinService.findWalletByUuid(walletUuid)
|
|
630
|
+
if (wallet) {
|
|
631
|
+
const address =
|
|
632
|
+
await this.bitcoinService.checkExistingConnection(wallet)
|
|
633
|
+
if (address) {
|
|
634
|
+
currentAddress = address
|
|
635
|
+
this.bitcoinService.setConnectionState(wallet, address)
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
if (currentAddress) {
|
|
642
|
+
const bitcoinAddresses = this.bitcoinService.buildAvailableAddresses(
|
|
643
|
+
provider.supportedNetworkIds,
|
|
644
|
+
currentAddress
|
|
645
|
+
)
|
|
646
|
+
availableAddresses.push(...bitcoinAddresses)
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
await this.checkAndAddOtherNamespaceAddresses(
|
|
650
|
+
'bip122',
|
|
651
|
+
provider,
|
|
652
|
+
availableAddresses
|
|
653
|
+
)
|
|
610
654
|
}
|
|
611
655
|
|
|
612
656
|
return { address: currentAddress, availableAddresses }
|
|
@@ -865,6 +909,47 @@ export class ConnectionManager {
|
|
|
865
909
|
return outcome
|
|
866
910
|
}
|
|
867
911
|
|
|
912
|
+
/**
|
|
913
|
+
* Secondary gather for an already-authorized Bitcoin account. Reads a live
|
|
914
|
+
* session first; if none, probes for an authorized account WITHOUT prompting
|
|
915
|
+
* — `checkExistingConnection` reads the wallet's already-exposed accounts and
|
|
916
|
+
* never calls `bitcoin:connect`. Deliberately has NO connect() fallback
|
|
917
|
+
* (unlike TON): a bitcoin connect prompts, which a best-effort secondary
|
|
918
|
+
* gather must never do.
|
|
919
|
+
*/
|
|
920
|
+
private async checkAndAddBitcoinAddresses(
|
|
921
|
+
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider,
|
|
922
|
+
availableAddresses: AvailableAddress[]
|
|
923
|
+
): Promise<SecondaryGatherOutcome> {
|
|
924
|
+
let bitcoinAccount = await this.bitcoinService.getLiveAccount()
|
|
925
|
+
|
|
926
|
+
if (!bitcoinAccount) {
|
|
927
|
+
const uuid = provider.namespaceMetaData?.bip122?.detectedWallet?.uuid
|
|
928
|
+
if (uuid) {
|
|
929
|
+
await this.bitcoinService.initializeDiscovery()
|
|
930
|
+
const detected = this.bitcoinService.findWalletByUuid(uuid)
|
|
931
|
+
if (detected) {
|
|
932
|
+
const existingAddress =
|
|
933
|
+
await this.bitcoinService.checkExistingConnection(detected)
|
|
934
|
+
if (existingAddress) {
|
|
935
|
+
this.bitcoinService.setConnectionState(detected, existingAddress)
|
|
936
|
+
bitcoinAccount = existingAddress
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
if (bitcoinAccount) {
|
|
943
|
+
availableAddresses.push(
|
|
944
|
+
...this.bitcoinService.buildAvailableAddresses(
|
|
945
|
+
provider.supportedNetworkIds,
|
|
946
|
+
bitcoinAccount
|
|
947
|
+
)
|
|
948
|
+
)
|
|
949
|
+
}
|
|
950
|
+
return 'ok'
|
|
951
|
+
}
|
|
952
|
+
|
|
868
953
|
/**
|
|
869
954
|
* After connecting one namespace, check and add addresses for all OTHER namespaces.
|
|
870
955
|
* E.g. after connecting Ethereum, also gather Solana, Tron, and TON addresses.
|
|
@@ -877,7 +962,7 @@ export class ConnectionManager {
|
|
|
877
962
|
* wallet's slowness into three missing addresses.
|
|
878
963
|
*/
|
|
879
964
|
private async checkAndAddOtherNamespaceAddresses(
|
|
880
|
-
connectedNamespace: 'eip155' | 'solana' | 'tron' | 'tvm',
|
|
965
|
+
connectedNamespace: 'eip155' | 'solana' | 'tron' | 'tvm' | 'bip122',
|
|
881
966
|
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider,
|
|
882
967
|
availableAddresses: AvailableAddress[]
|
|
883
968
|
): Promise<void> {
|
|
@@ -901,6 +986,11 @@ export class ConnectionManager {
|
|
|
901
986
|
this.checkAndAddTonAddresses(provider, availableAddresses)
|
|
902
987
|
)
|
|
903
988
|
}
|
|
989
|
+
if (connectedNamespace !== 'bip122') {
|
|
990
|
+
await this.gatherNamespace('bip122', () =>
|
|
991
|
+
this.checkAndAddBitcoinAddresses(provider, availableAddresses)
|
|
992
|
+
)
|
|
993
|
+
}
|
|
904
994
|
}
|
|
905
995
|
|
|
906
996
|
/**
|
|
@@ -1011,6 +1101,20 @@ export class ConnectionManager {
|
|
|
1011
1101
|
)
|
|
1012
1102
|
}
|
|
1013
1103
|
|
|
1104
|
+
// Bitcoin HAS a live-read path (unlike TON), so read it live here too —
|
|
1105
|
+
// closes the same account-switch staleness the eip155/solana/tron reads
|
|
1106
|
+
// above address. (In bridge mode the live read is a marshal-time snapshot;
|
|
1107
|
+
// the onAccountsChanged relay still delivers live session updates.)
|
|
1108
|
+
const bitcoinAccount = await this.bitcoinService.getLiveAccount()
|
|
1109
|
+
if (bitcoinAccount) {
|
|
1110
|
+
addresses.push(
|
|
1111
|
+
...this.bitcoinService.buildAvailableAddresses(
|
|
1112
|
+
provider.supportedNetworkIds,
|
|
1113
|
+
bitcoinAccount
|
|
1114
|
+
)
|
|
1115
|
+
)
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1014
1118
|
return addresses
|
|
1015
1119
|
}
|
|
1016
1120
|
|
|
@@ -1221,6 +1325,40 @@ export class ConnectionManager {
|
|
|
1221
1325
|
}
|
|
1222
1326
|
}
|
|
1223
1327
|
|
|
1328
|
+
/**
|
|
1329
|
+
* Connect to a Bitcoin (bip122) wallet
|
|
1330
|
+
*/
|
|
1331
|
+
async connectBitcoin(
|
|
1332
|
+
network: Network,
|
|
1333
|
+
provider: ExtensionInjectedProvider | IntegratedBrowserInjectedProvider
|
|
1334
|
+
): Promise<ConnectorResult> {
|
|
1335
|
+
try {
|
|
1336
|
+
const connectResult = await this.bitcoinService.connect(network, provider)
|
|
1337
|
+
this.currentNetworkId = network.id
|
|
1338
|
+
|
|
1339
|
+
const availableAddresses: AvailableAddress[] = [
|
|
1340
|
+
...connectResult.availableAddresses
|
|
1341
|
+
]
|
|
1342
|
+
|
|
1343
|
+
await this.checkAndAddOtherNamespaceAddresses(
|
|
1344
|
+
'bip122',
|
|
1345
|
+
provider,
|
|
1346
|
+
availableAddresses
|
|
1347
|
+
)
|
|
1348
|
+
|
|
1349
|
+
return {
|
|
1350
|
+
networkId: network.id,
|
|
1351
|
+
address: connectResult.address,
|
|
1352
|
+
...(connectResult.publicKey
|
|
1353
|
+
? { publicKey: connectResult.publicKey }
|
|
1354
|
+
: {}),
|
|
1355
|
+
availableAddresses
|
|
1356
|
+
}
|
|
1357
|
+
} catch (error) {
|
|
1358
|
+
parseError(error)
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1224
1362
|
/**
|
|
1225
1363
|
* Switch network
|
|
1226
1364
|
*/
|
|
@@ -1278,6 +1416,27 @@ export class ConnectionManager {
|
|
|
1278
1416
|
}
|
|
1279
1417
|
}
|
|
1280
1418
|
|
|
1419
|
+
// bip122 is Bitcoin-mainnet-only. Assert here — not just inside
|
|
1420
|
+
// connectBitcoin — so a switch while ALREADY connected can't fall
|
|
1421
|
+
// through to stamp the connected Bitcoin address onto a non-Bitcoin
|
|
1422
|
+
// bip122 chain id (e.g. Dogecoin/Litecoin), the same fund-loss class
|
|
1423
|
+
// `assertBitcoinNetwork` guards on connect.
|
|
1424
|
+
if (targetNamespace === 'bip122') {
|
|
1425
|
+
assertBitcoinNetwork(network, 'ConnectionManager.switchNetwork')
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
// If switching to bip122 but not connected, connect to Bitcoin. Same-
|
|
1429
|
+
// namespace switching between bip122 chains is out of scope — only
|
|
1430
|
+
// Bitcoin mainnet is supported.
|
|
1431
|
+
if (targetNamespace === 'bip122' && !this.bitcoinService.isConnected()) {
|
|
1432
|
+
const connectResult = await this.connectBitcoin(network, provider)
|
|
1433
|
+
return {
|
|
1434
|
+
networkId: connectResult.networkId,
|
|
1435
|
+
address: connectResult.address,
|
|
1436
|
+
availableAddresses: connectResult.availableAddresses
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1281
1440
|
// Same-namespace switching
|
|
1282
1441
|
if (targetNamespace === 'eip155') {
|
|
1283
1442
|
const supportsAddingNetworks =
|
|
@@ -1289,10 +1448,11 @@ export class ConnectionManager {
|
|
|
1289
1448
|
} else if (
|
|
1290
1449
|
targetNamespace !== 'solana' &&
|
|
1291
1450
|
targetNamespace !== 'tron' &&
|
|
1292
|
-
targetNamespace !== 'tvm'
|
|
1451
|
+
targetNamespace !== 'tvm' &&
|
|
1452
|
+
targetNamespace !== 'bip122'
|
|
1293
1453
|
) {
|
|
1294
1454
|
throw new Error(
|
|
1295
|
-
`Cannot switch to namespace '${network.namespace}'. Only 'eip155', 'solana', 'tron', and '
|
|
1455
|
+
`Cannot switch to namespace '${network.namespace}'. Only 'eip155', 'solana', 'tron', 'tvm', and 'bip122' are currently supported.`
|
|
1296
1456
|
)
|
|
1297
1457
|
}
|
|
1298
1458
|
|
|
@@ -1335,6 +1495,8 @@ export class ConnectionManager {
|
|
|
1335
1495
|
return this.tronService.getAccount()
|
|
1336
1496
|
} else if (namespace === 'tvm') {
|
|
1337
1497
|
return this.tonService.getAccount()
|
|
1498
|
+
} else if (namespace === 'bip122') {
|
|
1499
|
+
return this.bitcoinService.getAccount()
|
|
1338
1500
|
}
|
|
1339
1501
|
}
|
|
1340
1502
|
return null
|
|
@@ -1357,6 +1519,8 @@ export class ConnectionManager {
|
|
|
1357
1519
|
return this.tronService.getLiveAccount()
|
|
1358
1520
|
} else if (namespace === 'tvm') {
|
|
1359
1521
|
return this.tonService.getAccount()
|
|
1522
|
+
} else if (namespace === 'bip122') {
|
|
1523
|
+
return this.bitcoinService.getLiveAccount()
|
|
1360
1524
|
}
|
|
1361
1525
|
}
|
|
1362
1526
|
return null
|
|
@@ -1372,6 +1536,7 @@ export class ConnectionManager {
|
|
|
1372
1536
|
// async — sdk.disconnect() sends a message to the wallet via the
|
|
1373
1537
|
// JS Bridge protocol, so the wallet knows to end the session.
|
|
1374
1538
|
await this.tonService.disconnect()
|
|
1539
|
+
await this.bitcoinService.disconnect()
|
|
1375
1540
|
this.currentNetworkId = null
|
|
1376
1541
|
}
|
|
1377
1542
|
}
|