Skip to content

Commit

Permalink
fix cyclomatic complexity in web3-eth-accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns committed Jul 13, 2020
1 parent 0c7beb1 commit 1961eb5
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,7 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca
}

function signed(tx) {
if (tx.common && (tx.chain && tx.hardfork)) {
error = new Error(
'Please provide the ethereumjs-common object or the chain and hardfork property but not all together.'
);
}

if ((tx.chain && !tx.hardfork) || (tx.hardfork && !tx.chain)) {
error = new Error(
'When specifying chain and hardfork, both values must be defined. ' +
'Received "chain": ' + tx.chain + ', "hardfork": ' + tx.hardfork
);
}

if (!tx.gas && !tx.gasLimit) {
error = new Error('"gas" is missing');
}

if (tx.nonce < 0 ||
tx.gas < 0 ||
tx.gasPrice < 0 ||
tx.chainId < 0) {
error = new Error('Gas, gasPrice, nonce or chainId is lower than 0');
}
const error = _validateTransactionForSigning(tx);

if (error) {
callback(error);
Expand Down Expand Up @@ -280,6 +258,35 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca
});
};

function _validateTransactionForSigning(tx) {
if (tx.common && (tx.chain && tx.hardfork)) {
return new Error(
'Please provide the ethereumjs-common object or the chain and hardfork property but not all together.'
);
}

if ((tx.chain && !tx.hardfork) || (tx.hardfork && !tx.chain)) {
return new Error(
'When specifying chain and hardfork, both values must be defined. ' +
'Received "chain": ' + tx.chain + ', "hardfork": ' + tx.hardfork
);
}

if (!tx.gas && !tx.gasLimit) {
return new Error('"gas" is missing');
}

if (tx.nonce < 0 ||
tx.gas < 0 ||
tx.gasPrice < 0 ||
tx.chainId < 0) {
return new Error('Gas, gasPrice, nonce or chainId is lower than 0');
}

return;
}


/* jshint ignore:start */
Accounts.prototype.recoverTransaction = function recoverTransaction(rawTx) {
var values = RLP.decode(rawTx);
Expand Down Expand Up @@ -630,5 +637,4 @@ function storageAvailable(type) {
}
}


module.exports = Accounts;

0 comments on commit 1961eb5

Please sign in to comment.