@metamask/accounts-controller 14.0.0 → 16.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. package/CHANGELOG.md +34 -1
  2. package/dist/AccountsController.js +4 -3
  3. package/dist/AccountsController.mjs +3 -2
  4. package/dist/{chunk-J77K3UMX.mjs → chunk-3R7MA6KV.mjs} +166 -52
  5. package/dist/chunk-3R7MA6KV.mjs.map +1 -0
  6. package/dist/{chunk-YSAAOUCM.js → chunk-7CWWNB3P.js} +188 -74
  7. package/dist/chunk-7CWWNB3P.js.map +1 -0
  8. package/dist/{chunk-KCST55AQ.js → chunk-EIQ5DUI6.js} +10 -17
  9. package/dist/chunk-EIQ5DUI6.js.map +1 -0
  10. package/dist/chunk-IBSI66UQ.js +65 -0
  11. package/dist/chunk-IBSI66UQ.js.map +1 -0
  12. package/dist/chunk-QAHRSXGM.mjs +65 -0
  13. package/dist/chunk-QAHRSXGM.mjs.map +1 -0
  14. package/dist/chunk-UJIPPGP6.js +19 -0
  15. package/dist/chunk-UJIPPGP6.js.map +1 -0
  16. package/dist/{chunk-E7SBT5BV.mjs → chunk-YNILZSBK.mjs} +11 -18
  17. package/dist/chunk-YNILZSBK.mjs.map +1 -0
  18. package/dist/chunk-ZNSHBDHA.mjs +19 -0
  19. package/dist/chunk-ZNSHBDHA.mjs.map +1 -0
  20. package/dist/index.js +8 -3
  21. package/dist/index.mjs +7 -2
  22. package/dist/tests/mocks.js +8 -0
  23. package/dist/tests/mocks.js.map +1 -0
  24. package/dist/tests/mocks.mjs +8 -0
  25. package/dist/tests/mocks.mjs.map +1 -0
  26. package/dist/tsconfig.build.tsbuildinfo +1 -1
  27. package/dist/types/AccountsController.d.ts +39 -4
  28. package/dist/types/AccountsController.d.ts.map +1 -1
  29. package/dist/types/index.d.ts +2 -1
  30. package/dist/types/index.d.ts.map +1 -1
  31. package/dist/types/tests/mocks.d.ts +17 -0
  32. package/dist/types/tests/mocks.d.ts.map +1 -0
  33. package/dist/types/utils.d.ts +19 -0
  34. package/dist/types/utils.d.ts.map +1 -1
  35. package/dist/utils.js +7 -2
  36. package/dist/utils.mjs +6 -1
  37. package/package.json +11 -11
  38. package/dist/chunk-E7SBT5BV.mjs.map +0 -1
  39. package/dist/chunk-J77K3UMX.mjs.map +0 -1
  40. package/dist/chunk-KCST55AQ.js.map +0 -1
  41. package/dist/chunk-YSAAOUCM.js.map +0 -1
@@ -3,6 +3,7 @@ import { BaseController } from '@metamask/base-controller';
3
3
  import type { InternalAccount } from '@metamask/keyring-api';
4
4
  import type { KeyringControllerGetKeyringForAccountAction, KeyringControllerGetKeyringsByTypeAction, KeyringControllerGetAccountsAction, KeyringControllerStateChangeEvent } from '@metamask/keyring-controller';
5
5
  import type { SnapStateChange } from '@metamask/snaps-controllers';
6
+ import type { CaipChainId } from '@metamask/utils';
6
7
  declare const controllerName = "AccountsController";
