2021-02-04 19:15:23 +01:00
import { ethErrors } from 'eth-rpc-errors' ;
import { addHexPrefix } from '../../../lib/util' ;
2021-06-29 21:25:56 +02:00
import {
TRANSACTION _ENVELOPE _TYPES ,
TRANSACTION _STATUSES ,
} from '../../../../../shared/constants/transaction' ;
2021-05-17 21:00:59 +02:00
import { isValidHexAddress } from '../../../../../shared/modules/hexstring-utils' ;
2018-04-06 20:07:20 +02:00
2018-04-13 22:18:45 +02:00
const normalizers = {
2021-06-29 21:25:56 +02:00
from : addHexPrefix ,
2020-11-03 00:41:28 +01:00
to : ( to , lowerCase ) =>
lowerCase ? addHexPrefix ( to ) . toLowerCase ( ) : addHexPrefix ( to ) ,
2021-06-29 21:25:56 +02:00
nonce : addHexPrefix ,
value : addHexPrefix ,
data : addHexPrefix ,
gas : addHexPrefix ,
gasPrice : addHexPrefix ,
maxFeePerGas : addHexPrefix ,
maxPriorityFeePerGas : addHexPrefix ,
type : addHexPrefix ,
2021-02-04 19:15:23 +01:00
} ;
2018-04-19 20:29:26 +02:00
2021-03-30 16:54:05 +02:00
export function normalizeAndValidateTxParams ( txParams , lowerCase = true ) {
const normalizedTxParams = normalizeTxParams ( txParams , lowerCase ) ;
validateTxParams ( normalizedTxParams ) ;
return normalizedTxParams ;
}
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
2021-02-04 19:15:23 +01: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 ] ) {
2021-02-04 19:15:23 +01:00
normalizedTxParams [ key ] = normalizers [ key ] ( txParams [ key ] , lowerCase ) ;
2019-11-20 01:03:20 +01:00
}
2018-04-13 22:18:45 +02:00
}
2021-02-04 19:15:23 +01:00
return normalizedTxParams ;
2018-04-06 20:07:20 +02:00
}
2021-06-29 21:25:56 +02:00
/ * *
* Given two fields , ensure that the second field is not included in txParams ,
* and if it is throw an invalidParams error .
* @ param { Object } txParams - the transaction parameters object
* @ param { string } fieldBeingValidated - the current field being validated
* @ param { string } mutuallyExclusiveField - the field to ensure is not provided
* @ throws { ethErrors . rpc . invalidParams } - throws if mutuallyExclusiveField is
* present in txParams .
* /
function ensureMutuallyExclusiveFieldsNotProvided (
txParams ,
fieldBeingValidated ,
mutuallyExclusiveField ,
) {
if ( typeof txParams [ mutuallyExclusiveField ] !== 'undefined' ) {
throw ethErrors . rpc . invalidParams (
` Invalid transaction params: specified ${ fieldBeingValidated } but also included ${ mutuallyExclusiveField } , these cannot be mixed ` ,
) ;
}
}
/ * *
* Ensures that the provided value for field is a string , throws an
* invalidParams error if field is not a string .
* @ param { Object } txParams - the transaction parameters object
* @ param { string } field - the current field being validated
* @ throws { ethErrors . rpc . invalidParams } - throws if field is not a string
* /
function ensureFieldIsString ( txParams , field ) {
if ( typeof txParams [ field ] !== 'string' ) {
throw ethErrors . rpc . invalidParams (
` Invalid transaction params: ${ field } is not a string. got: ( ${ txParams [ field ] } ) ` ,
) ;
}
}
/ * *
* Ensures that the provided txParams has the proper 'type' specified for the
* given field , if it is provided . If types do not match throws an
* invalidParams error .
* @ param { Object } txParams - the transaction parameters object
* @ param { 'gasPrice' | 'maxFeePerGas' | 'maxPriorityFeePerGas' } field - the
* current field being validated
* @ throws { ethErrors . rpc . invalidParams } - throws if type does not match the
* expectations for provided field .
* /
function ensureProperTransactionEnvelopeTypeProvided ( txParams , field ) {
switch ( field ) {
case 'maxFeePerGas' :
case 'maxPriorityFeePerGas' :
if (
txParams . type &&
txParams . type !== TRANSACTION _ENVELOPE _TYPES . FEE _MARKET
) {
throw ethErrors . rpc . invalidParams (
` Invalid transaction envelope type: specified type " ${ txParams . type } " but including maxFeePerGas and maxPriorityFeePerGas requires type: " ${ TRANSACTION _ENVELOPE _TYPES . FEE _MARKET } " ` ,
) ;
}
break ;
case 'gasPrice' :
default :
if (
txParams . type &&
txParams . type === TRANSACTION _ENVELOPE _TYPES . FEE _MARKET
) {
throw ethErrors . rpc . invalidParams (
` Invalid transaction envelope type: specified type " ${ txParams . type } " but included a gasPrice instead of maxFeePerGas and maxPriorityFeePerGas ` ,
) ;
}
}
}
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 ) {
2020-12-04 03:15:59 +01:00
if ( ! txParams || typeof txParams !== 'object' || Array . isArray ( txParams ) ) {
throw ethErrors . rpc . invalidParams (
'Invalid transaction params: must be an object.' ,
2021-02-04 19:15:23 +01:00
) ;
2020-12-04 03:15:59 +01:00
}
if ( ! txParams . to && ! txParams . data ) {
throw ethErrors . rpc . invalidParams (
'Invalid transaction params: must specify "data" for contract deployments, or "to" (and optionally "data") for all other types of transactions.' ,
2021-02-04 19:15:23 +01:00
) ;
2020-12-04 03:15:59 +01:00
}
2021-03-30 16:54:05 +02:00
Object . entries ( txParams ) . forEach ( ( [ key , value ] ) => {
// validate types
switch ( key ) {
case 'from' :
validateFrom ( txParams ) ;
break ;
case 'to' :
validateRecipient ( txParams ) ;
break ;
2021-06-29 21:25:56 +02:00
case 'gasPrice' :
ensureProperTransactionEnvelopeTypeProvided ( txParams , 'gasPrice' ) ;
ensureMutuallyExclusiveFieldsNotProvided (
txParams ,
'gasPrice' ,
'maxFeePerGas' ,
) ;
ensureMutuallyExclusiveFieldsNotProvided (
txParams ,
'gasPrice' ,
'maxPriorityFeePerGas' ,
) ;
ensureFieldIsString ( txParams , 'gasPrice' ) ;
break ;
case 'maxFeePerGas' :
ensureProperTransactionEnvelopeTypeProvided ( txParams , 'maxFeePerGas' ) ;
ensureMutuallyExclusiveFieldsNotProvided (
txParams ,
'maxFeePerGas' ,
'gasPrice' ,
) ;
ensureFieldIsString ( txParams , 'maxFeePerGas' ) ;
break ;
case 'maxPriorityFeePerGas' :
ensureProperTransactionEnvelopeTypeProvided (
txParams ,
'maxPriorityFeePerGas' ,
) ;
ensureMutuallyExclusiveFieldsNotProvided (
txParams ,
'maxPriorityFeePerGas' ,
'gasPrice' ,
) ;
ensureFieldIsString ( txParams , 'maxPriorityFeePerGas' ) ;
break ;
2021-03-30 16:54:05 +02:00
case 'value' :
2021-06-29 21:25:56 +02:00
ensureFieldIsString ( txParams , 'value' ) ;
2021-03-30 16:54:05 +02:00
if ( value . toString ( ) . includes ( '-' ) ) {
throw ethErrors . rpc . invalidParams (
` Invalid transaction value " ${ value } ": not a positive number. ` ,
) ;
}
2018-04-06 20:07:20 +02:00
2021-03-30 16:54:05 +02:00
if ( value . toString ( ) . includes ( '.' ) ) {
throw ethErrors . rpc . invalidParams (
` Invalid transaction value of " ${ value } ": number must be in wei. ` ,
) ;
}
break ;
case 'chainId' :
if ( typeof value !== 'number' && typeof value !== 'string' ) {
throw ethErrors . rpc . invalidParams (
` Invalid transaction params: ${ key } is not a Number or hex string. got: ( ${ value } ) ` ,
) ;
}
break ;
default :
2021-06-29 21:25:56 +02:00
ensureFieldIsString ( txParams , key ) ;
2018-04-06 20:07:20 +02:00
}
2021-03-30 16:54:05 +02:00
} ) ;
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' ) ) {
2020-12-04 03:15:59 +01:00
throw ethErrors . rpc . invalidParams (
` Invalid "from" address " ${ txParams . from } ": not a string. ` ,
2021-02-04 19:15:23 +01:00
) ;
2019-11-20 01:03:20 +01:00
}
2021-05-17 21:00:59 +02:00
if ( ! isValidHexAddress ( txParams . from , { allowNonPrefixed : false } ) ) {
2021-02-04 19:15:23 +01:00
throw ethErrors . rpc . invalidParams ( 'Invalid "from" address.' ) ;
2019-11-20 01:03:20 +01:00
}
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 ) {
2021-02-04 19:15:23 +01:00
delete txParams . to ;
2018-04-06 20:07:20 +02:00
} else {
2021-02-04 19:15:23 +01:00
throw ethErrors . rpc . invalidParams ( 'Invalid "to" address.' ) ;
2018-04-06 20:07:20 +02:00
}
2021-05-17 21:00:59 +02:00
} else if (
txParams . to !== undefined &&
! isValidHexAddress ( txParams . to , { allowNonPrefixed : false } )
) {
2021-02-04 19:15:23 +01:00
throw ethErrors . rpc . invalidParams ( 'Invalid "to" address.' ) ;
2018-04-06 20:07:20 +02:00
}
2021-02-04 19:15:23 +01:00
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 [
2020-11-07 08:38:12 +01:00
TRANSACTION _STATUSES . REJECTED , // the user has responded no!
TRANSACTION _STATUSES . CONFIRMED , // the tx has been included in a block.
TRANSACTION _STATUSES . FAILED , // the tx failed for some reason, included on tx data.
TRANSACTION _STATUSES . DROPPED , // the tx nonce was already used
2021-02-04 19:15:23 +01:00
] ;
2018-04-19 20:29:26 +02:00
}