Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit eb3b986

Browse files
committed
Set password sooner to avoid redundant persistance
The vault password is now always set in the method responsible for creating the vault. Previously it was set when the vault was persisted, or when the first keyring was created. This led to redundant calls to persist the keyrings, just to achieve the side-effect of setting the password. This change eliminates those redundant calls, and a few more that had no obvious purpose. Note that the password is also set on unlock, and that has not changed here.
1 parent bc5716f commit eb3b986

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

index.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ class KeyringController extends EventEmitter {
7878
* @returns {Promise<Object>} A Promise that resolves to the state.
7979
*/
8080
async createNewVaultAndKeychain(password) {
81-
await this.createFirstKeyTree(password);
82-
await this.persistAllKeyrings(password);
81+
this.password = password;
82+
83+
await this.createFirstKeyTree();
8384
this.setUnlocked();
8485
this.fullUpdate();
8586
}
@@ -116,9 +117,9 @@ class KeyringController extends EventEmitter {
116117
throw new Error('Seed phrase is invalid.');
117118
}
118119

119-
this.clearKeyrings();
120+
this.password = password;
120121

121-
await this.persistAllKeyrings(password);
122+
this.clearKeyrings();
122123
const firstKeyring = await this.addNewKeyring(
123124
KEYRINGS_TYPE_MAP.HD_KEYRING,
124125
{
@@ -130,8 +131,6 @@ class KeyringController extends EventEmitter {
130131
if (!firstAccount) {
131132
throw new Error('KeyringController - First Account not found.');
132133
}
133-
134-
await this.persistAllKeyrings(password);
135134
this.setUnlocked();
136135
return this.fullUpdate();
137136
}
@@ -487,11 +486,9 @@ class KeyringController extends EventEmitter {
487486
* - Faucets that account on testnet
488487
* - Puts the current seed words into the state tree
489488
*
490-
* @param {string} password - The keyring controller password.
491489
* @returns {Promise<void>} - A promise that resolves if the operation was successful.
492490
*/
493-
async createFirstKeyTree(password) {
494-
this.password = password;
491+
async createFirstKeyTree() {
495492
this.clearKeyrings();
496493

497494
const keyring = await this.addNewKeyring(KEYRINGS_TYPE_MAP.HD_KEYRING);
@@ -513,15 +510,9 @@ class KeyringController extends EventEmitter {
513510
* encrypts that array with the provided `password`,
514511
* and persists that encrypted string to storage.
515512
*
516-
* @param {string} password - The keyring controller password.
517513
* @returns {Promise<boolean>} Resolves to true once keyrings are persisted.
518514
*/
519-
async persistAllKeyrings(password = this.password) {
520-
if (typeof password !== 'string') {
521-
throw new Error('KeyringController - password is not a string');
522-
}
523-
524-
this.password = password;
515+
async persistAllKeyrings() {
525516
const serializedKeyrings = await Promise.all(
526517
this.keyrings.map(async (keyring) => {
527518
const [type, data] = await Promise.all([
@@ -752,7 +743,7 @@ class KeyringController extends EventEmitter {
752743
forgetKeyring(keyring) {
753744
if (keyring.forgetDevice) {
754745
keyring.forgetDevice();
755-
this.persistAllKeyrings.bind(this)();
746+
this.persistAllKeyrings();
756747
this._updateMemStoreKeyrings.bind(this)();
757748
} else {
758749
throw new Error(

0 commit comments

Comments
 (0)