@@ -627,9 +627,9 @@ describe('TrezorKeyringV2', () => {
627627 expect ( account . options . entropy . derivationPath ) . toBe ( `m/44'/60'/0'/0` ) ;
628628 } ) ;
629629
630- it ( 'clears registry when switching HD paths' , async ( ) => {
631- // Create wrapper with account on BIP44 path
632- const inner = createInnerKeyring ( ) ;
630+ it ( 'clears registry when switching HD paths via createAccounts ' , async ( ) => {
631+ // Create wrapper with account on BIP44 standard path
632+ const inner = createInnerKeyring ( ) ; // Uses m/44'/60'/0'/0 by default
633633 inner . setAccountToUnlock ( 0 ) ;
634634 await inner . addAccounts ( 1 ) ;
635635
@@ -643,21 +643,60 @@ describe('TrezorKeyringV2', () => {
643643 expect ( initialAccounts ) . toHaveLength ( 1 ) ;
644644 const initialAccountId = getFirstAccount ( initialAccounts ) . id ;
645645
646- // Switch to legacy MEW path and add account
647- // This simulates what createAccounts does when path changes
648- inner . setHdPath ( `m/44'/60'/0'` ) ;
649- inner . hdk = fakeHdKey ; // Reset HDKey after setHdPath clears it
650- inner . setAccountToUnlock ( 0 ) ;
651- await inner . addAccounts ( 1 ) ;
646+ // Spy on registry.clear to verify it's called when path changes
647+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
648+ const clearSpy = jest . spyOn ( ( wrapper as any ) . registry , 'clear' ) ;
649+
650+ // inner.hdPath is m/44'/60'/0'/0
651+ // Call createAccounts with legacy MEW path (m/44'/60'/0') - this should trigger path change
652+ // We need to reset the HDKey after setHdPath clears it inside createAccounts
653+ const originalSetHdPath = inner . setHdPath . bind ( inner ) ;
654+ jest . spyOn ( inner , 'setHdPath' ) . mockImplementation ( ( path ) => {
655+ originalSetHdPath ( path ) ;
656+ inner . hdk = fakeHdKey ;
657+ } ) ;
658+
659+ await wrapper . createAccounts ( derivePathOptions ( `m/44'/60'/0'/0` ) ) ;
652660
653- // Call createAccounts with the new path - this should clear registry
654- await wrapper . createAccounts ( derivePathOptions ( `m/44'/60'/0'/1` ) ) ;
661+ // Verify registry. clear was called
662+ expect ( clearSpy ) . toHaveBeenCalled ( ) ;
655663
656- // The old account should no longer be accessible
664+ // The old account should no longer be accessible because registry was cleared
657665 await expect ( wrapper . getAccount ( initialAccountId ) ) . rejects . toThrow (
658666 / A c c o u n t n o t f o u n d / u,
659667 ) ;
660668 } ) ;
669+
670+ it ( 'does not clear registry when using same HD path' , async ( ) => {
671+ // Create wrapper with account on BIP44 standard path
672+ const inner = createInnerKeyring ( ) ;
673+ inner . setAccountToUnlock ( 0 ) ;
674+ await inner . addAccounts ( 1 ) ;
675+
676+ const wrapper = new TrezorKeyringV2 ( {
677+ legacyKeyring : inner ,
678+ entropySource,
679+ } ) ;
680+
681+ // Get initial accounts - this populates the registry
682+ const initialAccounts = await wrapper . getAccounts ( ) ;
683+ expect ( initialAccounts ) . toHaveLength ( 1 ) ;
684+ const initialAccountId = getFirstAccount ( initialAccounts ) . id ;
685+
686+ // Spy on registry.clear
687+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
688+ const clearSpy = jest . spyOn ( ( wrapper as any ) . registry , 'clear' ) ;
689+
690+ // Call createAccounts with same HD path base (m/44'/60'/0'/0) but different index
691+ await wrapper . createAccounts ( derivePathOptions ( `m/44'/60'/0'/0/1` ) ) ;
692+
693+ // Verify registry.clear was NOT called
694+ expect ( clearSpy ) . not . toHaveBeenCalled ( ) ;
695+
696+ // The original account should still be accessible
697+ const account = await wrapper . getAccount ( initialAccountId ) ;
698+ expect ( account ) . toBeDefined ( ) ;
699+ } ) ;
661700 } ) ;
662701
663702 describe ( 'locked device handling' , ( ) => {
0 commit comments