1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/app/scripts/controllers/transactions/lib/util.js

100 lines
2.5 KiB
JavaScript
Raw Normal View History

const {
addHexPrefix,
isValidAddress,
} = require('ethereumjs-util')
2018-04-19 20:29:26 +02:00
/**
@module
*/
module.exports = {
normalizeTxParams,
validateTxParams,
validateFrom,
2018-04-10 23:53:40 +02:00
validateRecipient,
2018-04-19 20:29:26 +02:00
getFinalStates,
}
2018-04-13 22:18:45 +02:00
// functions that handle normalizing of that key in txParams
const normalizers = {
from: from => addHexPrefix(from).toLowerCase(),
to: to => addHexPrefix(to).toLowerCase(),
nonce: nonce => addHexPrefix(nonce),
2018-04-19 20:29:26 +02:00
value: value => addHexPrefix(value),
2018-04-13 22:18:45 +02:00
data: data => addHexPrefix(data),
gas: gas => addHexPrefix(gas),
gasPrice: gasPrice => addHexPrefix(gasPrice),
}
2018-04-19 20:29:26 +02:00
2018-04-13 22:18:45 +02:00
/**
2018-04-19 20:29:26 +02:00
normalizes txParams
@param txParams {object}
@returns {object} normalized txParams
2018-04-13 22:18:45 +02:00
*/
function normalizeTxParams (txParams) {
2018-04-13 22:18:45 +02:00
// apply only keys in the normalizers
const normalizedTxParams = {}
2018-04-19 20:29:26 +02:00
for (const key in normalizers) {
2018-04-13 22:18:45 +02:00
if (txParams[key]) normalizedTxParams[key] = normalizers[key](txParams[key])
}
return normalizedTxParams
}
2018-04-19 20:29:26 +02:00
/**
validates txParams
@param txParams {object}
*/
function validateTxParams (txParams) {
validateFrom(txParams)
validateRecipient(txParams)
if ('value' in txParams) {
const value = txParams.value.toString()
if (value.includes('-')) {
throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`)
}
if (value.includes('.')) {
throw new Error(`Invalid transaction value of ${txParams.value} number must be in wei`)
}
}
}
2018-04-19 20:29:26 +02:00
/**
validates the from field in txParams
@param txParams {object}
*/
function validateFrom (txParams) {
2018-04-10 23:53:40 +02:00
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')
}
2018-04-19 20:29:26 +02:00
/**
validates the to field in txParams
@param txParams {object}
*/
function validateRecipient (txParams) {
2018-04-10 23:53:40 +02:00
if (txParams.to === '0x' || txParams.to === null) {
if (txParams.data) {
delete txParams.to
} else {
throw new Error('Invalid recipient address')
}
2018-04-10 23:53:40 +02:00
} else if (txParams.to !== undefined && !isValidAddress(txParams.to)) {
throw new Error('Invalid recipient address')
}
return txParams
2018-04-10 23:53:40 +02:00
}
2018-04-19 20:29:26 +02:00
/**
@returns an {array} of states that can be considered final
*/
function getFinalStates () {
return [
'rejected', // the user has responded no!
'confirmed', // the tx has been included in a block.
'failed', // the tx failed for some reason, included on tx data.
'dropped', // the tx nonce was already used
]
}