7
8
  export type AccountsControllerState = {
8
9
  internalAccounts: {
@@ -31,23 +32,35 @@ export type AccountsControllerGetSelectedAccountAction = {
31
32
  type: `${typeof controllerName}:getSelectedAccount`;
32
33
  handler: AccountsController['getSelectedAccount'];
33
34
  };
35
+ export type AccountsControllerGetSelectedMultichainAccountAction = {
36
+ type: `${typeof controllerName}:getSelectedMultichainAccount`;
37
+ handler: AccountsController['getSelectedMultichainAccount'];
38
+ };
34
39
  export type AccountsControllerGetAccountByAddressAction = {
35
40
  type: `${typeof controllerName}:getAccountByAddress`;
36
41
  handler: AccountsController['getAccountByAddress'];
37
42
  };
43
+ export type AccountsControllerGetNextAvailableAccountNameAction = {
44
+ type: `${typeof controllerName}:getNextAvailableAccountName`;
45
+ handler: AccountsController['getNextAvailableAccountName'];
46
+ };
38
47
  export type AccountsControllerGetAccountAction = {
39
48
  type: `${typeof controllerName}:getAccount`;
40
49
  handler: AccountsController['getAccount'];
41
50
  };
42
51
  export type AllowedActions = KeyringControllerGetKeyringForAccountAction | KeyringControllerGetKeyringsByTypeAction | KeyringControllerGetAccountsAction;
43
- export type AccountsControllerActions = AccountsControllerGetStateAction | AccountsControllerSetSelectedAccountAction | AccountsControllerListAccountsAction | AccountsControllerSetAccountNameAction | AccountsControllerUpdateAccountsAction | AccountsControllerGetAccountByAddressAction | AccountsControllerGetSelectedAccountAction | AccountsControllerGetAccountAction;
52
+ export type AccountsControllerActions = AccountsControllerGetStateAction | AccountsControllerSetSelectedAccountAction | AccountsControllerListAccountsAction | AccountsControllerSetAccountNameAction | AccountsControllerUpdateAccountsAction | AccountsControllerGetAccountByAddressAction | AccountsControllerGetSelectedAccountAction | AccountsControllerGetNextAvailableAccountNameAction | AccountsControllerGetAccountAction | AccountsControllerGetSelectedMultichainAccountAction;
44
53
  export type AccountsControllerChangeEvent = ControllerStateChangeEvent<typeof controllerName, AccountsControllerState>;
45
54
  export type AccountsControllerSelectedAccountChangeEvent = {
46
55
  type: `${typeof controllerName}:selectedAccountChange`;
47
56
  payload: [InternalAccount];
48
57
  };
58
+ export type AccountsControllerSelectedEvmAccountChangeEvent = {
59
+ type: `${typeof controllerName}:selectedEvmAccountChange`;
60
+ payload: [InternalAccount];
61
+ };
49
62
  export type AllowedEvents = SnapStateChange | KeyringControllerStateChangeEvent;
50
- export type AccountsControllerEvents = AccountsControllerChangeEvent | AccountsControllerSelectedAccountChangeEvent;
63
+ export type AccountsControllerEvents = AccountsControllerChangeEvent | AccountsControllerSelectedAccountChangeEvent | AccountsControllerSelectedEvmAccountChangeEvent;
51
64
  export type AccountsControllerMessenger = RestrictedControllerMessenger<typeof controllerName, AccountsControllerActions | AllowedActions, AccountsControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
52
65
  /**
53
66
  * Controller that manages internal accounts.
@@ -78,11 +91,18 @@ export declare class AccountsController extends BaseController<typeof controller
78
91
  */
79
92
  getAccount(accountId: string): InternalAccount | undefined;
80
93
  /**
81
- * Returns an array of all internal accounts.
94
+ * Returns an array of all evm internal accounts.
82
95
  *
83
96
  * @returns An array of InternalAccount objects.
84
97
  */
85
98
  listAccounts(): InternalAccount[];
99
+ /**
100
+ * Returns an array of all internal accounts.
101
+ *
102
+ * @param chainId - The chain ID.
103
+ * @returns An array of InternalAccount objects.
104
+ */
105
+ listMultichainAccounts(chainId?: CaipChainId): InternalAccount[];
86
106
  /**
87
107
  * Returns the internal account object for the given account ID.
88
108
  *
@@ -92,11 +112,20 @@ export declare class AccountsController extends BaseController<typeof controller
92
112
  */
93
113
  getAccountExpect(accountId: string): InternalAccount;
94
114
  /**
95
- * Returns the selected internal account.
115
+ * Returns the last selected evm account.
96
116
  *
97
117
  * @returns The selected internal account.
98
118
  */
99
119
  getSelectedAccount(): InternalAccount;
120
+ /**
121
+ * __WARNING The return value may be undefined if there isn't an account for that chain id.__
122
+ *
123
+ * Retrieves the last selected account by chain ID.
124
+ *
125
+ * @param chainId - The chain ID to filter the accounts.
126
+ * @returns The last selected account compatible with the specified chain ID or undefined.
127
+ */
128
+ getSelectedMultichainAccount(chainId?: CaipChainId): InternalAccount | undefined;
100
129
  /**
101
130
  * Returns the account with the specified address.
102
131
  * ! This method will only return the first account that matches the address
@@ -131,6 +160,12 @@ export declare class AccountsController extends BaseController<typeof controller
131
160
  * @param backup - The backup state to load.
132
161
  */
133
162
  loadBackup(backup: AccountsControllerState): void;
163
+ /**
164
+ * Returns the next account number for a given keyring type.
165
+ * @param keyringType - The type of keyring.
166
+ * @returns An object containing the account prefix and index to use.
167
+ */
168
+ getNextAvailableAccountName(keyringType?: string): string;
134
169
  }
135
170
  export {};
136
171
  //# sourceMappingURL=AccountsController.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AccountsController.d.ts","sourceRoot":"","sources":["../../src/AccountsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,6BAA6B,EAC9B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAG7D,OAAO,KAAK,EAE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H,qBAAa,kBAAmB,SAAQ,cAAc,CACpD,OAAO,cAAc,EACrB,uBAAuB,EACvB,2BAA2B,CAC5B;;IACC;;;;;;OAMG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,2BAA2B,CAAC;QACvC,KAAK,EAAE,uBAAuB,CAAC;KAChC;IAwBD;;;;;OAKG;IACH,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAI1D;;;;OAIG;IACH,YAAY,IAAI,eAAe,EAAE;IAKjC;;;;;OAKG;IACH,sBAAsB,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,eAAe,EAAE;IAehE;;;;;;OAMG;IACH,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,eAAe;IA2BpD;;;;OAIG;IACH,kBAAkB,IAAI,eAAe;IAoBrC;;;;;;;OAOG;IACH,4BAA4B,CAC1B,OAAO,CAAC,EAAE,WAAW,GACpB,eAAe,GAAG,SAAS;IAgB9B;;;;;OAKG;IACH,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAMjE;;;;OAIG;IACH,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAsB3C;;;;;;OAMG;IACH,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI;IA2B5D;;;;;OAKG;IACG,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IAwDrC;;;;OAIG;IACH,UAAU,CAAC,MAAM,EAAE,uBAAuB,GAAG,IAAI;IAsVjD;;;;OAIG;IACH,2BAA2B,CAAC,WAAW,GAAE,MAAwB,GAAG,MAAM;CAiL3E"}
@@ -1,4 +1,5 @@
1
- export type { AccountsControllerState, AccountsControllerGetStateAction, AccountsControllerSetSelectedAccountAction, AccountsControllerSetAccountNameAction, AccountsControllerListAccountsAction, AccountsControllerUpdateAccountsAction, AccountsControllerGetSelectedAccountAction, AccountsControllerGetAccountByAddressAction, AccountsControllerGetAccountAction, AccountsControllerActions, AccountsControllerChangeEvent, AccountsControllerSelectedAccountChangeEvent, AccountsControllerEvents, AccountsControllerMessenger, } from './AccountsController';
1
+ export type { AccountsControllerState, AccountsControllerGetStateAction, AccountsControllerSetSelectedAccountAction, AccountsControllerSetAccountNameAction, AccountsControllerListAccountsAction, AccountsControllerUpdateAccountsAction, AccountsControllerGetSelectedAccountAction, AccountsControllerGetAccountByAddressAction, AccountsControllerGetAccountAction, AccountsControllerActions, AccountsControllerChangeEvent, AccountsControllerSelectedAccountChangeEvent, AccountsControllerSelectedEvmAccountChangeEvent, AccountsControllerEvents, AccountsControllerMessenger, } from './AccountsController';
2
2
  export { AccountsController } from './AccountsController';
3
3
  export { keyringTypeToName, getUUIDFromAddressOfNormalAccount } from './utils';
4
+ export { createMockInternalAccount } from './tests/mocks';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,uBAAuB,EACvB,gCAAgC,EAChC,0CAA0C,EAC1C,sCAAsC,EACtC,oCAAoC,EACpC,sCAAsC,EACtC,0CAA0C,EAC1C,2CAA2C,EAC3C,kCAAkC,EAClC,yBAAyB,EACzB,6BAA6B,EAC7B,4CAA4C,EAC5C,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,iCAAiC,EAAE,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,uBAAuB,EACvB,gCAAgC,EAChC,0CAA0C,EAC1C,sCAAsC,EACtC,oCAAoC,EACpC,sCAAsC,EACtC,0CAA0C,EAC1C,2CAA2C,EAC3C,kCAAkC,EAClC,yBAAyB,EACzB,6BAA6B,EAC7B,4CAA4C,EAC5C,+CAA+C,EAC/C,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,iCAAiC,EAAE,MAAM,SAAS,CAAC;AAC/E,OAAO,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { InternalAccount, InternalAccountType } from '@metamask/keyring-api';
2
+ import { KeyringTypes } from '@metamask/keyring-controller';
3
+ export declare const createMockInternalAccount: ({ id, address, type, name, keyringType, snap, importTime, lastSelected, }?: {
4
+ id?: string | undefined;
5
+ address?: string | undefined;
6
+ type?: InternalAccountType | undefined;
7
+ name?: string | undefined;
8
+ keyringType?: KeyringTypes | undefined;
9
+ snap?: {
10
+ id: string;
11
+ enabled: boolean;
12
+ name: string;
13
+ } | undefined;
14
+ importTime?: number | undefined;
15
+ lastSelected?: number | undefined;
16
+ }) => InternalAccount;
17
+ //# sourceMappingURL=mocks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mocks.d.ts","sourceRoot":"","sources":["../../../src/tests/mocks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,mBAAmB,EACpB,MAAM,uBAAuB,CAAC;AAQ/B,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAG5D,eAAO,MAAM,yBAAyB;;;;;;;YAgB9B,MAAM;iBACD,OAAO;cACV,MAAM;;;;MAIP,eA0CR,CAAC"}
@@ -1,4 +1,7 @@
1
+ import { KeyringTypes } from '@metamask/keyring-controller';
2
+ import type { Draft } from 'immer';
1
3
  import type { V4Options } from 'uuid';
4
+ import type { AccountsControllerState } from './AccountsController';
2
5
  /**
3
6
  * Returns the name of the keyring type.
4
7
  *
@@ -18,4 +21,20 @@ export declare function getUUIDOptionsFromAddressOfNormalAccount(address: string
18
21
  * @returns The generated UUID.
19
22
  */
20
23
  export declare function getUUIDFromAddressOfNormalAccount(address: string): string;
24
+ /**
25
+ * Check if a keyring type is considered a "normal" keyring.
26
+ * @param keyringType - The account's keyring type.
27
+ * @returns True if the keyring type is considered a "normal" keyring, false otherwise.
28
+ */
29
+ export declare function isNormalKeyringType(keyringType: KeyringTypes): boolean;
30
+ /**
31
+ * WARNING: To be removed once type issue is fixed. https://github.com/MetaMask/utils/issues/168
32
+ *
33
+ * Creates a deep clone of the given object.
34
+ * This is to get around error `Type instantiation is excessively deep and possibly infinite.`
35
+ *
36
+ * @param obj - The object to be cloned.
37
+ * @returns The deep clone of the object.
38
+ */
39
+ export declare function deepCloneDraft(obj: Draft<AccountsControllerState>): AccountsControllerState;
21
40
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAGtC;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAiC7D;AAED;;;;GAIG;AACH,wBAAgB,wCAAwC,CACtD,OAAO,EAAE,MAAM,GACd,SAAS,CAMX;AAED;;;;GAIG;AACH,wBAAgB,iCAAiC,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEzE"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAG9E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAGtC,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAEpE;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAiC7D;AAED;;;;GAIG;AACH,wBAAgB,wCAAwC,CACtD,OAAO,EAAE,MAAM,GACd,SAAS,CAMX;AAED;;;;GAIG;AACH,wBAAgB,iCAAiC,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEzE;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,YAAY,GAAG,OAAO,CAItE;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,KAAK,CAAC,uBAAuB,CAAC,GAClC,uBAAuB,CAGzB"}
package/dist/utils.js CHANGED
@@ -2,10 +2,15 @@
2
2
 
