@@ -2739,10 +2739,12 @@ describe('SnapKeyring', () => {
27392739 }
27402740 } ) ;
27412741
2742- it ( 'throws an error when creating generic accounts if not allowed' , async ( ) => {
2742+ it ( 'skips and cleans up unsupported generic accounts when not allowed' , async ( ) => {
27432743 mockCallbacks . addAccount . mockClear ( ) ;
27442744 mockCallbacks . saveState . mockClear ( ) ;
27452745
2746+ mockMessenger . get . mockReturnValue ( snapMetadata ) ;
2747+
27462748 // Create a keyring with isAnyAccountTypeAllowed = false
27472749 const restrictedKeyring = new SnapKeyring ( {
27482750 messenger : mockSnapKeyringMessenger ,
@@ -2752,11 +2754,26 @@ describe('SnapKeyring', () => {
27522754
27532755 const genericAccount = {
27542756 ...newAccount1 ,
2757+ id : 'aa11bb22-cc33-4d44-8e55-ff6677889900' ,
2758+ address : '0xaabbccddee00112233445566778899aabbccddee' ,
27552759 type : AnyAccountType . Account ,
27562760 } ;
27572761
2762+ const supportedAccount = {
2763+ ...newAccount2 ,
2764+ id : 'bb11bb22-cc33-4d44-9e55-ff6677889900' ,
2765+ address : '0xbbccddee00112233445566778899aabbccddeeff' ,
2766+ type : EthAccountType . Eoa ,
2767+ } ;
2768+
2769+ const consoleSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ) ;
2770+
27582771 mockMessengerHandleRequest ( {
2759- [ KeyringRpcMethod . CreateAccounts ] : async ( ) => [ genericAccount ] ,
2772+ [ KeyringRpcMethod . CreateAccounts ] : async ( ) => [
2773+ genericAccount ,
2774+ supportedAccount ,
2775+ ] ,
2776+ [ KeyringRpcMethod . DeleteAccount ] : async ( ) => null ,
27602777 } ) ;
27612778
27622779 const options : CreateAccountOptions = {
@@ -2765,12 +2782,31 @@ describe('SnapKeyring', () => {
27652782 groupIndex : 0 ,
27662783 } ;
27672784
2768- await expect (
2769- restrictedKeyring . createAccounts ( snapId , options ) ,
2770- ) . rejects . toThrow ( `Cannot create generic account '${ genericAccount . id } '` ) ;
2785+ const result = await restrictedKeyring . createAccounts ( snapId , options ) ;
27712786
2772- // State should not be saved if validation fails
2773- expect ( mockCallbacks . saveState ) . not . toHaveBeenCalled ( ) ;
2787+ expect ( result ) . toHaveLength ( 1 ) ;
2788+ expect ( result ) . not . toContain ( genericAccount ) ;
2789+ expect ( result ) . toContain ( supportedAccount ) ;
2790+ expect (
2791+ restrictedKeyring . getAccountByAddress ( genericAccount . address ) ,
2792+ ) . toBeUndefined ( ) ;
2793+ expect (
2794+ restrictedKeyring . getAccountByAddress ( supportedAccount . address ) ,
2795+ ) . toBeDefined ( ) ;
2796+
2797+ expect ( consoleSpy ) . toHaveBeenCalledWith (
2798+ `SnapKeyring - Found an unsupported generic account: ${ genericAccount . id } ` ,
2799+ ) ;
2800+
2801+ expect ( mockMessenger . handleRequest ) . toHaveBeenCalledWith (
2802+ mockKeyringRpcRequest ( KeyringRpcMethod . DeleteAccount , {
2803+ id : genericAccount . id ,
2804+ } ) ,
2805+ ) ;
2806+
2807+ expect ( mockCallbacks . saveState ) . toHaveBeenCalled ( ) ;
2808+
2809+ consoleSpy . mockRestore ( ) ;
27742810 } ) ;
27752811 } ) ;
27762812
0 commit comments