Skip to content

Commit a36d3af

Browse files
committed
add method to export vault encryption key
1 parent 514a984 commit a36d3af

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/keyring-controller/src/KeyringController.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,20 @@ function assertIsExportableKeyEncryptor(
527527
}
528528
}
529529

530+
/**
531+
* Assert that the encryption key is set.
532+
*
533+
* @param encryptionKey - The encryption key to check.
534+
* @throws If the encryption key is not set.
535+
*/
536+
function assertIsEncryptionKeySet(
537+
encryptionKey: string | undefined,
538+
): asserts encryptionKey is string {
539+
if (!encryptionKey) {
540+
throw new Error(KeyringControllerError.EncryptionKeyNotSet);
541+
}
542+
}
543+
530544
/**
531545
* Assert that the provided password is a valid non-empty string.
532546
*
@@ -1471,6 +1485,16 @@ export class KeyringController extends BaseController<
14711485
}
14721486
}
14731487

1488+
/**
1489+
* Exports the vault encryption key.
1490+
*
1491+
* @returns The vault encryption key.
1492+
*/
1493+
async exportEncryptionKey(): Promise<string> {
1494+
assertIsEncryptionKeySet(this.state.encryptionKey);
1495+
return this.state.encryptionKey;
1496+
}
1497+
14741498
/**
14751499
* Attempts to decrypt the current vault and load its keyrings,
14761500
* using the given password.

packages/keyring-controller/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export enum KeyringControllerError {
44
UnsafeDirectKeyringAccess = 'KeyringController - Returning keyring instances is unsafe',
55
WrongPasswordType = 'KeyringController - Password must be of type string.',
66
InvalidEmptyPassword = 'KeyringController - Password cannot be empty.',
7+
EncryptionKeyNotSet = 'KeyringController - Encryption key not set.',
78
NoFirstAccount = 'KeyringController - First Account not found.',
89
DuplicatedAccount = 'KeyringController - The account you are trying to import is a duplicate',
910
VaultError = 'KeyringController - Cannot unlock without a previous vault.',

0 commit comments

Comments
 (0)