1
1
import HdKeyring from '@metamask/eth-hd-keyring' ;
2
2
import { normalize as normalizeAddress } from '@metamask/eth-sig-util' ;
3
- import type { Hex , Json , Keyring , KeyringClass } from '@metamask/utils' ;
3
+ import type { Hex } from '@metamask/utils' ;
4
4
import { strict as assert } from 'assert' ;
5
5
import Wallet from 'ethereumjs-wallet' ;
6
6
import * as sinon from 'sinon' ;
@@ -44,11 +44,7 @@ describe('KeyringController', () => {
44
44
keyringController = new KeyringController ( {
45
45
encryptor : mockEncryptor ,
46
46
cacheEncryptionKey : false ,
47
- keyringBuilders : [
48
- keyringBuilderFactory (
49
- KeyringMockWithInit as unknown as KeyringClass < any > ,
50
- ) ,
51
- ] ,
47
+ keyringBuilders : [ keyringBuilderFactory ( KeyringMockWithInit ) ] ,
52
48
} ) ;
53
49
54
50
await keyringController . createNewVaultAndKeychain ( PASSWORD ) ;
@@ -454,7 +450,8 @@ describe('KeyringController', () => {
454
450
455
451
const keyring = await keyringController . restoreKeyring ( mockSerialized ) ;
456
452
// eslint-disable-next-line no-unsafe-optional-chaining
457
- const { numberOfAccounts } = ( await keyring ?. serialize ( ) ) as any ;
453
+ // @ts -expect-error this value should never be undefined in this specific context.
454
+ const { numberOfAccounts } = await keyring . serialize ( ) ;
458
455
expect ( numberOfAccounts ) . toBe ( 1 ) ;
459
456
460
457
const accounts = await keyring ?. getAccounts ( ) ;
@@ -474,15 +471,17 @@ describe('KeyringController', () => {
474
471
it ( 'returns the result of getAccounts for each keyring' , async ( ) => {
475
472
keyringController . keyrings = [
476
473
{
474
+ // @ts -expect-error there's only a need to mock the getAccounts method for this test.
477
475
async getAccounts ( ) {
478
476
return Promise . resolve ( [ 1 , 2 , 3 ] ) ;
479
477
} ,
480
- } as unknown as Keyring < Json > ,
478
+ } ,
481
479
{
480
+ // @ts -expect-error there's only a need to mock the getAccounts method for this test.
482
481
async getAccounts ( ) {
483
482
return Promise . resolve ( [ 4 , 5 , 6 ] ) ;
484
483
} ,
485
- } as unknown as Keyring < Json > ,
484
+ } ,
486
485
] ;
487
486
488
487
const result = await keyringController . getAccounts ( ) ;
@@ -499,7 +498,7 @@ describe('KeyringController', () => {
499
498
500
499
describe ( 'removeAccount' , ( ) => {
501
500
it ( 'removes an account from the corresponding keyring' , async ( ) => {
502
- const account = {
501
+ const account : { privateKey : string ; publicKey : Hex } = {
503
502
privateKey :
504
503
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3' ,
505
504
publicKey : '0x627306090abab3a6e1400e9345bc60c78a8bef57' ,
@@ -514,7 +513,7 @@ describe('KeyringController', () => {
514
513
expect ( keyringController . keyrings ) . toHaveLength ( 2 ) ;
515
514
516
515
// remove that account that we just added
517
- await keyringController . removeAccount ( account . publicKey as Hex ) ;
516
+ await keyringController . removeAccount ( account . publicKey ) ;
518
517
519
518
expect ( keyringController . keyrings ) . toHaveLength ( 1 ) ;
520
519
// fetch accounts after removal
@@ -523,7 +522,7 @@ describe('KeyringController', () => {
523
522
} ) ;
524
523
525
524
it ( 'removes the keyring if there are no accounts after removal' , async ( ) => {
526
- const account = {
525
+ const account : { privateKey : string ; publicKey : Hex } = {
527
526
privateKey :
528
527
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3' ,
529
528
publicKey : '0x627306090abab3a6e1400e9345bc60c78a8bef57' ,
@@ -538,7 +537,7 @@ describe('KeyringController', () => {
538
537
expect ( keyringController . keyrings ) . toHaveLength ( 2 ) ;
539
538
540
539
// remove that account that we just added
541
- await keyringController . removeAccount ( account . publicKey as Hex ) ;
540
+ await keyringController . removeAccount ( account . publicKey ) ;
542
541
543
542
// Check that the previous keyring with only one account
544
543
// was also removed after removing the account
@@ -556,7 +555,8 @@ describe('KeyringController', () => {
556
555
expect ( keyringController . keyrings ) . toHaveLength ( 2 ) ;
557
556
558
557
// remove one account from the keyring we just added
559
- await keyringController . removeAccount ( walletTwoAddresses [ 0 ] as Hex ) ;
558
+ // @ts -expect-error this value should never be undefied
559
+ await keyringController . removeAccount ( walletTwoAddresses [ 0 ] ) ;
560
560
561
561
// Check that the newly added keyring was not removed after
562
562
// removing the account since it still has an account left
@@ -571,7 +571,8 @@ describe('KeyringController', () => {
571
571
expect ( keyrings ) . toHaveLength ( 1 ) ;
572
572
await Promise . all (
573
573
keyrings . map ( async ( keyring ) => {
574
- const { numberOfAccounts } = ( await keyring . serialize ( ) ) as any ;
574
+ // @ts -expect-error numberOfAccounts mising in Json specification.
575
+ const { numberOfAccounts } = await keyring . serialize ( ) ;
575
576
expect ( numberOfAccounts ) . toBe ( 1 ) ;
576
577
} ) ,
577
578
) ;
@@ -592,11 +593,7 @@ describe('KeyringController', () => {
592
593
describe ( 'verifyPassword' , ( ) => {
593
594
beforeEach ( ( ) => {
594
595
keyringController = new KeyringController ( {
595
- keyringBuilders : [
596
- keyringBuilderFactory (
597
- KeyringMockWithInit as unknown as KeyringClass < any > ,
598
- ) ,
599
- ] ,
596
+ keyringBuilders : [ keyringBuilderFactory ( KeyringMockWithInit ) ] ,
600
597
encryptor : mockEncryptor ,
601
598
cacheEncryptionKey : false ,
602
599
} ) ;
@@ -626,7 +623,8 @@ describe('KeyringController', () => {
626
623
const initialAccounts = await HDKeyring ?. getAccounts ( ) ;
627
624
expect ( initialAccounts ) . toHaveLength ( 1 ) ;
628
625
629
- await keyringController . addNewAccount ( HDKeyring as Keyring < Json > ) ;
626
+ // @ts -expect-error this value should never be undefined in this specific context.
627
+ await keyringController . addNewAccount ( HDKeyring ) ;
630
628
const accountsAfterAdd = await HDKeyring ?. getAccounts ( ) ;
631
629
expect ( accountsAfterAdd ) . toHaveLength ( 2 ) ;
632
630
} ) ;
@@ -718,10 +716,11 @@ describe('KeyringController', () => {
718
716
it ( 'throws error when there are no matching keyrings' , async ( ) => {
719
717
keyringController . keyrings = [
720
718
{
719
+ // @ts -expect-error there's only a need to mock the getAccounts method for this test.
721
720
async getAccounts ( ) {
722
721
return Promise . resolve ( [ 1 , 2 , 3 ] ) ;
723
722
} ,
724
- } as unknown as Keyring < Json > ,
723
+ } ,
725
724
] ;
726
725
727
726
await expect (
@@ -808,13 +807,15 @@ describe('KeyringController', () => {
808
807
809
808
const [ firstKeyring ] = keyringController . keyrings ;
810
809
811
- await keyringController . addNewAccount ( firstKeyring as Keyring < Json > ) ;
810
+ // @ts -expect-error this value should never be undefined in this specific context.
811
+ await keyringController . addNewAccount ( firstKeyring ) ;
812
812
expect ( await keyringController . getAccounts ( ) ) . toHaveLength ( 2 ) ;
813
813
814
- await keyringController . addNewAccount ( firstKeyring as Keyring < Json > ) ;
814
+ // @ts -expect-error this value should never be undefined in this specific context.
815
+ await keyringController . addNewAccount ( firstKeyring ) ;
815
816
expect ( await keyringController . getAccounts ( ) ) . toHaveLength ( 3 ) ;
816
817
817
- const account = {
818
+ const account : { privateKey : string ; publicKey : Hex } = {
818
819
privateKey :
819
820
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3' ,
820
821
publicKey : '0x627306090abab3a6e1400e9345bc60c78a8bef57' ,
@@ -827,7 +828,7 @@ describe('KeyringController', () => {
827
828
expect ( await keyringController . getAccounts ( ) ) . toHaveLength ( 4 ) ;
828
829
829
830
// remove that account that we just added
830
- await keyringController . removeAccount ( account . publicKey as Hex ) ;
831
+ await keyringController . removeAccount ( account . publicKey ) ;
831
832
expect ( await keyringController . getAccounts ( ) ) . toHaveLength ( 3 ) ;
832
833
} ) ;
833
834
@@ -865,7 +866,8 @@ describe('KeyringController', () => {
865
866
walletOneSeedWords ,
866
867
) ;
867
868
const privateKey = await keyringController . exportAccount (
868
- walletOneAddresses [ 0 ] as Hex ,
869
+ // @ts -expect-error this value should never be undefined in this specific context.
870
+ walletOneAddresses [ 0 ] ,
869
871
) ;
870
872
expect ( privateKey ) . toStrictEqual ( walletOnePrivateKey [ 0 ] ) ;
871
873
} ) ;
@@ -881,10 +883,11 @@ describe('KeyringController', () => {
881
883
882
884
it ( 'signMessage' , async ( ) => {
883
885
const inputParams = {
884
- from : walletOneAddresses [ 0 ] as Hex ,
886
+ from : walletOneAddresses [ 0 ] ,
885
887
data : '0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0' ,
886
888
origin : 'https://metamask.github.io' ,
887
889
} ;
890
+ // @ts -expect-error this value should never be undefined in this specific context.
888
891
const result = await keyringController . signMessage ( inputParams ) ;
889
892
expect ( result ) . toMatchInlineSnapshot (
890
893
`"0x93e0035090e8144debae03f45c5339a78d24c41e38e810a82dd3387e48353db645bd77716f3b7c4fb1f07f3b97bdbd33b0d7c55f7e7eedf3a678a2081948b67f1c"` ,
@@ -893,10 +896,11 @@ describe('KeyringController', () => {
893
896
894
897
it ( 'signPersonalMessage' , async ( ) => {
895
898
const inputParams = {
896
- from : walletOneAddresses [ 0 ] as Hex ,
899
+ from : walletOneAddresses [ 0 ] ,
897
900
data : '0x4578616d706c652060706572736f6e616c5f7369676e60206d657373616765' ,
898
901
origin : 'https://metamask.github.io' ,
899
902
} ;
903
+ // @ts -expect-error this value should never be undefined in this specific context.
900
904
const result = await keyringController . signPersonalMessage ( inputParams ) ;
901
905
expect ( result ) . toBe (
902
906
'0xfa2e5989b483e1f40a41b306f275b0009bcc07bfe5322c87682145e7d4889a3247182b4bd8138a965a7e37dea9d9b492b6f9f6d01185412f2d80466237b2805e1b' ,
@@ -905,7 +909,8 @@ describe('KeyringController', () => {
905
909
906
910
it ( 'getEncryptionPublicKey' , async ( ) => {
907
911
const result = await keyringController . getEncryptionPublicKey (
908
- walletOneAddresses [ 0 ] as Hex ,
912
+ // @ts -expect-error this value should never be undefined in this specific context.
913
+ walletOneAddresses [ 0 ] ,
909
914
) ;
910
915
expect ( result ) . toBe ( 'SR6bQ1m3OTHvI1FLwcGzm+Uk6hffoFPxsQ0DTOeKMEc=' ) ;
911
916
} ) ;
0 commit comments