-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathqr-keyring-v2.test.ts
More file actions
753 lines (621 loc) · 23.3 KB
/
Copy pathqr-keyring-v2.test.ts
File metadata and controls
753 lines (621 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import type { Bip44Account } from '@metamask/account-api';
import {
EthAccountType,
EthMethod,
EthScope,
type KeyringAccount,
KeyringAccountEntropyTypeOption,
KeyringType,
} from '@metamask/keyring-api';
import type { QrKeyringBridge } from '.';
import { QrKeyring } from '.';
import {
QrKeyringV2,
type QrKeyringCreateAccountOptions,
} from './qr-keyring-v2';
import {
ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS,
EXPECTED_ACCOUNTS,
HDKEY_SERIALIZED_KEYRING_WITH_ACCOUNTS,
HDKEY_SERIALIZED_KEYRING_WITH_NO_ACCOUNTS,
KNOWN_CRYPTO_ACCOUNT_UR,
KNOWN_HDKEY_UR,
} from '../test/fixtures';
/**
* Type alias for QR keyring HD mode accounts (BIP-44 derived).
*/
type QrHdAccount = Bip44Account<KeyringAccount>;
/**
* Get the first account from an array, throwing if empty.
*
* @param accounts - The accounts array.
* @returns The first account.
*/
function getFirstAccount(accounts: KeyringAccount[]): KeyringAccount {
if (accounts.length === 0) {
throw new Error('Expected at least one account');
}
return accounts[0] as KeyringAccount;
}
/**
* Get the first HD mode account from an array, throwing if empty.
*
* @param accounts - The accounts array.
* @returns The first account cast as QrHdAccount.
*/
function getFirstHdAccount(accounts: KeyringAccount[]): QrHdAccount {
if (accounts.length === 0) {
throw new Error('Expected at least one account');
}
return accounts[0] as QrHdAccount;
}
/**
* Get an account at a specific index, throwing if not present.
*
* @param accounts - The accounts array.
* @param index - The index to retrieve.
* @returns The account at the index.
*/
function getAccountAt(
accounts: KeyringAccount[],
index: number,
): KeyringAccount {
if (accounts.length <= index) {
throw new Error(`Expected account at index ${index}`);
}
return accounts[index] as KeyringAccount;
}
const entropySource = HDKEY_SERIALIZED_KEYRING_WITH_NO_ACCOUNTS.xfp;
/**
* Expected methods supported by QR keyring accounts.
*/
const EXPECTED_METHODS = [
EthMethod.SignTransaction,
EthMethod.PersonalSign,
EthMethod.SignTypedDataV4,
];
/**
* Get a mock bridge for the QrKeyring.
*
* @returns A mock bridge with a requestScan method.
*/
function getMockBridge(): QrKeyringBridge {
return {
requestScan: jest.fn(),
};
}
/**
* Create a fresh QrKeyring with HD mode device.
*
* @returns The inner keyring.
*/
function createInnerKeyring(): QrKeyring {
return new QrKeyring({
bridge: getMockBridge(),
ur: KNOWN_HDKEY_UR,
});
}
/**
* Create a QrKeyringV2 wrapper with a paired HD mode device and accounts.
*
* @param accountCount - Number of accounts to add.
* @returns The wrapper and inner keyring.
*/
async function createWrapperWithAccounts(accountCount = 3): Promise<{
wrapper: QrKeyringV2;
inner: QrKeyring;
}> {
const inner = createInnerKeyring();
await inner.addAccounts(accountCount);
const wrapper = new QrKeyringV2({ legacyKeyring: inner, entropySource });
return { wrapper, inner };
}
/**
* Create a QrKeyringV2 wrapper without any accounts.
*
* @returns The wrapper and inner keyring.
*/
function createEmptyWrapper(): { wrapper: QrKeyringV2; inner: QrKeyring } {
const inner = createInnerKeyring();
const wrapper = new QrKeyringV2({ legacyKeyring: inner, entropySource });
return { wrapper, inner };
}
/**
* Create a QrKeyringV2 wrapper with a paired Account mode device.
*
* @returns The wrapper and inner keyring.
*/
async function createAccountModeWrapper(): Promise<{
wrapper: QrKeyringV2;
inner: QrKeyring;
}> {
const inner = new QrKeyring({
bridge: getMockBridge(),
ur: KNOWN_CRYPTO_ACCOUNT_UR,
});
await inner.addAccounts(1);
const wrapper = new QrKeyringV2({
legacyKeyring: inner,
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
});
return { wrapper, inner };
}
/**
* Helper to create account options for bip44:derive-index.
*
* @param groupIndex - The group index to derive.
* @param source - Optional entropy source override.
* @returns The create account options.
*/
function deriveIndexOptions(
groupIndex: number,
source: string = entropySource,
): {
type: 'bip44:derive-index';
entropySource: string;
groupIndex: number;
} {
return {
type: 'bip44:derive-index' as const,
entropySource: source,
groupIndex,
};
}
/**
* Helper to create custom account options for Account mode.
*
* @param addressIndex - The index of the pre-defined address.
* @param source - Optional entropy source override.
* @returns The create account options.
*/
function customAccountOptions(
addressIndex: number,
source: string = ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
): {
type: 'custom';
entropySource: string;
addressIndex: number;
} {
return {
type: 'custom' as const,
entropySource: source,
addressIndex,
};
}
describe('QrKeyringV2', () => {
describe('constructor', () => {
it('creates a wrapper with correct type and capabilities', () => {
const { wrapper } = createEmptyWrapper();
expect(wrapper.type).toBe(KeyringType.Qr);
// Default capabilities when no device is paired
expect(wrapper.capabilities).toStrictEqual({
scopes: [EthScope.Eoa],
bip44: {
deriveIndex: true,
},
});
});
});
describe('getAccounts', () => {
it('returns correct capabilities immediately for pre-paired Account mode device', async () => {
const inner = new QrKeyring({
bridge: getMockBridge(),
ur: KNOWN_CRYPTO_ACCOUNT_UR,
});
await inner.addAccounts(1);
const wrapper = new QrKeyringV2({
legacyKeyring: inner,
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
});
// Capabilities are correct immediately - no need to call getAccounts first
expect(wrapper.capabilities).toStrictEqual({
scopes: [EthScope.Eoa],
custom: {
createAccounts: true,
},
});
});
it('returns empty array when no device is paired', async () => {
const inner = new QrKeyring({ bridge: getMockBridge() });
const wrapper = new QrKeyringV2({ legacyKeyring: inner, entropySource });
const accounts = await wrapper.getAccounts();
expect(accounts).toStrictEqual([]);
});
it('returns all accounts from the legacy keyring', async () => {
const { wrapper } = await createWrapperWithAccounts(3);
const accounts = await wrapper.getAccounts();
expect(accounts).toHaveLength(3);
expect(accounts.map((a) => a.address)).toStrictEqual(
EXPECTED_ACCOUNTS.slice(0, 3),
);
});
it('creates KeyringAccount objects with correct structure', async () => {
const { wrapper } = await createWrapperWithAccounts(1);
const accounts = await wrapper.getAccounts();
const account = getFirstAccount(accounts);
expect(account).toMatchObject({
type: EthAccountType.Eoa,
address: EXPECTED_ACCOUNTS[0],
scopes: [EthScope.Eoa],
methods: EXPECTED_METHODS,
options: {
entropy: {
type: KeyringAccountEntropyTypeOption.Mnemonic,
id: entropySource,
groupIndex: 0,
derivationPath: `m/44'/60'/0'/0/0`,
},
},
});
expect(account.id).toBeDefined();
});
it('returns correct groupIndex for multiple accounts', async () => {
const { wrapper } = await createWrapperWithAccounts(3);
const accounts = await wrapper.getAccounts();
accounts.forEach((account, index) => {
expect((account as QrHdAccount).options.entropy.groupIndex).toBe(index);
});
});
it('throws error in HD mode when address is not in indexes map', async () => {
const { wrapper, inner } = await createWrapperWithAccounts(1);
// Mock serialize to return an inconsistent state
jest.spyOn(inner, 'serialize').mockResolvedValue({
...HDKEY_SERIALIZED_KEYRING_WITH_ACCOUNTS,
indexes: {}, // Empty indexes - inconsistent with accounts
});
await expect(wrapper.getAccounts()).rejects.toThrow(
/not found in device indexes/u,
);
});
it('throws error when getPathFromAddress returns undefined', async () => {
const { wrapper, inner } = await createWrapperWithAccounts(1);
jest.spyOn(inner, 'getPathFromAddress').mockReturnValue(undefined);
await expect(wrapper.getAccounts()).rejects.toThrow(
/derivation path not found in keyring/u,
);
});
});
describe('deserialize', () => {
it('deserializes the legacy keyring state', async () => {
const inner = new QrKeyring({ bridge: getMockBridge() });
const wrapper = new QrKeyringV2({ legacyKeyring: inner, entropySource });
await wrapper.deserialize(HDKEY_SERIALIZED_KEYRING_WITH_ACCOUNTS);
const accounts = await wrapper.getAccounts();
expect(accounts).toHaveLength(3);
expect(accounts.map((a) => a.address)).toStrictEqual(
EXPECTED_ACCOUNTS.slice(0, 3),
);
});
it('clears the cache and rebuilds it', async () => {
const { wrapper } = await createWrapperWithAccounts(2);
const accountsBefore = await wrapper.getAccounts();
expect(accountsBefore).toHaveLength(2);
await wrapper.deserialize(HDKEY_SERIALIZED_KEYRING_WITH_ACCOUNTS);
const accountsAfter = await wrapper.getAccounts();
expect(accountsAfter).toHaveLength(3);
// The accounts should be new objects (cache was cleared)
expect(accountsAfter[0]).not.toBe(accountsBefore[0]);
});
it('updates capabilities to HD mode after deserializing HD device state', async () => {
const inner = new QrKeyring({ bridge: getMockBridge() });
const wrapper = new QrKeyringV2({ legacyKeyring: inner, entropySource });
await wrapper.deserialize(HDKEY_SERIALIZED_KEYRING_WITH_ACCOUNTS);
expect(wrapper.capabilities).toStrictEqual({
scopes: [EthScope.Eoa],
bip44: {
deriveIndex: true,
},
});
});
it('updates capabilities to Account mode after deserializing Account device state', async () => {
const inner = new QrKeyring({ bridge: getMockBridge() });
const wrapper = new QrKeyringV2({
legacyKeyring: inner,
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
});
// Initially, capabilities are HD mode defaults (derivePath is false for QR)
await wrapper.deserialize(ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS);
// After deserializing Account mode state, capabilities should update
expect(wrapper.capabilities).toStrictEqual({
scopes: [EthScope.Eoa],
custom: {
createAccounts: true,
},
});
});
it('clears custom capability when transitioning from Account mode to HD mode', async () => {
const inner = new QrKeyring({ bridge: getMockBridge() });
const wrapper = new QrKeyringV2({
legacyKeyring: inner,
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
});
// First deserialize Account mode to get custom capability
await wrapper.deserialize(ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS);
expect(wrapper.capabilities.custom).toStrictEqual({
createAccounts: true,
});
// Now deserialize HD mode state - custom capability should be cleared
await wrapper.deserialize(HDKEY_SERIALIZED_KEYRING_WITH_ACCOUNTS);
expect(wrapper.capabilities.custom).toBeUndefined();
expect(wrapper.capabilities).toStrictEqual({
scopes: [EthScope.Eoa],
bip44: {
deriveIndex: true,
},
});
});
});
describe('createAccounts', () => {
it('creates the first account at index 0', async () => {
const { wrapper } = createEmptyWrapper();
const newAccounts = await wrapper.createAccounts(deriveIndexOptions(0));
const account = getFirstAccount(newAccounts);
expect(account.address).toBe(EXPECTED_ACCOUNTS[0]);
});
it('creates an account at a specific index', async () => {
const { wrapper } = await createWrapperWithAccounts(2);
const newAccounts = await wrapper.createAccounts(deriveIndexOptions(2));
const account = getFirstAccount(newAccounts);
expect(account.address).toBe(EXPECTED_ACCOUNTS[2]);
});
it('returns existing account if groupIndex already exists', async () => {
const { wrapper } = await createWrapperWithAccounts(2);
const existingAccounts = await wrapper.getAccounts();
const existingAccount = getFirstAccount(existingAccounts);
const newAccounts = await wrapper.createAccounts(deriveIndexOptions(0));
const returnedAccount = getFirstAccount(newAccounts);
expect(returnedAccount).toBe(existingAccount);
});
it('throws error for unsupported account creation type', async () => {
const { wrapper } = await createWrapperWithAccounts(0);
await expect(
wrapper.createAccounts({
type: 'private-key:import',
entropySource,
accountType: EthAccountType.Eoa,
encoding: 'hexadecimal',
privateKey: '0xabc',
} as unknown as QrKeyringCreateAccountOptions),
).rejects.toThrow(
/Unsupported account creation type for HD mode QrKeyring/u,
);
});
it('throws error for entropy source mismatch', async () => {
const { wrapper } = await createWrapperWithAccounts(0);
await expect(
wrapper.createAccounts(deriveIndexOptions(0, 'wrong-entropy')),
).rejects.toThrow(/Entropy source mismatch/u);
});
it('throws error when no device is paired', async () => {
const inner = new QrKeyring({ bridge: getMockBridge() });
const wrapper = new QrKeyringV2({ legacyKeyring: inner, entropySource });
await expect(
wrapper.createAccounts(deriveIndexOptions(0, 'some-entropy')),
).rejects.toThrow('No device paired. Cannot create accounts.');
});
it('allows deriving accounts at any index (non-sequential)', async () => {
const { wrapper } = createEmptyWrapper();
const newAccounts = await wrapper.createAccounts(deriveIndexOptions(5));
const account = getFirstHdAccount(newAccounts);
expect(account.address).toBe(EXPECTED_ACCOUNTS[5]);
expect(account.options.entropy.groupIndex).toBe(5);
});
it('creates multiple accounts sequentially', async () => {
const { wrapper } = createEmptyWrapper();
const results = await Promise.all([
wrapper.createAccounts(deriveIndexOptions(0)),
wrapper.createAccounts(deriveIndexOptions(1)),
wrapper.createAccounts(deriveIndexOptions(2)),
]);
results.forEach((accounts, index) => {
const account = getFirstAccount(accounts);
expect(account.address).toBe(EXPECTED_ACCOUNTS[index]);
});
});
it('throws error when inner keyring fails to create account', async () => {
const { wrapper, inner } = createEmptyWrapper();
jest.spyOn(inner, 'addAccounts').mockResolvedValueOnce([]);
await expect(
wrapper.createAccounts(deriveIndexOptions(0)),
).rejects.toThrow('Failed to create new account');
});
it('throws error for negative groupIndex', async () => {
const { wrapper } = createEmptyWrapper();
await expect(
wrapper.createAccounts(deriveIndexOptions(-1)),
).rejects.toThrow(/Invalid groupIndex: -1/u);
});
it('throws error for unsupported derive-path type', async () => {
const { wrapper } = createEmptyWrapper();
await expect(
wrapper.createAccounts({
type: 'bip44:derive-path',
entropySource,
derivationPath: `m/44'/60'/0'/0/0`,
} as unknown as QrKeyringCreateAccountOptions),
).rejects.toThrow(
/Unsupported account creation type for HD mode QrKeyring: bip44:derive-path/u,
);
});
});
describe('deleteAccount', () => {
it('removes an account from the keyring', async () => {
const { wrapper } = await createWrapperWithAccounts(3);
const accountsBefore = await wrapper.getAccounts();
const accountToDelete = getAccountAt(accountsBefore, 1);
await wrapper.deleteAccount(accountToDelete.id);
const accountsAfter = await wrapper.getAccounts();
expect(accountsAfter).toHaveLength(2);
expect(accountsAfter.map((a) => a.address)).not.toContain(
accountToDelete.address,
);
});
it('removes the account from the cache', async () => {
const { wrapper } = await createWrapperWithAccounts(2);
const accounts = await wrapper.getAccounts();
const accountToDelete = getFirstAccount(accounts);
await wrapper.deleteAccount(accountToDelete.id);
await expect(wrapper.getAccount(accountToDelete.id)).rejects.toThrow(
/Account not found/u,
);
});
it('throws error for non-existent account', async () => {
const { wrapper } = await createWrapperWithAccounts(1);
await expect(wrapper.deleteAccount('non-existent-id')).rejects.toThrow(
/Account not found/u,
);
});
});
describe('getAccount', () => {
it('returns the account by ID', async () => {
const { wrapper } = await createWrapperWithAccounts(2);
const accounts = await wrapper.getAccounts();
const expectedAccount = getFirstAccount(accounts);
const account = await wrapper.getAccount(expectedAccount.id);
expect(account).toBe(expectedAccount);
});
it('throws error for non-existent account', async () => {
const { wrapper } = await createWrapperWithAccounts(1);
await expect(wrapper.getAccount('non-existent-id')).rejects.toThrow(
/Account not found/u,
);
});
});
describe('serialize', () => {
it('serializes the legacy keyring state', async () => {
const { wrapper, inner } = await createWrapperWithAccounts(2);
const wrapperSerialized = await wrapper.serialize();
const innerSerialized = await inner.serialize();
expect(wrapperSerialized).toStrictEqual(innerSerialized);
});
});
describe('Account Mode (CryptoAccount)', () => {
describe('getAccounts', () => {
it('returns accounts with PrivateKey entropy type (pre-defined addresses)', async () => {
const { wrapper } = await createAccountModeWrapper();
const accounts = await wrapper.getAccounts();
const account = getFirstAccount(accounts);
expect(account.address).toBe(
ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.accounts[0],
);
expect(account.options.entropy).toStrictEqual({
type: KeyringAccountEntropyTypeOption.Custom,
});
});
it('throws error in Account mode when address is not in paths map', async () => {
const { wrapper, inner } = await createAccountModeWrapper();
// Mock serialize to return an inconsistent state
jest.spyOn(inner, 'serialize').mockResolvedValue({
...ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS,
paths: {}, // Empty paths - inconsistent with accounts
});
await expect(wrapper.getAccounts()).rejects.toThrow(
/not found in device paths/u,
);
});
});
describe('createAccounts', () => {
it('throws error when trying to derive by index', async () => {
const { wrapper } = await createAccountModeWrapper();
await expect(
wrapper.createAccounts({
type: 'bip44:derive-index',
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
groupIndex: 0,
}),
).rejects.toThrow(
/Account mode devices only support 'custom' account creation type/u,
);
});
it('throws error when trying to derive by path', async () => {
const { wrapper } = await createAccountModeWrapper();
await expect(
wrapper.createAccounts({
type: 'bip44:derive-path',
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
derivationPath: `m/44'/60'/0'/0/0`,
} as unknown as QrKeyringCreateAccountOptions),
).rejects.toThrow(
/Account mode devices only support 'custom' account creation type/u,
);
});
it('creates account using custom type with valid addressIndex', async () => {
const inner = new QrKeyring({
bridge: getMockBridge(),
ur: KNOWN_CRYPTO_ACCOUNT_UR,
});
// Do not add any accounts initially
const wrapper = new QrKeyringV2({
legacyKeyring: inner,
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
});
const newAccounts = await wrapper.createAccounts(
customAccountOptions(0),
);
const account = getFirstAccount(newAccounts);
expect(account.address).toBe(
ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.accounts[0],
);
expect(account.options.entropy).toStrictEqual({
type: KeyringAccountEntropyTypeOption.Custom,
});
});
it('returns existing account if addressIndex already exists', async () => {
const { wrapper } = await createAccountModeWrapper();
const existingAccounts = await wrapper.getAccounts();
const existingAccount = getFirstAccount(existingAccounts);
const newAccounts = await wrapper.createAccounts(
customAccountOptions(0),
);
const returnedAccount = getFirstAccount(newAccounts);
expect(returnedAccount).toBe(existingAccount);
});
it('throws error for invalid addressIndex type', async () => {
const { wrapper } = await createAccountModeWrapper();
await expect(
wrapper.createAccounts({
type: 'custom',
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
addressIndex: 'invalid',
} as unknown as { type: 'custom'; entropySource: string; addressIndex: number }),
).rejects.toThrow(/Invalid addressIndex/u);
});
it('throws error for negative addressIndex', async () => {
const { wrapper } = await createAccountModeWrapper();
await expect(
wrapper.createAccounts(customAccountOptions(-1)),
).rejects.toThrow(/Invalid addressIndex/u);
});
it('throws error for addressIndex out of bounds', async () => {
const { wrapper } = await createAccountModeWrapper();
await expect(
wrapper.createAccounts(customAccountOptions(999)),
).rejects.toThrow(/Address index 999 is out of bounds/u);
});
it('throws error when inner keyring fails to create account', async () => {
const inner = new QrKeyring({
bridge: getMockBridge(),
ur: KNOWN_CRYPTO_ACCOUNT_UR,
});
const wrapper = new QrKeyringV2({
legacyKeyring: inner,
entropySource: ACCOUNT_SERIALIZED_KEYRING_WITH_ACCOUNTS.xfp,
});
jest.spyOn(inner, 'addAccounts').mockResolvedValueOnce([]);
await expect(
wrapper.createAccounts(customAccountOptions(0)),
).rejects.toThrow('Failed to create new account');
});
});
describe('deleteAccount', () => {
it('removes an account from the keyring', async () => {
const { wrapper, inner } = await createAccountModeWrapper();
const accounts = await wrapper.getAccounts();
const account = getFirstAccount(accounts);
await wrapper.deleteAccount(account.id);
const remainingAddresses = await inner.getAccounts();
expect(remainingAddresses).toHaveLength(0);
});
});
});
});