Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Display warning for passphrase format - Closes #337 #644

Merged
merged 4 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions src/utils/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import { passphrase as passphraseModule } from 'lisk-elements';
import { getStdIn, getPassphrase, getData } from './utils';

export const getFirstLineFromString = multilineString =>
Expand Down Expand Up @@ -57,6 +58,29 @@ const getInputsFromSources = async ({
})
: stdIn.secondPassphrase || null;

const passphraseErrors = [passphrase, secondPassphrase]
.filter(Boolean)
.map(pass =>
passphraseModule.validation
.getPassphraseValidationErrors(pass)
.filter(error => error.message),
);

passphraseErrors.forEach(errors => {
if (errors.length > 0) {
const passphraseWarning = errors
.filter(error => error.code !== 'INVALID_MNEMONIC')
.reduce(
(accumulator, error) =>
accumulator.concat(
`${error.message.replace(' Please check the passphrase.', '')} `,
),
'Warning: ',
);
console.warn(passphraseWarning);
}
});

const password =
typeof stdIn.password !== 'string' && passwordInput
? await getPassphrase(passwordInput.source, {
Expand Down
27 changes: 27 additions & 0 deletions test/utils/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('input utils', () => {
data: null,
});
sandbox.stub(inputUtils, 'getPassphrase');
sandbox.stub(console, 'warn').returns('');
return sandbox.stub(inputUtils, 'getData');
});

Expand Down Expand Up @@ -80,6 +81,19 @@ describe('input utils', () => {
expect(inputUtils.getPassphrase).not.to.be.called;
return expect(result.passphrase).to.be.null;
});

it('should print a warning if passphase not in mnemonic format', async () => {
const stdin = 'some passphrase';
inputUtils.getStdIn.resolves({
passphrase: stdin,
});
await getInputsFromSources(inputs);
expect(
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
console.warn.calledWith(
'Warning: Passphrase contains 2 words instead of expected 12. ',
),
).to.be.true;
});
});

describe('get secondPassphrase', async () => {
Expand Down Expand Up @@ -116,6 +130,19 @@ describe('input utils', () => {
expect(inputUtils.getPassphrase).not.to.be.called;
return expect(result.secondPassphrase).to.be.null;
});

it('should print a warning if secondPassphase not in mnemonic format', async () => {
const stdin = 'some passphrase';
inputUtils.getStdIn.resolves({
secondPassphrase: stdin,
});
await getInputsFromSources(inputs);
expect(
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
console.warn.calledWith(
'Warning: Passphrase contains 2 words instead of expected 12. ',
),
).to.be.true;
});
});

describe('get password', async () => {
Expand Down