2020-11-06 22:18:00 +01:00
|
|
|
import { isValidAddress } from 'ethereumjs-util'
|
|
|
|
import { addHexPrefix } from '../../../lib/util'
|
2018-04-06 20:07:20 +02:00
|
|
|
|
2018-04-13 22:18:45 +02:00
|
|
|
const normalizers = {
|
2020-06-23 18:12:11 +02:00
|
|
|
from: (from) => addHexPrefix(from),
|
2020-11-03 00:41:28 +01:00
|
|
|
to: (to, lowerCase) =>
|
|
|
|
lowerCase ? addHexPrefix(to).toLowerCase() : addHexPrefix(to),
|
2020-02-15 21:34:12 +01:00
|
|
|
nonce: (nonce) => addHexPrefix(nonce),
|
|
|
|
value: (value) => addHexPrefix(value),
|
|
|
|
data: (data) => addHexPrefix(data),
|
|
|
|
gas: (gas) => addHexPrefix(gas),
|
|
|
|
gasPrice: (gasPrice) => addHexPrefix(gasPrice),
|
2018-04-13 22:18:45 +02:00
|
|
|
}
|
2018-04-19 20:29:26 +02:00
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2020-04-20 21:12:24 +02:00
|
|
|
* Normalizes the given txParams
|
2020-06-23 18:12:11 +02:00
|
|
|
* @param {Object} txParams - The transaction params
|
|
|
|
* @param {boolean} [lowerCase] - Whether to lowercase the 'to' address.
|
|
|
|
* Default: true
|
2020-04-20 21:12:24 +02:00
|
|
|
* @returns {Object} the normalized tx params
|
2018-04-13 22:18:45 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function normalizeTxParams(txParams, lowerCase = true) {
|
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]) {
|
2020-04-20 21:12:24 +02:00
|
|
|
normalizedTxParams[key] = normalizers[key](txParams[key], lowerCase)
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
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
|
|
|
/**
|
2020-04-20 21:12:24 +02:00
|
|
|
* Validates the given tx parameters
|
|
|
|
* @param {Object} txParams - the tx params
|
|
|
|
* @throws {Error} if the tx params contains invalid fields
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +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('-')) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`Invalid transaction value of ${txParams.value} not a positive number.`,
|
|
|
|
)
|
2018-04-06 20:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.includes('.')) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`Invalid transaction value of ${txParams.value} number must be in wei`,
|
|
|
|
)
|
2018-04-06 20:07:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2020-04-20 21:12:24 +02:00
|
|
|
* Validates the {@code from} field in the given tx params
|
|
|
|
* @param {Object} txParams
|
|
|
|
* @throws {Error} if the from address isn't valid
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +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
|
|
|
/**
|
2020-04-20 21:12:24 +02:00
|
|
|
* Validates the {@code to} field in the given tx params
|
|
|
|
* @param {Object} txParams - the tx params
|
|
|
|
* @returns {Object} the tx params
|
|
|
|
* @throws {Error} if the recipient is invalid OR there isn't tx data
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +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-04-20 21:12:24 +02:00
|
|
|
* Returns a list of final states
|
|
|
|
* @returns {string[]} the states that can be considered final states
|
|
|
|
*/
|
2020-11-03 00:41:28 +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
|
|
|
|
]
|
|
|
|
}
|