@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
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
2
|
+
import type { ExtensionInjectedProvider, Network } from '@meshconnect/uwc-types'
|
|
3
|
+
import { BitcoinWalletService } from './bitcoin-wallet-service'
|
|
4
|
+
import { getBitcoinWallets } from '../../wallet-standard-discovery'
|
|
5
|
+
|
|
6
|
+
vi.mock('../../wallet-standard-discovery', () => ({
|
|
7
|
+
getBitcoinWallets: vi.fn()
|
|
8
|
+
}))
|
|
9
|
+
|
|
10
|
+
// The service resolves the OKX legacy provider via `resolveInjectedProperty`.
|
|
11
|
+
const { resolveInjectedProperty } = vi.hoisted(() => ({
|
|
12
|
+
resolveInjectedProperty: vi.fn()
|
|
13
|
+
}))
|
|
14
|
+
vi.mock('../../legacy-injected-solana-adapter', () => ({
|
|
15
|
+
resolveInjectedProperty: (id: string) => resolveInjectedProperty(id)
|
|
16
|
+
}))
|
|
17
|
+
|
|
18
|
+
const NETWORK: Network = {
|
|
19
|
+
id: 'bip122:000000000019d6689c085ae165831e93',
|
|
20
|
+
namespace: 'bip122'
|
|
21
|
+
} as unknown as Network
|
|
22
|
+
|
|
23
|
+
function makeProvider(uuid?: string): ExtensionInjectedProvider {
|
|
24
|
+
return {
|
|
25
|
+
supportedNetworkIds: [NETWORK.id],
|
|
26
|
+
namespaceMetaData: {
|
|
27
|
+
bip122: uuid
|
|
28
|
+
? { detectedWallet: { uuid, name: 'MetaMask', features: [] } }
|
|
29
|
+
: {}
|
|
30
|
+
}
|
|
31
|
+
} as unknown as ExtensionInjectedProvider
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function makeRawWallet(
|
|
35
|
+
name: string,
|
|
36
|
+
accounts: {
|
|
37
|
+
address: string
|
|
38
|
+
publicKey: Uint8Array
|
|
39
|
+
purpose?: 'payment' | 'ordinals'
|
|
40
|
+
}[]
|
|
41
|
+
) {
|
|
42
|
+
return {
|
|
43
|
+
name,
|
|
44
|
+
accounts,
|
|
45
|
+
chains: ['bitcoin:mainnet'],
|
|
46
|
+
features: {
|
|
47
|
+
'bitcoin:connect': {
|
|
48
|
+
connect: vi.fn().mockResolvedValue({ accounts })
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe('BitcoinWalletService', () => {
|
|
55
|
+
let service: BitcoinWalletService
|
|
56
|
+
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
vi.clearAllMocks()
|
|
59
|
+
service = new BitcoinWalletService()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
describe('findWallet (via connect) — fail-closed resolution', () => {
|
|
63
|
+
it('throws when the provider has no detected bip122 wallet uuid', async () => {
|
|
64
|
+
await expect(service.connect(NETWORK, makeProvider())).rejects.toThrow(
|
|
65
|
+
'No bitcoin wallet found for this connection'
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('throws when the uuid does not resolve to any discovered wallet (no arbitrary fallback)', async () => {
|
|
70
|
+
const rawWallet = makeRawWallet('MetaMask', [
|
|
71
|
+
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
72
|
+
])
|
|
73
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([
|
|
74
|
+
{
|
|
75
|
+
uuid: 'metamask-uuid',
|
|
76
|
+
name: 'MetaMask',
|
|
77
|
+
features: [],
|
|
78
|
+
wallet: rawWallet as any
|
|
79
|
+
}
|
|
80
|
+
])
|
|
81
|
+
await service.initializeDiscovery()
|
|
82
|
+
|
|
83
|
+
// Provider references a DIFFERENT uuid than the one discovered.
|
|
84
|
+
await expect(
|
|
85
|
+
service.connect(NETWORK, makeProvider('some-other-uuid'))
|
|
86
|
+
).rejects.toThrow('No bitcoin wallet found for this connection')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('connects to the specific wallet matched by uuid', async () => {
|
|
90
|
+
const rawWallet = makeRawWallet('MetaMask', [
|
|
91
|
+
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
92
|
+
])
|
|
93
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([
|
|
94
|
+
{
|
|
95
|
+
uuid: 'metamask-uuid',
|
|
96
|
+
name: 'MetaMask',
|
|
97
|
+
features: [],
|
|
98
|
+
wallet: rawWallet as any
|
|
99
|
+
}
|
|
100
|
+
])
|
|
101
|
+
await service.initializeDiscovery()
|
|
102
|
+
|
|
103
|
+
const result = await service.connect(
|
|
104
|
+
NETWORK,
|
|
105
|
+
makeProvider('metamask-uuid')
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
expect(result.address).toBe('bc1qtest')
|
|
109
|
+
expect(service.isConnected()).toBe(true)
|
|
110
|
+
expect(service.getAccount()).toBe('bc1qtest')
|
|
111
|
+
expect(service.getConnectedWalletUuid()).toBe('metamask-uuid')
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
describe('checkExistingConnection', () => {
|
|
116
|
+
it('is non-prompting: reads wallet.accounts without invoking bitcoin:connect', async () => {
|
|
117
|
+
const connectFeature = vi.fn()
|
|
118
|
+
const detectedWallet = {
|
|
119
|
+
uuid: 'metamask-uuid',
|
|
120
|
+
name: 'MetaMask',
|
|
121
|
+
features: [],
|
|
122
|
+
wallet: {
|
|
123
|
+
name: 'MetaMask',
|
|
124
|
+
accounts: [
|
|
125
|
+
{ address: 'bc1qexisting', publicKey: new Uint8Array([4, 5]) }
|
|
126
|
+
],
|
|
127
|
+
features: { 'bitcoin:connect': { connect: connectFeature } }
|
|
128
|
+
} as any
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const address = await service.checkExistingConnection(detectedWallet)
|
|
132
|
+
|
|
133
|
+
expect(address).toBe('bc1qexisting')
|
|
134
|
+
expect(connectFeature).not.toHaveBeenCalled()
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('returns null when the wallet exposes no accounts', async () => {
|
|
138
|
+
const detectedWallet = {
|
|
139
|
+
uuid: 'metamask-uuid',
|
|
140
|
+
name: 'MetaMask',
|
|
141
|
+
features: [],
|
|
142
|
+
wallet: { name: 'MetaMask', accounts: [], features: {} } as any
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
expect(await service.checkExistingConnection(detectedWallet)).toBeNull()
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
describe('setConnectionState', () => {
|
|
150
|
+
it('re-derives a full BitcoinAccount (hex publicKey) from the wallet accounts list', () => {
|
|
151
|
+
const detectedWallet = {
|
|
152
|
+
uuid: 'metamask-uuid',
|
|
153
|
+
name: 'MetaMask',
|
|
154
|
+
features: [],
|
|
155
|
+
wallet: {
|
|
156
|
+
name: 'MetaMask',
|
|
157
|
+
accounts: [
|
|
158
|
+
{ address: 'bc1qrestored', publicKey: new Uint8Array([0xab, 0xcd]) }
|
|
159
|
+
],
|
|
160
|
+
features: {}
|
|
161
|
+
} as any
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
service.setConnectionState(detectedWallet, 'bc1qrestored')
|
|
165
|
+
|
|
166
|
+
expect(service.isConnected()).toBe(true)
|
|
167
|
+
expect(service.getAccount()).toBe('bc1qrestored')
|
|
168
|
+
expect(service.getConnectedWalletUuid()).toBe('metamask-uuid')
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it('clears state when passed null', () => {
|
|
172
|
+
service.setConnectionState(null, null)
|
|
173
|
+
expect(service.isConnected()).toBe(false)
|
|
174
|
+
expect(service.getAccount()).toBeNull()
|
|
175
|
+
expect(service.getConnectedWalletUuid()).toBeNull()
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
describe('buildAvailableAddresses', () => {
|
|
180
|
+
it('includes ONLY Bitcoin mainnet, not other bip122 chains (Doge/Litecoin) or other namespaces', () => {
|
|
181
|
+
const addresses = service.buildAvailableAddresses(
|
|
182
|
+
[
|
|
183
|
+
'bip122:000000000019d6689c085ae165831e93', // Bitcoin mainnet
|
|
184
|
+
'bip122:1a91e3dace36e2be3bf030a65679fe82', // Dogecoin — must be excluded
|
|
185
|
+
'bip122:12a765e31ffd4059bada1e25190f6e98', // Litecoin — must be excluded
|
|
186
|
+
'eip155:1',
|
|
187
|
+
'solana:foo'
|
|
188
|
+
],
|
|
189
|
+
'bc1qaddr'
|
|
190
|
+
)
|
|
191
|
+
// The Bitcoin address must NOT be stamped onto Doge/Litecoin chain ids.
|
|
192
|
+
expect(addresses).toEqual([
|
|
193
|
+
{
|
|
194
|
+
address: 'bc1qaddr',
|
|
195
|
+
networkId: 'bip122:000000000019d6689c085ae165831e93'
|
|
196
|
+
}
|
|
197
|
+
])
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
describe('disconnect', () => {
|
|
202
|
+
it('clears all connection state', async () => {
|
|
203
|
+
const rawWallet = makeRawWallet('MetaMask', [
|
|
204
|
+
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
205
|
+
])
|
|
206
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([
|
|
207
|
+
{
|
|
208
|
+
uuid: 'metamask-uuid',
|
|
209
|
+
name: 'MetaMask',
|
|
210
|
+
features: [],
|
|
211
|
+
wallet: rawWallet as any
|
|
212
|
+
}
|
|
213
|
+
])
|
|
214
|
+
await service.initializeDiscovery()
|
|
215
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
216
|
+
|
|
217
|
+
await service.disconnect()
|
|
218
|
+
|
|
219
|
+
expect(service.isConnected()).toBe(false)
|
|
220
|
+
expect(service.getAccount()).toBeNull()
|
|
221
|
+
expect(service.getConnectedWalletUuid()).toBeNull()
|
|
222
|
+
})
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
describe('signMessage — signs the LIVE active account (signer/address consistency)', () => {
|
|
226
|
+
function makeSignableWallet(
|
|
227
|
+
accounts: { address: string; publicKey: Uint8Array }[]
|
|
228
|
+
) {
|
|
229
|
+
const signMessage = vi
|
|
230
|
+
.fn()
|
|
231
|
+
.mockResolvedValue([
|
|
232
|
+
{ signature: new Uint8Array([9, 9]), signedMessage: new Uint8Array() }
|
|
233
|
+
])
|
|
234
|
+
const connect = vi.fn().mockResolvedValue({ accounts })
|
|
235
|
+
const wallet = {
|
|
236
|
+
name: 'MetaMask',
|
|
237
|
+
accounts,
|
|
238
|
+
chains: ['bitcoin:mainnet'],
|
|
239
|
+
features: {
|
|
240
|
+
'bitcoin:connect': { connect },
|
|
241
|
+
'bitcoin:signMessage': { signMessage }
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return { wallet, signMessage, connect }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
it('signs with the live accounts[0] after an in-wallet switch, not the connect-time cache', async () => {
|
|
248
|
+
const acc1 = { address: 'bc1qACC1', publicKey: new Uint8Array([1]) }
|
|
249
|
+
const acc2 = { address: 'bc1qACC2', publicKey: new Uint8Array([2]) }
|
|
250
|
+
const { wallet, signMessage } = makeSignableWallet([acc1])
|
|
251
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([
|
|
252
|
+
{
|
|
253
|
+
uuid: 'metamask-uuid',
|
|
254
|
+
name: 'MetaMask',
|
|
255
|
+
features: [],
|
|
256
|
+
wallet: wallet as any
|
|
257
|
+
}
|
|
258
|
+
])
|
|
259
|
+
await service.initializeDiscovery()
|
|
260
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
261
|
+
|
|
262
|
+
// In-wallet switch: the wallet mutates its accounts in place.
|
|
263
|
+
;(wallet as any).accounts = [acc2]
|
|
264
|
+
|
|
265
|
+
await service.signMessage('verify me')
|
|
266
|
+
|
|
267
|
+
// The account handed to the wallet's signMessage feature is the LIVE one,
|
|
268
|
+
// and getLiveAccount (the returned `address`) agrees — no mismatch.
|
|
269
|
+
expect(signMessage).toHaveBeenCalledWith(
|
|
270
|
+
expect.objectContaining({
|
|
271
|
+
account: expect.objectContaining({ address: 'bc1qACC2' })
|
|
272
|
+
})
|
|
273
|
+
)
|
|
274
|
+
expect(await service.getLiveAccount()).toBe('bc1qACC2')
|
|
275
|
+
})
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
describe('setConnectionState — restore hardening (E + C)', () => {
|
|
279
|
+
it('seeds the adapter so the first sign after restore does not re-invoke bitcoin:connect', async () => {
|
|
280
|
+
const connect = vi.fn()
|
|
281
|
+
const signMessage = vi
|
|
282
|
+
.fn()
|
|
283
|
+
.mockResolvedValue([
|
|
284
|
+
{ signature: new Uint8Array([1]), signedMessage: new Uint8Array() }
|
|
285
|
+
])
|
|
286
|
+
const detectedWallet = {
|
|
287
|
+
uuid: 'metamask-uuid',
|
|
288
|
+
name: 'MetaMask',
|
|
289
|
+
features: [],
|
|
290
|
+
wallet: {
|
|
291
|
+
name: 'MetaMask',
|
|
292
|
+
accounts: [
|
|
293
|
+
{ address: 'bc1qrestored', publicKey: new Uint8Array([7]) }
|
|
294
|
+
],
|
|
295
|
+
features: {
|
|
296
|
+
'bitcoin:connect': { connect },
|
|
297
|
+
'bitcoin:signMessage': { signMessage }
|
|
298
|
+
}
|
|
299
|
+
} as any
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
service.setConnectionState(detectedWallet, 'bc1qrestored')
|
|
303
|
+
await service.signMessage('hi')
|
|
304
|
+
|
|
305
|
+
expect(connect).not.toHaveBeenCalled()
|
|
306
|
+
expect(signMessage).toHaveBeenCalled()
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
it('fails closed when the address is not among the wallet accounts', () => {
|
|
310
|
+
const detectedWallet = {
|
|
311
|
+
uuid: 'metamask-uuid',
|
|
312
|
+
name: 'MetaMask',
|
|
313
|
+
features: [],
|
|
314
|
+
wallet: {
|
|
315
|
+
name: 'MetaMask',
|
|
316
|
+
accounts: [{ address: 'bc1qKNOWN', publicKey: new Uint8Array([1]) }],
|
|
317
|
+
features: {}
|
|
318
|
+
} as any
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
service.setConnectionState(detectedWallet, 'bc1qUNKNOWN')
|
|
322
|
+
|
|
323
|
+
expect(service.isConnected()).toBe(false)
|
|
324
|
+
expect(service.getAccount()).toBeNull()
|
|
325
|
+
expect(service.getConnectedWalletUuid()).toBeNull()
|
|
326
|
+
})
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
describe('getLiveAccount / onAccountsChanged — live account tracking', () => {
|
|
330
|
+
afterEach(() => {
|
|
331
|
+
delete (window as any).UWCBridgeChildInitialized
|
|
332
|
+
delete (window as any).__uwcSubscribeBip122WalletEvent
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
function makeStandardEventsWallet(
|
|
336
|
+
name: string,
|
|
337
|
+
accounts: { address: string; publicKey: Uint8Array }[]
|
|
338
|
+
) {
|
|
339
|
+
let changeListener: ((props: unknown) => void) | undefined
|
|
340
|
+
const off = vi.fn()
|
|
341
|
+
const wallet = {
|
|
342
|
+
name,
|
|
343
|
+
accounts,
|
|
344
|
+
chains: ['bitcoin:mainnet'],
|
|
345
|
+
features: {
|
|
346
|
+
'bitcoin:connect': {
|
|
347
|
+
connect: vi.fn().mockResolvedValue({ accounts })
|
|
348
|
+
},
|
|
349
|
+
'standard:events': {
|
|
350
|
+
on: vi.fn((event: string, listener: (props: unknown) => void) => {
|
|
351
|
+
if (event === 'change') changeListener = listener
|
|
352
|
+
return off
|
|
353
|
+
})
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return { wallet, off, fire: (props: unknown) => changeListener?.(props) }
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
async function connectWith(wallet: unknown) {
|
|
361
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([
|
|
362
|
+
{
|
|
363
|
+
uuid: 'metamask-uuid',
|
|
364
|
+
name: 'MetaMask',
|
|
365
|
+
features: [],
|
|
366
|
+
wallet: wallet as any
|
|
367
|
+
}
|
|
368
|
+
])
|
|
369
|
+
await service.initializeDiscovery()
|
|
370
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
describe('getLiveAccount', () => {
|
|
374
|
+
it('returns null when there is no connected wallet', async () => {
|
|
375
|
+
expect(await service.getLiveAccount()).toBeNull()
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
it("reads the connected wallet's CURRENT accounts[0], not a connect-time cache", async () => {
|
|
379
|
+
const { wallet } = makeStandardEventsWallet('MetaMask', [
|
|
380
|
+
{ address: 'bc1qoriginal', publicKey: new Uint8Array([1]) }
|
|
381
|
+
])
|
|
382
|
+
await connectWith(wallet)
|
|
383
|
+
expect(await service.getLiveAccount()).toBe('bc1qoriginal')
|
|
384
|
+
|
|
385
|
+
// Simulate an in-wallet account switch — the wallet mutates its own
|
|
386
|
+
// `accounts` in place; getLiveAccount must reflect it immediately,
|
|
387
|
+
// with no re-connect and no cache update.
|
|
388
|
+
;(wallet as any).accounts = [
|
|
389
|
+
{ address: 'bc1qswitched', publicKey: new Uint8Array([2]) }
|
|
390
|
+
]
|
|
391
|
+
expect(await service.getLiveAccount()).toBe('bc1qswitched')
|
|
392
|
+
expect(service.getAccount()).toBe('bc1qoriginal')
|
|
393
|
+
})
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
describe('onAccountsChanged — local (unframed) path', () => {
|
|
397
|
+
it('subscribes to standard:events change and forwards the active address', async () => {
|
|
398
|
+
const { wallet, off, fire } = makeStandardEventsWallet('MetaMask', [
|
|
399
|
+
{ address: 'bc1qoriginal', publicKey: new Uint8Array([1]) }
|
|
400
|
+
])
|
|
401
|
+
await connectWith(wallet)
|
|
402
|
+
|
|
403
|
+
const listener = vi.fn()
|
|
404
|
+
const teardown = service.onAccountsChanged(listener)
|
|
405
|
+
expect(wallet.features['standard:events'].on).toHaveBeenCalledWith(
|
|
406
|
+
'change',
|
|
407
|
+
expect.any(Function)
|
|
408
|
+
)
|
|
409
|
+
;(wallet as any).accounts = [
|
|
410
|
+
{ address: 'bc1qswitched', publicKey: new Uint8Array([2]) }
|
|
411
|
+
]
|
|
412
|
+
fire({ accounts: wallet.accounts })
|
|
413
|
+
expect(listener).toHaveBeenCalledWith(['bc1qswitched'])
|
|
414
|
+
;(wallet as any).accounts = []
|
|
415
|
+
fire({ accounts: [] })
|
|
416
|
+
expect(listener).toHaveBeenCalledWith([])
|
|
417
|
+
|
|
418
|
+
// A pure chain/feature change (no `accounts` key) must be ignored.
|
|
419
|
+
fire({ chains: ['bitcoin:mainnet'] })
|
|
420
|
+
expect(listener).toHaveBeenCalledTimes(2)
|
|
421
|
+
|
|
422
|
+
teardown?.()
|
|
423
|
+
expect(off).toHaveBeenCalledTimes(1)
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
it('returns undefined when there is no connected wallet', () => {
|
|
427
|
+
expect(service.onAccountsChanged(vi.fn())).toBeUndefined()
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
it('returns undefined when the connected wallet exposes no standard:events', async () => {
|
|
431
|
+
const rawWallet = makeRawWallet('MetaMask', [
|
|
432
|
+
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
433
|
+
])
|
|
434
|
+
await connectWith(rawWallet)
|
|
435
|
+
expect(service.onAccountsChanged(vi.fn())).toBeUndefined()
|
|
436
|
+
})
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
describe('onAccountsChanged — bridged path', () => {
|
|
440
|
+
it('relays through window.__uwcSubscribeBip122WalletEvent using connectedWalletUuid', async () => {
|
|
441
|
+
const rawWallet = makeRawWallet('MetaMask', [
|
|
442
|
+
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
443
|
+
])
|
|
444
|
+
await connectWith(rawWallet)
|
|
445
|
+
;(window as any).UWCBridgeChildInitialized = true
|
|
446
|
+
const bridgeTeardown = vi.fn()
|
|
447
|
+
const bridgeSubscribe = vi.fn().mockReturnValue(bridgeTeardown)
|
|
448
|
+
;(window as any).__uwcSubscribeBip122WalletEvent = bridgeSubscribe
|
|
449
|
+
|
|
450
|
+
const listener = vi.fn()
|
|
451
|
+
const teardown = service.onAccountsChanged(listener)
|
|
452
|
+
|
|
453
|
+
expect(bridgeSubscribe).toHaveBeenCalledWith(
|
|
454
|
+
'metamask-uuid',
|
|
455
|
+
expect.any(Function)
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
const bridgeCallback = bridgeSubscribe.mock.calls[0][1]
|
|
459
|
+
bridgeCallback('bc1qswitched')
|
|
460
|
+
expect(listener).toHaveBeenCalledWith(['bc1qswitched'])
|
|
461
|
+
bridgeCallback(null)
|
|
462
|
+
expect(listener).toHaveBeenCalledWith([])
|
|
463
|
+
|
|
464
|
+
teardown?.()
|
|
465
|
+
expect(bridgeTeardown).toHaveBeenCalledTimes(1)
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
it('returns undefined when bridged but the global is missing (older bridge-parent)', async () => {
|
|
469
|
+
const rawWallet = makeRawWallet('MetaMask', [
|
|
470
|
+
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
471
|
+
])
|
|
472
|
+
await connectWith(rawWallet)
|
|
473
|
+
;(window as any).UWCBridgeChildInitialized = true
|
|
474
|
+
|
|
475
|
+
expect(service.onAccountsChanged(vi.fn())).toBeUndefined()
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
it('returns undefined when bridged but there is no connected wallet uuid', () => {
|
|
479
|
+
;(window as any).UWCBridgeChildInitialized = true
|
|
480
|
+
;(window as any).__uwcSubscribeBip122WalletEvent = vi.fn()
|
|
481
|
+
|
|
482
|
+
expect(service.onAccountsChanged(vi.fn())).toBeUndefined()
|
|
483
|
+
})
|
|
484
|
+
})
|
|
485
|
+
})
|
|
486
|
+
})
|
|
487
|
+
|
|
488
|
+
describe('BitcoinWalletService — OKX (legacy-injected) path', () => {
|
|
489
|
+
let service: BitcoinWalletService
|
|
490
|
+
|
|
491
|
+
function fakeOkxProvider(overrides: Record<string, unknown> = {}) {
|
|
492
|
+
return {
|
|
493
|
+
requestAccounts: vi.fn().mockResolvedValue(['bc1qokx']),
|
|
494
|
+
getAccounts: vi.fn().mockResolvedValue(['bc1qokx']),
|
|
495
|
+
getPublicKey: vi.fn().mockResolvedValue('02aabb'),
|
|
496
|
+
signMessage: vi.fn().mockResolvedValue('c2ln'),
|
|
497
|
+
signPsbt: vi.fn().mockResolvedValue('abcd'),
|
|
498
|
+
on: vi.fn(),
|
|
499
|
+
removeListener: vi.fn(),
|
|
500
|
+
...overrides
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function okxDetected() {
|
|
505
|
+
return {
|
|
506
|
+
uuid: 'okx-uuid',
|
|
507
|
+
name: 'OKX Wallet',
|
|
508
|
+
features: [
|
|
509
|
+
'bitcoin:connect',
|
|
510
|
+
'bitcoin:signMessage',
|
|
511
|
+
'bitcoin:signTransaction'
|
|
512
|
+
],
|
|
513
|
+
wallet: {
|
|
514
|
+
name: 'OKX Wallet',
|
|
515
|
+
accounts: [
|
|
516
|
+
{ address: 'bc1qokx', publicKey: new Uint8Array([2, 0xaa, 0xbb]) }
|
|
517
|
+
],
|
|
518
|
+
features: {}
|
|
519
|
+
},
|
|
520
|
+
legacyInjectedId: 'okxwallet.bitcoin'
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
async function connectOkx(provider: ReturnType<typeof fakeOkxProvider>) {
|
|
525
|
+
resolveInjectedProperty.mockReturnValue(provider)
|
|
526
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([okxDetected() as any])
|
|
527
|
+
await service.initializeDiscovery()
|
|
528
|
+
return service.connect(NETWORK, makeProvider('okx-uuid'))
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
beforeEach(() => {
|
|
532
|
+
vi.clearAllMocks()
|
|
533
|
+
service = new BitcoinWalletService()
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
afterEach(() => {
|
|
537
|
+
delete (window as any).UWCBridgeChildInitialized
|
|
538
|
+
})
|
|
539
|
+
|
|
540
|
+
it('connect builds the OKX adapter and returns the address via requestAccounts', async () => {
|
|
541
|
+
const provider = fakeOkxProvider()
|
|
542
|
+
const result = await connectOkx(provider)
|
|
543
|
+
|
|
544
|
+
expect(provider.requestAccounts).toHaveBeenCalledOnce()
|
|
545
|
+
expect(result.address).toBe('bc1qokx')
|
|
546
|
+
expect(service.isConnected()).toBe(true)
|
|
547
|
+
expect(service.getConnectedWalletUuid()).toBe('okx-uuid')
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
it('signMessage routes to the OKX provider (default ecdsa protocol)', async () => {
|
|
551
|
+
const provider = fakeOkxProvider()
|
|
552
|
+
await connectOkx(provider)
|
|
553
|
+
|
|
554
|
+
const sig = await service.signMessage('verify')
|
|
555
|
+
expect(provider.signMessage).toHaveBeenCalledWith('verify', 'ecdsa')
|
|
556
|
+
expect(sig).toEqual({ type: 'standard', signature: 'c2ln' })
|
|
557
|
+
})
|
|
558
|
+
|
|
559
|
+
it('sendTransaction (PSBT) routes to the OKX provider signPsbt (sign-only)', async () => {
|
|
560
|
+
const provider = fakeOkxProvider()
|
|
561
|
+
await connectOkx(provider)
|
|
562
|
+
|
|
563
|
+
await service.sendTransaction({ from: 'bc1qokx', psbt: 'AQID' })
|
|
564
|
+
expect(provider.signPsbt).toHaveBeenCalledWith(expect.any(String), {
|
|
565
|
+
autoFinalized: false
|
|
566
|
+
})
|
|
567
|
+
})
|
|
568
|
+
|
|
569
|
+
it('getLiveAccount reads the OKX provider live accounts', async () => {
|
|
570
|
+
const provider = fakeOkxProvider({
|
|
571
|
+
getAccounts: vi.fn().mockResolvedValue(['bc1qSWITCHED'])
|
|
572
|
+
})
|
|
573
|
+
await connectOkx(provider)
|
|
574
|
+
|
|
575
|
+
expect(await service.getLiveAccount()).toBe('bc1qSWITCHED')
|
|
576
|
+
})
|
|
577
|
+
|
|
578
|
+
it('onAccountsChanged wires the OKX provider accountsChanged event (direct, never the bridge)', async () => {
|
|
579
|
+
const provider = fakeOkxProvider()
|
|
580
|
+
await connectOkx(provider)
|
|
581
|
+
// Even if the bridge global is present, OKX (in-frame) subscribes directly.
|
|
582
|
+
;(window as any).UWCBridgeChildInitialized = true
|
|
583
|
+
|
|
584
|
+
const listener = vi.fn()
|
|
585
|
+
const teardown = service.onAccountsChanged(listener)
|
|
586
|
+
|
|
587
|
+
expect(provider.on).toHaveBeenCalledWith(
|
|
588
|
+
'accountsChanged',
|
|
589
|
+
expect.any(Function)
|
|
590
|
+
)
|
|
591
|
+
const handler = (provider.on as any).mock.calls[0][1]
|
|
592
|
+
handler(['bc1qNEW'])
|
|
593
|
+
expect(listener).toHaveBeenCalledWith(['bc1qNEW'])
|
|
594
|
+
|
|
595
|
+
teardown?.()
|
|
596
|
+
expect(provider.removeListener).toHaveBeenCalledWith(
|
|
597
|
+
'accountsChanged',
|
|
598
|
+
handler
|
|
599
|
+
)
|
|
600
|
+
})
|
|
601
|
+
|
|
602
|
+
it('disconnect clears the OKX provider so later reads fall back to Wallet Standard', async () => {
|
|
603
|
+
const provider = fakeOkxProvider()
|
|
604
|
+
await connectOkx(provider)
|
|
605
|
+
await service.disconnect()
|
|
606
|
+
|
|
607
|
+
expect(service.isConnected()).toBe(false)
|
|
608
|
+
// With no OKX provider and no connected wallet, live read is null.
|
|
609
|
+
expect(await service.getLiveAccount()).toBeNull()
|
|
610
|
+
})
|
|
611
|
+
|
|
612
|
+
it('does NOT leave connectedOkxProvider set when the connect prompt is rejected', async () => {
|
|
613
|
+
// requestAccounts rejects (user declined) — connect must throw AND leave the
|
|
614
|
+
// service fully unconnected, not with a dangling OKX provider that
|
|
615
|
+
// getLiveAccount / onAccountsChanged would then treat as a live session.
|
|
616
|
+
const provider = fakeOkxProvider({
|
|
617
|
+
requestAccounts: vi.fn().mockRejectedValue(new Error('user rejected'))
|
|
618
|
+
})
|
|
619
|
+
resolveInjectedProperty.mockReturnValue(provider)
|
|
620
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([okxDetected() as any])
|
|
621
|
+
await service.initializeDiscovery()
|
|
622
|
+
|
|
623
|
+
await expect(
|
|
624
|
+
service.connect(NETWORK, makeProvider('okx-uuid'))
|
|
625
|
+
).rejects.toThrow()
|
|
626
|
+
|
|
627
|
+
expect(service.isConnected()).toBe(false)
|
|
628
|
+
// The tell: getLiveAccount must NOT hit the OKX provider (it would report an
|
|
629
|
+
// account and falsely publish bip122); onAccountsChanged must not subscribe.
|
|
630
|
+
expect(await service.getLiveAccount()).toBeNull()
|
|
631
|
+
expect(service.onAccountsChanged(vi.fn())).toBeUndefined()
|
|
632
|
+
expect(provider.on).not.toHaveBeenCalled()
|
|
633
|
+
})
|
|
634
|
+
|
|
635
|
+
it('refreshes getAccount() to the OKX live account on signMessage (post-switch)', async () => {
|
|
636
|
+
const provider = fakeOkxProvider()
|
|
637
|
+
await connectOkx(provider)
|
|
638
|
+
// In-wallet switch: the provider now reports a different active address.
|
|
639
|
+
provider.getAccounts.mockResolvedValue(['bc1qSWITCHED'])
|
|
640
|
+
|
|
641
|
+
await service.signMessage('verify')
|
|
642
|
+
|
|
643
|
+
// getAccount() (connect-time cache) was refreshed via the provider.
|
|
644
|
+
expect(service.getAccount()).toBe('bc1qSWITCHED')
|
|
645
|
+
})
|
|
646
|
+
})
|