@metamask/accounts-controller 10.0.0 → 12.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- {"version":3,"file":"AccountsController.js","sourceRoot":"","sources":["../src/AccountsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAKA,+DAA2D;AAC3D,iEAAyD;AAEzD,uDAAkE;AAClE,qEAA4D;AAe5D,qDAAmD;AAEnD,+BAAkC;AAElC,mCAA+E;AAE/E,MAAM,cAAc,GAAG,oBAAoB,CAAC;AA2F5C,MAAM,0BAA0B,GAAG;IACjC,gBAAgB,EAAE;QAChB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEF,MAAM,YAAY,GAA4B;IAC5C,gBAAgB,EAAE;QAChB,QAAQ,EAAE,EAAE;QACZ,eAAe,EAAE,EAAE;KACpB;CACF,CAAC;AAEF;;;;;;;GAOG;AACH,MAAa,kBAAmB,SAAQ,gCAIvC;IACC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,0BAA0B;YACpC,KAAK,kCACA,YAAY,GACZ,KAAK,CACT;SACF,CAAC,CAAC;;QAEH,IAAI,CAAC,eAAe,CAAC,SAAS,CAC5B,4BAA4B,EAC5B,CAAC,cAAc,EAAE,EAAE,CAAC,uBAAA,IAAI,kFAAyB,MAA7B,IAAI,EAA0B,cAAc,CAAC,CAClE,CAAC;QAEF,IAAI,CAAC,eAAe,CAAC,SAAS,CAC5B,+BAA+B,EAC/B,CAAC,YAAY,EAAE,EAAE,CAAC,uBAAA,IAAI,qFAA4B,MAAhC,IAAI,EAA6B,YAAY,CAAC,CACjE,CAAC;QAEF,uBAAA,IAAI,kFAAyB,MAA7B,IAAI,CAA2B,CAAC;IAClC,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,SAAiB;QAC1B,OAAO,IAAI,CAAC,KAAK,C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B,EACvC,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,CACpC,CAAC;IAEF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,+BAA+B,EAC/B,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAC3B,CAAC;AACJ,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedControllerMessenger,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport type { InternalAccount } from '@metamask/keyring-api';\nimport { EthAccountType, EthMethod } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n KeyringControllerState,\n KeyringControllerEvents,\n KeyringControllerGetKeyringForAccountAction,\n KeyringControllerGetKeyringsByTypeAction,\n KeyringControllerGetAccountsAction,\n} from '@metamask/keyring-controller';\nimport type {\n SnapControllerEvents,\n SnapControllerState,\n} from '@metamask/snaps-controllers';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport type { Snap } from '@metamask/snaps-utils';\nimport type { Keyring, Json } from '@metamask/utils';\nimport { sha256FromString } from 'ethereumjs-util';\nimport type { Draft } from 'immer';\nimport { v4 as uuid } from 'uuid';\n\nimport { getUUIDFromAddressOfNormalAccount, keyringTypeToName } from './utils';\n\nconst controllerName = 'AccountsController';\n\nexport type AccountsControllerState = {\n internalAccounts: {\n accounts: Record<string, InternalAccount>;\n selectedAccount: string; // id of the selected account\n };\n};\n\nexport type AccountsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AccountsControllerState\n>;\n\nexport type AccountsControllerSetSelectedAccountAction = {\n type: `${typeof controllerName}:setSelectedAccount`;\n handler: AccountsController['setSelectedAccount'];\n};\n\nexport type AccountsControllerSetAccountNameAction = {\n type: `${typeof controllerName}:setAccountName`;\n handler: AccountsController['setAccountName'];\n};\n\nexport type AccountsControllerListAccountsAction = {\n type: `${typeof controllerName}:listAccounts`;\n handler: AccountsController['listAccounts'];\n};\n\nexport type AccountsControllerUpdateAccountsAction = {\n type: `${typeof controllerName}:updateAccounts`;\n handler: AccountsController['updateAccounts'];\n};\n\nexport type AccountsControllerGetSelectedAccountAction = {\n type: `${typeof controllerName}:getSelectedAccount`;\n handler: AccountsController['getSelectedAccount'];\n};\n\nexport type AccountsControllerGetAccountByAddressAction = {\n type: `${typeof controllerName}:getAccountByAddress`;\n handler: AccountsController['getAccountByAddress'];\n};\n\nexport type AccountsControllerGetAccountAction = {\n type: `${typeof controllerName}:getAccount`;\n handler: AccountsController['getAccount'];\n};\n\nexport type AccountsControllerActions =\n | AccountsControllerGetStateAction\n | AccountsControllerSetSelectedAccountAction\n | AccountsControllerListAccountsAction\n | AccountsControllerSetAccountNameAction\n | AccountsControllerUpdateAccountsAction\n | AccountsControllerGetAccountByAddressAction\n | AccountsControllerGetSelectedAccountAction\n | AccountsControllerGetAccountAction\n | KeyringControllerGetKeyringForAccountAction\n | KeyringControllerGetKeyringsByTypeAction\n | KeyringControllerGetAccountsAction;\n\nexport type AccountsControllerChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AccountsControllerState\n>;\n\nexport type AccountsControllerSelectedAccountChangeEvent = {\n type: `${typeof controllerName}:selectedAccountChange`;\n payload: [InternalAccount];\n};\n\nexport type AccountsControllerEvents =\n | AccountsControllerChangeEvent\n | AccountsControllerSelectedAccountChangeEvent\n | SnapControllerEvents\n | KeyringControllerEvents;\n\nexport type AccountsControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n AccountsControllerActions,\n AccountsControllerEvents,\n string,\n string\n>;\n\ntype AddressAndKeyringTypeObject = {\n address: string;\n type: string;\n};\n\nconst accountsControllerMetadata = {\n internalAccounts: {\n persist: true,\n anonymous: false,\n },\n};\n\nconst defaultState: AccountsControllerState = {\n internalAccounts: {\n accounts: {},\n selectedAccount: '',\n },\n};\n\n/**\n * Controller that manages internal accounts.\n * The accounts controller is responsible for creating and managing internal accounts.\n * It also provides convenience methods for accessing and updating the internal accounts.\n * The accounts controller also listens for keyring state changes and updates the internal accounts accordingly.\n * The accounts controller also listens for snap state changes and updates the internal accounts accordingly.\n *\n */\nexport class AccountsController extends BaseController<\n typeof controllerName,\n AccountsControllerState,\n AccountsControllerMessenger\n> {\n /**\n * Constructor for AccountsController.\n *\n * @param options - The controller options.\n * @param options.messenger - The messenger object.\n * @param options.state - Initial state to set on this controller\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: AccountsControllerMessenger;\n state: AccountsControllerState;\n }) {\n super({\n messenger,\n name: controllerName,\n metadata: accountsControllerMetadata,\n state: {\n ...defaultState,\n ...state,\n },\n });\n\n this.messagingSystem.subscribe(\n 'SnapController:stateChange',\n (snapStateState) => this.#handleOnSnapStateChange(snapStateState),\n );\n\n this.messagingSystem.subscribe(\n 'KeyringController:stateChange',\n (keyringState) => this.#handleOnKeyringStateChange(keyringState),\n );\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Returns the internal account object for the given account ID, if it exists.\n *\n * @param accountId - The ID of the account to retrieve.\n * @returns The internal account object, or undefined if the account does not exist.\n */\n getAccount(accountId: string): InternalAccount | undefined {\n return this.state.internalAccounts.accounts[accountId];\n }\n\n /**\n * Returns an array of all internal accounts.\n *\n * @returns An array of InternalAccount objects.\n */\n listAccounts(): InternalAccount[] {\n return Object.values(this.state.internalAccounts.accounts);\n }\n\n /**\n * Returns the internal account object for the given account ID.\n *\n * @param accountId - The ID of the account to retrieve.\n * @returns The internal account object.\n * @throws An error if the account ID is not found.\n */\n getAccountExpect(accountId: string): InternalAccount {\n // Edge case where the extension is setup but the srp is not yet created\n // certain ui elements will query the selected address before any accounts are created.\n if (!accountId) {\n return {\n id: '',\n address: '',\n options: {},\n methods: [],\n type: EthAccountType.Eoa,\n metadata: {\n name: '',\n keyring: {\n type: '',\n },\n },\n };\n }\n\n const account = this.getAccount(accountId);\n if (account === undefined) {\n throw new Error(`Account Id ${accountId} not found`);\n }\n return account;\n }\n\n /**\n * Returns the selected internal account.\n *\n * @returns The selected internal account.\n */\n getSelectedAccount(): InternalAccount {\n return this.getAccountExpect(this.state.internalAccounts.selectedAccount);\n }\n\n /**\n * Returns the account with the specified address.\n * ! This method will only return the first account that matches the address\n * @param address - The address of the account to retrieve.\n * @returns The account with the specified address, or undefined if not found.\n */\n getAccountByAddress(address: string): InternalAccount | undefined {\n return this.listAccounts().find(\n (account) => account.address.toLowerCase() === address.toLowerCase(),\n );\n }\n\n /**\n * Sets the selected account by its ID.\n *\n * @param accountId - The ID of the account to be selected.\n */\n setSelectedAccount(accountId: string): void {\n const account = this.getAccount(accountId);\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n if (account) {\n currentState.internalAccounts.accounts[\n account.id\n ].metadata.lastSelected = Date.now();\n currentState.internalAccounts.selectedAccount = account.id;\n } else {\n currentState.internalAccounts.selectedAccount = '';\n }\n });\n\n if (account) {\n this.messagingSystem.publish(\n 'AccountsController:selectedAccountChange',\n account,\n );\n }\n }\n\n /**\n * Sets the name of the account with the given ID.\n *\n * @param accountId - The ID of the account to set the name for.\n * @param accountName - The new name for the account.\n * @throws An error if an account with the same name already exists.\n */\n setAccountName(accountId: string, accountName: string): void {\n const account = this.getAccountExpect(accountId);\n\n if (\n this.listAccounts().find(\n (internalAccount) =>\n internalAccount.metadata.name === accountName &&\n internalAccount.id !== accountId,\n )\n ) {\n throw new Error('Account name already exists');\n }\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n const internalAccount = {\n ...account,\n metadata: { ...account.metadata, name: accountName },\n };\n currentState.internalAccounts.accounts[accountId] =\n // @ts-expect-error Assigning a complex type `T` to `Draft<T>` causes an excessive type instantiation depth error.\n internalAccount as Draft<InternalAccount>;\n });\n }\n\n /**\n * Updates the internal accounts list by retrieving normal and snap accounts,\n * removing duplicates, and updating the metadata of each account.\n *\n * @returns A Promise that resolves when the accounts have been updated.\n */\n async updateAccounts(): Promise<void> {\n const snapAccounts: InternalAccount[] = await this.#listSnapAccounts();\n const normalAccounts = (await this.#listNormalAccounts()).filter(\n (account) =>\n !snapAccounts.find(\n (snapAccount) => snapAccount.address === account.address,\n ),\n );\n\n // keyring type map.\n const keyringTypes = new Map<string, number>();\n const previousAccounts = this.state.internalAccounts.accounts;\n\n const accounts: Record<string, InternalAccount> = [\n ...normalAccounts,\n ...snapAccounts,\n ].reduce((internalAccountMap, internalAccount) => {\n const keyringTypeName = keyringTypeToName(\n internalAccount.metadata.keyring.type,\n );\n const keyringAccountIndex = keyringTypes.get(keyringTypeName) ?? 0;\n if (keyringAccountIndex) {\n keyringTypes.set(keyringTypeName, keyringAccountIndex + 1);\n } else {\n keyringTypes.set(keyringTypeName, 1);\n }\n\n const existingAccount = previousAccounts[internalAccount.id];\n\n internalAccountMap[internalAccount.id] = {\n ...internalAccount,\n\n metadata: {\n ...internalAccount.metadata,\n name:\n existingAccount && existingAccount.metadata.name !== ''\n ? existingAccount.metadata.name\n : `${keyringTypeName} ${keyringAccountIndex + 1}`,\n lastSelected: existingAccount?.metadata?.lastSelected,\n },\n };\n\n return internalAccountMap;\n }, {} as Record<string, InternalAccount>);\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n (currentState as AccountsControllerState).internalAccounts.accounts =\n accounts;\n });\n }\n\n /**\n * Loads the backup state of the accounts controller.\n *\n * @param backup - The backup state to load.\n */\n loadBackup(backup: AccountsControllerState): void {\n if (backup.internalAccounts) {\n this.update((currentState: Draft<AccountsControllerState>) => {\n (currentState as AccountsControllerState).internalAccounts =\n backup.internalAccounts;\n });\n }\n }\n\n /**\n * Generates an internal account for a non-Snap account.\n * @param address - The address of the account.\n * @param type - The type of the account.\n * @returns The generated internal account.\n */\n #generateInternalAccountForNonSnapAccount(\n address: string,\n type: string,\n ): InternalAccount {\n return {\n id: getUUIDFromAddressOfNormalAccount(address),\n address,\n options: {},\n methods: [\n EthMethod.PersonalSign,\n EthMethod.Sign,\n EthMethod.SignTransaction,\n EthMethod.SignTypedDataV1,\n EthMethod.SignTypedDataV3,\n EthMethod.SignTypedDataV4,\n ],\n type: EthAccountType.Eoa,\n metadata: {\n name: '',\n keyring: {\n type,\n },\n },\n };\n }\n\n /**\n * Returns a list of internal accounts created using the SnapKeyring.\n *\n * @returns A promise that resolves to an array of InternalAccount objects.\n */\n async #listSnapAccounts(): Promise<InternalAccount[]> {\n const [snapKeyring] = this.messagingSystem.call(\n 'KeyringController:getKeyringsByType',\n SnapKeyring.type,\n );\n // snap keyring is not available until the first account is created in the keyring controller\n if (!snapKeyring) {\n return [];\n }\n\n const snapAccounts = (snapKeyring as SnapKeyring).listAccounts();\n\n return snapAccounts;\n }\n\n /**\n * Returns a list of normal accounts.\n * Note: listNormalAccounts is a temporary method until the keyrings all implement the InternalAccount interface.\n * Once all keyrings implement the InternalAccount interface, this method can be removed and getAccounts can be used instead.\n *\n * @returns A Promise that resolves to an array of InternalAccount objects.\n */\n async #listNormalAccounts(): Promise<InternalAccount[]> {\n const addresses = await this.messagingSystem.call(\n 'KeyringController:getAccounts',\n );\n const internalAccounts: InternalAccount[] = [];\n for (const address of addresses) {\n const keyring = await this.messagingSystem.call(\n 'KeyringController:getKeyringForAccount',\n address,\n );\n const v4options = {\n random: sha256FromString(address).slice(0, 16),\n };\n\n internalAccounts.push({\n id: uuid(v4options),\n address,\n options: {},\n methods: [\n EthMethod.PersonalSign,\n EthMethod.Sign,\n EthMethod.SignTransaction,\n EthMethod.SignTypedDataV1,\n EthMethod.SignTypedDataV3,\n EthMethod.SignTypedDataV4,\n ],\n type: EthAccountType.Eoa,\n metadata: {\n name: '',\n keyring: {\n type: (keyring as Keyring<Json>).type,\n },\n },\n });\n }\n\n return internalAccounts.filter(\n (account) => account.metadata.keyring.type !== KeyringTypes.snap,\n );\n }\n\n /**\n * Handles changes in the keyring state, specifically when new accounts are added or removed.\n *\n * @param keyringState - The new state of the keyring controller.\n */\n #handleOnKeyringStateChange(keyringState: KeyringControllerState): void {\n // check if there are any new accounts added\n // TODO: change when accountAdded event is added to the keyring controller\n\n // We check for keyrings length to be greater than 0 because the extension client may try execute\n // submit password twice and clear the keyring state.\n // https://github.com/MetaMask/KeyringController/blob/2d73a4deed8d013913f6ef0c9f5c0bb7c614f7d3/src/KeyringController.ts#L910\n if (keyringState.isUnlocked && keyringState.keyrings.length > 0) {\n const updatedNormalKeyringAddresses: AddressAndKeyringTypeObject[] = [];\n const updatedSnapKeyringAddresses: AddressAndKeyringTypeObject[] = [];\n\n for (const keyring of keyringState.keyrings) {\n if (keyring.type === KeyringTypes.snap) {\n updatedSnapKeyringAddresses.push(\n ...keyring.accounts.map((address) => {\n return {\n address,\n type: keyring.type,\n };\n }),\n );\n } else {\n updatedNormalKeyringAddresses.push(\n ...keyring.accounts.map((address) => {\n return {\n address,\n type: keyring.type,\n };\n }),\n );\n }\n }\n\n const { previousNormalInternalAccounts, previousSnapInternalAccounts } =\n this.listAccounts().reduce(\n (accumulator, account) => {\n if (account.metadata.keyring.type === KeyringTypes.snap) {\n accumulator.previousSnapInternalAccounts.push(account);\n } else {\n accumulator.previousNormalInternalAccounts.push(account);\n }\n return accumulator;\n },\n {\n previousNormalInternalAccounts: [] as InternalAccount[],\n previousSnapInternalAccounts: [] as InternalAccount[],\n },\n );\n\n const addedAccounts: AddressAndKeyringTypeObject[] = [];\n const deletedAccounts: InternalAccount[] = [];\n\n // snap account ids are random uuid while normal accounts\n // are determininistic based on the address\n\n // ^NOTE: This will be removed when normal accounts also implement internal accounts\n // finding all the normal accounts that were added\n for (const account of updatedNormalKeyringAddresses) {\n if (\n !this.state.internalAccounts.accounts[\n getUUIDFromAddressOfNormalAccount(account.address)\n ]\n ) {\n addedAccounts.push(account);\n }\n }\n\n // finding all the snap accounts that were added\n for (const account of updatedSnapKeyringAddresses) {\n if (\n !previousSnapInternalAccounts.find(\n (internalAccount) =>\n internalAccount.address.toLowerCase() ===\n account.address.toLowerCase(),\n )\n ) {\n addedAccounts.push(account);\n }\n }\n\n // finding all the normal accounts that were deleted\n for (const account of previousNormalInternalAccounts) {\n if (\n !updatedNormalKeyringAddresses.find(\n ({ address }) =>\n address.toLowerCase() === account.address.toLowerCase(),\n )\n ) {\n deletedAccounts.push(account);\n }\n }\n\n // finding all the snap accounts that were deleted\n for (const account of previousSnapInternalAccounts) {\n if (\n !updatedSnapKeyringAddresses.find(\n ({ address }) =>\n address.toLowerCase() === account.address.toLowerCase(),\n )\n ) {\n deletedAccounts.push(account);\n }\n }\n\n if (deletedAccounts.length > 0) {\n for (const account of deletedAccounts) {\n this.#handleAccountRemoved(account.id);\n }\n }\n\n if (addedAccounts.length > 0) {\n for (const account of addedAccounts) {\n this.#handleNewAccountAdded(account);\n }\n }\n\n // handle if the selected account was deleted\n if (!this.getAccount(this.state.internalAccounts.selectedAccount)) {\n const [accountToSelect] = this.listAccounts().sort(\n (accountA, accountB) => {\n // sort by lastSelected descending\n return (\n (accountB.metadata.lastSelected ?? 0) -\n (accountA.metadata.lastSelected ?? 0)\n );\n },\n );\n\n // if the accountToSelect is undefined, then there are no accounts\n // it mean the keyring was reinitialized.\n this.setSelectedAccount(accountToSelect?.id);\n }\n }\n }\n\n /**\n * Handles the change in SnapControllerState by updating the metadata of accounts that have a snap enabled.\n *\n * @param snapState - The new SnapControllerState.\n */\n #handleOnSnapStateChange(snapState: SnapControllerState) {\n // only check if snaps changed in status\n const { snaps } = snapState;\n const accounts = this.listAccounts().filter(\n (account) => account.metadata.snap,\n );\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n accounts.forEach((account) => {\n const currentAccount =\n currentState.internalAccounts.accounts[account.id];\n if (currentAccount.metadata.snap) {\n const snapId = currentAccount.metadata.snap.id;\n const storedSnap: Snap = snaps[snapId as SnapId];\n if (storedSnap) {\n currentAccount.metadata.snap.enabled =\n storedSnap.enabled && !storedSnap.blocked;\n }\n }\n });\n });\n }\n\n /**\n * Returns the next account number for a given keyring type.\n * @param keyringType - The type of keyring.\n * @returns An object containing the account prefix and index to use.\n */\n #getNextAccountNumber(keyringType: string): {\n accountPrefix: string;\n indexToUse: number;\n } {\n const keyringName = keyringTypeToName(keyringType);\n const previousKeyringAccounts = this.listAccounts().filter(\n (internalAccount) => {\n if (\n keyringType === KeyringTypes.hd ||\n keyringType === KeyringTypes.simple\n ) {\n return (\n internalAccount.metadata.keyring.type === KeyringTypes.hd ||\n internalAccount.metadata.keyring.type === KeyringTypes.simple\n );\n }\n return internalAccount.metadata.keyring.type === keyringType;\n },\n );\n const lastDefaultIndexUsedForKeyringType =\n previousKeyringAccounts\n .filter((internalAccount) =>\n new RegExp(`${keyringName} \\\\d+$`, 'u').test(\n internalAccount.metadata.name,\n ),\n )\n .map((internalAccount) => {\n const nameToWords = internalAccount.metadata.name.split(' '); // get the index of a default account name\n return parseInt(nameToWords[nameToWords.length], 10);\n })\n .sort((a, b) => b - a)[0] || 0;\n\n const indexToUse = Math.max(\n previousKeyringAccounts.length + 1,\n lastDefaultIndexUsedForKeyringType + 1,\n );\n\n return { accountPrefix: keyringName, indexToUse };\n }\n\n /**\n * Handles the addition of a new account to the controller.\n * If the account is not a Snap Keyring account, generates an internal account for it and adds it to the controller.\n * If the account is a Snap Keyring account, retrieves the account from the keyring and adds it to the controller.\n * @param account - The address and keyring type object of the new account.\n */\n #handleNewAccountAdded(account: AddressAndKeyringTypeObject) {\n let newAccount: InternalAccount;\n if (account.type !== KeyringTypes.snap) {\n newAccount = this.#generateInternalAccountForNonSnapAccount(\n account.address,\n account.type,\n );\n } else {\n const [snapKeyring] = this.messagingSystem.call(\n 'KeyringController:getKeyringsByType',\n SnapKeyring.type,\n );\n\n newAccount = (snapKeyring as SnapKeyring).getAccountByAddress(\n account.address,\n ) as InternalAccount;\n\n // The snap deleted the account before the keyring controller could add it\n if (!newAccount) {\n return;\n }\n }\n\n // get next index number for the keyring type\n const { accountPrefix, indexToUse } = this.#getNextAccountNumber(\n newAccount.metadata.keyring.type,\n );\n\n const accountName = `${accountPrefix} ${indexToUse}`;\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n (currentState as AccountsControllerState).internalAccounts.accounts[\n newAccount.id\n ] = {\n ...newAccount,\n metadata: {\n ...newAccount.metadata,\n name: accountName,\n lastSelected: Date.now(),\n },\n };\n });\n\n this.setSelectedAccount(newAccount.id);\n }\n\n /**\n * Handles the removal of an account from the internal accounts list.\n * @param accountId - The ID of the account to be removed.\n */\n #handleAccountRemoved(accountId: string) {\n this.update((currentState: Draft<AccountsControllerState>) => {\n delete currentState.internalAccounts.accounts[accountId];\n });\n }\n\n /**\n * Registers message handlers for the AccountsController.\n * @private\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:setSelectedAccount`,\n this.setSelectedAccount.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:listAccounts`,\n this.listAccounts.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:setAccountName`,\n this.setAccountName.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateAccounts`,\n this.updateAccounts.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getSelectedAccount`,\n this.getSelectedAccount.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getAccountByAddress`,\n this.getAccountByAddress.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `AccountsController:getAccount`,\n this.getAccount.bind(this),\n );\n }\n}\n"]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -0,0 +1,8 @@
1
+ import {
2
+ AccountsController
3
+ } from "./chunk-GATPL76V.mjs";
4
+ import "./chunk-HD5HN6GK.mjs";
5
+ export {
6
+ AccountsController
7
+ };
8
+ //# sourceMappingURL=AccountsController.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,561 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+
6
+ var _chunkMF4BFCSUjs = require('./chunk-MF4BFCSU.js');
7
+
8
+ // src/AccountsController.ts
9
+ var _util = require('@ethereumjs/util');
10
+ var _basecontroller = require('@metamask/base-controller');
11
+ var _ethsnapkeyring = require('@metamask/eth-snap-keyring');
12
+ var _keyringapi = require('@metamask/keyring-api');
13
+ var _keyringcontroller = require('@metamask/keyring-controller');
14
+ var _sha256 = require('ethereum-cryptography/sha256');
15
+ var _uuid = require('uuid');
16
+ var controllerName = "AccountsController";
17
+ var accountsControllerMetadata = {
18
+ internalAccounts: {
19
+ persist: true,
20
+ anonymous: false
21
+ }
22
+ };
23
+ var defaultState = {
24
+ internalAccounts: {
25
+ accounts: {},
26
+ selectedAccount: ""
27
+ }
28
+ };
29
+ var _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn, _listSnapAccounts, listSnapAccounts_fn, _listNormalAccounts, listNormalAccounts_fn, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn, _handleOnSnapStateChange, handleOnSnapStateChange_fn, _getNextAccountNumber, getNextAccountNumber_fn, _handleNewAccountAdded, handleNewAccountAdded_fn, _handleAccountRemoved, handleAccountRemoved_fn, _registerMessageHandlers, registerMessageHandlers_fn;
30
+ var AccountsController = class extends _basecontroller.BaseController {
31
+ /**
32
+ * Constructor for AccountsController.
33
+ *
34
+ * @param options - The controller options.
35
+ * @param options.messenger - The messenger object.
36
+ * @param options.state - Initial state to set on this controller
37
+ */
38
+ constructor({
39
+ messenger,
40
+ state
41
+ }) {
42
+ super({
43
+ messenger,
44
+ name: controllerName,
45
+ metadata: accountsControllerMetadata,
46
+ state: {
47
+ ...defaultState,
48
+ ...state
49
+ }
50
+ });
51
+ /**
52
+ * Generates an internal account for a non-Snap account.
53
+ * @param address - The address of the account.
54
+ * @param type - The type of the account.
55
+ * @returns The generated internal account.
56
+ */
57
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _generateInternalAccountForNonSnapAccount);
58
+ /**
59
+ * Returns a list of internal accounts created using the SnapKeyring.
60
+ *
61
+ * @returns A promise that resolves to an array of InternalAccount objects.
62
+ */
63
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _listSnapAccounts);
64
+ /**
65
+ * Returns a list of normal accounts.
66
+ * Note: listNormalAccounts is a temporary method until the keyrings all implement the InternalAccount interface.
67
+ * Once all keyrings implement the InternalAccount interface, this method can be removed and getAccounts can be used instead.
68
+ *
69
+ * @returns A Promise that resolves to an array of InternalAccount objects.
70
+ */
71
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _listNormalAccounts);
72
+ /**
73
+ * Handles changes in the keyring state, specifically when new accounts are added or removed.
74
+ *
75
+ * @param keyringState - The new state of the keyring controller.
76
+ */
77
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleOnKeyringStateChange);
78
+ /**
79
+ * Handles the change in SnapControllerState by updating the metadata of accounts that have a snap enabled.
80
+ *
81
+ * @param snapState - The new SnapControllerState.
82
+ */
83
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleOnSnapStateChange);
84
+ /**
85
+ * Returns the next account number for a given keyring type.
86
+ * @param keyringType - The type of keyring.
87
+ * @returns An object containing the account prefix and index to use.
88
+ */
89
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _getNextAccountNumber);
90
+ /**
91
+ * Handles the addition of a new account to the controller.
92
+ * If the account is not a Snap Keyring account, generates an internal account for it and adds it to the controller.
93
+ * If the account is a Snap Keyring account, retrieves the account from the keyring and adds it to the controller.
94
+ * @param account - The address and keyring type object of the new account.
95
+ */
96
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleNewAccountAdded);
97
+ /**
98
+ * Handles the removal of an account from the internal accounts list.
99
+ * @param accountId - The ID of the account to be removed.
100
+ */
101
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleAccountRemoved);
102
+ /**
103
+ * Registers message handlers for the AccountsController.
104
+ * @private
105
+ */
106
+ _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _registerMessageHandlers);
107
+ this.messagingSystem.subscribe(
108
+ "SnapController:stateChange",
109
+ (snapStateState) => _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleOnSnapStateChange, handleOnSnapStateChange_fn).call(this, snapStateState)
110
+ );
111
+ this.messagingSystem.subscribe(
112
+ "KeyringController:stateChange",
113
+ (keyringState) => _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn).call(this, keyringState)
114
+ );
115
+ _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
116
+ }
117
+ /**
118
+ * Returns the internal account object for the given account ID, if it exists.
119
+ *
120
+ * @param accountId - The ID of the account to retrieve.
121
+ * @returns The internal account object, or undefined if the account does not exist.
122
+ */
123
+ getAccount(accountId) {
124
+ return this.state.internalAccounts.accounts[accountId];
125
+ }
126
+ /**
127
+ * Returns an array of all internal accounts.
128
+ *
129
+ * @returns An array of InternalAccount objects.
130
+ */
131
+ listAccounts() {
132
+ return Object.values(this.state.internalAccounts.accounts);
133
+ }
134
+ /**
135
+ * Returns the internal account object for the given account ID.
136
+ *
137
+ * @param accountId - The ID of the account to retrieve.
138
+ * @returns The internal account object.
139
+ * @throws An error if the account ID is not found.
140
+ */
141
+ getAccountExpect(accountId) {
142
+ if (!accountId) {
143
+ return {
144
+ id: "",
145
+ address: "",
146
+ options: {},
147
+ methods: [],
148
+ type: _keyringapi.EthAccountType.Eoa,
149
+ metadata: {
150
+ name: "",
151
+ keyring: {
152
+ type: ""
153
+ }
154
+ }
155
+ };
156
+ }
157
+ const account = this.getAccount(accountId);
158
+ if (account === void 0) {
159
+ throw new Error(`Account Id ${accountId} not found`);
160
+ }
161
+ return account;
162
+ }
163
+ /**
164
+ * Returns the selected internal account.
165
+ *
166
+ * @returns The selected internal account.
167
+ */
168
+ getSelectedAccount() {
169
+ return this.getAccountExpect(this.state.internalAccounts.selectedAccount);
170
+ }
171
+ /**
172
+ * Returns the account with the specified address.
173
+ * ! This method will only return the first account that matches the address
174
+ * @param address - The address of the account to retrieve.
175
+ * @returns The account with the specified address, or undefined if not found.
176
+ */
177
+ getAccountByAddress(address) {
178
+ return this.listAccounts().find(
179
+ (account) => account.address.toLowerCase() === address.toLowerCase()
180
+ );
181
+ }
182
+ /**
183
+ * Sets the selected account by its ID.
184
+ *
185
+ * @param accountId - The ID of the account to be selected.
186
+ */
187
+ setSelectedAccount(accountId) {
188
+ const account = this.getAccount(accountId);
189
+ this.update((currentState) => {
190
+ if (account) {
191
+ currentState.internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
192
+ currentState.internalAccounts.selectedAccount = account.id;
193
+ } else {
194
+ currentState.internalAccounts.selectedAccount = "";
195
+ }
196
+ });
197
+ if (account) {
198
+ this.messagingSystem.publish(
199
+ "AccountsController:selectedAccountChange",
200
+ account
201
+ );
202
+ }
203
+ }
204
+ /**
205
+ * Sets the name of the account with the given ID.
206
+ *
207
+ * @param accountId - The ID of the account to set the name for.
208
+ * @param accountName - The new name for the account.
209
+ * @throws An error if an account with the same name already exists.
210
+ */
211
+ setAccountName(accountId, accountName) {
212
+ const account = this.getAccountExpect(accountId);
213
+ if (this.listAccounts().find(
214
+ (internalAccount) => internalAccount.metadata.name === accountName && internalAccount.id !== accountId
215
+ )) {
216
+ throw new Error("Account name already exists");
217
+ }
218
+ this.update((currentState) => {
219
+ const internalAccount = {
220
+ ...account,
221
+ metadata: { ...account.metadata, name: accountName }
222
+ };
223
+ currentState.internalAccounts.accounts[accountId] = // @ts-expect-error Assigning a complex type `T` to `Draft<T>` causes an excessive type instantiation depth error.
224
+ internalAccount;
225
+ });
226
+ }
227
+ /**
228
+ * Updates the internal accounts list by retrieving normal and snap accounts,
229
+ * removing duplicates, and updating the metadata of each account.
230
+ *
231
+ * @returns A Promise that resolves when the accounts have been updated.
232
+ */
233
+ async updateAccounts() {
234
+ const snapAccounts = await _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _listSnapAccounts, listSnapAccounts_fn).call(this);
235
+ const normalAccounts = (await _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _listNormalAccounts, listNormalAccounts_fn).call(this)).filter(
236
+ (account) => !snapAccounts.find(
237
+ (snapAccount) => snapAccount.address === account.address
238
+ )
239
+ );
240
+ const keyringTypes = /* @__PURE__ */ new Map();
241
+ const previousAccounts = this.state.internalAccounts.accounts;
242
+ const accounts = [
243
+ ...normalAccounts,
244
+ ...snapAccounts
245
+ ].reduce((internalAccountMap, internalAccount) => {
246
+ const keyringTypeName = _chunkMF4BFCSUjs.keyringTypeToName.call(void 0,
247
+ internalAccount.metadata.keyring.type
248
+ );
249
+ const keyringAccountIndex = keyringTypes.get(keyringTypeName) ?? 0;
250
+ if (keyringAccountIndex) {
251
+ keyringTypes.set(keyringTypeName, keyringAccountIndex + 1);
252
+ } else {
253
+ keyringTypes.set(keyringTypeName, 1);
254
+ }
255
+ const existingAccount = previousAccounts[internalAccount.id];
256
+ internalAccountMap[internalAccount.id] = {
257
+ ...internalAccount,
258
+ metadata: {
259
+ ...internalAccount.metadata,
260
+ name: existingAccount && existingAccount.metadata.name !== "" ? existingAccount.metadata.name : `${keyringTypeName} ${keyringAccountIndex + 1}`,
261
+ lastSelected: existingAccount?.metadata?.lastSelected
262
+ }
263
+ };
264
+ return internalAccountMap;
265
+ }, {});
266
+ this.update((currentState) => {
267
+ currentState.internalAccounts.accounts = accounts;
268
+ });
269
+ }
270
+ /**
271
+ * Loads the backup state of the accounts controller.
272
+ *
273
+ * @param backup - The backup state to load.
274
+ */
275
+ loadBackup(backup) {
276
+ if (backup.internalAccounts) {
277
+ this.update((currentState) => {
278
+ currentState.internalAccounts = backup.internalAccounts;
279
+ });
280
+ }
281
+ }
282
+ };
283
+ _generateInternalAccountForNonSnapAccount = new WeakSet();
284
+ generateInternalAccountForNonSnapAccount_fn = function(address, type) {
285
+ return {
286
+ id: _chunkMF4BFCSUjs.getUUIDFromAddressOfNormalAccount.call(void 0, address),
287
+ address,
288
+ options: {},
289
+ methods: [
290
+ _keyringapi.EthMethod.PersonalSign,
291
+ _keyringapi.EthMethod.Sign,
292
+ _keyringapi.EthMethod.SignTransaction,
293
+ _keyringapi.EthMethod.SignTypedDataV1,
294
+ _keyringapi.EthMethod.SignTypedDataV3,
295
+ _keyringapi.EthMethod.SignTypedDataV4
296
+ ],
297
+ type: _keyringapi.EthAccountType.Eoa,
298
+ metadata: {
299
+ name: "",
300
+ keyring: {
301
+ type
302
+ }
303
+ }
304
+ };
305
+ };
306
+ _listSnapAccounts = new WeakSet();
307
+ listSnapAccounts_fn = async function() {
308
+ const [snapKeyring] = this.messagingSystem.call(
309
+ "KeyringController:getKeyringsByType",
310
+ _ethsnapkeyring.SnapKeyring.type
311
+ );
312
+ if (!snapKeyring) {
313
+ return [];
314
+ }
315
+ const snapAccounts = snapKeyring.listAccounts();
316
+ return snapAccounts;
317
+ };
318
+ _listNormalAccounts = new WeakSet();
319
+ listNormalAccounts_fn = async function() {
320
+ const addresses = await this.messagingSystem.call(
321
+ "KeyringController:getAccounts"
322
+ );
323
+ const internalAccounts = [];
324
+ for (const address of addresses) {
325
+ const keyring = await this.messagingSystem.call(
326
+ "KeyringController:getKeyringForAccount",
327
+ address
328
+ );
329
+ const v4options = {
330
+ random: _sha256.sha256.call(void 0, _util.toBuffer.call(void 0, address)).slice(0, 16)
331
+ };
332
+ internalAccounts.push({
333
+ id: _uuid.v4.call(void 0, v4options),
334
+ address,
335
+ options: {},
336
+ methods: [
337
+ _keyringapi.EthMethod.PersonalSign,
338
+ _keyringapi.EthMethod.Sign,
339
+ _keyringapi.EthMethod.SignTransaction,
340
+ _keyringapi.EthMethod.SignTypedDataV1,
341
+ _keyringapi.EthMethod.SignTypedDataV3,
342
+ _keyringapi.EthMethod.SignTypedDataV4
343
+ ],
344
+ type: _keyringapi.EthAccountType.Eoa,
345
+ metadata: {
346
+ name: "",
347
+ keyring: {
348
+ type: keyring.type
349
+ }
350
+ }
351
+ });
352
+ }
353
+ return internalAccounts.filter(
354
+ (account) => account.metadata.keyring.type !== _keyringcontroller.KeyringTypes.snap
355
+ );
356
+ };
357
+ _handleOnKeyringStateChange = new WeakSet();
358
+ handleOnKeyringStateChange_fn = function(keyringState) {
359
+ if (keyringState.isUnlocked && keyringState.keyrings.length > 0) {
360
+ const updatedNormalKeyringAddresses = [];
361
+ const updatedSnapKeyringAddresses = [];
362
+ for (const keyring of keyringState.keyrings) {
363
+ if (keyring.type === _keyringcontroller.KeyringTypes.snap) {
364
+ updatedSnapKeyringAddresses.push(
365
+ ...keyring.accounts.map((address) => {
366
+ return {
367
+ address,
368
+ type: keyring.type
369
+ };
370
+ })
371
+ );
372
+ } else {
373
+ updatedNormalKeyringAddresses.push(
374
+ ...keyring.accounts.map((address) => {
375
+ return {
376
+ address,
377
+ type: keyring.type
378
+ };
379
+ })
380
+ );
381
+ }
382
+ }
383
+ const { previousNormalInternalAccounts, previousSnapInternalAccounts } = this.listAccounts().reduce(
384
+ (accumulator, account) => {
385
+ if (account.metadata.keyring.type === _keyringcontroller.KeyringTypes.snap) {
386
+ accumulator.previousSnapInternalAccounts.push(account);
387
+ } else {
388
+ accumulator.previousNormalInternalAccounts.push(account);
389
+ }
390
+ return accumulator;
391
+ },
392
+ {
393
+ previousNormalInternalAccounts: [],
394
+ previousSnapInternalAccounts: []
395
+ }
396
+ );
397
+ const addedAccounts = [];
398
+ const deletedAccounts = [];
399
+ for (const account of updatedNormalKeyringAddresses) {
400
+ if (!this.state.internalAccounts.accounts[_chunkMF4BFCSUjs.getUUIDFromAddressOfNormalAccount.call(void 0, account.address)]) {
401
+ addedAccounts.push(account);
402
+ }
403
+ }
404
+ for (const account of updatedSnapKeyringAddresses) {
405
+ if (!previousSnapInternalAccounts.find(
406
+ (internalAccount) => internalAccount.address.toLowerCase() === account.address.toLowerCase()
407
+ )) {
408
+ addedAccounts.push(account);
409
+ }
410
+ }
411
+ for (const account of previousNormalInternalAccounts) {
412
+ if (!updatedNormalKeyringAddresses.find(
413
+ ({ address }) => address.toLowerCase() === account.address.toLowerCase()
414
+ )) {
415
+ deletedAccounts.push(account);
416
+ }
417
+ }
418
+ for (const account of previousSnapInternalAccounts) {
419
+ if (!updatedSnapKeyringAddresses.find(
420
+ ({ address }) => address.toLowerCase() === account.address.toLowerCase()
421
+ )) {
422
+ deletedAccounts.push(account);
423
+ }
424
+ }
425
+ if (deletedAccounts.length > 0) {
426
+ for (const account of deletedAccounts) {
427
+ _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleAccountRemoved, handleAccountRemoved_fn).call(this, account.id);
428
+ }
429
+ }
430
+ if (addedAccounts.length > 0) {
431
+ for (const account of addedAccounts) {
432
+ _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleNewAccountAdded, handleNewAccountAdded_fn).call(this, account);
433
+ }
434
+ }
435
+ if (!this.getAccount(this.state.internalAccounts.selectedAccount)) {
436
+ const [accountToSelect] = this.listAccounts().sort(
437
+ (accountA, accountB) => {
438
+ return (accountB.metadata.lastSelected ?? 0) - (accountA.metadata.lastSelected ?? 0);
439
+ }
440
+ );
441
+ this.setSelectedAccount(accountToSelect?.id);
442
+ }
443
+ }
444
+ };
445
+ _handleOnSnapStateChange = new WeakSet();
446
+ handleOnSnapStateChange_fn = function(snapState) {
447
+ const { snaps } = snapState;
448
+ const accounts = this.listAccounts().filter(
449
+ (account) => account.metadata.snap
450
+ );
451
+ this.update((currentState) => {
452
+ accounts.forEach((account) => {
453
+ const currentAccount = currentState.internalAccounts.accounts[account.id];
454
+ if (currentAccount.metadata.snap) {
455
+ const snapId = currentAccount.metadata.snap.id;
456
+ const storedSnap = snaps[snapId];
457
+ if (storedSnap) {
458
+ currentAccount.metadata.snap.enabled = storedSnap.enabled && !storedSnap.blocked;
459
+ }
460
+ }
461
+ });
462
+ });
463
+ };
464
+ _getNextAccountNumber = new WeakSet();
465
+ getNextAccountNumber_fn = function(keyringType) {
466
+ const keyringName = _chunkMF4BFCSUjs.keyringTypeToName.call(void 0, keyringType);
467
+ const previousKeyringAccounts = this.listAccounts().filter(
468
+ (internalAccount) => {
469
+ if (keyringType === _keyringcontroller.KeyringTypes.hd || keyringType === _keyringcontroller.KeyringTypes.simple) {
470
+ return internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.hd || internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.simple;
471
+ }
472
+ return internalAccount.metadata.keyring.type === keyringType;
473
+ }
474
+ );
475
+ const lastDefaultIndexUsedForKeyringType = previousKeyringAccounts.filter(
476
+ (internalAccount) => new RegExp(`${keyringName} \\d+$`, "u").test(
477
+ internalAccount.metadata.name
478
+ )
479
+ ).map((internalAccount) => {
480
+ const nameToWords = internalAccount.metadata.name.split(" ");
481
+ return parseInt(nameToWords[nameToWords.length], 10);
482
+ }).sort((a, b) => b - a)[0] || 0;
483
+ const indexToUse = Math.max(
484
+ previousKeyringAccounts.length + 1,
485
+ lastDefaultIndexUsedForKeyringType + 1
486
+ );
487
+ return { accountPrefix: keyringName, indexToUse };
488
+ };
489
+ _handleNewAccountAdded = new WeakSet();
490
+ handleNewAccountAdded_fn = function(account) {
491
+ let newAccount;
492
+ if (account.type !== _keyringcontroller.KeyringTypes.snap) {
493
+ newAccount = _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn).call(this, account.address, account.type);
494
+ } else {
495
+ const [snapKeyring] = this.messagingSystem.call(
496
+ "KeyringController:getKeyringsByType",
497
+ _ethsnapkeyring.SnapKeyring.type
498
+ );
499
+ newAccount = snapKeyring.getAccountByAddress(
500
+ account.address
501
+ );
502
+ if (!newAccount) {
503
+ return;
504
+ }
505
+ }
506
+ const { accountPrefix, indexToUse } = _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _getNextAccountNumber, getNextAccountNumber_fn).call(this, newAccount.metadata.keyring.type);
507
+ const accountName = `${accountPrefix} ${indexToUse}`;
508
+ this.update((currentState) => {
509
+ currentState.internalAccounts.accounts[newAccount.id] = {
510
+ ...newAccount,
511
+ metadata: {
512
+ ...newAccount.metadata,
513
+ name: accountName,
514
+ lastSelected: Date.now()
515
+ }
516
+ };
517
+ });
518
+ this.setSelectedAccount(newAccount.id);
519
+ };
520
+ _handleAccountRemoved = new WeakSet();
521
+ handleAccountRemoved_fn = function(accountId) {
522
+ this.update((currentState) => {
523
+ delete currentState.internalAccounts.accounts[accountId];
524
+ });
525
+ };
526
+ _registerMessageHandlers = new WeakSet();
527
+ registerMessageHandlers_fn = function() {
528
+ this.messagingSystem.registerActionHandler(
529
+ `${controllerName}:setSelectedAccount`,
530
+ this.setSelectedAccount.bind(this)
531
+ );
532
+ this.messagingSystem.registerActionHandler(
533
+ `${controllerName}:listAccounts`,
534
+ this.listAccounts.bind(this)
535
+ );
536
+ this.messagingSystem.registerActionHandler(
537
+ `${controllerName}:setAccountName`,
538
+ this.setAccountName.bind(this)
539
+ );
540
+ this.messagingSystem.registerActionHandler(
541
+ `${controllerName}:updateAccounts`,
542
+ this.updateAccounts.bind(this)
543
+ );
544
+ this.messagingSystem.registerActionHandler(
545
+ `${controllerName}:getSelectedAccount`,
546
+ this.getSelectedAccount.bind(this)
547
+ );
548
+ this.messagingSystem.registerActionHandler(
549
+ `${controllerName}:getAccountByAddress`,
550
+ this.getAccountByAddress.bind(this)
551
+ );
552
+ this.messagingSystem.registerActionHandler(
553
+ `AccountsController:getAccount`,
554
+ this.getAccount.bind(this)
555
+ );
556
+ };
557
+
558
+
559
+
560
+ exports.AccountsController = AccountsController;
561
+ //# sourceMappingURL=chunk-7FX4HSTV.js.map