diff options
author | frankiebee <frankie.diamond@gmail.com> | 2018-04-03 04:17:54 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2018-04-03 04:17:54 +0800 |
commit | e8a480aac44546e6bd5d7457545bc951a8787814 (patch) | |
tree | f025e3f380e60b02fca366435855800043363d62 | |
parent | 69a867b4a420acd4b4354aab2cc4051736a778cb (diff) | |
download | tangerine-wallet-browser-e8a480aac44546e6bd5d7457545bc951a8787814.tar tangerine-wallet-browser-e8a480aac44546e6bd5d7457545bc951a8787814.tar.gz tangerine-wallet-browser-e8a480aac44546e6bd5d7457545bc951a8787814.tar.bz2 tangerine-wallet-browser-e8a480aac44546e6bd5d7457545bc951a8787814.tar.lz tangerine-wallet-browser-e8a480aac44546e6bd5d7457545bc951a8787814.tar.xz tangerine-wallet-browser-e8a480aac44546e6bd5d7457545bc951a8787814.tar.zst tangerine-wallet-browser-e8a480aac44546e6bd5d7457545bc951a8787814.zip |
transactions validationt - valdate from field on txParams
-rw-r--r-- | app/scripts/lib/tx-gas-utils.js | 9 | ||||
-rw-r--r-- | test/unit/tx-gas-util-test.js | 24 |
2 files changed, 32 insertions, 1 deletions
diff --git a/app/scripts/lib/tx-gas-utils.js b/app/scripts/lib/tx-gas-utils.js index 0fa9dd8d4..4be7738b0 100644 --- a/app/scripts/lib/tx-gas-utils.js +++ b/app/scripts/lib/tx-gas-utils.js @@ -100,6 +100,7 @@ module.exports = class TxGasUtil { } async validateTxParams (txParams) { + this.validateFrom(txParams) this.validateRecipient(txParams) if ('value' in txParams) { const value = txParams.value.toString() @@ -112,6 +113,12 @@ module.exports = class TxGasUtil { } } } + + validateFrom (txParams) { + if ( !(typeof txParams.from === 'string') ) throw new Error(`Invalid from address ${txParams.from} not a string`) + if (!isValidAddress(txParams.from)) throw new Error('Invalid from address') + } + validateRecipient (txParams) { if (txParams.to === '0x' || txParams.to === null ) { if (txParams.data) { @@ -124,4 +131,4 @@ module.exports = class TxGasUtil { } return txParams } -} +}
\ No newline at end of file diff --git a/test/unit/tx-gas-util-test.js b/test/unit/tx-gas-util-test.js index d9a12d1c3..15d412c72 100644 --- a/test/unit/tx-gas-util-test.js +++ b/test/unit/tx-gas-util-test.js @@ -29,4 +29,28 @@ describe('Tx Gas Util', function () { } assert.throws(() => { txGasUtil.validateRecipient(zeroRecipientTxParams) }, Error, 'Invalid recipient address') }) + + it('should error when from is not a hex string', function () { + + // where from is undefined + const txParams = {} + assert.throws(() => { txGasUtil.validateFrom(txParams) }, Error, `Invalid from address ${txParams.from} not a string`) + + // where from is array + txParams.from = [] + assert.throws(() => { txGasUtil.validateFrom(txParams) }, Error, `Invalid from address ${txParams.from} not a string`) + + // where from is a object + txParams.from = {} + assert.throws(() => { txGasUtil.validateFrom(txParams) }, Error, `Invalid from address ${txParams.from} not a string`) + + // where from is a invalid address + txParams.from = 'im going to fail' + assert.throws(() => { txGasUtil.validateFrom(txParams) }, Error, `Invalid from address`) + + // should run + txParams.from ='0x1678a085c290ebd122dc42cba69373b5953b831d' + txGasUtil.validateFrom(txParams) + }) + }) |