Skip to content

Commit 9f8b301

Browse files
committed
add key derivation function
1 parent 4b3465a commit 9f8b301

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

packages/keyring-controller/src/KeyringController.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,18 @@ describe('KeyringController', () => {
816816
);
817817
});
818818

819+
it('should create new vault with encryption key from key seed', async () => {
820+
await withController(
821+
{ cacheEncryptionKey: true, skipVaultCreation: true },
822+
async ({ controller }) => {
823+
const key = await controller.deriveKeyFromSeed(password);
824+
await controller.createNewVaultAndKeychain(password, key);
825+
expect(controller.state.encryptionKey).toBeDefined();
826+
expect(controller.state.encryptedEncryptionKey).toBeDefined();
827+
},
828+
);
829+
});
830+
819831
it('should throw error if creating new vault with encryption key and cacheEncryptionKey is false', async () => {
820832
await withController(
821833
{ cacheEncryptionKey: false, skipVaultCreation: true },

packages/keyring-controller/src/KeyringController.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,21 @@ export class KeyringController extends BaseController<
809809
});
810810
}
811811

812+
/**
813+
* Derives an encryption key from a key seed.
814+
*
815+
* @param keySeed - The key seed to derive the key from.
816+
* @returns Promise resolving to the derived encryption key.
817+
*/
818+
async deriveKeyFromSeed(keySeed: string): Promise<string> {
819+
assertIsExportableKeyEncryptor(this.#encryptor);
820+
const { exportedKeyString } = await this.#encryptor.encryptWithDetail(
821+
keySeed,
822+
null, // dummy value, we just want the key
823+
);
824+
return exportedKeyString;
825+
}
826+
812827
/**
813828
* Effectively the same as creating a new keychain then populating it
814829
* using the given seed phrase.

0 commit comments

Comments
 (0)