3
3
 
4
4
 
5
- var _chunkKCST55AQjs = require('./chunk-KCST55AQ.js');
6
5
 
7
6
 
7
+ var _chunkEIQ5DUI6js = require('./chunk-EIQ5DUI6.js');
8
+ require('./chunk-UJIPPGP6.js');
8
9
 
9
10
 
10
- exports.getUUIDFromAddressOfNormalAccount = _chunkKCST55AQjs.getUUIDFromAddressOfNormalAccount; exports.getUUIDOptionsFromAddressOfNormalAccount = _chunkKCST55AQjs.getUUIDOptionsFromAddressOfNormalAccount; exports.keyringTypeToName = _chunkKCST55AQjs.keyringTypeToName;
11
+
12
+
13
+
14
+
15
+ exports.deepCloneDraft = _chunkEIQ5DUI6js.deepCloneDraft; exports.getUUIDFromAddressOfNormalAccount = _chunkEIQ5DUI6js.getUUIDFromAddressOfNormalAccount; exports.getUUIDOptionsFromAddressOfNormalAccount = _chunkEIQ5DUI6js.getUUIDOptionsFromAddressOfNormalAccount; exports.isNormalKeyringType = _chunkEIQ5DUI6js.isNormalKeyringType; exports.keyringTypeToName = _chunkEIQ5DUI6js.keyringTypeToName;
11
16
  //# sourceMappingURL=utils.js.map
