Skip to content

Commit

Permalink
Add method to validate keyring password (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
gantunesr authored Aug 15, 2022
1 parent eaab028 commit 9b70924
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/keyring/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ describe('KeyringController', () => {
expect(seedPhrase).toBeDefined();
});

it('should validate password as true if it matches with input', () => {
expect(keyringController.validatePassword(password)).toBe(true);
});

it('should validate password as false if it does not match with input', () => {
expect(keyringController.validatePassword('12341234')).toBe(false);
});

describe('QR keyring', () => {
const composeMockSignature = (
requestId: string,
Expand Down
14 changes: 12 additions & 2 deletions src/keyring/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ export class KeyringController extends BaseController<
}
}

/**
* Method to validate a password against the password from the keyring.
*
* @param password - Password of the keyring.
* @returns Boolean indicating if input password is valid
*/
validatePassword(password: string): boolean {
return this.#keyring.password === password;
}

/**
* Returns the status of the vault.
*
Expand All @@ -298,7 +308,7 @@ export class KeyringController extends BaseController<
* @returns Promise resolving to the seed phrase.
*/
exportSeedPhrase(password: string) {
if (this.#keyring.password === password) {
if (this.validatePassword(password)) {
return this.#keyring.keyrings[0].mnemonic;
}
throw new Error('Invalid password');
Expand All @@ -312,7 +322,7 @@ export class KeyringController extends BaseController<
* @returns Promise resolving to the private key for an address.
*/
exportAccount(password: string, address: string): Promise<string> {
if (this.#keyring.password === password) {
if (this.validatePassword(password)) {
return this.#keyring.exportAccount(address);
}
throw new Error('Invalid password');
Expand Down

0 comments on commit 9b70924

Please sign in to comment.