Skip to content

Commit

Permalink
Skip vault creation if one already exists (#1324)
Browse files Browse the repository at this point in the history
* feat:  don't create new vault if one already exists

* fix: use internal fullUpdate
  • Loading branch information
mikesposito authored and MajorLift committed Oct 11, 2023
1 parent 9874615 commit 1923c42
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
56 changes: 41 additions & 15 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
});
});
});

Expand Down
13 changes: 10 additions & 3 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1923c42

Please sign in to comment.