package/dist/utils.mjs CHANGED
@@ -1,11 +1,16 @@
1
1
  import {
2
+ deepCloneDraft,
2
3
  getUUIDFromAddressOfNormalAccount,
3
4
  getUUIDOptionsFromAddressOfNormalAccount,
5
+ isNormalKeyringType,
4
6
  keyringTypeToName
5
- } from "./chunk-E7SBT5BV.mjs";
7
+ } from "./chunk-YNILZSBK.mjs";
8
+ import "./chunk-ZNSHBDHA.mjs";
6
9
  export {
10
+ deepCloneDraft,
7
11
  getUUIDFromAddressOfNormalAccount,
8
12
  getUUIDOptionsFromAddressOfNormalAccount,
13
+ isNormalKeyringType,
9
14
  keyringTypeToName
10
15
  };
11
16
  //# sourceMappingURL=utils.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/accounts-controller",
3
- "version": "14.0.0",
3
+ "version": "16.0.0",
4
4
  "description": "Manages internal accounts",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -42,11 +42,11 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@ethereumjs/util": "^8.1.0",
45
- "@metamask/base-controller": "^5.0.2",
46
- "@metamask/eth-snap-keyring": "^4.0.0",
47
- "@metamask/keyring-api": "^6.0.0",
48
- "@metamask/snaps-sdk": "^4.0.1",
49
- "@metamask/snaps-utils": "^7.1.0",
45
+ "@metamask/base-controller": "^6.0.0",
46
+ "@metamask/eth-snap-keyring": "^4.1.1",
47
+ "@metamask/keyring-api": "^6.1.1",
48
+ "@metamask/snaps-sdk": "^4.2.0",
49
+ "@metamask/snaps-utils": "^7.4.0",
50
50
  "@metamask/utils": "^8.3.0",
