diff --git a/packages/keyring-controller/src/KeyringController.test.ts b/packages/keyring-controller/src/KeyringController.test.ts index 2f05c1ee4ef..2ab4f35f470 100644 --- a/packages/keyring-controller/src/KeyringController.test.ts +++ b/packages/keyring-controller/src/KeyringController.test.ts @@ -10,7 +10,7 @@ import { TransactionFactory } from '@ethereumjs/tx'; import { MetaMaskKeyring as QRKeyring } from '@keystonehq/metamask-airgapped-keyring'; import { CryptoHDKey, ETHSignature } from '@keystonehq/bc-ur-registry-eth'; import * as uuid from 'uuid'; -import { NetworkType } from '@metamask/controller-utils'; +import { isValidHexAddress, NetworkType } from '@metamask/controller-utils'; import { keyringBuilderFactory } from '@metamask/eth-keyring-controller'; import MockEncryptor from '../tests/mocks/mockEncryptor'; import { @@ -217,20 +217,46 @@ describe('KeyringController', () => { }); describe('createNewVaultAndKeychain', () => { - it('should create new vault, mnemonic and keychain', async () => { - const initialSeedWord = await keyringController.exportSeedPhrase( - password, - ); - expect(initialSeedWord).toBeDefined(); - const currentState = await keyringController.createNewVaultAndKeychain( - password, - ); - expect(initialState).not.toBe(currentState); - const currentSeedWord = await keyringController.exportSeedPhrase( - password, - ); - expect(currentSeedWord).toBeDefined(); - expect(initialSeedWord).not.toBe(currentSeedWord); + describe('when there is no existing vault', () => { + it('should create new vault, mnemonic and keychain', async () => { + const cleanKeyringController = new KeyringController( + preferences, + baseConfig, + ); + const initialSeedWord = await keyringController.exportSeedPhrase( + password, + ); + const currentState = + await cleanKeyringController.createNewVaultAndKeychain(password); + const currentSeedWord = await cleanKeyringController.exportSeedPhrase( + password, + ); + expect(initialSeedWord).toBeDefined(); + expect(initialState).not.toBe(currentState); + expect(currentSeedWord).toBeDefined(); + expect(initialSeedWord).not.toBe(currentSeedWord); + expect(isValidHexAddress(currentState.keyrings[0].accounts[0])).toBe( + true, + ); + }); + }); + + describe('when there is an existing vault', () => { + it('should return existing vault', async () => { + const initialSeedWord = await keyringController.exportSeedPhrase( + password, + ); + const currentState = await keyringController.createNewVaultAndKeychain( + password, + ); + const currentSeedWord = await keyringController.exportSeedPhrase( + password, + ); + expect(initialSeedWord).toBeDefined(); + expect(initialState).toBe(currentState); + expect(currentSeedWord).toBeDefined(); + expect(initialSeedWord).toBe(currentSeedWord); + }); }); }); diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index db64868587a..f5622721fe7 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -305,9 +305,16 @@ export class KeyringController extends BaseController< async createNewVaultAndKeychain(password: string) { const releaseLock = await this.mutex.acquire(); try { - const vault = await this.#keyring.createNewVaultAndKeychain(password); - this.updateIdentities(await this.#keyring.getAccounts()); - this.fullUpdate(); + let vault; + const accounts = await this.getAccounts(); + if (accounts.length > 0) { + vault = await this.fullUpdate(); + } else { + vault = await this.#keyring.createNewVaultAndKeychain(password); + this.updateIdentities(await this.getAccounts()); + this.fullUpdate(); + } + return vault; } finally { releaseLock();