@meshconnect/uwc-injected-connector 0.23.3 → 0.24.1
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/injected-connector.d.ts +11 -2
- package/dist/injected-connector.d.ts.map +1 -1
- package/dist/injected-connector.js +22 -7
- package/dist/injected-connector.js.map +1 -1
- package/dist/services/bitcoin/bitcoin-wallet-service.d.ts +96 -3
- package/dist/services/bitcoin/bitcoin-wallet-service.d.ts.map +1 -1
- package/dist/services/bitcoin/bitcoin-wallet-service.js +271 -17
- package/dist/services/bitcoin/bitcoin-wallet-service.js.map +1 -1
- package/dist/services/connection-manager.d.ts +2 -2
- package/dist/services/connection-manager.d.ts.map +1 -1
- package/dist/services/connection-manager.js +41 -8
- package/dist/services/connection-manager.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +4 -0
- package/src/injected-connector.test.ts +10 -0
- package/src/injected-connector.ts +52 -7
- package/src/services/bitcoin/bitcoin-wallet-service.test.ts +671 -41
- package/src/services/bitcoin/bitcoin-wallet-service.ts +329 -24
- package/src/services/connection-manager.test.ts +38 -0
- package/src/services/connection-manager.ts +60 -9
|
@@ -51,6 +51,18 @@ function makeRawWallet(
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Publish `wallet` as the single discovered bip122 wallet. Every test that
|
|
56
|
+
* exercises `connect()` needs this same registration, so it lives here rather
|
|
57
|
+
* than being restated per test.
|
|
58
|
+
*/
|
|
59
|
+
function discoverWallet(wallet: unknown, uuid = 'metamask-uuid') {
|
|
60
|
+
vi.mocked(getBitcoinWallets).mockResolvedValue([
|
|
61
|
+
{ uuid, name: 'MetaMask', features: [], wallet: wallet as any }
|
|
62
|
+
])
|
|
63
|
+
return wallet as any
|
|
64
|
+
}
|
|
65
|
+
|
|
54
66
|
describe('BitcoinWalletService', () => {
|
|
55
67
|
let service: BitcoinWalletService
|
|
56
68
|
|
|
@@ -70,14 +82,7 @@ describe('BitcoinWalletService', () => {
|
|
|
70
82
|
const rawWallet = makeRawWallet('MetaMask', [
|
|
71
83
|
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
72
84
|
])
|
|
73
|
-
|
|
74
|
-
{
|
|
75
|
-
uuid: 'metamask-uuid',
|
|
76
|
-
name: 'MetaMask',
|
|
77
|
-
features: [],
|
|
78
|
-
wallet: rawWallet as any
|
|
79
|
-
}
|
|
80
|
-
])
|
|
85
|
+
discoverWallet(rawWallet)
|
|
81
86
|
await service.initializeDiscovery()
|
|
82
87
|
|
|
83
88
|
// Provider references a DIFFERENT uuid than the one discovered.
|
|
@@ -90,14 +95,7 @@ describe('BitcoinWalletService', () => {
|
|
|
90
95
|
const rawWallet = makeRawWallet('MetaMask', [
|
|
91
96
|
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
92
97
|
])
|
|
93
|
-
|
|
94
|
-
{
|
|
95
|
-
uuid: 'metamask-uuid',
|
|
96
|
-
name: 'MetaMask',
|
|
97
|
-
features: [],
|
|
98
|
-
wallet: rawWallet as any
|
|
99
|
-
}
|
|
100
|
-
])
|
|
98
|
+
discoverWallet(rawWallet)
|
|
101
99
|
await service.initializeDiscovery()
|
|
102
100
|
|
|
103
101
|
const result = await service.connect(
|
|
@@ -146,6 +144,613 @@ describe('BitcoinWalletService', () => {
|
|
|
146
144
|
})
|
|
147
145
|
})
|
|
148
146
|
|
|
147
|
+
// `wallet.accounts` is the PER-ORIGIN authorized set, not the wallet's live
|
|
148
|
+
// UI selection — MetaMask keeps serving the account this origin first
|
|
149
|
+
// authorized. Reporting it as an existing connection regardless of the
|
|
150
|
+
// caller's pin stranded the session on the wrong account with no way back,
|
|
151
|
+
// because the prompting connect() that could re-authorize was skipped.
|
|
152
|
+
describe('checkExistingConnection — requiredAddresses (pinned allow-list)', () => {
|
|
153
|
+
const detectedWith = (addresses: string[]) => ({
|
|
154
|
+
uuid: 'metamask-uuid',
|
|
155
|
+
name: 'MetaMask',
|
|
156
|
+
features: [],
|
|
157
|
+
wallet: {
|
|
158
|
+
name: 'MetaMask',
|
|
159
|
+
accounts: addresses.map(address => ({
|
|
160
|
+
address,
|
|
161
|
+
publicKey: new Uint8Array([1])
|
|
162
|
+
})),
|
|
163
|
+
features: { 'bitcoin:connect': { connect: vi.fn() } }
|
|
164
|
+
} as any
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
// One rule under test: which authorized account (if any) satisfies the pin.
|
|
168
|
+
// Table-driven because the bodies are identical apart from the data — the
|
|
169
|
+
// interesting content is the casing rules, not the plumbing.
|
|
170
|
+
const LEGACY = '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2'
|
|
171
|
+
it.each([
|
|
172
|
+
{
|
|
173
|
+
name: 'returns the PINNED account, not accounts[0]',
|
|
174
|
+
authorized: ['bc1qfirst', 'bc1qwanted'],
|
|
175
|
+
pin: ['bc1qwanted'],
|
|
176
|
+
expected: 'bc1qwanted'
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
name: 'returns null when nothing authorized matches, so the caller re-prompts',
|
|
180
|
+
authorized: ['bc1qstale'],
|
|
181
|
+
pin: ['bc1qwanted'],
|
|
182
|
+
expected: null
|
|
183
|
+
},
|
|
184
|
+
// BIP-173 allows bech32 all-lower OR all-upper, and forbids mixed case.
|
|
185
|
+
{
|
|
186
|
+
name: 'folds an all-uppercase bech32 address',
|
|
187
|
+
authorized: ['BC1QWANTED'],
|
|
188
|
+
pin: ['bc1qwanted'],
|
|
189
|
+
expected: 'BC1QWANTED'
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
name: 'folds an all-uppercase bech32 pin',
|
|
193
|
+
authorized: ['bc1qwanted'],
|
|
194
|
+
pin: ['BC1QWANTED'],
|
|
195
|
+
expected: 'bc1qwanted'
|
|
196
|
+
},
|
|
197
|
+
// Mixed case is not a valid bech32 address at all, so folding it would let
|
|
198
|
+
// an INVALID pin match a valid wallet address.
|
|
199
|
+
{
|
|
200
|
+
name: 'does NOT fold a mixed-case (invalid) bech32 pin',
|
|
201
|
+
authorized: ['bc1qwanted'],
|
|
202
|
+
pin: ['bC1qWaNtEd'],
|
|
203
|
+
expected: null
|
|
204
|
+
},
|
|
205
|
+
// Base58Check is case-SENSITIVE: its characters carry different values per
|
|
206
|
+
// case, so a mis-cased pin decodes to a different payload entirely.
|
|
207
|
+
{
|
|
208
|
+
name: 'does NOT fold case for legacy base58',
|
|
209
|
+
authorized: [LEGACY],
|
|
210
|
+
pin: [LEGACY.toLowerCase()],
|
|
211
|
+
expected: null
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
name: 'matches an exactly-cased base58 pin, trimmed',
|
|
215
|
+
authorized: [LEGACY],
|
|
216
|
+
pin: [` ${LEGACY} `],
|
|
217
|
+
expected: LEGACY
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
name: 'falls back to accounts[0] with no pin (unchanged behaviour)',
|
|
221
|
+
authorized: ['bc1qfirst', 'bc1qsecond'],
|
|
222
|
+
pin: undefined,
|
|
223
|
+
expected: 'bc1qfirst'
|
|
224
|
+
},
|
|
225
|
+
// An all-blank list is no real constraint — treating it as one would fail
|
|
226
|
+
// every account instead of matching none.
|
|
227
|
+
{
|
|
228
|
+
name: 'treats an all-blank pin list as no constraint',
|
|
229
|
+
authorized: ['bc1qfirst'],
|
|
230
|
+
pin: ['', ' '],
|
|
231
|
+
expected: 'bc1qfirst'
|
|
232
|
+
}
|
|
233
|
+
])('$name', async ({ authorized, pin, expected }) => {
|
|
234
|
+
const address = await service.checkExistingConnection(
|
|
235
|
+
detectedWith(authorized),
|
|
236
|
+
pin
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
expect(address).toBe(expected)
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('stays non-prompting even when the pin does not match', async () => {
|
|
243
|
+
const detected = detectedWith(['bc1qstale'])
|
|
244
|
+
|
|
245
|
+
await service.checkExistingConnection(detected, ['bc1qwanted'])
|
|
246
|
+
|
|
247
|
+
expect(
|
|
248
|
+
detected.wallet.features['bitcoin:connect'].connect
|
|
249
|
+
).not.toHaveBeenCalled()
|
|
250
|
+
})
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
describe('connect — requiredAddresses selects the active account', () => {
|
|
254
|
+
const discover = (addresses: string[]) => {
|
|
255
|
+
const rawWallet = makeRawWallet(
|
|
256
|
+
'MetaMask',
|
|
257
|
+
addresses.map(address => ({
|
|
258
|
+
address,
|
|
259
|
+
publicKey: new Uint8Array([1, 2, 3])
|
|
260
|
+
}))
|
|
261
|
+
)
|
|
262
|
+
discoverWallet(rawWallet)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
it('makes the pinned account active when the wallet authorizes several', async () => {
|
|
266
|
+
discover(['bc1qfirst', 'bc1qwanted'])
|
|
267
|
+
await service.initializeDiscovery()
|
|
268
|
+
|
|
269
|
+
const result = await service.connect(
|
|
270
|
+
NETWORK,
|
|
271
|
+
makeProvider('metamask-uuid'),
|
|
272
|
+
['bc1qwanted']
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
expect(result.address).toBe('bc1qwanted')
|
|
276
|
+
// Every authorized account still surfaces — only the ACTIVE one changed.
|
|
277
|
+
expect(result.availableAddresses.map(a => a.address)).toEqual([
|
|
278
|
+
'bc1qfirst',
|
|
279
|
+
'bc1qwanted'
|
|
280
|
+
])
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it('falls back to accounts[0] when the pin matches nothing the wallet returned', async () => {
|
|
284
|
+
discover(['bc1qfirst', 'bc1qsecond'])
|
|
285
|
+
await service.initializeDiscovery()
|
|
286
|
+
|
|
287
|
+
const result = await service.connect(
|
|
288
|
+
NETWORK,
|
|
289
|
+
makeProvider('metamask-uuid'),
|
|
290
|
+
['bc1qabsent']
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
expect(result.address).toBe('bc1qfirst')
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
// A real wallet's `accounts` reflects the authorization LIVE — MetaMask
|
|
297
|
+
// sets its account before `bitcoin:connect` resolves. `grantAware` models
|
|
298
|
+
// that: connecting populates `accounts`, revoking empties it. A mock with a
|
|
299
|
+
// static `accounts` cannot catch an order-of-reads bug in the caller.
|
|
300
|
+
const grantAware = (opts: {
|
|
301
|
+
/** Accounts this origin already holds a grant for, before any connect. */
|
|
302
|
+
granted?: string[]
|
|
303
|
+
/**
|
|
304
|
+
* What the FIRST connect yields. Defaults to `granted` (the wallet
|
|
305
|
+
* replaying its grant). With no grant, that first connect IS the picker,
|
|
306
|
+
* so pass what the user chooses there.
|
|
307
|
+
*/
|
|
308
|
+
firstConnect?: string[]
|
|
309
|
+
/** What a connect yields once the grant has been revoked. */
|
|
310
|
+
afterReconnect: string[]
|
|
311
|
+
withDisconnect?: boolean
|
|
312
|
+
}) => {
|
|
313
|
+
const acct = (address: string) => ({
|
|
314
|
+
address,
|
|
315
|
+
publicKey: new Uint8Array([1])
|
|
316
|
+
})
|
|
317
|
+
// The grant is replayed until it is revoked — that is the whole mechanic
|
|
318
|
+
// this feature exists to defeat. Connecting while still granted returns
|
|
319
|
+
// the SAME accounts no matter how many times it is called; only a revoke
|
|
320
|
+
// lets the next connect return something else.
|
|
321
|
+
let revoked = false
|
|
322
|
+
const wallet: any = {
|
|
323
|
+
name: 'MetaMask',
|
|
324
|
+
accounts: (opts.granted ?? []).map(acct),
|
|
325
|
+
chains: ['bitcoin:mainnet'],
|
|
326
|
+
features: {
|
|
327
|
+
'bitcoin:connect': {
|
|
328
|
+
connect: vi.fn(async () => {
|
|
329
|
+
wallet.accounts = (
|
|
330
|
+
revoked
|
|
331
|
+
? opts.afterReconnect
|
|
332
|
+
: (opts.firstConnect ?? opts.granted ?? [])
|
|
333
|
+
).map(acct)
|
|
334
|
+
return { accounts: wallet.accounts }
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (opts.withDisconnect !== false) {
|
|
340
|
+
wallet.features['bitcoin:disconnect'] = {
|
|
341
|
+
disconnect: vi.fn(async () => {
|
|
342
|
+
revoked = true
|
|
343
|
+
wallet.accounts = []
|
|
344
|
+
})
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return discoverWallet(wallet)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// `bitcoin:connect` is authorization, not account selection: once an origin
|
|
351
|
+
// holds a grant the wallet replays it and returns the SAME account however
|
|
352
|
+
// many times it is called. Only revoking first turns the next connect into
|
|
353
|
+
// a real prompt with an account picker.
|
|
354
|
+
it('revokes via bitcoin:disconnect when the pin is unsatisfiable, then re-prompts', async () => {
|
|
355
|
+
const wallet = grantAware({
|
|
356
|
+
granted: ['bc1qstale'],
|
|
357
|
+
afterReconnect: ['bc1qwanted']
|
|
358
|
+
})
|
|
359
|
+
await service.initializeDiscovery()
|
|
360
|
+
|
|
361
|
+
const result = await service.connect(
|
|
362
|
+
NETWORK,
|
|
363
|
+
makeProvider('metamask-uuid'),
|
|
364
|
+
['bc1qwanted']
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
expect(
|
|
368
|
+
wallet.features['bitcoin:disconnect'].disconnect
|
|
369
|
+
).toHaveBeenCalled()
|
|
370
|
+
expect(result.address).toBe('bc1qwanted')
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
// The guard that must actually gate. With no prior grant the first connect
|
|
374
|
+
// already showed the picker, so re-prompting asks the user to choose again
|
|
375
|
+
// the instant after they chose. This fails if `previouslyGranted` is read
|
|
376
|
+
// AFTER getAccounts(), because connecting populates `accounts`.
|
|
377
|
+
it('does NOT revoke when the origin held no grant before connecting', async () => {
|
|
378
|
+
const wallet = grantAware({
|
|
379
|
+
granted: [], // no prior grant → the first connect is the picker
|
|
380
|
+
firstConnect: ['bc1qpicked'], // what the user chooses there
|
|
381
|
+
afterReconnect: ['bc1qpicked']
|
|
382
|
+
})
|
|
383
|
+
await service.initializeDiscovery()
|
|
384
|
+
|
|
385
|
+
const result = await service.connect(
|
|
386
|
+
NETWORK,
|
|
387
|
+
makeProvider('metamask-uuid'),
|
|
388
|
+
['bc1qwanted'] // pin the user did NOT pick
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
expect(
|
|
392
|
+
wallet.features['bitcoin:disconnect'].disconnect
|
|
393
|
+
).not.toHaveBeenCalled()
|
|
394
|
+
expect(result.address).toBe('bc1qpicked')
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
it('does NOT revoke when an authorized account already satisfies the pin', async () => {
|
|
398
|
+
const wallet = grantAware({
|
|
399
|
+
granted: ['bc1qwanted'],
|
|
400
|
+
afterReconnect: ['bc1qwanted']
|
|
401
|
+
})
|
|
402
|
+
await service.initializeDiscovery()
|
|
403
|
+
|
|
404
|
+
const result = await service.connect(
|
|
405
|
+
NETWORK,
|
|
406
|
+
makeProvider('metamask-uuid'),
|
|
407
|
+
['bc1qwanted']
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
expect(
|
|
411
|
+
wallet.features['bitcoin:disconnect'].disconnect
|
|
412
|
+
).not.toHaveBeenCalled()
|
|
413
|
+
expect(result.address).toBe('bc1qwanted')
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
// The revoke is destructive: once the grant is dropped the pre-revoke
|
|
417
|
+
// accounts are no longer authorized, so falling back to them would report a
|
|
418
|
+
// live session the wallet just revoked — and report success for a connect
|
|
419
|
+
// the user explicitly declined.
|
|
420
|
+
it('fails the connect when the re-prompt is declined after the grant was revoked', async () => {
|
|
421
|
+
const wallet = grantAware({
|
|
422
|
+
granted: ['bc1qstale'],
|
|
423
|
+
afterReconnect: ['bc1qstale']
|
|
424
|
+
})
|
|
425
|
+
let revoked = false
|
|
426
|
+
wallet.features['bitcoin:disconnect'].disconnect = vi.fn(async () => {
|
|
427
|
+
revoked = true
|
|
428
|
+
wallet.accounts = []
|
|
429
|
+
})
|
|
430
|
+
wallet.features['bitcoin:connect'].connect = vi.fn(async () => {
|
|
431
|
+
if (revoked) throw new Error('User rejected the request')
|
|
432
|
+
wallet.accounts = [
|
|
433
|
+
{ address: 'bc1qstale', publicKey: new Uint8Array([1]) }
|
|
434
|
+
]
|
|
435
|
+
return { accounts: wallet.accounts }
|
|
436
|
+
})
|
|
437
|
+
await service.initializeDiscovery()
|
|
438
|
+
|
|
439
|
+
await expect(
|
|
440
|
+
service.connect(NETWORK, makeProvider('metamask-uuid'), ['bc1qwanted'])
|
|
441
|
+
).rejects.toThrow('User rejected the request')
|
|
442
|
+
expect(service.isConnected()).toBe(false)
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
// The reconnect case this feature exists for: a session is ALREADY live
|
|
446
|
+
// when the pinned connect runs. Session state commits at the END of a
|
|
447
|
+
// successful connect, so the previous account is still held here — and the
|
|
448
|
+
// revoke just invalidated it at the wallet. Without an explicit teardown a
|
|
449
|
+
// declined re-prompt throws while `isConnected()` still reports the revoked
|
|
450
|
+
// account, and the next `signMessage` would address it.
|
|
451
|
+
it('invalidates an already-live session when the re-prompt is declined after a revoke', async () => {
|
|
452
|
+
const wallet = grantAware({
|
|
453
|
+
granted: ['bc1qstale'],
|
|
454
|
+
afterReconnect: ['bc1qstale']
|
|
455
|
+
})
|
|
456
|
+
await service.initializeDiscovery()
|
|
457
|
+
|
|
458
|
+
// Establish a real session first (no pin → adopts the granted account).
|
|
459
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
460
|
+
expect(service.isConnected()).toBe(true)
|
|
461
|
+
expect(service.getAccount()).toBe('bc1qstale')
|
|
462
|
+
|
|
463
|
+
// Now decline the re-prompt that follows the revoke.
|
|
464
|
+
let revoked = false
|
|
465
|
+
wallet.features['bitcoin:disconnect'].disconnect = vi.fn(async () => {
|
|
466
|
+
revoked = true
|
|
467
|
+
wallet.accounts = []
|
|
468
|
+
})
|
|
469
|
+
wallet.features['bitcoin:connect'].connect = vi.fn(async () => {
|
|
470
|
+
if (revoked) throw new Error('User rejected the request')
|
|
471
|
+
wallet.accounts = [
|
|
472
|
+
{ address: 'bc1qstale', publicKey: new Uint8Array([1]) }
|
|
473
|
+
]
|
|
474
|
+
return { accounts: wallet.accounts }
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
await expect(
|
|
478
|
+
service.connect(NETWORK, makeProvider('metamask-uuid'), ['bc1qwanted'])
|
|
479
|
+
).rejects.toThrow('User rejected the request')
|
|
480
|
+
|
|
481
|
+
// The revoked account must not survive as a live session.
|
|
482
|
+
expect(service.isConnected()).toBe(false)
|
|
483
|
+
expect(service.getAccount()).toBeNull()
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
// FRAMED: `wallet.accounts` is a one-time snapshot marshaled by
|
|
487
|
+
// `BridgeChild` — proxied `bitcoin:connect` calls never update it. A bridge
|
|
488
|
+
// that started with no grant reports empty forever, so keying the guard on
|
|
489
|
+
// that alone would skip the revoke and hand back the stale account. The
|
|
490
|
+
// connector's own `this.account` covers the gap.
|
|
491
|
+
it('revokes when the bridged accounts snapshot is stale but a session is live', async () => {
|
|
492
|
+
const stale = { address: 'bc1qstale', publicKey: new Uint8Array([1]) }
|
|
493
|
+
const wanted = { address: 'bc1qwanted', publicKey: new Uint8Array([2]) }
|
|
494
|
+
let revoked = false
|
|
495
|
+
// Note `accounts` never changes — exactly how the bridged snapshot behaves.
|
|
496
|
+
const rawWallet: any = {
|
|
497
|
+
name: 'MetaMask',
|
|
498
|
+
accounts: [],
|
|
499
|
+
chains: ['bitcoin:mainnet'],
|
|
500
|
+
features: {
|
|
501
|
+
'bitcoin:connect': {
|
|
502
|
+
connect: vi.fn(async () => ({
|
|
503
|
+
accounts: revoked ? [wanted] : [stale]
|
|
504
|
+
}))
|
|
505
|
+
},
|
|
506
|
+
'bitcoin:disconnect': {
|
|
507
|
+
disconnect: vi.fn(async () => {
|
|
508
|
+
revoked = true
|
|
509
|
+
})
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
discoverWallet(rawWallet)
|
|
514
|
+
await service.initializeDiscovery()
|
|
515
|
+
|
|
516
|
+
// First connect establishes a live session; the snapshot stays empty.
|
|
517
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
518
|
+
expect(service.getAccount()).toBe('bc1qstale')
|
|
519
|
+
expect(rawWallet.accounts).toEqual([])
|
|
520
|
+
|
|
521
|
+
// Now pin a different account. The snapshot still says "no grant", so
|
|
522
|
+
// only `this.account` can reveal that a revoke is needed.
|
|
523
|
+
const result = await service.connect(
|
|
524
|
+
NETWORK,
|
|
525
|
+
makeProvider('metamask-uuid'),
|
|
526
|
+
['bc1qwanted']
|
|
527
|
+
)
|
|
528
|
+
|
|
529
|
+
expect(
|
|
530
|
+
rawWallet.features['bitcoin:disconnect'].disconnect
|
|
531
|
+
).toHaveBeenCalled()
|
|
532
|
+
expect(result.address).toBe('bc1qwanted')
|
|
533
|
+
})
|
|
534
|
+
|
|
535
|
+
// No revoke available (a wallet without `bitcoin:disconnect`, or a bridge
|
|
536
|
+
// that doesn't marshal it): the grant is INTACT, so re-connecting would
|
|
537
|
+
// return the same accounts. Keep the connection instead of burning a prompt
|
|
538
|
+
// — and never fail a connect that succeeded.
|
|
539
|
+
it('keeps the connection when no revoke is available', async () => {
|
|
540
|
+
grantAware({
|
|
541
|
+
granted: ['bc1qstale'],
|
|
542
|
+
afterReconnect: ['bc1qstale'],
|
|
543
|
+
withDisconnect: false
|
|
544
|
+
})
|
|
545
|
+
await service.initializeDiscovery()
|
|
546
|
+
|
|
547
|
+
const result = await service.connect(
|
|
548
|
+
NETWORK,
|
|
549
|
+
makeProvider('metamask-uuid'),
|
|
550
|
+
['bc1qwanted']
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
expect(result.address).toBe('bc1qstale')
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
it('reports the re-authorization outcome for telemetry', async () => {
|
|
557
|
+
const reported: unknown[] = []
|
|
558
|
+
service = new BitcoinWalletService(undefined, info => reported.push(info))
|
|
559
|
+
grantAware({
|
|
560
|
+
granted: ['bc1qstale'],
|
|
561
|
+
afterReconnect: ['bc1qwanted']
|
|
562
|
+
})
|
|
563
|
+
await service.initializeDiscovery()
|
|
564
|
+
|
|
565
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'), [
|
|
566
|
+
'bc1qwanted'
|
|
567
|
+
])
|
|
568
|
+
|
|
569
|
+
expect(reported).toEqual([
|
|
570
|
+
{ walletName: 'MetaMask', revoked: true, outcome: 'revoked' }
|
|
571
|
+
])
|
|
572
|
+
})
|
|
573
|
+
|
|
574
|
+
// Revoking makes a wallet announce "no accounts". Core reads `[]` as a
|
|
575
|
+
// namespace disconnect and calls `finishDisconnect()` — which, during a
|
|
576
|
+
// pinned reconnect, lands in the middle of the connect that ASKED for the
|
|
577
|
+
// revoke. `eventedGrantAware` emits that event the way MetaMask does:
|
|
578
|
+
// synchronously, from inside `bitcoin:disconnect`.
|
|
579
|
+
const eventedGrantAware = (opts: {
|
|
580
|
+
granted: string[]
|
|
581
|
+
afterReconnect: string[]
|
|
582
|
+
}) => {
|
|
583
|
+
const acct = (address: string) => ({
|
|
584
|
+
address,
|
|
585
|
+
publicKey: new Uint8Array([1])
|
|
586
|
+
})
|
|
587
|
+
const changeHandlers: ((properties: { accounts?: unknown }) => void)[] =
|
|
588
|
+
[]
|
|
589
|
+
let revoked = false
|
|
590
|
+
const wallet: any = {
|
|
591
|
+
name: 'MetaMask',
|
|
592
|
+
accounts: opts.granted.map(acct),
|
|
593
|
+
chains: ['bitcoin:mainnet'],
|
|
594
|
+
features: {
|
|
595
|
+
'bitcoin:connect': {
|
|
596
|
+
connect: vi.fn(async () => {
|
|
597
|
+
wallet.accounts = (
|
|
598
|
+
revoked ? opts.afterReconnect : opts.granted
|
|
599
|
+
).map(acct)
|
|
600
|
+
return { accounts: wallet.accounts }
|
|
601
|
+
})
|
|
602
|
+
},
|
|
603
|
+
'bitcoin:disconnect': {
|
|
604
|
+
disconnect: vi.fn(async () => {
|
|
605
|
+
revoked = true
|
|
606
|
+
wallet.accounts = []
|
|
607
|
+
// The event the guard has to absorb.
|
|
608
|
+
for (const handler of changeHandlers) handler({ accounts: [] })
|
|
609
|
+
})
|
|
610
|
+
},
|
|
611
|
+
'bitcoin:events': {
|
|
612
|
+
on: vi.fn((event: string, handler: any) => {
|
|
613
|
+
if (event === 'change') changeHandlers.push(handler)
|
|
614
|
+
return () => {}
|
|
615
|
+
})
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return { wallet: discoverWallet(wallet), changeHandlers }
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
it('does not report a disconnect for the empty accounts a revoke emits', async () => {
|
|
623
|
+
const { wallet } = eventedGrantAware({
|
|
624
|
+
granted: ['bc1qstale'],
|
|
625
|
+
afterReconnect: ['bc1qwanted']
|
|
626
|
+
})
|
|
627
|
+
await service.initializeDiscovery()
|
|
628
|
+
|
|
629
|
+
// A live session with an active subscription — the state a pinned
|
|
630
|
+
// reconnect actually runs against.
|
|
631
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
632
|
+
const listener = vi.fn()
|
|
633
|
+
service.onAccountsChanged(listener)
|
|
634
|
+
|
|
635
|
+
const result = await service.connect(
|
|
636
|
+
NETWORK,
|
|
637
|
+
makeProvider('metamask-uuid'),
|
|
638
|
+
['bc1qwanted']
|
|
639
|
+
)
|
|
640
|
+
|
|
641
|
+
// The revoke happened, but its teardown signal must not have escaped.
|
|
642
|
+
expect(
|
|
643
|
+
wallet.features['bitcoin:disconnect'].disconnect
|
|
644
|
+
).toHaveBeenCalled()
|
|
645
|
+
expect(listener).not.toHaveBeenCalledWith([])
|
|
646
|
+
// ...and the connect it was interrupting still completed.
|
|
647
|
+
expect(result.address).toBe('bc1qwanted')
|
|
648
|
+
expect(service.isConnected()).toBe(true)
|
|
649
|
+
})
|
|
650
|
+
|
|
651
|
+
it('resumes reporting disconnects once the re-authorization finishes', async () => {
|
|
652
|
+
const { wallet, changeHandlers } = eventedGrantAware({
|
|
653
|
+
granted: ['bc1qstale'],
|
|
654
|
+
afterReconnect: ['bc1qwanted']
|
|
655
|
+
})
|
|
656
|
+
await service.initializeDiscovery()
|
|
657
|
+
|
|
658
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
659
|
+
const listener = vi.fn()
|
|
660
|
+
service.onAccountsChanged(listener)
|
|
661
|
+
|
|
662
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'), [
|
|
663
|
+
'bc1qwanted'
|
|
664
|
+
])
|
|
665
|
+
|
|
666
|
+
// A genuine user disconnect AFTER the window must still get through —
|
|
667
|
+
// the guard suppresses one event, it does not deafen the subscription.
|
|
668
|
+
// The direct path re-reads the wallet rather than trusting the payload,
|
|
669
|
+
// so the wallet has to actually be empty for this to mean "disconnected".
|
|
670
|
+
wallet.accounts = []
|
|
671
|
+
for (const handler of changeHandlers) handler({ accounts: [] })
|
|
672
|
+
expect(listener).toHaveBeenCalledWith([])
|
|
673
|
+
})
|
|
674
|
+
|
|
675
|
+
it('still forwards a real account switch during the re-authorization window', async () => {
|
|
676
|
+
// Only `[]` is suppressed. A non-empty list carries no teardown meaning,
|
|
677
|
+
// so narrowing the guard to the destructive case keeps switches visible.
|
|
678
|
+
const { wallet, changeHandlers } = eventedGrantAware({
|
|
679
|
+
granted: ['bc1qstale'],
|
|
680
|
+
afterReconnect: ['bc1qwanted']
|
|
681
|
+
})
|
|
682
|
+
wallet.features['bitcoin:disconnect'].disconnect = vi.fn(async () => {
|
|
683
|
+
wallet.accounts = [
|
|
684
|
+
{ address: 'bc1qother', publicKey: new Uint8Array([1]) }
|
|
685
|
+
]
|
|
686
|
+
for (const handler of changeHandlers) handler({ accounts: [{}] })
|
|
687
|
+
})
|
|
688
|
+
await service.initializeDiscovery()
|
|
689
|
+
|
|
690
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
691
|
+
const listener = vi.fn()
|
|
692
|
+
service.onAccountsChanged(listener)
|
|
693
|
+
|
|
694
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'), [
|
|
695
|
+
'bc1qwanted'
|
|
696
|
+
])
|
|
697
|
+
|
|
698
|
+
expect(listener).toHaveBeenCalledWith(['bc1qother'])
|
|
699
|
+
})
|
|
700
|
+
|
|
701
|
+
// Overlapping pinned connects share this service instance: core supersedes a
|
|
702
|
+
// re-entrant connect by aborting the previous attempt's wrapper, which cannot
|
|
703
|
+
// cancel a wallet prompt already open. With a boolean guard the older
|
|
704
|
+
// attempt's `finally` cleared the newer attempt's window.
|
|
705
|
+
it('keeps the guard while an overlapping re-authorization is still in flight', async () => {
|
|
706
|
+
const { wallet, changeHandlers } = eventedGrantAware({
|
|
707
|
+
granted: ['bc1qstale'],
|
|
708
|
+
afterReconnect: ['bc1qwanted']
|
|
709
|
+
})
|
|
710
|
+
// Hold the first revoke open so a second connect overlaps it.
|
|
711
|
+
let releaseFirst: () => void = () => {}
|
|
712
|
+
const firstRevoke = new Promise<void>(r => {
|
|
713
|
+
releaseFirst = r
|
|
714
|
+
})
|
|
715
|
+
let call = 0
|
|
716
|
+
wallet.features['bitcoin:disconnect'].disconnect = vi.fn(async () => {
|
|
717
|
+
call++
|
|
718
|
+
if (call === 1) await firstRevoke
|
|
719
|
+
wallet.accounts = []
|
|
720
|
+
for (const h of changeHandlers) h({ accounts: [] })
|
|
721
|
+
})
|
|
722
|
+
await service.initializeDiscovery()
|
|
723
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
724
|
+
const listener = vi.fn()
|
|
725
|
+
service.onAccountsChanged(listener)
|
|
726
|
+
|
|
727
|
+
const first = service.connect(NETWORK, makeProvider('metamask-uuid'), [
|
|
728
|
+
'bc1qwanted'
|
|
729
|
+
])
|
|
730
|
+
// Second attempt enters and LEAVES its window while the first is parked.
|
|
731
|
+
await service.connect(NETWORK, makeProvider('metamask-uuid'), [
|
|
732
|
+
'bc1qwanted'
|
|
733
|
+
])
|
|
734
|
+
releaseFirst()
|
|
735
|
+
await first
|
|
736
|
+
|
|
737
|
+
// The still-in-flight first attempt's revoke event must not have escaped.
|
|
738
|
+
expect(listener).not.toHaveBeenCalledWith([])
|
|
739
|
+
})
|
|
740
|
+
|
|
741
|
+
it('picks accounts[0] with no pin (unchanged behaviour)', async () => {
|
|
742
|
+
discover(['bc1qfirst', 'bc1qsecond'])
|
|
743
|
+
await service.initializeDiscovery()
|
|
744
|
+
|
|
745
|
+
const result = await service.connect(
|
|
746
|
+
NETWORK,
|
|
747
|
+
makeProvider('metamask-uuid')
|
|
748
|
+
)
|
|
749
|
+
|
|
750
|
+
expect(result.address).toBe('bc1qfirst')
|
|
751
|
+
})
|
|
752
|
+
})
|
|
753
|
+
|
|
149
754
|
describe('setConnectionState', () => {
|
|
150
755
|
it('re-derives a full BitcoinAccount (hex publicKey) from the wallet accounts list', () => {
|
|
151
756
|
const detectedWallet = {
|
|
@@ -203,14 +808,7 @@ describe('BitcoinWalletService', () => {
|
|
|
203
808
|
const rawWallet = makeRawWallet('MetaMask', [
|
|
204
809
|
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
205
810
|
])
|
|
206
|
-
|
|
207
|
-
{
|
|
208
|
-
uuid: 'metamask-uuid',
|
|
209
|
-
name: 'MetaMask',
|
|
210
|
-
features: [],
|
|
211
|
-
wallet: rawWallet as any
|
|
212
|
-
}
|
|
213
|
-
])
|
|
811
|
+
discoverWallet(rawWallet)
|
|
214
812
|
await service.initializeDiscovery()
|
|
215
813
|
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
216
814
|
|
|
@@ -248,14 +846,7 @@ describe('BitcoinWalletService', () => {
|
|
|
248
846
|
const acc1 = { address: 'bc1qACC1', publicKey: new Uint8Array([1]) }
|
|
249
847
|
const acc2 = { address: 'bc1qACC2', publicKey: new Uint8Array([2]) }
|
|
250
848
|
const { wallet, signMessage } = makeSignableWallet([acc1])
|
|
251
|
-
|
|
252
|
-
{
|
|
253
|
-
uuid: 'metamask-uuid',
|
|
254
|
-
name: 'MetaMask',
|
|
255
|
-
features: [],
|
|
256
|
-
wallet: wallet as any
|
|
257
|
-
}
|
|
258
|
-
])
|
|
849
|
+
discoverWallet(wallet)
|
|
259
850
|
await service.initializeDiscovery()
|
|
260
851
|
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
261
852
|
|
|
@@ -358,14 +949,7 @@ describe('BitcoinWalletService', () => {
|
|
|
358
949
|
}
|
|
359
950
|
|
|
360
951
|
async function connectWith(wallet: unknown) {
|
|
361
|
-
|
|
362
|
-
{
|
|
363
|
-
uuid: 'metamask-uuid',
|
|
364
|
-
name: 'MetaMask',
|
|
365
|
-
features: [],
|
|
366
|
-
wallet: wallet as any
|
|
367
|
-
}
|
|
368
|
-
])
|
|
952
|
+
discoverWallet(wallet)
|
|
369
953
|
await service.initializeDiscovery()
|
|
370
954
|
await service.connect(NETWORK, makeProvider('metamask-uuid'))
|
|
371
955
|
}
|
|
@@ -427,13 +1011,59 @@ describe('BitcoinWalletService', () => {
|
|
|
427
1011
|
expect(service.onAccountsChanged(vi.fn())).toBeUndefined()
|
|
428
1012
|
})
|
|
429
1013
|
|
|
430
|
-
it('returns undefined when the connected wallet exposes no
|
|
1014
|
+
it('returns undefined when the connected wallet exposes no change-event feature at all', async () => {
|
|
431
1015
|
const rawWallet = makeRawWallet('MetaMask', [
|
|
432
1016
|
{ address: 'bc1qtest', publicKey: new Uint8Array([1, 2, 3]) }
|
|
433
1017
|
])
|
|
434
1018
|
await connectWith(rawWallet)
|
|
435
1019
|
expect(service.onAccountsChanged(vi.fn())).toBeUndefined()
|
|
436
1020
|
})
|
|
1021
|
+
|
|
1022
|
+
// Real MetaMask publishes `bitcoin:events`, not `standard:events` (Trust
|
|
1023
|
+
// uses the latter). Reading only the standard key made every MetaMask
|
|
1024
|
+
// account switch silently unobservable.
|
|
1025
|
+
it('falls back to bitcoin:events when standard:events is absent (MetaMask)', async () => {
|
|
1026
|
+
let changeListener: ((props: unknown) => void) | undefined
|
|
1027
|
+
const off = vi.fn()
|
|
1028
|
+
const wallet = {
|
|
1029
|
+
name: 'MetaMask',
|
|
1030
|
+
accounts: [
|
|
1031
|
+
{ address: 'bc1qoriginal', publicKey: new Uint8Array([1]) }
|
|
1032
|
+
],
|
|
1033
|
+
chains: ['bitcoin:mainnet'],
|
|
1034
|
+
features: {
|
|
1035
|
+
'bitcoin:connect': {
|
|
1036
|
+
connect: vi.fn().mockResolvedValue({
|
|
1037
|
+
accounts: [
|
|
1038
|
+
{ address: 'bc1qoriginal', publicKey: new Uint8Array([1]) }
|
|
1039
|
+
]
|
|
1040
|
+
})
|
|
1041
|
+
},
|
|
1042
|
+
'bitcoin:events': {
|
|
1043
|
+
on: vi.fn((event: string, listener: (props: unknown) => void) => {
|
|
1044
|
+
if (event === 'change') changeListener = listener
|
|
1045
|
+
return off
|
|
1046
|
+
})
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
await connectWith(wallet)
|
|
1051
|
+
|
|
1052
|
+
const listener = vi.fn()
|
|
1053
|
+
const teardown = service.onAccountsChanged(listener)
|
|
1054
|
+
expect(wallet.features['bitcoin:events'].on).toHaveBeenCalledWith(
|
|
1055
|
+
'change',
|
|
1056
|
+
expect.any(Function)
|
|
1057
|
+
)
|
|
1058
|
+
;(wallet as any).accounts = [
|
|
1059
|
+
{ address: 'bc1qswitched', publicKey: new Uint8Array([2]) }
|
|
1060
|
+
]
|
|
1061
|
+
changeListener?.({ accounts: wallet.accounts })
|
|
1062
|
+
expect(listener).toHaveBeenCalledWith(['bc1qswitched'])
|
|
1063
|
+
|
|
1064
|
+
teardown?.()
|
|
1065
|
+
expect(off).toHaveBeenCalledTimes(1)
|
|
1066
|
+
})
|
|
437
1067
|
})
|
|
438
1068
|
|
|
439
1069
|
describe('onAccountsChanged — bridged path', () => {
|