51
51
  "deepmerge": "^4.2.2",
52
52
  "ethereum-cryptography": "^2.1.2",
@@ -55,8 +55,8 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@metamask/auto-changelog": "^3.4.4",
58
- "@metamask/keyring-controller": "^16.0.0",
59
- "@metamask/snaps-controllers": "^7.0.1",
58
+ "@metamask/keyring-controller": "^17.0.0",
59
+ "@metamask/snaps-controllers": "^8.1.1",
60
60
  "@types/jest": "^27.4.1",
61
61
  "@types/readable-stream": "^2.3.0",
62
62
  "jest": "^27.5.1",
@@ -66,11 +66,11 @@
66
66
  "typescript": "~4.9.5"
67
67
  },
68
68
  "peerDependencies": {
69
- "@metamask/keyring-controller": "^16.0.0",
70
- "@metamask/snaps-controllers": "^7.0.1"
69
+ "@metamask/keyring-controller": "^17.0.0",
70
+ "@metamask/snaps-controllers": "^8.1.1"
71
71
  },
72
72
  "engines": {
73
- "node": ">=16.0.0"
73
+ "node": "^18.18 || >=20"
74
74
  },
75
75
  "publishConfig": {
76
76
  "access": "public",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { toBuffer } from '@ethereumjs/util';\nimport { isCustodyKeyring, KeyringTypes } from '@metamask/keyring-controller';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport type { V4Options } from 'uuid';\nimport { v4 as uuid } from 'uuid';\n\n/**\n * Returns the name of the keyring type.\n *\n * @param keyringType - The type of the keyring.\n * @returns The name of the keyring type.\n */\nexport function keyringTypeToName(keyringType: string): string {\n // Custody keyrings are a special case, as they are not a single type\n // they just start with the prefix `Custody`\n if (isCustodyKeyring(keyringType)) {\n return 'Custody';\n }\n\n switch (keyringType) {\n case KeyringTypes.simple: {\n return 'Account';\n }\n case KeyringTypes.hd: {\n return 'Account';\n }\n case KeyringTypes.trezor: {\n return 'Trezor';\n }\n case KeyringTypes.ledger: {\n return 'Ledger';\n }\n case KeyringTypes.lattice: {\n return 'Lattice';\n }\n case KeyringTypes.qr: {\n return 'QR';\n }\n case KeyringTypes.snap: {\n return 'Snap Account';\n }\n default: {\n throw new Error(`Unknown keyring ${keyringType}`);\n }\n }\n}\n\n/**\n * Generates a UUID v4 options from a given Ethereum address.\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The UUID v4 options.\n */\nexport function getUUIDOptionsFromAddressOfNormalAccount(\n address: string,\n): V4Options {\n const v4options = {\n random: sha256(toBuffer(address)).slice(0, 16),\n };\n\n return v4options;\n}\n\n/**\n * Generates a UUID from a given Ethereum address.\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The generated UUID.\n */\nexport function getUUIDFromAddressOfNormalAccount(address: string): string {\n return uuid(getUUIDOptionsFromAddressOfNormalAccount(address));\n}\n"],"mappings":";;;;;;;;;;;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,kBAAkB,oBAAoB;AAC/C,SAAS,cAAc;AAEvB,SAAS,MAAM,YAAY;AAQpB,SAAS,kBAAkB,aAA6B;AAG7D,MAAI,iBAAiB,WAAW,GAAG;AACjC,WAAO;AAAA,EACT;AAEA,UAAQ,aAAa;AAAA,IACnB,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,IAAI;AACpB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,SAAS;AACzB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,IAAI;AACpB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,MAAM;AACtB,aAAO;AAAA,IACT;AAAA,IACA,SAAS;AACP,YAAM,IAAI,MAAM,mBAAmB,WAAW,EAAE;AAAA,IAClD;AAAA,EACF;AACF;AAOO,SAAS,yCACd,SACW;AACX,QAAM,YAAY;AAAA,IAChB,QAAQ,OAAO,SAAS,OAAO,CAAC,EAAE,MAAM,GAAG,EAAE;AAAA,EAC/C;AAEA,SAAO;AACT;AAOO,SAAS,kCAAkC,SAAyB;AACzE,SAAO,KAAK,yCAAyC,OAAO,CAAC;AAC/D;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/AccountsController.ts"],"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 KeyringControllerGetKeyringForAccountAction,\n KeyringControllerGetKeyringsByTypeAction,\n KeyringControllerGetAccountsAction,\n KeyringControllerStateChangeEvent,\n} from '@metamask/keyring-controller';\nimport type {\n SnapControllerState,\n SnapStateChange,\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 type { Draft } from 'immer';\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 AllowedActions =\n | KeyringControllerGetKeyringForAccountAction\n | KeyringControllerGetKeyringsByTypeAction\n | KeyringControllerGetAccountsAction;\n\nexport type AccountsControllerActions =\n | AccountsControllerGetStateAction\n | AccountsControllerSetSelectedAccountAction\n | AccountsControllerListAccountsAction\n | AccountsControllerSetAccountNameAction\n | AccountsControllerUpdateAccountsAction\n | AccountsControllerGetAccountByAddressAction\n | AccountsControllerGetSelectedAccountAction\n | AccountsControllerGetAccountAction;\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 AllowedEvents = SnapStateChange | KeyringControllerStateChangeEvent;\n\nexport type AccountsControllerEvents =\n | AccountsControllerChangeEvent\n | AccountsControllerSelectedAccountChangeEvent;\n\nexport type AccountsControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n AccountsControllerActions | AllowedActions,\n AccountsControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\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 importTime: 0,\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.getAccountExpect(accountId);\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n currentState.internalAccounts.accounts[account.id].metadata.lastSelected =\n Date.now();\n currentState.internalAccounts.selectedAccount = account.id;\n });\n\n this.messagingSystem.publish(\n 'AccountsController:selectedAccountChange',\n account,\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] = 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 this.#populateExistingMetadata(existingAccount?.id, 'name') ??\n `${keyringTypeName} ${keyringAccountIndex + 1}`,\n importTime:\n this.#populateExistingMetadata(existingAccount?.id, 'importTime') ??\n Date.now(),\n lastSelected: this.#populateExistingMetadata(\n existingAccount?.id,\n 'lastSelected',\n ),\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 importTime: Date.now(),\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\n const id = getUUIDFromAddressOfNormalAccount(address);\n\n internalAccounts.push({\n id,\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: this.#populateExistingMetadata(id, 'name') ?? '',\n importTime:\n this.#populateExistingMetadata(id, 'importTime') ?? Date.now(),\n lastSelected: this.#populateExistingMetadata(id, 'lastSelected'),\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: 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 if (!accountToSelect) {\n this.update((currentState: Draft<AccountsControllerState>) => {\n currentState.internalAccounts.selectedAccount = '';\n });\n return;\n }\n\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 list of accounts for a given keyring type.\n * @param keyringType - The type of keyring.\n * @returns The list of accounts associcated with this keyring type.\n */\n #getAccountsByKeyringType(keyringType: string) {\n return this.listAccounts().filter((internalAccount) => {\n // We do consider `hd` and `simple` keyrings to be of same type. So we check those 2 types\n // to group those accounts together!\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\n return internalAccount.metadata.keyring.type === keyringType;\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 keyringAccounts = this.#getAccountsByKeyringType(keyringType);\n const lastDefaultIndexUsedForKeyringType = keyringAccounts.reduce(\n (maxInternalAccountIndex, internalAccount) => {\n // We **DO NOT USE** `\\d+` here to only consider valid \"human\"\n // number (rounded decimal number)\n const match = new RegExp(`${keyringName} ([0-9]+)$`, 'u').exec(\n internalAccount.metadata.name,\n );\n\n if (match) {\n // Quoting `RegExp.exec` documentation:\n // > The returned array has the matched text as the first item, and then one item for\n // > each capturing group of the matched text.\n // So use `match[1]` to get the captured value\n const internalAccountIndex = parseInt(match[1], 10);\n return Math.max(maxInternalAccountIndex, internalAccountIndex);\n }\n\n return maxInternalAccountIndex;\n },\n 0,\n );\n\n const indexToUse = Math.max(\n keyringAccounts.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 importTime: Date.now(),\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 * Retrieves the value of a specific metadata key for an existing account.\n * @param accountId - The ID of the account.\n * @param metadataKey - The key of the metadata to retrieve.\n * @returns The value of the specified metadata key, or undefined if the account or metadata key does not exist.\n */\n #populateExistingMetadata<T extends keyof InternalAccount['metadata']>(\n accountId: string,\n metadataKey: T,\n ): InternalAccount['metadata'][T] | undefined {\n const internalAccount = this.getAccount(accountId);\n return internalAccount ? internalAccount.metadata[metadataKey] : undefined;\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"],"mappings":";;;;;;;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AK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,oBAAoB,KAAK,IAAI;AAAA,EACpC;AAEA,OAAK,gBAAgB;AAAA,IACnB;AAAA,IACA,KAAK,WAAW,KAAK,IAAI;AAAA,EAC3B;AACF;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,kBAAkB,oBAAoB;AAC/C,SAAS,cAAc;AAEvB,SAAS,MAAM,YAAY;AAQpB,SAAS,kBAAkB,aAA6B;AAG7D,MAAI,iBAAiB,WAAW,GAAG;AACjC,WAAO;AAAA,EACT;AAEA,UAAQ,aAAa;AAAA,IACnB,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,IAAI;AACpB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,SAAS;AACzB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,IAAI;AACpB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,MAAM;AACtB,aAAO;AAAA,IACT;AAAA,IACA,SAAS;AACP,YAAM,IAAI,MAAM,mBAAmB,WAAW,EAAE;AAAA,IAClD;AAAA,EACF;AACF;AAOO,SAAS,yCACd,SACW;AACX,QAAM,YAAY;AAAA,IAChB,QAAQ,OAAO,SAAS,OAAO,CAAC,EAAE,MAAM,GAAG,EAAE;AAAA,EAC/C;AAEA,SAAO;AACT;AAOO,SAAS,kCAAkC,SAAyB;AACzE,SAAO,KAAK,yCAAyC,OAAO,CAAC;AAC/D","sourcesContent":["import { toBuffer } from '@ethereumjs/util';\nimport { isCustodyKeyring, KeyringTypes } from '@metamask/keyring-controller';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport type { V4Options } from 'uuid';\nimport { v4 as uuid } from 'uuid';\n\n/**\n * Returns the name of the keyring type.\n *\n * @param keyringType - The type of the keyring.\n * @returns The name of the keyring type.\n */\nexport function keyringTypeToName(keyringType: string): string {\n // Custody keyrings are a special case, as they are not a single type\n // they just start with the prefix `Custody`\n if (isCustodyKeyring(keyringType)) {\n return 'Custody';\n }\n\n switch (keyringType) {\n case KeyringTypes.simple: {\n return 'Account';\n }\n case KeyringTypes.hd: {\n return 'Account';\n }\n case KeyringTypes.trezor: {\n return 'Trezor';\n }\n case KeyringTypes.ledger: {\n return 'Ledger';\n }\n case KeyringTypes.lattice: {\n return 'Lattice';\n }\n case KeyringTypes.qr: {\n return 'QR';\n }\n case KeyringTypes.snap: {\n return 'Snap Account';\n }\n default: {\n throw new Error(`Unknown keyring ${keyringType}`);\n }\n }\n}\n\n/**\n * Generates a UUID v4 options from a given Ethereum address.\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The UUID v4 options.\n */\nexport function getUUIDOptionsFromAddressOfNormalAccount(\n address: string,\n): V4Options {\n const v4options = {\n random: sha256(toBuffer(address)).slice(0, 16),\n };\n\n return v4options;\n}\n\n/**\n * Generates a UUID from a given Ethereum address.\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The generated UUID.\n */\nexport function getUUIDFromAddressOfNormalAccount(address: string): string {\n return uuid(getUUIDOptionsFromAddressOfNormalAccount(address));\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/AccountsController.ts"],"names":[],"mappings":";;;;;;;;AAKA,SAAS,sBAAsB;AAC/B,SAAS,mBAAmB;AAE5B,SAAS,gBAAgB,iBAAiB;AAC1C,SAAS,oBAAoB;AAmB7B,IAAM,iBAAiB;AA6FvB,IAAM,6BAA6B;AAAA,EACjC,kBAAkB;AAAA,IAChB,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AACF;AAEA,IAAM,eAAwC;AAAA,EAC5C,kBAAkB;AAAA,IAChB,UAAU,CAAC;AA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MACH,UAAU;AAAA,QACR,GAAG,WAAW;AAAA,QACd,MAAM;AAAA,QACN,YAAY,KAAK,IAAI;AAAA,QACrB,cAAc,KAAK,IAAI;AAAA,MACzB;AAAA,IACF;AAAA,EACF,CAAC;AAED,OAAK,mBAAmB,WAAW,EAAE;AACvC;AAMA;AAAA,0BAAqB,SAAC,WAAmB;AACvC,OAAK,OAAO,CAAC,iBAAiD;AAC5D,WAAO,aAAa,iBAAiB,SAAS,SAAS;AAAA,EACzD,CAAC;AACH;AAQA;AAAA,8BAAsE,SACpE,WACA,aAC4C;AAC5C,QAAM,kBAAkB,KAAK,WAAW,SAAS;AACjD,SAAO,kBAAkB,gBAAgB,SAAS,WAAW,IAAI;AACnE;AAMA;AAAA,6BAAwB,WAAG;AACzB,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,mBAAmB,KAAK,IAAI;AAAA,EACnC;AAEA,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,aAAa,KAAK,IAAI;AAAA,EAC7B;AAEA,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,eAAe,KAAK,IAAI;AAAA,EAC/B;AAEA,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,eAAe,KAAK,IAAI;AAAA,EAC/B;AAEA,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,mBAAmB,KAAK,IAAI;AAAA,EACnC;AAEA,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,oBAAoB,KAAK,IAAI;AAAA,EACpC;AAEA,OAAK,gBAAgB;AAAA,IACnB;AAAA,IACA,KAAK,WAAW,KAAK,IAAI;AAAA,EAC3B;AACF","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 KeyringControllerGetKeyringForAccountAction,\n KeyringControllerGetKeyringsByTypeAction,\n KeyringControllerGetAccountsAction,\n KeyringControllerStateChangeEvent,\n} from '@metamask/keyring-controller';\nimport type {\n SnapControllerState,\n SnapStateChange,\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 type { Draft } from 'immer';\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 AllowedActions =\n | KeyringControllerGetKeyringForAccountAction\n | KeyringControllerGetKeyringsByTypeAction\n | KeyringControllerGetAccountsAction;\n\nexport type AccountsControllerActions =\n | AccountsControllerGetStateAction\n | AccountsControllerSetSelectedAccountAction\n | AccountsControllerListAccountsAction\n | AccountsControllerSetAccountNameAction\n | AccountsControllerUpdateAccountsAction\n | AccountsControllerGetAccountByAddressAction\n | AccountsControllerGetSelectedAccountAction\n | AccountsControllerGetAccountAction;\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 AllowedEvents = SnapStateChange | KeyringControllerStateChangeEvent;\n\nexport type AccountsControllerEvents =\n | AccountsControllerChangeEvent\n | AccountsControllerSelectedAccountChangeEvent;\n\nexport type AccountsControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n AccountsControllerActions | AllowedActions,\n AccountsControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\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 importTime: 0,\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.getAccountExpect(accountId);\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n currentState.internalAccounts.accounts[account.id].metadata.lastSelected =\n Date.now();\n currentState.internalAccounts.selectedAccount = account.id;\n });\n\n this.messagingSystem.publish(\n 'AccountsController:selectedAccountChange',\n account,\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] = 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 this.#populateExistingMetadata(existingAccount?.id, 'name') ??\n `${keyringTypeName} ${keyringAccountIndex + 1}`,\n importTime:\n this.#populateExistingMetadata(existingAccount?.id, 'importTime') ??\n Date.now(),\n lastSelected: this.#populateExistingMetadata(\n existingAccount?.id,\n 'lastSelected',\n ),\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 importTime: Date.now(),\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\n const id = getUUIDFromAddressOfNormalAccount(address);\n\n internalAccounts.push({\n id,\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: this.#populateExistingMetadata(id, 'name') ?? '',\n importTime:\n this.#populateExistingMetadata(id, 'importTime') ?? Date.now(),\n lastSelected: this.#populateExistingMetadata(id, 'lastSelected'),\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: 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 if (!accountToSelect) {\n this.update((currentState: Draft<AccountsControllerState>) => {\n currentState.internalAccounts.selectedAccount = '';\n });\n return;\n }\n\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 list of accounts for a given keyring type.\n * @param keyringType - The type of keyring.\n * @returns The list of accounts associcated with this keyring type.\n */\n #getAccountsByKeyringType(keyringType: string) {\n return this.listAccounts().filter((internalAccount) => {\n // We do consider `hd` and `simple` keyrings to be of same type. So we check those 2 types\n // to group those accounts together!\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\n return internalAccount.metadata.keyring.type === keyringType;\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 keyringAccounts = this.#getAccountsByKeyringType(keyringType);\n const lastDefaultIndexUsedForKeyringType = keyringAccounts.reduce(\n (maxInternalAccountIndex, internalAccount) => {\n // We **DO NOT USE** `\\d+` here to only consider valid \"human\"\n // number (rounded decimal number)\n const match = new RegExp(`${keyringName} ([0-9]+)$`, 'u').exec(\n internalAccount.metadata.name,\n );\n\n if (match) {\n // Quoting `RegExp.exec` documentation:\n // > The returned array has the matched text as the first item, and then one item for\n // > each capturing group of the matched text.\n // So use `match[1]` to get the captured value\n const internalAccountIndex = parseInt(match[1], 10);\n return Math.max(maxInternalAccountIndex, internalAccountIndex);\n }\n\n return maxInternalAccountIndex;\n },\n 0,\n );\n\n const indexToUse = Math.max(\n keyringAccounts.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 importTime: Date.now(),\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 * Retrieves the value of a specific metadata key for an existing account.\n * @param accountId - The ID of the account.\n * @param metadataKey - The key of the metadata to retrieve.\n * @returns The value of the specified metadata key, or undefined if the account or metadata key does not exist.\n */\n #populateExistingMetadata<T extends keyof InternalAccount['metadata']>(\n accountId: string,\n metadataKey: T,\n ): InternalAccount['metadata'][T] | undefined {\n const internalAccount = this.getAccount(accountId);\n return internalAccount ? internalAccount.metadata[metadataKey] : undefined;\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"]}