diff --git a/packages/shield-controller/CHANGELOG.md b/packages/shield-controller/CHANGELOG.md index 30fa896757b..a995521c9fe 100644 --- a/packages/shield-controller/CHANGELOG.md +++ b/packages/shield-controller/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added `transactionMeta.rawTx` to the `logTransaction` request body. ([#7178](https://github.com/MetaMask/core/pull/7178)) + +### Changed + +- Skipped transaction coverage check when the transaction is `submitted` or `confirmed`. ([#7178](https://github.com/MetaMask/core/pull/7178)) + ## [2.1.0] ### Added diff --git a/packages/shield-controller/src/ShieldController.test.ts b/packages/shield-controller/src/ShieldController.test.ts index d25e15cb221..91e1904c60f 100644 --- a/packages/shield-controller/src/ShieldController.test.ts +++ b/packages/shield-controller/src/ShieldController.test.ts @@ -461,6 +461,7 @@ describe('ShieldController', () => { txMeta: updatedTxMeta, status: 'shown', transactionHash: '0x00', + rawTransactionHex: '0xdeadbeef', }); }); @@ -478,6 +479,7 @@ describe('ShieldController', () => { expect(components.backend.logTransaction).toHaveBeenCalledWith({ status: 'not_shown', transactionHash: '0x00', + rawTransactionHex: '0xdeadbeef', txMeta: updatedTxMeta, }); }); @@ -492,6 +494,17 @@ describe('ShieldController', () => { // Check that backend was not called expect(components.backend.logTransaction).not.toHaveBeenCalled(); }); + + it('does not log when raw transaction hex is missing', async () => { + const components = setup(); + + await runTest(components, { + updateTransaction: (txMeta) => delete txMeta.rawTx, + }); + + // Check that backend was not called + expect(components.backend.logTransaction).not.toHaveBeenCalled(); + }); }); describe('metadata', () => { diff --git a/packages/shield-controller/src/ShieldController.ts b/packages/shield-controller/src/ShieldController.ts index 8a422b1b548..296df877717 100644 --- a/packages/shield-controller/src/ShieldController.ts +++ b/packages/shield-controller/src/ShieldController.ts @@ -284,24 +284,30 @@ export class ShieldController extends BaseController< for (const transaction of transactions) { const previousTransaction = previousTransactionsById.get(transaction.id); - // Check if the simulation data has changed. - const simulationDataChanged = - // only check if the previous transaction has simulation data and if it has changed - // this is to avoid checking coverage for the `TWICE` (once when it's added to the state and once when it's simulated for the first time). - // we only need to update the coverage result when the simulation data has changed. - Boolean(previousTransaction?.simulationData) && - !isEqual( - transaction.simulationData, - previousTransaction?.simulationData, - ); - - // Check coverage if the transaction is new or if the simulation data has - // changed. - if (!previousTransaction || simulationDataChanged) { - this.checkCoverage(transaction).catch( - // istanbul ignore next - (error) => log('Error checking coverage:', error), - ); + if ( + // We don't need to check coverage for submitted or confirmed transactions. + transaction.status !== TransactionStatus.submitted && + transaction.status !== TransactionStatus.confirmed + ) { + // Check if the simulation data has changed. + const simulationDataChanged = + // only check if the previous transaction has simulation data and if it has changed + // this is to avoid checking coverage for the `TWICE` (once when it's added to the state and once when it's simulated for the first time). + // we only need to update the coverage result when the simulation data has changed. + previousTransaction?.simulationData && + !isEqual( + transaction.simulationData, + previousTransaction.simulationData, + ); + + // Check coverage if the transaction is new or if the simulation data has + // changed. + if (!previousTransaction || simulationDataChanged) { + this.checkCoverage(transaction).catch( + // istanbul ignore next + (error) => log('Error checking coverage:', error), + ); + } } // Log transaction once it has been submitted. @@ -446,11 +452,17 @@ export class ShieldController extends BaseController< throw new Error('Transaction hash not found'); } + const rawTransactionHex = txMeta.rawTx; + if (!rawTransactionHex) { + throw new Error('Raw transaction hex not found'); + } + const { status } = this.#getCoverageStatus(txMeta.id); await this.#backend.logTransaction({ txMeta, transactionHash, + rawTransactionHex, status, }); } diff --git a/packages/shield-controller/src/backend.test.ts b/packages/shield-controller/src/backend.test.ts index b5777f6bbc7..820986bc948 100644 --- a/packages/shield-controller/src/backend.test.ts +++ b/packages/shield-controller/src/backend.test.ts @@ -362,6 +362,7 @@ describe('ShieldRemoteBackend', () => { await backend.logTransaction({ txMeta: generateMockTxMeta(), transactionHash: '0x00', + rawTransactionHex: '0xdeadbeef', status: 'shown', }); expect(fetchMock).toHaveBeenCalledTimes(1); @@ -377,6 +378,7 @@ describe('ShieldRemoteBackend', () => { backend.logTransaction({ txMeta: generateMockTxMeta(), transactionHash: '0x00', + rawTransactionHex: '0xdeadbeef', status: 'shown', }), ).rejects.toThrow('Failed to log transaction: 500'); diff --git a/packages/shield-controller/src/backend.ts b/packages/shield-controller/src/backend.ts index 0db6e677ef2..cd94bc910a7 100644 --- a/packages/shield-controller/src/backend.ts +++ b/packages/shield-controller/src/backend.ts @@ -177,8 +177,10 @@ export class ShieldRemoteBackend implements ShieldBackend { async logTransaction(req: LogTransactionRequest): Promise { const initBody = makeInitCoverageCheckBody(req.txMeta); + const body = { transactionHash: req.transactionHash, + rawTransactionHex: req.rawTransactionHex, status: req.status, ...initBody, }; diff --git a/packages/shield-controller/src/types.ts b/packages/shield-controller/src/types.ts index 1c9a911c691..7f2d32fb183 100644 --- a/packages/shield-controller/src/types.ts +++ b/packages/shield-controller/src/types.ts @@ -23,6 +23,7 @@ export type LogSignatureRequest = { export type LogTransactionRequest = { txMeta: TransactionMeta; transactionHash: string; + rawTransactionHex: string; status: string; }; diff --git a/packages/shield-controller/tests/utils.ts b/packages/shield-controller/tests/utils.ts index 70f00b0be8d..5243d32804c 100644 --- a/packages/shield-controller/tests/utils.ts +++ b/packages/shield-controller/tests/utils.ts @@ -34,6 +34,7 @@ export function generateMockTxMeta(): TransactionMeta { type: TransactionType.contractInteraction, origin: 'https://metamask.io', submittedTime: Date.now(), + rawTx: '0xdeadbeef', }; }