Skip to content

Commit d785af7

Browse files
montelaidevccharly
authored andcommitted
fix: update transaction controllers to use selected account (#4244)
This pr updates the transaction controller to use account id from the InternalAccount instead of an address Related to https://github.com/MetaMask/accounts-planning/issues/381 - **BREAKING**: `getSelectedAddress` is replaced with `getSelectedAccount` in the `TransactionController` - **BREAKING**: `getCurrentAccount` returns an `InternalAccount` instead of a `string` in the `IncomingTransactionHelper` - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate Fixes https://github.com/MetaMask/accounts-planning/issues/381 --------- Co-authored-by: Charly Chevalier <[email protected]>
1 parent 6afa236 commit d785af7

File tree

9 files changed

+245
-66
lines changed

9 files changed

+245
-66
lines changed

packages/transaction-controller/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@
6666
},
6767
"devDependencies": {
6868
"@babel/runtime": "^7.23.9",
69+
"@metamask/accounts-controller": "^16.0.0",
6970
"@metamask/auto-changelog": "^3.4.4",
7071
"@metamask/eth-json-rpc-provider": "^4.0.0",
7172
"@metamask/ethjs-provider-http": "^0.3.0",
73+
"@metamask/keyring-api": "^6.4.0",
7274
"@types/bn.js": "^5.1.5",
7375
"@types/jest": "^27.4.1",
7476
"@types/node": "^16.18.54",
@@ -84,6 +86,7 @@
8486
},
8587
"peerDependencies": {
8688
"@babel/runtime": "^7.23.9",
89+
"@metamask/accounts-controller": "^16.0.0",
8790
"@metamask/approval-controller": "^6.0.2",
8891
"@metamask/gas-fee-controller": "^16.0.0",
8992
"@metamask/network-controller": "^18.1.3"

packages/transaction-controller/src/TransactionController.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
1818
import EthQuery from '@metamask/eth-query';
1919
import HttpProvider from '@metamask/ethjs-provider-http';
20+
import type { InternalAccount } from '@metamask/keyring-api';
21+
import { EthAccountType } from '@metamask/keyring-api';
2022
import type {
2123
BlockTracker,
2224
NetworkController,
@@ -439,6 +441,20 @@ const MOCK_CUSTOM_NETWORK: MockNetwork = {
439441
};
440442

441443
const ACCOUNT_MOCK = '0x6bf137f335ea1b8f193b8f6ea92561a60d23a207';
444+
const INTERNAL_ACCOUNT_MOCK = {
445+
id: '58def058-d35f-49a1-a7ab-e2580565f6f5',
446+
address: ACCOUNT_MOCK,
447+
type: EthAccountType.Eoa,
448+
options: {},
449+
methods: [],
450+
metadata: {
451+
name: 'Account 1',
452+
keyring: { type: 'HD Key Tree' },
453+
importTime: 1631619180000,
454+
lastSelected: 1631619180000,
455+
},
456+
};
457+
442458
const ACCOUNT_2_MOCK = '0x08f137f335ea1b8f193b8f6ea92561a60d23a211';
443459
const NONCE_MOCK = 12;
444460
const ACTION_ID_MOCK = '123456';
@@ -551,12 +567,14 @@ describe('TransactionController', () => {
551567
* messenger.
552568
* @param args.messengerOptions.addTransactionApprovalRequest - Options to mock
553569
* the `ApprovalController:addRequest` action call for transactions.
570+
* @param args.selectedAccount - The selected account to use with the controller.
554571
* @returns The new TransactionController instance.
555572
*/
556573
function setupController({
557574
options: givenOptions = {},
558575
network = MOCK_NETWORK,
559576
messengerOptions = {},
577+
selectedAccount = INTERNAL_ACCOUNT_MOCK,
560578
}: {
561579
options?: Partial<ConstructorParameters<typeof TransactionController>[0]>;
562580
network?: MockNetwork;
@@ -565,6 +583,7 @@ describe('TransactionController', () => {
565583
typeof mockAddTransactionApprovalRequest
566584
>[1];
567585
};
586+
selectedAccount?: InternalAccount;
568587
} = {}) {
569588
const unrestrictedMessenger: UnrestrictedControllerMessenger =
570589
new ControllerMessenger();
@@ -587,7 +606,6 @@ describe('TransactionController', () => {
587606
// eslint-disable-next-line @typescript-eslint/no-explicit-any
588607
getNetworkClientRegistry: () => ({} as any),
589608
getPermittedAccounts: async () => [ACCOUNT_MOCK],
590-
getSelectedAddress: () => ACCOUNT_MOCK,
591609
isMultichainEnabled: false,
592610
hooks: {},
593611
onNetworkStateChange: network.subscribe,
@@ -605,10 +623,17 @@ describe('TransactionController', () => {
605623
'ApprovalController:addRequest',
606624
'NetworkController:getNetworkClientById',
607625
'NetworkController:findNetworkClientIdByChainId',
626+
'AccountsController:getSelectedAccount',
608627
],
609628
allowedEvents: [],
610629
});
611630

631+
const mockGetSelectedAccount = jest.fn().mockReturnValue(selectedAccount);
632+
unrestrictedMessenger.registerActionHandler(
633+
'AccountsController:getSelectedAccount',
634+
mockGetSelectedAccount,
635+
);
636+
612637
const controller = new TransactionController({
613638
...otherOptions,
614639
messenger: restrictedMessenger,
@@ -618,6 +643,7 @@ describe('TransactionController', () => {
618643
controller,
619644
messenger: unrestrictedMessenger,
620645
mockTransactionApprovalRequest,
646+
mockGetSelectedAccount,
621647
};
622648
}
623649

packages/transaction-controller/src/TransactionController.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Hardfork, Common, type ChainConfig } from '@ethereumjs/common';
22
import type { TypedTransaction } from '@ethereumjs/tx';
33
import { TransactionFactory } from '@ethereumjs/tx';
44
import { bufferToHex } from '@ethereumjs/util';
5+
import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';
56
import type {
67
AcceptResultCallbacks,
78
AddApprovalRequest,
@@ -297,7 +298,6 @@ export type TransactionControllerOptions = {
297298
getNetworkState: () => NetworkState;
298299
getPermittedAccounts: (origin?: string) => Promise<string[]>;
299300
getSavedGasFees?: (chainId: Hex) => SavedGasFees | undefined;
300-
getSelectedAddress: () => string;
301301
incomingTransactions?: IncomingTransactionOptions;
302302
isMultichainEnabled: boolean;
303303
isSimulationEnabled?: () => boolean;
@@ -344,7 +344,8 @@ const controllerName = 'TransactionController';
344344
export type AllowedActions =
345345
| AddApprovalRequest
346346
| NetworkControllerFindNetworkClientIdByChainIdAction
347-
| NetworkControllerGetNetworkClientByIdAction;
347+
| NetworkControllerGetNetworkClientByIdAction
348+
| AccountsControllerGetSelectedAccountAction;
348349

349350
/**
350351
* The external events available to the {@link TransactionController}.
@@ -614,8 +615,6 @@ export class TransactionController extends BaseController<
614615

615616
private readonly getPermittedAccounts: (origin?: string) => Promise<string[]>;
616617

617-
private readonly getSelectedAddress: () => string;
618-
619618
private readonly getExternalPendingTransactions: (
620619
address: string,
621620
chainId?: string,
@@ -733,7 +732,6 @@ export class TransactionController extends BaseController<
733732
* @param options.getNetworkState - Gets the state of the network controller.
734733
* @param options.getPermittedAccounts - Get accounts that a given origin has permissions for.
735734
* @param options.getSavedGasFees - Gets the saved gas fee config.
736-
* @param options.getSelectedAddress - Gets the address of the currently selected account.
737735
* @param options.incomingTransactions - Configuration options for incoming transaction support.
738736
* @param options.isMultichainEnabled - Enable multichain support.
739737
* @param options.isSimulationEnabled - Whether new transactions will be automatically simulated.
@@ -761,7 +759,6 @@ export class TransactionController extends BaseController<
761759
getNetworkState,
762760
getPermittedAccounts,
763761
getSavedGasFees,
764-
getSelectedAddress,
765762
incomingTransactions = {},
766763
isMultichainEnabled = false,
767764
isSimulationEnabled,
@@ -802,7 +799,6 @@ export class TransactionController extends BaseController<
802799
this.getGasFeeEstimates =
803800
getGasFeeEstimates || (() => Promise.resolve({} as GasFeeState));
804801
this.getPermittedAccounts = getPermittedAccounts;
805-
this.getSelectedAddress = getSelectedAddress;
806802
this.getExternalPendingTransactions =
807803
getExternalPendingTransactions ?? (() => []);
808804
this.securityProviderRequest = securityProviderRequest;
@@ -1035,7 +1031,7 @@ export class TransactionController extends BaseController<
10351031
if (origin) {
10361032
await validateTransactionOrigin(
10371033
await this.getPermittedAccounts(origin),
1038-
this.getSelectedAddress(),
1034+
this.#getSelectedAccount().address,
10391035
txParams.from,
10401036
origin,
10411037
);
@@ -3430,7 +3426,7 @@ export class TransactionController extends BaseController<
34303426
}): IncomingTransactionHelper {
34313427
const incomingTransactionHelper = new IncomingTransactionHelper({
34323428
blockTracker,
3433-
getCurrentAccount: this.getSelectedAddress,
3429+
getCurrentAccount: () => this.#getSelectedAccount(),
34343430
getLastFetchedBlockNumbers: () => this.state.lastFetchedBlockNumbers,
34353431
getChainId: chainId ? () => chainId : this.getChainId.bind(this),
34363432
isEnabled: this.#incomingTransactionOptions.isEnabled,
@@ -3843,4 +3839,8 @@ export class TransactionController extends BaseController<
38433839
).configuration.type === NetworkClientType.Custom
38443840
);
38453841
}
3842+
3843+
#getSelectedAccount() {
3844+
return this.messagingSystem.call('AccountsController:getSelectedAccount');
3845+
}
38463846
}

0 commit comments

Comments
 (0)