2020-01-09 04:34:58 +01:00
|
|
|
import { addHexPrefix, isValidAddress } from 'ethereumjs-util'
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
|
2018-04-13 22:18:45 +02:00
|
|
|
// functions that handle normalizing of that key in txParams
|
|
|
|
const normalizers = {
|
2019-12-03 17:53:13 +01:00
|
|
|
from: (from, LowerCase = true) => (LowerCase ? addHexPrefix(from).toLowerCase() : addHexPrefix(from)),
|
|
|
|
to: (to, LowerCase = true) => (LowerCase ? addHexPrefix(to).toLowerCase() : addHexPrefix(to)),
|
2018-04-13 22:18:45 +02:00
|
|
|
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
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2018-04-19 20:29:26 +02:00
|
|
|
normalizes txParams
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {Object} txParams
|
|
|
|
@returns {Object} - normalized txParams
|
2018-04-13 22:18:45 +02:00
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export function normalizeTxParams (txParams, LowerCase) {
|
2018-04-13 22:18:45 +02:00
|
|
|
// apply only keys in the normalizers
|
2018-04-06 20:07:20 +02:00
|
|
|
const normalizedTxParams = {}
|
2018-04-19 20:29:26 +02:00
|
|
|
for (const key in normalizers) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (txParams[key]) {
|
|
|
|
normalizedTxParams[key] = normalizers[key](txParams[key], LowerCase)
|
|
|
|
}
|
2018-04-13 22:18:45 +02:00
|
|
|
}
|
2018-04-06 20:07:20 +02:00
|
|
|
return normalizedTxParams
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2018-04-19 20:29:26 +02:00
|
|
|
validates txParams
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {Object} txParams
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export function validateTxParams (txParams) {
|
2018-04-06 20:07:20 +02:00
|
|
|
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`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2018-04-19 20:29:26 +02:00
|
|
|
validates the from field in txParams
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {Object} txParams
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export function validateFrom (txParams) {
|
2019-11-20 01:03:20 +01: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-06 20:07:20 +02:00
|
|
|
}
|
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2018-04-19 20:29:26 +02:00
|
|
|
validates the to field in txParams
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {Object} txParams
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export function validateRecipient (txParams) {
|
2018-04-10 23:53:40 +02:00
|
|
|
if (txParams.to === '0x' || txParams.to === null) {
|
2018-04-06 20:07:20 +02:00
|
|
|
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)) {
|
2018-04-06 20:07:20 +02:00
|
|
|
throw new Error('Invalid recipient address')
|
|
|
|
}
|
|
|
|
return txParams
|
2018-04-10 23:53:40 +02:00
|
|
|
}
|
2018-04-19 20:29:26 +02:00
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2020-01-13 19:36:36 +01:00
|
|
|
@returns {array} - states that can be considered final
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
export function getFinalStates () {
|
2018-04-19 20:29:26 +02:00
|
|
|
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
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|