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

Commit

Permalink
♻️ Update commands and specifications for input refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
willclarktech committed Nov 9, 2017
1 parent c13ee43 commit f7ceca3
Show file tree
Hide file tree
Showing 15 changed files with 434 additions and 706 deletions.
34 changes: 13 additions & 21 deletions src/commands/createTransactionCreateMultisignatureAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import {
getStdIn,
getPassphrase,
getFirstLineFromString,
} from '../utils/input';
import { createCommand } from '../utils/helpers';
import getInputsFromSources from '../utils/input';
import commonOptions from '../utils/options';
import transactions from '../utils/transactions';

Expand All @@ -33,7 +29,7 @@ const description = `Creates a transaction which will register a multisignature
`;

const createMultisignatureAccount = (lifetime, minimum, keysgroup) =>
([passphrase, secondPassphrase]) =>
({ passphrase, secondPassphrase }) =>
transactions.createMultisignature(
passphrase,
secondPassphrase,
Expand All @@ -51,8 +47,8 @@ export const actionCreator = vorpal => async ({ lifetime, minimum, keysgroup, op
const publicKeysWithPlus = keysgroup.map((publicKey) => {
try {
Buffer.from(publicKey, 'hex').toString('hex');
} catch (e) {
throw new Error(`${e} ${publicKey}`);
} catch (error) {
throw new Error(`Error processing public key ${publicKey}: ${error.message}.`);
}
if (publicKey.length !== 64) {
throw new Error(`Public key ${publicKey} length differs from the expected 64 hex characters for a public key.`);
Expand All @@ -69,20 +65,16 @@ export const actionCreator = vorpal => async ({ lifetime, minimum, keysgroup, op
throw new Error('Minimum confirmations must be a number.');
}

return getStdIn({
passphraseIsRequired: passphraseSource === 'stdin',
dataIsRequired: secondPassphraseSource === 'stdin',
return getInputsFromSources(vorpal, {
passphrase: {
source: passphraseSource,
repeatPrompt: true,
},
secondPassphrase: !secondPassphraseSource ? null : {
source: secondPassphraseSource,
repeatPrompt: true,
},
})
.then(stdIn => getPassphrase(vorpal, passphraseSource, stdIn.passphrase, { shouldRepeat: true })
.then(passphrase => (secondPassphraseSource ? getPassphrase(
vorpal,
secondPassphraseSource,
getFirstLineFromString(stdIn.data),
{ shouldRepeat: true, displayName: 'your second secret passphrase' },
) : Promise.resolve(null))
.then(secondPassphrase => [passphrase, secondPassphrase]),
),
)
.then(createMultisignatureAccount(
transactionLifetime,
transactionMinimumConfirmations,
Expand Down
30 changes: 11 additions & 19 deletions src/commands/createTransactionRegisterDelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import {
getStdIn,
getPassphrase,
getFirstLineFromString,
} from '../utils/input';
import { createCommand } from '../utils/helpers';
import getInputsFromSources from '../utils/input';
import commonOptions from '../utils/options';
import transactions from '../utils/transactions';

Expand All @@ -29,7 +25,7 @@ const description = `Creates a transaction which will register an existing accou
- create transaction 2 username
`;

const createDelegate = username => ([passphrase, secondPassphrase]) =>
const createDelegate = username => ({ passphrase, secondPassphrase }) =>
transactions.createDelegate(passphrase, username, secondPassphrase);

export const actionCreator = vorpal => async ({ username, options }) => {
Expand All @@ -38,20 +34,16 @@ export const actionCreator = vorpal => async ({ username, options }) => {
'second-passphrase': secondPassphraseSource,
} = options;

return getStdIn({
passphraseIsRequired: passphraseSource === 'stdin',
dataIsRequired: secondPassphraseSource === 'stdin',
return getInputsFromSources(vorpal, {
passphrase: {
source: passphraseSource,
repeatPrompt: true,
},
secondPassphrase: !secondPassphraseSource ? null : {
source: secondPassphraseSource,
repeatPrompt: true,
},
})
.then(stdIn => getPassphrase(vorpal, passphraseSource, stdIn.passphrase, { shouldRepeat: true })
.then(passphrase => (secondPassphraseSource ? getPassphrase(
vorpal,
secondPassphraseSource,
getFirstLineFromString(stdIn.data),
{ shouldRepeat: true, displayName: 'your second secret passphrase' },
) : Promise.resolve(null))
.then(secondPassphrase => [passphrase, secondPassphrase]),
),
)
.then(createDelegate(username));
};

Expand Down
35 changes: 14 additions & 21 deletions src/commands/createTransactionRegisterSecondPassphrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
import {
getStdIn,
getPassphrase,
getFirstLineFromString,
} from '../utils/input';
import { createCommand } from '../utils/helpers';
import getInputsFromSources from '../utils/input';
import commonOptions from '../utils/options';
import transactions from '../utils/transactions';

Expand All @@ -29,30 +25,27 @@ const description = `Creates a transaction which will register a second passphra
- create transaction 1
`;

export const createSignature = (
[passphrase, secondPassphrase],
) => transactions.createSignature(passphrase, secondPassphrase);
export const createSignature = ({
passphrase,
secondPassphrase,
}) => transactions.createSignature(passphrase, secondPassphrase);

export const actionCreator = vorpal => async ({ options }) => {
const {
passphrase: passphraseSource,
'second-passphrase': secondPassphraseSource,
} = options;

return getStdIn({
passphraseIsRequired: passphraseSource === 'stdin',
dataIsRequired: secondPassphraseSource === 'stdin',
return getInputsFromSources(vorpal, {
passphrase: {
source: passphraseSource,
repeatPrompt: true,
},
secondPassphrase: {
source: secondPassphraseSource,
repeatPrompt: true,
},
})
.then(stdIn => getPassphrase(vorpal, passphraseSource, stdIn.passphrase, { shouldRepeat: true })
.then(passphrase => getPassphrase(
vorpal,
secondPassphraseSource,
getFirstLineFromString(stdIn.data),
{ shouldRepeat: true, displayName: 'your second secret passphrase' },
)
.then(secondPassphrase => [passphrase, secondPassphrase]),
),
)
.then(createSignature);
};

Expand Down
26 changes: 11 additions & 15 deletions src/commands/decryptMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
import cryptoModule from '../utils/cryptoModule';
import { createCommand } from '../utils/helpers';
import {
getStdIn,
getPassphrase,
getData,
} from '../utils/input';
import getInputsFromSources from '../utils/input';
import commonOptions from '../utils/options';

const description = `Decrypt an encrypted message from a given sender public key for a known nonce using your secret passphrase.
Example: decrypt message bba7e2e6a4639c431b68e31115a71ffefcb4e025a4d1656405dfdcd8384719e0 349d300c906a113340ff0563ef14a96c092236f331ca4639 e501c538311d38d3857afefa26207408f4bf7f1228
`;

const handlePassphrase = (nonce, senderPublicKey) => ([passphrase, data]) =>
cryptoModule.decryptMessage(data, nonce, passphrase, senderPublicKey);
const handlePassphrase = (nonce, senderPublicKey, message) => ({ passphrase, data }) =>
cryptoModule.decryptMessage(message || data, nonce, passphrase, senderPublicKey);

export const actionCreator = vorpal => async ({ message, nonce, senderPublicKey, options }) => {
const passphraseSource = options.passphrase;
Expand All @@ -38,15 +34,15 @@ export const actionCreator = vorpal => async ({ message, nonce, senderPublicKey,
throw new Error('No message was provided.');
}

return getStdIn({
passphraseIsRequired: passphraseSource === 'stdin',
dataIsRequired: messageSource === 'stdin',
return getInputsFromSources(vorpal, {
passphrase: {
source: passphraseSource,
},
data: message ? null : {
source: messageSource,
},
})
.then(stdIn => Promise.all([
getPassphrase(vorpal, options.passphrase, stdIn.passphrase),
getData(message, messageSource, stdIn.data),
]))
.then(handlePassphrase(nonce, senderPublicKey));
.then(handlePassphrase(nonce, senderPublicKey, message));
};

const decryptMessage = createCommand({
Expand Down
34 changes: 14 additions & 20 deletions src/commands/decryptPassphrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@
*/
import cryptoModule from '../utils/cryptoModule';
import { createCommand } from '../utils/helpers';
import {
getStdIn,
getPassphrase,
getFirstLineFromString,
getData,
} from '../utils/input';
import getInputsFromSources, { getFirstLineFromString } from '../utils/input';
import commonOptions from '../utils/options';

const PASSWORD_DISPLAY_NAME = 'your password';

const description = `Decrypt your secret passphrase using a password. You need the initialisation vector (IV) output at the time of encryption.
Example: decrypt passphrase f74d6bc3bc68c9798213ee80444149e8 09dfba9040a1f2cc0b622dae18a158558b82f5ee953ece4e1ca43b8e81b15a7a
Expand All @@ -39,8 +32,11 @@ const passphraseOptionDescription = `Specifies a source for providing an encrypt
- --passphrase stdin (takes the first line only)
`;

const handleInput = iv => ([cipher, password]) =>
cryptoModule.decryptPassphrase({ cipher, iv }, password);
const handlePasswordAndPassphrase = (iv, passphrase) => ({ password, data }) =>
cryptoModule.decryptPassphrase({
cipher: passphrase || getFirstLineFromString(data),
iv,
}, password);

export const actionCreator = vorpal => async ({ iv, passphrase, options }) => {
const passphraseSource = options.passphrase;
Expand All @@ -50,17 +46,15 @@ export const actionCreator = vorpal => async ({ iv, passphrase, options }) => {
throw new Error('No encrypted passphrase was provided.');
}

return getStdIn({
passphraseIsRequired: passwordSource === 'stdin',
dataIsRequired: passphraseSource === 'stdin',
return getInputsFromSources(vorpal, {
password: {
source: passwordSource,
},
data: passphrase ? null : {
source: passphraseSource,
},
})
.then(stdIn => Promise.all([
getData(passphrase, passphraseSource, getFirstLineFromString(stdIn.data)),
getPassphrase(vorpal, passwordSource, stdIn.passphrase, {
displayName: PASSWORD_DISPLAY_NAME,
}),
]))
.then(handleInput(iv));
.then(handlePasswordAndPassphrase(iv, passphrase));
};

const decryptPassphrase = createCommand({
Expand Down
27 changes: 12 additions & 15 deletions src/commands/encryptMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
import cryptoModule from '../utils/cryptoModule';
import { createCommand } from '../utils/helpers';
import {
getStdIn,
getPassphrase,
getData,
} from '../utils/input';
import getInputsFromSources from '../utils/input';
import commonOptions from '../utils/options';

const description = `Encrypt a message for a given recipient public key using your secret passphrase.
Example: encrypt message bba7e2e6a4639c431b68e31115a71ffefcb4e025a4d1656405dfdcd8384719e0 'Hello world'
`;

const handlePassphraseAndMessage = recipient => ([passphrase, message]) =>
cryptoModule.encryptMessage(message, passphrase, recipient);
const handlePassphraseAndMessage = (recipient, message) => ({ passphrase, data }) =>
cryptoModule.encryptMessage(message || data, passphrase, recipient);

export const actionCreator = vorpal => async ({ recipient, message, options }) => {
const messageSource = options.message;
Expand All @@ -38,15 +34,16 @@ export const actionCreator = vorpal => async ({ recipient, message, options }) =
throw new Error('No message was provided.');
}

return getStdIn({
passphraseIsRequired: passphraseSource === 'stdin',
dataIsRequired: messageSource === 'stdin',
return getInputsFromSources(vorpal, {
passphrase: {
source: passphraseSource,
repeatPrompt: true,
},
data: message ? null : {
source: messageSource,
},
})
.then(stdIn => Promise.all([
getPassphrase(vorpal, passphraseSource, stdIn.passphrase, { shouldRepeat: true }),
getData(message, messageSource, stdIn.data),
]))
.then(handlePassphraseAndMessage(recipient));
.then(handlePassphraseAndMessage(recipient, message));
};

const encryptMessage = createCommand({
Expand Down
32 changes: 11 additions & 21 deletions src/commands/encryptPassphrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@
*/
import cryptoModule from '../utils/cryptoModule';
import { createCommand } from '../utils/helpers';
import {
getStdIn,
getPassphrase,
getFirstLineFromString,
} from '../utils/input';
import getInputsFromSources from '../utils/input';
import commonOptions from '../utils/options';

const PASSWORD_DISPLAY_NAME = 'your password';

const description = `Encrypt your secret passphrase under a password.
Example: encrypt passphrase
Expand All @@ -34,7 +28,7 @@ const outputPublicKeyOption = [
'Includes the public key in the output. This option is provided for the convenience of node operators.',
];

const handleInput = outputPublicKey => ([passphrase, password]) => {
const handleInput = outputPublicKey => ({ passphrase, password }) => {
const cipherAndIv = cryptoModule.encryptPassphrase(passphrase, password);
return outputPublicKey
? Object.assign({}, cipherAndIv, {
Expand All @@ -50,20 +44,16 @@ export const actionCreator = vorpal => async ({ options }) => {
'output-public-key': outputPublicKey,
} = options;

return getStdIn({
passphraseIsRequired: passphraseSource === 'stdin',
dataIsRequired: passwordSource === 'stdin',
return getInputsFromSources(vorpal, {
passphrase: {
source: passphraseSource,
repeatPrompt: true,
},
password: {
source: passwordSource,
repeatPrompt: true,
},
})
.then(stdIn => getPassphrase(vorpal, passphraseSource, stdIn.passphrase, { shouldRepeat: true })
.then(passphrase => getPassphrase(
vorpal,
passwordSource,
getFirstLineFromString(stdIn.data),
{ displayName: PASSWORD_DISPLAY_NAME, shouldRepeat: true },
)
.then(password => [passphrase, password]),
),
)
.then(handleInput(outputPublicKey));
};

Expand Down

0 comments on commit f7ceca3

Please sign in to comment.