From 0b3ed4342fc485687ec15ed88ac0645ccf22b077 Mon Sep 17 00:00:00 2001 From: Derek Brans Date: Fri, 31 May 2024 10:14:57 -0400 Subject: [PATCH] fix: TransactionController afterSign hook should be allowed to modify the transaction (#4343) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > [!NOTE] > This change is intended to be cherry-picked into MetaMask/extension v11.17.0 via an upgrade to the @metamask/transaction-controller package. ## Explanation **Issue:** A recent update to the transaction-controller has made the TransactionMeta object passed to the `afterSign` hook frozen. This change prevents adding new properties, leading to the error: “Cannot add property custodyId, object is not extensible.” This bug is breaking all transactions for MMI as the original txMeta cannot store required properties like custodyId. **Fix:** We deep clone the transaction meta before passing it to the hook. A deep clone is used because transactionMeta is recursively frozen by immer. This fix was intended to minimize the change for the cherry-pick going into v11.17.0. A longer term solution might involve using immer more throughout the TransactionController.ts file to make it clearer when a transactionMeta is being mutated and how. **Testing:** This fix was applied to and verified with the MMI extension. ## References ## Changelog ### `@metamask/transaction-controller` - **FIXED**: afterSign hook is now able to modify the transaction ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've highlighted breaking changes using the "BREAKING" category above as appropriate --- .../transaction-controller/src/TransactionController.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/transaction-controller/src/TransactionController.ts b/packages/transaction-controller/src/TransactionController.ts index 76a8158ddf..06010fc314 100644 --- a/packages/transaction-controller/src/TransactionController.ts +++ b/packages/transaction-controller/src/TransactionController.ts @@ -3289,9 +3289,10 @@ export class TransactionController extends BaseController< return undefined; } - if (!this.afterSign(transactionMeta, signedTx)) { + const transactionMetaFromHook = cloneDeep(transactionMeta); + if (!this.afterSign(transactionMetaFromHook, signedTx)) { this.updateTransaction( - transactionMeta, + transactionMetaFromHook, 'TransactionController#signTransaction - Update after sign', ); @@ -3301,7 +3302,7 @@ export class TransactionController extends BaseController< } const transactionMetaWithRsv = { - ...this.updateTransactionMetaRSV(transactionMeta, signedTx), + ...this.updateTransactionMetaRSV(transactionMetaFromHook, signedTx), status: TransactionStatus.signed as const, };