From 07601f4a90172ab17271d7b213eaf99f4da50163 Mon Sep 17 00:00:00 2001 From: ryanml Date: Mon, 12 Jul 2021 13:26:53 -0700 Subject: [PATCH] Setting tx param type before signing transactions (#11497) Fixes MetaMask/metamask-extension#11491 --- app/scripts/controllers/transactions/index.js | 5 ++ .../controllers/transactions/index.test.js | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 56ccfa286..0a19a238c 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -24,6 +24,7 @@ import { hexWEIToDecGWEI } from '../../../../ui/helpers/utils/conversions.util'; import { TRANSACTION_STATUSES, TRANSACTION_TYPES, + TRANSACTION_ENVELOPE_TYPES, } from '../../../../shared/constants/transaction'; import { METAMASK_CONTROLLER_EVENTS } from '../../metamask-controller'; import { GAS_LIMITS } from '../../../../shared/constants/gas'; @@ -727,7 +728,11 @@ export default class TransactionController extends EventEmitter { const txMeta = this.txStateManager.getTransaction(txId); // add network/chain id const chainId = this.getChainId(); + const type = isEIP1559Transaction(txMeta) + ? TRANSACTION_ENVELOPE_TYPES.FEE_MARKET + : TRANSACTION_ENVELOPE_TYPES.LEGACY; const txParams = { + type, ...txMeta.txParams, chainId, gasLimit: txMeta.txParams.gas, diff --git a/app/scripts/controllers/transactions/index.test.js b/app/scripts/controllers/transactions/index.test.js index b04132850..c753d70d0 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -773,6 +773,59 @@ describe('Transaction Controller', function () { }); }); + describe('#signTransaction', function () { + let fromTxDataSpy; + + beforeEach(function () { + fromTxDataSpy = sinon.spy(TransactionFactory, 'fromTxData'); + }); + + afterEach(function () { + fromTxDataSpy.restore(); + }); + + it('sets txParams.type to 0x0 (non-EIP-1559)', async function () { + txController.txStateManager._addTransactionsToState([ + { + status: TRANSACTION_STATUSES.UNAPPROVED, + id: 1, + metamaskNetworkId: currentNetworkId, + history: [{}], + txParams: { + from: VALID_ADDRESS_TWO, + to: VALID_ADDRESS, + gasPrice: '0x77359400', + gas: '0x7b0d', + nonce: '0x4b', + }, + }, + ]); + await txController.signTransaction('1'); + assert.equal(fromTxDataSpy.getCall(0).args[0].type, '0x0'); + }); + + it('sets txParams.type to 0x2 (EIP-1559)', async function () { + txController.txStateManager._addTransactionsToState([ + { + status: TRANSACTION_STATUSES.UNAPPROVED, + id: 2, + metamaskNetworkId: currentNetworkId, + history: [{}], + txParams: { + from: VALID_ADDRESS_TWO, + to: VALID_ADDRESS, + maxFeePerGas: '0x77359400', + maxPriorityFeePerGas: '0x77359400', + gas: '0x7b0d', + nonce: '0x4b', + }, + }, + ]); + await txController.signTransaction('2'); + assert.equal(fromTxDataSpy.getCall(0).args[0].type, '0x2'); + }); + }); + describe('#publishTransaction', function () { let hash, txMeta, trackTransactionMetricsEventSpy;