From 1360b2bf6b047f50ef5007319f866af092e1b5c1 Mon Sep 17 00:00:00 2001 From: ryanml Date: Mon, 2 Aug 2021 08:38:01 -0700 Subject: [PATCH] Rejecting EIP-1559 transactions on unsupported networks (#11722) --- app/scripts/controllers/transactions/index.js | 3 ++- .../controllers/transactions/lib/util.js | 9 ++++++- .../controllers/transactions/lib/util.test.js | 27 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index f9b6121ae..ae6293b9a 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.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. diff --git a/app/scripts/controllers/transactions/lib/util.js b/app/scripts/controllers/transactions/lib/util.js index a31cac3a1..7e4e0f4fb 100644 --- a/app/scripts/controllers/transactions/lib/util.js +++ b/app/scripts/controllers/transactions/lib/util.js @@ -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 diff --git a/app/scripts/controllers/transactions/lib/util.test.js b/app/scripts/controllers/transactions/lib/util.test.js index ad90faf54..910454e2f 100644 --- a/app/scripts/controllers/transactions/lib/util.test.js +++ b/app/scripts/controllers/transactions/lib/util.test.js @@ -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 () {