Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ class KeyringController extends EventEmitter {
});
}

/**
* Submit Password
*
* Attempts to decrypt the current vault and load its keyrings
* into memory.
*
* @emits KeyringController#unlock
* @param {string} key - The decrypted vault key
* @returns {Promise<Object>} A Promise that resolves to the state.
*/
async submitDecryptedKey(key) {
this.keyrings = await this.unlockKeyringsByDecryptedKey(key);
this.setUnlocked();
return this.fullUpdate();
}

/**
* Verify Password
*
Expand Down Expand Up @@ -592,13 +608,50 @@ class KeyringController extends EventEmitter {
}

await this.clearKeyrings();
const vault = await this.encryptor.decrypt(password, encryptedVault);
const { result: vault, key } = await this.encryptor.decrypt(password, encryptedVault);
this.password = password;
this.decryptedKey = key;

await Promise.all(vault.map(this._restoreKeyring.bind(this)));
await this._updateMemStoreKeyrings();
return this.keyrings;
}

/**
* Unlock Keyrings By Decrypted Key
*
* Attempts to unlock the persisted encrypted storage,
* initializing the persisted keyrings to RAM.
*
* @param {string} key - The decrypted key
* @returns {Promise<Array<Keyring>>} The keyrings.
*/
async unlockKeyringsByDecryptedKey(key) {
const encryptedVault = this.store.getState().vault;
if (!encryptedVault) {
throw new Error('Cannot unlock without a previous vault.');
}

await this.clearKeyrings();
const { result: vault, key: decryptedKey } = await this.encryptor.decryptWithKey(key, encryptedVault);
this.decryptedKey = decryptedKey;

await Promise.all(vault.map(this._restoreKeyring.bind(this)));
await this._updateMemStoreKeyrings();
return this.keyrings;
}

/**
* Get Decrypted Key
*
* Provides the decypted key to the caller
*
* @returns {string} The decrypted key
*/
getDecryptedKey() {
return this.decryptedKey;
}

/**
* Restore Keyring
*
Expand Down