Rejecting EIP-1559 transactions on unsupported networks (#11722)

feature/default_network_editable
ryanml 3 years ago committed by GitHub
parent 668fd2db66
commit 1360b2bf6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/scripts/controllers/transactions/index.js
  2. 9
      app/scripts/controllers/transactions/lib/util.js
  3. 27
      app/scripts/controllers/transactions/lib/util.test.js

@ -327,8 +327,9 @@ export default class TransactionController extends EventEmitter {
async addUnapprovedTransaction(txParams, origin) {
// validate
const normalizedTxParams = txUtils.normalizeTxParams(txParams);
const eip1559Compatibility = await this.getEIP1559Compatibility();
txUtils.validateTxParams(normalizedTxParams);
txUtils.validateTxParams(normalizedTxParams, eip1559Compatibility);
/**
`generateTxMeta` adds the default txMeta properties to the passed object.

@ -4,6 +4,7 @@ import {
TRANSACTION_ENVELOPE_TYPES,
TRANSACTION_STATUSES,
} from '../../../../../shared/constants/transaction';
import { isEIP1559Transaction } from '../../../../../shared/modules/transaction.utils';
import { isValidHexAddress } from '../../../../../shared/modules/hexstring-utils';
const normalizers = {
@ -119,9 +120,10 @@ function ensureProperTransactionEnvelopeTypeProvided(txParams, field) {
/**
* Validates the given tx parameters
* @param {Object} txParams - the tx params
* @param {boolean} eip1559Compatibility - whether or not the current network supports EIP-1559 transactions
* @throws {Error} if the tx params contains invalid fields
*/
export function validateTxParams(txParams) {
export function validateTxParams(txParams, eip1559Compatibility = true) {
if (!txParams || typeof txParams !== 'object' || Array.isArray(txParams)) {
throw ethErrors.rpc.invalidParams(
'Invalid transaction params: must be an object.',
@ -132,6 +134,11 @@ export function validateTxParams(txParams) {
'Invalid transaction params: must specify "data" for contract deployments, or "to" (and optionally "data") for all other types of transactions.',
);
}
if (isEIP1559Transaction({ txParams }) && !eip1559Compatibility) {
throw ethErrors.rpc.invalidParams(
'Invalid transaction params: params specify an EIP-1559 transaction but the current network does not support EIP-1559',
);
}
Object.entries(txParams).forEach(([key, value]) => {
// validate types

@ -283,6 +283,33 @@ describe('txUtils', function () {
assert.doesNotThrow(() => txUtils.validateTxParams(txParams));
});
});
describe('when validating EIP-1559 transactions', function () {
it('should error when network does not support EIP-1559', function () {
const txParams = {
maxPriorityFeePerGas: '0x1',
maxFeePerGas: '0x1',
to: BURN_ADDRESS,
};
assert.throws(
() => {
txUtils.validateTxParams(txParams, false);
},
{
message:
'Invalid transaction params: params specify an EIP-1559 transaction but the current network does not support EIP-1559',
},
);
});
it('should validate when network does support EIP-1559', function () {
const txParams = {
maxPriorityFeePerGas: '0x1',
maxFeePerGas: '0x1',
to: BURN_ADDRESS,
};
assert.doesNotThrow(() => txUtils.validateTxParams(txParams, true));
});
});
});
describe('#normalizeTxParams', function () {

Loading…
Cancel
Save