diff --git a/src/utils/input/index.js b/src/utils/input/index.js index f107a726..6c263ccb 100644 --- a/src/utils/input/index.js +++ b/src/utils/input/index.js @@ -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 => @@ -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, { diff --git a/test/utils/input/index.js b/test/utils/input/index.js index 971aed5b..34e7188e 100644 --- a/test/utils/input/index.js +++ b/test/utils/input/index.js @@ -46,6 +46,7 @@ describe('input utils', () => { data: null, }); sandbox.stub(inputUtils, 'getPassphrase'); + sandbox.stub(console, 'warn').returns(''); return sandbox.stub(inputUtils, 'getData'); }); @@ -80,6 +81,17 @@ 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); + return expect(console.warn).to.be.calledWithExactly( + 'Warning: Passphrase contains 2 words instead of expected 12. ', + ); + }); }); describe('get secondPassphrase', async () => { @@ -116,6 +128,17 @@ 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); + return expect(console.warn).to.be.calledWithExactly( + 'Warning: Passphrase contains 2 words instead of expected 12. ', + ); + }); }); describe('get password', async () => {