From 70eeaa494d9ab12547e921e90a352899ce112780 Mon Sep 17 00:00:00 2001 From: Ben Burns <803016+benjamincburns@users.noreply.github.com> Date: Mon, 13 Jul 2020 15:59:45 -0700 Subject: [PATCH] fix cyclomatic complexity in web3-eth-accounts --- packages/web3-eth-accounts/src/index.js | 54 ++++++++++++++----------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 32c69695b31..ffc5da3f577 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -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'); - } + _validateTransactionForSigning(tx); if (error) { callback(error); @@ -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); @@ -630,5 +637,4 @@ function storageAvailable(type) { } } - module.exports = Accounts;