mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Rejecting EIP-1559 transactions on unsupported networks (#11722)
This commit is contained in:
parent
668fd2db66
commit
1360b2bf6b
@ -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…
Reference in New Issue
Block a user