@@ -12,6 +12,7 @@ import {
1212 recoverEIP7702Authorization ,
1313} from '@metamask/eth-sig-util' ;
1414import SimpleKeyring from '@metamask/eth-simple-keyring' ;
15+ import { KeyringType } from '@metamask/keyring-api' ;
1516import type { EthKeyring } from '@metamask/keyring-internal-api' ;
1617import type { KeyringClass } from '@metamask/keyring-utils' ;
1718import { MOCK_ANY_NAMESPACE , Messenger } from '@metamask/messenger' ;
@@ -4192,10 +4193,7 @@ describe('KeyringController', () => {
41924193
41934194 const fn = jest . fn ( ) ;
41944195 await expect (
4195- controller . withKeyringV2 (
4196- { type : MockShallowKeyring . type } ,
4197- fn ,
4198- ) ,
4196+ controller . withKeyringV2 ( { type : MockShallowKeyring . type } , fn ) ,
41994197 ) . rejects . toThrow (
42004198 KeyringControllerErrorMessage . KeyringV2NotSupported ,
42014199 ) ;
@@ -4223,10 +4221,7 @@ describe('KeyringController', () => {
42234221 await controller . setLocked ( ) ;
42244222
42254223 await expect (
4226- controller . withKeyringV2 (
4227- { type : KeyringTypes . hd } ,
4228- jest . fn ( ) ,
4229- ) ,
4224+ controller . withKeyringV2 ( { type : KeyringTypes . hd } , jest . fn ( ) ) ,
42304225 ) . rejects . toThrow ( KeyringControllerErrorMessage . ControllerLocked ) ;
42314226 } ) ;
42324227 } ) ;
@@ -4268,21 +4263,21 @@ describe('KeyringController', () => {
42684263 it ( 'should throw KeyringNotFound if no keyring has the id' , async ( ) => {
42694264 await withController ( async ( { controller } ) => {
42704265 await expect (
4271- controller . withKeyringV2 (
4272- { id : 'non-existent-id' } ,
4273- jest . fn ( ) ,
4274- ) ,
4266+ controller . withKeyringV2 ( { id : 'non-existent-id' } , jest . fn ( ) ) ,
42754267 ) . rejects . toThrow ( KeyringControllerErrorMessage . KeyringNotFound ) ;
42764268 } ) ;
42774269 } ) ;
42784270 } ) ;
42794271
42804272 describe ( 'when the keyring is selected by filter' , ( ) => {
4281- it ( 'should wrap the V1 keyring that matches the filter' , async ( ) => {
4273+ it ( 'should use the V2 keyring instance that matches the filter' , async ( ) => {
42824274 await withController ( async ( { controller } ) => {
42834275 const fn = jest . fn ( ) ;
42844276 await controller . withKeyringV2 (
4285- { filter : ( k ) : boolean => k . type === KeyringTypes . hd } ,
4277+ {
4278+ filter : ( k ) : boolean =>
4279+ k . type === KeyringType . Hd /* New V2 enum */ ,
4280+ } ,
42864281 fn ,
42874282 ) ;
42884283
@@ -4295,6 +4290,29 @@ describe('KeyringController', () => {
42954290 } ) ;
42964291 } ) ;
42974292
4293+ it ( 'should skip instances that do not support v2' , async ( ) => {
4294+ await withController (
4295+ {
4296+ keyringBuilders : [
4297+ keyringBuilderFactory ( MockKeyring ) ,
4298+ ] /* No V2 support for this type */ ,
4299+ } ,
4300+ async ( { controller } ) => {
4301+ await controller . addNewKeyring ( MockKeyring . type ) ;
4302+
4303+ // 1. The HD keyring that supports V2, so we explicitly skip it.
4304+ // 2. The mock keyring that we want to filter, but that will get implicitly skipped because it doesn't support V2.
4305+ const filter = jest
4306+ . fn ( )
4307+ . mockReturnValueOnce ( false )
4308+ . mockReturnValueOnce ( true ) ;
4309+
4310+ const fn = jest . fn ( ) ;
4311+ await expect ( controller . withKeyringV2 ( { filter } , fn ) ) . rejects . toThrow ( KeyringControllerErrorMessage . KeyringNotFound ) ;
4312+ } ,
4313+ ) ;
4314+ } ) ;
4315+
42984316 it ( 'should throw KeyringNotFound if no keyring matches the filter' , async ( ) => {
42994317 await withController ( async ( { controller } ) => {
43004318 await expect (
@@ -4311,12 +4329,9 @@ describe('KeyringController', () => {
43114329 it ( 'should rollback the underlying V1 keyring if the operation throws' , async ( ) => {
43124330 await withController ( async ( { controller, initialState } ) => {
43134331 await expect (
4314- controller . withKeyringV2 (
4315- { type : KeyringTypes . hd } ,
4316- async ( ) => {
4317- throw new Error ( 'Rollback test' ) ;
4318- } ,
4319- ) ,
4332+ controller . withKeyringV2 ( { type : KeyringTypes . hd } , async ( ) => {
4333+ throw new Error ( 'Rollback test' ) ;
4334+ } ) ,
43204335 ) . rejects . toThrow ( 'Rollback test' ) ;
43214336
43224337 expect ( controller . state . keyrings [ 0 ] . accounts ) . toStrictEqual (
0 commit comments