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

Commit

Permalink
♻️ Fix updates from other PR and update the format
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Aug 2, 2018
1 parent d589967 commit c04cbed
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 67 deletions.
19 changes: 12 additions & 7 deletions src/commands/passphrase/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*
*/
import { flags as flagParser } from '@oclif/command';
import { cryptography } from 'lisk-elements';
import BaseCommand from '../../base';
import cryptography from '../../utils/cryptography';
import { ValidationError } from '../../utils/error';
import commonOptions from '../../utils/options';
import commonFlags from '../../utils/flags';
import getInputsFromSources, {
getFirstLineFromString,
} from '../../utils/input';
Expand All @@ -31,11 +31,16 @@ const passphraseOptionDescription = `Specifies a source for providing an encrypt
- --passphrase stdin (takes the first line only)
`;

const processInputs = encryptedPassphrase => ({ password, data }) =>
cryptography.decryptPassphrase({
encryptedPassphrase: encryptedPassphrase || getFirstLineFromString(data),
const processInputs = encryptedPassphrase => ({ password, data }) => {
const encryptedPassphraseObject = cryptography.parseEncryptedPassphrase(
encryptedPassphrase || getFirstLineFromString(data),
);
const passphrase = cryptography.decryptPassphraseWithPassword(
encryptedPassphraseObject,
password,
});
);
return { passphrase };
};

