1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

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

This commit is contained in:
ryanml 2021-08-02 08:38:01 -07:00 committed by GitHub
parent 668fd2db66
commit 1360b2bf6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 2 deletions

View File

@ -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.

View File

@ -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

View File

@ -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 () {