export default class DecryptCommand extends BaseCommand {
async run() {
Expand Down Expand Up @@ -71,7 +76,7 @@ DecryptCommand.args = [

DecryptCommand.flags = {
...BaseCommand.flags,
password: flagParser.string(commonOptions.password),
password: flagParser.string(commonFlags.password),
passphrase: flagParser.string({
description: passphraseOptionDescription,
}),
Expand Down
17 changes: 12 additions & 5 deletions src/commands/passphrase/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@
*
*/
import { flags as flagParser } from '@oclif/command';
import { cryptography } from 'lisk-elements';
import BaseCommand from '../../base';
import commonOptions from '../../utils/options';
import commonFlags from '../../utils/flags';
import getInputsFromSources from '../../utils/input';
import cryptography from '../../utils/cryptography';

const outputPublicKeyOptionDescription =
'Includes the public key in the output. This option is provided for the convenience of node operators.';

const processInputs = outputPublicKey => ({ passphrase, password }) => {
const cipherAndIv = cryptography.encryptPassphrase({ passphrase, password });
const encryptedPassphraseObject = cryptography.encryptPassphraseWithPassword(
passphrase,
password,
);
const encryptedPassphrase = cryptography.stringifyEncryptedPassphrase(
encryptedPassphraseObject,
);
const cipherAndIv = { encryptedPassphrase };
return outputPublicKey
? Object.assign({}, cipherAndIv, {
publicKey: cryptography.getKeys(passphrase).publicKey,
Expand Down Expand Up @@ -57,8 +64,8 @@ export default class EncryptCommand extends BaseCommand {

EncryptCommand.flags = {
...BaseCommand.flags,
password: flagParser.string(commonOptions.password),
passphrase: flagParser.string(commonOptions.passphrase),
password: flagParser.string(commonFlags.password),
passphrase: flagParser.string(commonFlags.passphrase),
outputPublicKey: flagParser.boolean({
description: outputPublicKeyOptionDescription,
}),
Expand Down
78 changes: 51 additions & 27 deletions test/commands/passphrase/decrypt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,22 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import { expect, test } from '../../test';
import { expect, test } from '@oclif/test';
import { cryptography } from 'lisk-elements';
import * as config from '../../../src/utils/config';
import * as print from '../../../src/utils/print';
import cryptography from '../../../src/utils/cryptography';
import * as getInputsFromSources from '../../../src/utils/input';

describe('passphrase:decrypt', () => {
const defaultConfig = {
api: {
network: 'main',
nodes: ['http://localhost:4000'],
},
};

const defaultEncryptedPassphrase =
'salt=d3887df959ed2bfe5961a6831da6e177&cipherText=1c08a1&iv=096ede534df9092fd4523ec7&tag=2a055e1c860b3ef76084a6c9aca68ce9&version=1';
const passphrase = {
passphrase: '123',
const passphrase = '123';
const encryptedPassphraseObject = {
salt: 'salt',
cipherText: 'cipherText',
iv: 'iv',
tag: 'tag',
version: 1,
};
const defaultInputs = {
password: '456',
Expand All @@ -40,8 +38,17 @@ describe('passphrase:decrypt', () => {
const printMethodStub = sandbox.stub();
const setupStub = test
.stub(print, 'default', sandbox.stub().returns(printMethodStub))
.stub(config, 'getConfig', sandbox.stub().returns(defaultConfig))
.stub(cryptography, 'decryptPassphrase', sandbox.stub().returns(passphrase))
.stub(config, 'getConfig', sandbox.stub().returns({}))
.stub(
cryptography,
'parseEncryptedPassphrase',
sandbox.stub().returns(encryptedPassphraseObject),
)
.stub(
cryptography,
'decryptPassphraseWithPassword',
sandbox.stub().returns(passphrase),
)
.stub(
getInputsFromSources,
'default',
Expand Down Expand Up @@ -71,11 +78,16 @@ describe('passphrase:decrypt', () => {
},
data: null,
});
expect(cryptography.decryptPassphrase).to.be.calledWithExactly({
encryptedPassphrase: defaultEncryptedPassphrase,
password: defaultInputs.password,
});
return expect(printMethodStub).to.be.calledWithExactly(passphrase);
expect(cryptography.parseEncryptedPassphrase).to.be.calledWithExactly(
defaultEncryptedPassphrase,
);
expect(
cryptography.decryptPassphraseWithPassword,
).to.be.calledWithExactly(
encryptedPassphraseObject,
defaultInputs.password,
);
return expect(printMethodStub).to.be.calledWithExactly({ passphrase });
});
});

Expand All @@ -93,11 +105,16 @@ describe('passphrase:decrypt', () => {
source: passphraseSource,
},
});
expect(cryptography.decryptPassphrase).to.be.calledWithExactly({
encryptedPassphrase: defaultEncryptedPassphrase,
password: defaultInputs.password,
});
return expect(printMethodStub).to.be.calledWithExactly(passphrase);
expect(cryptography.parseEncryptedPassphrase).to.be.calledWithExactly(
defaultEncryptedPassphrase,
);
expect(
cryptography.decryptPassphraseWithPassword,
).to.be.calledWithExactly(
encryptedPassphraseObject,
defaultInputs.password,
);
return expect(printMethodStub).to.be.calledWithExactly({ passphrase });
});
});

Expand All @@ -121,11 +138,18 @@ describe('passphrase:decrypt', () => {
source: passphraseSource,
},
});
expect(cryptography.decryptPassphrase).to.be.calledWithExactly({
encryptedPassphrase: defaultEncryptedPassphrase,
password: defaultInputs.password,
expect(cryptography.parseEncryptedPassphrase).to.be.calledWithExactly(
defaultEncryptedPassphrase,
);
expect(
cryptography.decryptPassphraseWithPassword,
).to.be.calledWithExactly(
encryptedPassphraseObject,
defaultInputs.password,
);
return expect(printMethodStub).to.be.calledWithExactly({
passphrase,
});
return expect(printMethodStub).to.be.calledWithExactly(passphrase);
},
);
});
Expand Down
91 changes: 63 additions & 28 deletions test/commands/passphrase/encrypt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import { expect, test } from '../../test';
import { expect, test } from '@oclif/test';
import { cryptography } from 'lisk-elements';
import * as config from '../../../src/utils/config';
import * as print from '../../../src/utils/print';
import cryptography from '../../../src/utils/cryptography';
import * as getInputsFromSources from '../../../src/utils/input';

describe('passphrase:encrypt', () => {
const defaultEncryptedPassphrase = {
encryptedPassphrase:
'salt=683425ca06c9ff88a5ab292bb5066dc5&cipherText=4ce151&iv=bfaeef79a466e370e210f3c6&tag=e84bf097b1ec5ae428dd7ed3b4cce522&version=1',
};
const encryptedPassphraseString =
'salt=683425ca06c9ff88a5ab292bb5066dc5&cipherText=4ce151&iv=bfaeef79a466e370e210f3c6&tag=e84bf097b1ec5ae428dd7ed3b4cce522&version=1';
const defaultKeys = {
publicKey:
'a4465fd76c16fcc458448076372abf1912cc5b150663a64dffefe550f96feadd',
};
const encryptedPassphraseObject = {
salt: 'salt',
cipherText: 'cipherText',
iv: 'iv',
tag: 'tag',
version: 1,
};
const defaultInputs = {
passphrase: '123',
password: '456',
Expand All @@ -40,8 +45,13 @@ describe('passphrase:encrypt', () => {
.stub(cryptography, 'getKey', sandbox.stub().returns(defaultKeys))
.stub(
cryptography,
'encryptPassphrase',
sandbox.stub().returns(defaultEncryptedPassphrase),
'encryptPassphraseWithPassword',
sandbox.stub().returns(encryptedPassphraseObject),
)
.stub(
cryptography,
'stringifyEncryptedPassphrase',
sandbox.stub().returns(encryptedPassphraseString),
)
.stub(
getInputsFromSources,
Expand All @@ -54,9 +64,15 @@ describe('passphrase:encrypt', () => {
.stdout()
.command(['passphrase:encrypt'])
.it('should encrypt passphrase', () => {
expect(cryptography.encryptPassphrase).to.be.calledWithExactly(
defaultInputs,
expect(
cryptography.encryptPassphraseWithPassword,
).to.be.calledWithExactly(
defaultInputs.passphrase,
defaultInputs.password,
);
expect(
cryptography.stringifyEncryptedPassphrase,
).to.be.calledWithExactly(encryptedPassphraseObject);
expect(getInputsFromSources.default).to.be.calledWithExactly({
passphrase: {
source: undefined,
Expand All @@ -67,9 +83,9 @@ describe('passphrase:encrypt', () => {
repeatPrompt: true,
},
});
return expect(printMethodStub).to.be.calledWithExactly(
defaultEncryptedPassphrase,
);
return expect(printMethodStub).to.be.calledWithExactly({
encryptedPassphrase: encryptedPassphraseString,
});
});
});

Expand All @@ -78,9 +94,15 @@ describe('passphrase:encrypt', () => {
.stdout()
.command(['passphrase:encrypt', '--outputPublicKey'])
.it('should encrypt passphrase and output public key', () => {
expect(cryptography.encryptPassphrase).to.be.calledWithExactly(
defaultInputs,
expect(
cryptography.encryptPassphraseWithPassword,
).to.be.calledWithExactly(
defaultInputs.passphrase,
defaultInputs.password,
);
expect(
cryptography.stringifyEncryptedPassphrase,
).to.be.calledWithExactly(encryptedPassphraseObject);
expect(getInputsFromSources.default).to.be.calledWithExactly({
passphrase: {
source: undefined,
Expand All @@ -91,9 +113,10 @@ describe('passphrase:encrypt', () => {
repeatPrompt: true,
},
});
return expect(printMethodStub).to.be.calledWithExactly(
Object.assign({}, defaultEncryptedPassphrase, defaultKeys),
);
return expect(printMethodStub).to.be.calledWithExactly({
encryptedPassphrase: encryptedPassphraseString,
...defaultKeys,
});
});
});

Expand All @@ -102,9 +125,15 @@ describe('passphrase:encrypt', () => {
.stdout()
.command(['passphrase:encrypt', '--passphrase=pass:123'])
.it('should call print with the user config', () => {
expect(cryptography.encryptPassphrase).to.be.calledWithExactly(
defaultInputs,
expect(
cryptography.encryptPassphraseWithPassword,
).to.be.calledWithExactly(
defaultInputs.passphrase,
defaultInputs.password,
);
expect(
cryptography.stringifyEncryptedPassphrase,
).to.be.calledWithExactly(encryptedPassphraseObject);
expect(getInputsFromSources.default).to.be.calledWithExactly({
passphrase: {
source: 'pass:123',
Expand All @@ -115,9 +144,9 @@ describe('passphrase:encrypt', () => {
repeatPrompt: true,
},
});
return expect(printMethodStub).to.be.calledWithExactly(
defaultEncryptedPassphrase,
);
return expect(printMethodStub).to.be.calledWithExactly({
encryptedPassphrase: encryptedPassphraseString,
});
});
});

Expand All @@ -132,9 +161,15 @@ describe('passphrase:encrypt', () => {
.it(
'should encrypt passphrase from passphrase and password flags',
() => {
expect(cryptography.encryptPassphrase).to.be.calledWithExactly(
defaultInputs,
expect(
cryptography.encryptPassphraseWithPassword,
).to.be.calledWithExactly(
defaultInputs.passphrase,
defaultInputs.password,
);
expect(
cryptography.stringifyEncryptedPassphrase,
).to.be.calledWithExactly(encryptedPassphraseObject);
expect(getInputsFromSources.default).to.be.calledWithExactly({
passphrase: {
source: 'pass:123',
Expand All @@ -145,9 +180,9 @@ describe('passphrase:encrypt', () => {
repeatPrompt: true,
},
});
return expect(printMethodStub).to.be.calledWithExactly(
defaultEncryptedPassphrase,
);
return expect(printMethodStub).to.be.calledWithExactly({
encryptedPassphrase: encryptedPassphraseString,
});
},
);
});
Expand Down

0 comments on commit c04cbed

Please sign in to comment.