@metamask-previews/account-tree-controller 0.6.0-preview-f476a4a9 → 0.6.0-preview-ec3106a5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -4
- package/dist/AccountTreeController.cjs +89 -138
- package/dist/AccountTreeController.cjs.map +1 -1
- package/dist/AccountTreeController.d.cts +19 -61
- package/dist/AccountTreeController.d.cts.map +1 -1
- package/dist/AccountTreeController.d.mts +19 -61
- package/dist/AccountTreeController.d.mts.map +1 -1
- package/dist/AccountTreeController.mjs +87 -136
- package/dist/AccountTreeController.mjs.map +1 -1
- package/dist/AccountTreeGroup.cjs +44 -35
- package/dist/AccountTreeGroup.cjs.map +1 -1
- package/dist/AccountTreeGroup.d.cts +15 -22
- package/dist/AccountTreeGroup.d.cts.map +1 -1
- package/dist/AccountTreeGroup.d.mts +15 -22
- package/dist/AccountTreeGroup.d.mts.map +1 -1
- package/dist/AccountTreeGroup.mjs +42 -33
- package/dist/AccountTreeGroup.mjs.map +1 -1
- package/dist/AccountTreeRule.cjs +37 -0
- package/dist/AccountTreeRule.cjs.map +1 -0
- package/dist/AccountTreeRule.d.cts +53 -0
- package/dist/AccountTreeRule.d.cts.map +1 -0
- package/dist/AccountTreeRule.d.mts +53 -0
- package/dist/AccountTreeRule.d.mts.map +1 -0
- package/dist/AccountTreeRule.mjs +33 -0
- package/dist/AccountTreeRule.mjs.map +1 -0
- package/dist/AccountTreeWallet.cjs +53 -26
- package/dist/AccountTreeWallet.cjs.map +1 -1
- package/dist/AccountTreeWallet.d.cts +26 -21
- package/dist/AccountTreeWallet.d.cts.map +1 -1
- package/dist/AccountTreeWallet.d.mts +26 -21
- package/dist/AccountTreeWallet.d.mts.map +1 -1
- package/dist/AccountTreeWallet.mjs +52 -25
- package/dist/AccountTreeWallet.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/rules/entropy.cjs +68 -0
- package/dist/rules/entropy.cjs.map +1 -0
- package/dist/rules/entropy.d.cts +13 -0
- package/dist/rules/entropy.d.cts.map +1 -0
- package/dist/rules/entropy.d.mts +13 -0
- package/dist/rules/entropy.d.mts.map +1 -0
- package/dist/rules/entropy.mjs +64 -0
- package/dist/rules/entropy.mjs.map +1 -0
- package/dist/rules/keyring.cjs +86 -0
- package/dist/rules/keyring.cjs.map +1 -0
- package/dist/rules/keyring.d.cts +21 -0
- package/dist/rules/keyring.d.cts.map +1 -0
- package/dist/rules/keyring.d.mts +21 -0
- package/dist/rules/keyring.d.mts.map +1 -0
- package/dist/rules/keyring.mjs +81 -0
- package/dist/rules/keyring.mjs.map +1 -0
- package/dist/rules/snap.cjs +58 -0
- package/dist/rules/snap.cjs.map +1 -0
- package/dist/rules/snap.d.cts +22 -0
- package/dist/rules/snap.d.cts.map +1 -0
- package/dist/rules/snap.d.mts +22 -0
- package/dist/rules/snap.d.mts.map +1 -0
- package/dist/rules/snap.mjs +54 -0
- package/dist/rules/snap.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +74 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +74 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/rules/EntropySourceWalletRule.cjs +0 -100
- package/dist/rules/EntropySourceWalletRule.cjs.map +0 -1
- package/dist/rules/EntropySourceWalletRule.d.cts +0 -19
- package/dist/rules/EntropySourceWalletRule.d.cts.map +0 -1
- package/dist/rules/EntropySourceWalletRule.d.mts +0 -19
- package/dist/rules/EntropySourceWalletRule.d.mts.map +0 -1
- package/dist/rules/EntropySourceWalletRule.mjs +0 -95
- package/dist/rules/EntropySourceWalletRule.mjs.map +0 -1
- package/dist/rules/KeyringWalletRule.cjs +0 -99
- package/dist/rules/KeyringWalletRule.cjs.map +0 -1
- package/dist/rules/KeyringWalletRule.d.cts +0 -18
- package/dist/rules/KeyringWalletRule.d.cts.map +0 -1
- package/dist/rules/KeyringWalletRule.d.mts +0 -18
- package/dist/rules/KeyringWalletRule.d.mts.map +0 -1
- package/dist/rules/KeyringWalletRule.mjs +0 -94
- package/dist/rules/KeyringWalletRule.mjs.map +0 -1
- package/dist/rules/SnapWalletRule.cjs +0 -70
- package/dist/rules/SnapWalletRule.cjs.map +0 -1
- package/dist/rules/SnapWalletRule.d.cts +0 -10
- package/dist/rules/SnapWalletRule.d.cts.map +0 -1
- package/dist/rules/SnapWalletRule.d.mts +0 -10
- package/dist/rules/SnapWalletRule.d.mts.map +0 -1
- package/dist/rules/SnapWalletRule.mjs +0 -66
- package/dist/rules/SnapWalletRule.mjs.map +0 -1
- package/dist/rules/WalletRule.cjs +0 -13
- package/dist/rules/WalletRule.cjs.map +0 -1
- package/dist/rules/WalletRule.d.cts +0 -37
- package/dist/rules/WalletRule.d.cts.map +0 -1
- package/dist/rules/WalletRule.d.mts +0 -37
- package/dist/rules/WalletRule.d.mts.map +0 -1
- package/dist/rules/WalletRule.mjs +0 -9
- package/dist/rules/WalletRule.mjs.map +0 -1
- package/dist/rules/index.cjs +0 -21
- package/dist/rules/index.cjs.map +0 -1
- package/dist/rules/index.d.cts +0 -5
- package/dist/rules/index.d.cts.map +0 -1
- package/dist/rules/index.d.mts +0 -5
- package/dist/rules/index.d.mts.map +0 -1
- package/dist/rules/index.mjs +0 -5
- package/dist/rules/index.mjs.map +0 -1
- package/dist/rules/utils.cjs +0 -15
- package/dist/rules/utils.cjs.map +0 -1
- package/dist/rules/utils.d.cts +0 -11
- package/dist/rules/utils.d.cts.map +0 -1
- package/dist/rules/utils.d.mts +0 -11
- package/dist/rules/utils.d.mts.map +0 -1
- package/dist/rules/utils.mjs +0 -11
- package/dist/rules/utils.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,10 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
|
-
- Add
|
|
13
|
-
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
- Add BIP-44/multichain accounts support ([#6185](https://github.com/MetaMask/core/pull/6185))
|
|
13
|
+
- Those are being attached to the `entropy` wallet category.
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
|
|
17
|
+
- Now use one account group per account for `snap` and `keyring` wallet categories ([#6185](https://github.com/MetaMask/core/pull/6185))
|
|
18
|
+
- We used to group all accounts under the `'default'` group, but we now compute the group ID using the address of each accounts.
|
|
19
|
+
- Compute account group name based on their underlying account. ([#6185](https://github.com/MetaMask/core/pull/6185))
|
|
20
|
+
- This replaces the previous `'Default'` name for groups.
|
|
16
21
|
|
|
17
22
|
## [0.6.0]
|
|
18
23
|
|
|
@@ -10,21 +10,21 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _AccountTreeController_instances,
|
|
13
|
+
var _AccountTreeController_instances, _AccountTreeController_accountIdToContext, _AccountTreeController_rules, _AccountTreeController_categoryToRule, _AccountTreeController_renameAccountWalletIfNeeded, _AccountTreeController_renameAccountGroupIfNeeded, _AccountTreeController_handleAccountAdded, _AccountTreeController_handleAccountRemoved, _AccountTreeController_insert, _AccountTreeController_listAccounts;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.AccountTreeController = exports.getDefaultAccountTreeControllerState = void 0;
|
|
15
|
+
exports.AccountTreeController = exports.getDefaultAccountTreeControllerState = exports.controllerName = void 0;
|
|
16
|
+
const account_api_1 = require("@metamask/account-api");
|
|
16
17
|
const base_controller_1 = require("@metamask/base-controller");
|
|
17
|
-
const
|
|
18
|
-
const
|
|
18
|
+
const AccountTreeWallet_1 = require("./AccountTreeWallet.cjs");
|
|
19
|
+
const entropy_1 = require("./rules/entropy.cjs");
|
|
20
|
+
const keyring_1 = require("./rules/keyring.cjs");
|
|
21
|
+
const snap_1 = require("./rules/snap.cjs");
|
|
22
|
+
exports.controllerName = 'AccountTreeController';
|
|
19
23
|
const accountTreeControllerMetadata = {
|
|
20
24
|
accountTree: {
|
|
21
25
|
persist: false,
|
|
22
26
|
anonymous: false,
|
|
23
27
|
},
|
|
24
|
-
selectedAccountGroup: {
|
|
25
|
-
persist: true,
|
|
26
|
-
anonymous: false,
|
|
27
|
-
},
|
|
28
28
|
};
|
|
29
29
|
/**
|
|
30
30
|
* Gets default state of the `AccountTreeController`.
|
|
@@ -36,7 +36,6 @@ function getDefaultAccountTreeControllerState() {
|
|
|
36
36
|
accountTree: {
|
|
37
37
|
wallets: {},
|
|
38
38
|
},
|
|
39
|
-
selectedAccountGroup: '',
|
|
40
39
|
};
|
|
41
40
|
}
|
|
42
41
|
exports.getDefaultAccountTreeControllerState = getDefaultAccountTreeControllerState;
|
|
@@ -51,7 +50,7 @@ class AccountTreeController extends base_controller_1.BaseController {
|
|
|
51
50
|
constructor({ messenger, state, }) {
|
|
52
51
|
super({
|
|
53
52
|
messenger,
|
|
54
|
-
name: controllerName,
|
|
53
|
+
name: exports.controllerName,
|
|
55
54
|
metadata: accountTreeControllerMetadata,
|
|
56
55
|
state: {
|
|
57
56
|
...getDefaultAccountTreeControllerState(),
|
|
@@ -59,20 +58,24 @@ class AccountTreeController extends base_controller_1.BaseController {
|
|
|
59
58
|
},
|
|
60
59
|
});
|
|
61
60
|
_AccountTreeController_instances.add(this);
|
|
62
|
-
|
|
61
|
+
_AccountTreeController_accountIdToContext.set(this, void 0);
|
|
63
62
|
_AccountTreeController_rules.set(this, void 0);
|
|
64
|
-
|
|
65
|
-
__classPrivateFieldSet(this, _AccountTreeController_wallets, new Map(), "f");
|
|
63
|
+
_AccountTreeController_categoryToRule.set(this, void 0);
|
|
66
64
|
// Reverse map to allow fast node access from an account ID.
|
|
67
|
-
__classPrivateFieldSet(this,
|
|
65
|
+
__classPrivateFieldSet(this, _AccountTreeController_accountIdToContext, new Map(), "f");
|
|
68
66
|
// Rules to apply to construct the wallets tree.
|
|
67
|
+
__classPrivateFieldSet(this, _AccountTreeController_categoryToRule, {
|
|
68
|
+
[account_api_1.AccountWalletCategory.Entropy]: new entropy_1.EntropyRule(this.messagingSystem),
|
|
69
|
+
[account_api_1.AccountWalletCategory.Snap]: new snap_1.SnapRule(this.messagingSystem),
|
|
70
|
+
[account_api_1.AccountWalletCategory.Keyring]: new keyring_1.KeyringRule(this.messagingSystem),
|
|
71
|
+
}, "f");
|
|
69
72
|
__classPrivateFieldSet(this, _AccountTreeController_rules, [
|
|
70
73
|
// 1. We group by entropy-source
|
|
71
|
-
|
|
74
|
+
__classPrivateFieldGet(this, _AccountTreeController_categoryToRule, "f")[account_api_1.AccountWalletCategory.Entropy],
|
|
72
75
|
// 2. We group by Snap ID
|
|
73
|
-
|
|
76
|
+
__classPrivateFieldGet(this, _AccountTreeController_categoryToRule, "f")[account_api_1.AccountWalletCategory.Snap],
|
|
74
77
|
// 3. We group by wallet type (this rule cannot fail and will group all non-matching accounts)
|
|
75
|
-
|
|
78
|
+
__classPrivateFieldGet(this, _AccountTreeController_categoryToRule, "f")[account_api_1.AccountWalletCategory.Keyring],
|
|
76
79
|
], "f");
|
|
77
80
|
this.messagingSystem.subscribe('AccountsController:accountAdded', (account) => {
|
|
78
81
|
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_handleAccountAdded).call(this, account);
|
|
@@ -80,10 +83,6 @@ class AccountTreeController extends base_controller_1.BaseController {
|
|
|
80
83
|
this.messagingSystem.subscribe('AccountsController:accountRemoved', (accountId) => {
|
|
81
84
|
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_handleAccountRemoved).call(this, accountId);
|
|
82
85
|
});
|
|
83
|
-
this.messagingSystem.subscribe('AccountsController:selectedAccountChange', (account) => {
|
|
84
|
-
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_handleSelectedAccountChange).call(this, account);
|
|
85
|
-
});
|
|
86
|
-
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_registerMessageHandlers).call(this);
|
|
87
86
|
}
|
|
88
87
|
init() {
|
|
89
88
|
const wallets = {};
|
|
@@ -91,119 +90,109 @@ class AccountTreeController extends base_controller_1.BaseController {
|
|
|
91
90
|
for (const account of __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_listAccounts).call(this)) {
|
|
92
91
|
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insert).call(this, wallets, account);
|
|
93
92
|
}
|
|
93
|
+
// Once we have the account tree, we can compute the name.
|
|
94
|
+
for (const wallet of Object.values(wallets)) {
|
|
95
|
+
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_renameAccountWalletIfNeeded).call(this, wallet);
|
|
96
|
+
for (const group of Object.values(wallet.groups)) {
|
|
97
|
+
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_renameAccountGroupIfNeeded).call(this, wallet, group);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
94
100
|
this.update((state) => {
|
|
95
101
|
state.accountTree.wallets = wallets;
|
|
96
|
-
if (state.selectedAccountGroup === '') {
|
|
97
|
-
// No group is selected yet, re-sync with the AccountsController.
|
|
98
|
-
state.selectedAccountGroup =
|
|
99
|
-
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getDefaultSelectedAccountGroup).call(this, wallets);
|
|
100
|
-
}
|
|
101
102
|
});
|
|
102
103
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return Array.from(__classPrivateFieldGet(this, _AccountTreeController_wallets, "f").values());
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Gets the currently selected account group ID.
|
|
111
|
-
*
|
|
112
|
-
* @returns The selected account group ID or empty string if none selected.
|
|
113
|
-
*/
|
|
114
|
-
getSelectedAccountGroup() {
|
|
115
|
-
return this.state.selectedAccountGroup;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Sets the selected account group and updates the AccountsController selectedAccount accordingly.
|
|
119
|
-
*
|
|
120
|
-
* @param accountGroupId - The account group ID to select.
|
|
121
|
-
*/
|
|
122
|
-
setSelectedAccountGroup(accountGroupId) {
|
|
123
|
-
const currentSelectedGroup = this.state.selectedAccountGroup;
|
|
124
|
-
// Idempotent check - if the same group is already selected, do nothing
|
|
125
|
-
if (currentSelectedGroup === accountGroupId) {
|
|
126
|
-
return;
|
|
104
|
+
getAccountWallet(walletId) {
|
|
105
|
+
const wallet = this.state.accountTree.wallets[walletId];
|
|
106
|
+
if (!wallet) {
|
|
107
|
+
return undefined;
|
|
127
108
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
// Update our state first
|
|
134
|
-
this.update((state) => {
|
|
135
|
-
state.selectedAccountGroup = accountGroupId;
|
|
109
|
+
return new AccountTreeWallet_1.AccountTreeWallet({ messenger: this.messagingSystem, wallet });
|
|
110
|
+
}
|
|
111
|
+
getAccountWallets() {
|
|
112
|
+
return Object.values(this.state.accountTree.wallets).map((wallet) => {
|
|
113
|
+
return new AccountTreeWallet_1.AccountTreeWallet({ messenger: this.messagingSystem, wallet });
|
|
136
114
|
});
|
|
137
|
-
// Update AccountsController - this will trigger selectedAccountChange event,
|
|
138
|
-
// but our handler is idempotent so it won't cause infinite loop
|
|
139
|
-
this.messagingSystem.call('AccountsController:setSelectedAccount', accountToSelect);
|
|
140
115
|
}
|
|
141
116
|
}
|
|
142
117
|
exports.AccountTreeController = AccountTreeController;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const accountMapping = __classPrivateFieldGet(this, _AccountTreeController_reverse, "f").get(selectedAccount.id);
|
|
147
|
-
if (accountMapping) {
|
|
148
|
-
const { groupId } = accountMapping;
|
|
149
|
-
return groupId;
|
|
150
|
-
}
|
|
118
|
+
_AccountTreeController_accountIdToContext = new WeakMap(), _AccountTreeController_rules = new WeakMap(), _AccountTreeController_categoryToRule = new WeakMap(), _AccountTreeController_instances = new WeakSet(), _AccountTreeController_renameAccountWalletIfNeeded = function _AccountTreeController_renameAccountWalletIfNeeded(wallet) {
|
|
119
|
+
if (wallet.metadata.name) {
|
|
120
|
+
return;
|
|
151
121
|
}
|
|
152
|
-
|
|
153
|
-
|
|
122
|
+
const rule = __classPrivateFieldGet(this, _AccountTreeController_categoryToRule, "f")[wallet.metadata.type];
|
|
123
|
+
wallet.metadata.name = rule.getDefaultAccountWalletName(wallet);
|
|
124
|
+
}, _AccountTreeController_renameAccountGroupIfNeeded = function _AccountTreeController_renameAccountGroupIfNeeded(wallet, group) {
|
|
125
|
+
if (group.metadata.name) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const rule = __classPrivateFieldGet(this, _AccountTreeController_categoryToRule, "f")[wallet.metadata.type];
|
|
129
|
+
group.metadata.name = rule.getDefaultAccountGroupName(group);
|
|
154
130
|
}, _AccountTreeController_handleAccountAdded = function _AccountTreeController_handleAccountAdded(account) {
|
|
155
131
|
this.update((state) => {
|
|
156
132
|
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insert).call(this, state.accountTree.wallets, account);
|
|
133
|
+
const context = __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(account.id);
|
|
134
|
+
if (context) {
|
|
135
|
+
const { walletId, groupId } = context;
|
|
136
|
+
const wallet = state.accountTree.wallets[walletId];
|
|
137
|
+
if (wallet) {
|
|
138
|
+
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_renameAccountWalletIfNeeded).call(this, wallet);
|
|
139
|
+
const group = wallet.groups[groupId];
|
|
140
|
+
if (group) {
|
|
141
|
+
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_renameAccountGroupIfNeeded).call(this, wallet, group);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
157
145
|
});
|
|
158
146
|
}, _AccountTreeController_handleAccountRemoved = function _AccountTreeController_handleAccountRemoved(accountId) {
|
|
159
|
-
const
|
|
160
|
-
if (
|
|
161
|
-
const { walletId, groupId } =
|
|
162
|
-
// Clean up the reverse mapping to prevent memory leaks
|
|
163
|
-
__classPrivateFieldGet(this, _AccountTreeController_reverse, "f").delete(accountId);
|
|
147
|
+
const context = __classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").get(accountId);
|
|
148
|
+
if (context) {
|
|
149
|
+
const { walletId, groupId } = context;
|
|
164
150
|
this.update((state) => {
|
|
165
|
-
const
|
|
166
|
-
const { accounts } = group;
|
|
151
|
+
const { accounts } = state.accountTree.wallets[walletId].groups[groupId];
|
|
167
152
|
const index = accounts.indexOf(accountId);
|
|
168
153
|
if (index !== -1) {
|
|
169
154
|
accounts.splice(index, 1);
|
|
170
155
|
}
|
|
171
|
-
// Check if we need to update selectedAccountGroup
|
|
172
|
-
if (state.selectedAccountGroup === groupId && accounts.length === 0) {
|
|
173
|
-
// The currently selected group is now empty, find a new group to select
|
|
174
|
-
state.selectedAccountGroup = __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_findFirstNonEmptyGroup).call(this, state.accountTree.wallets);
|
|
175
|
-
}
|
|
176
156
|
});
|
|
157
|
+
// Clear reverse-mapping for that account.
|
|
158
|
+
__classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").delete(accountId);
|
|
177
159
|
}
|
|
178
160
|
}, _AccountTreeController_insert = function _AccountTreeController_insert(wallets, account) {
|
|
179
161
|
for (const rule of __classPrivateFieldGet(this, _AccountTreeController_rules, "f")) {
|
|
180
|
-
const
|
|
181
|
-
if (!
|
|
162
|
+
const result = rule.match(account);
|
|
163
|
+
if (!result) {
|
|
182
164
|
// No match for that rule, we go to the next one.
|
|
183
165
|
continue;
|
|
184
166
|
}
|
|
185
|
-
const { wallet, group } = match;
|
|
186
|
-
// Update in-memory wallet/group instances.
|
|
187
|
-
__classPrivateFieldGet(this, _AccountTreeController_wallets, "f").set(wallet.id, wallet);
|
|
188
167
|
// Update controller's state.
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
168
|
+
const walletId = result.wallet.id;
|
|
169
|
+
let wallet = wallets[walletId];
|
|
170
|
+
if (!wallet) {
|
|
171
|
+
wallets[walletId] = {
|
|
172
|
+
id: walletId,
|
|
173
|
+
groups: {},
|
|
174
|
+
metadata: {
|
|
175
|
+
name: '',
|
|
176
|
+
...result.wallet.metadata,
|
|
198
177
|
},
|
|
178
|
+
};
|
|
179
|
+
wallet = wallets[walletId];
|
|
180
|
+
}
|
|
181
|
+
const groupId = result.group.id;
|
|
182
|
+
let group = wallet.groups[groupId];
|
|
183
|
+
if (!group) {
|
|
184
|
+
wallet.groups[groupId] = {
|
|
185
|
+
id: groupId,
|
|
186
|
+
accounts: [],
|
|
199
187
|
metadata: {
|
|
200
|
-
name:
|
|
188
|
+
name: '', // Will get updated later.
|
|
201
189
|
},
|
|
202
190
|
};
|
|
191
|
+
group = wallet.groups[groupId];
|
|
203
192
|
}
|
|
204
|
-
|
|
193
|
+
group.accounts.push(account.id);
|
|
205
194
|
// Update the reverse mapping for this account.
|
|
206
|
-
__classPrivateFieldGet(this,
|
|
195
|
+
__classPrivateFieldGet(this, _AccountTreeController_accountIdToContext, "f").set(account.id, {
|
|
207
196
|
walletId: wallet.id,
|
|
208
197
|
groupId: group.id,
|
|
209
198
|
});
|
|
@@ -211,43 +200,5 @@ _AccountTreeController_reverse = new WeakMap(), _AccountTreeController_rules = n
|
|
|
211
200
|
}
|
|
212
201
|
}, _AccountTreeController_listAccounts = function _AccountTreeController_listAccounts() {
|
|
213
202
|
return this.messagingSystem.call('AccountsController:listMultichainAccounts');
|
|
214
|
-
}, _AccountTreeController_handleSelectedAccountChange = function _AccountTreeController_handleSelectedAccountChange(account) {
|
|
215
|
-
const accountMapping = __classPrivateFieldGet(this, _AccountTreeController_reverse, "f").get(account.id);
|
|
216
|
-
if (!accountMapping) {
|
|
217
|
-
// Account not in tree yet, might be during initialization
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
const { groupId } = accountMapping;
|
|
221
|
-
const currentSelectedGroup = this.state.selectedAccountGroup;
|
|
222
|
-
// Idempotent check - if the same group is already selected, do nothing
|
|
223
|
-
if (currentSelectedGroup === groupId) {
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
// Update selectedAccountGroup to match the selected account
|
|
227
|
-
this.update((state) => {
|
|
228
|
-
state.selectedAccountGroup = groupId;
|
|
229
|
-
});
|
|
230
|
-
}, _AccountTreeController_getFirstAccountInGroup = function _AccountTreeController_getFirstAccountInGroup(accountGroupId) {
|
|
231
|
-
for (const wallet of Object.values(this.state.accountTree.wallets)) {
|
|
232
|
-
if (Object.hasOwnProperty.call(wallet.groups, accountGroupId)) {
|
|
233
|
-
const group = wallet.groups[accountGroupId];
|
|
234
|
-
if (group && group.accounts.length > 0) {
|
|
235
|
-
return group.accounts[0];
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return undefined;
|
|
240
|
-
}, _AccountTreeController_findFirstNonEmptyGroup = function _AccountTreeController_findFirstNonEmptyGroup(wallets) {
|
|
241
|
-
for (const wallet of Object.values(wallets)) {
|
|
242
|
-
for (const group of Object.values(wallet.groups)) {
|
|
243
|
-
if (group.accounts.length > 0) {
|
|
244
|
-
return group.id;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return '';
|
|
249
|
-
}, _AccountTreeController_registerMessageHandlers = function _AccountTreeController_registerMessageHandlers() {
|
|
250
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:getSelectedAccountGroup`, this.getSelectedAccountGroup.bind(this));
|
|
251
|
-
this.messagingSystem.registerActionHandler(`${controllerName}:setSelectedAccountGroup`, this.setSelectedAccountGroup.bind(this));
|
|
252
203
|
};
|
|
253
204
|
//# sourceMappingURL=AccountTreeController.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTreeController.cjs","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAYA,+DAKmC;AAOnC,6CAIiB;AAEjB,MAAM,cAAc,GAAG,uBAAuB,CAAC;AA0F/C,MAAM,6BAA6B,GACjC;IACE,WAAW,EAAE;QACX,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,oBAAoB,EAAE;QACpB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEJ;;;;GAIG;AACH,SAAgB,oCAAoC;IAClD,OAAO;QACL,WAAW,EAAE;YACX,OAAO,EAAE,EAAE;SACZ;QACD,oBAAoB,EAAE,EAAE;KACzB,CAAC;AACJ,CAAC;AAPD,oFAOC;AAED,MAAa,qBAAsB,SAAQ,gCAI1C;IAOC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,6BAA6B;YACvC,KAAK,EAAE;gBACL,GAAG,oCAAoC,EAAE;gBACzC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;;QA5BI,iDAAgD;QAEhD,+CAAqB;QAErB,iDAAkD;QAyBzD,uBAAA,IAAI,kCAAY,IAAI,GAAG,EAAE,MAAA,CAAC;QAE1B,4DAA4D;QAC5D,uBAAA,IAAI,kCAA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AE;gBACtC,OAAO,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;aAC1B;SACF;KACF;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,yGAQuB,OAEvB;IACC,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE;QAC3C,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE;YAChD,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;gBAC7B,OAAO,KAAK,CAAC,EAAE,CAAC;aACjB;SACF;KACF;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;IAMC,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,cAAc,0BAA0B,EAC3C,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,CACxC,CAAC;IAEF,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,cAAc,0BAA0B,EAC3C,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,CACxC,CAAC;AACJ,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport type {\n AccountId,\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetAccountAction,\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerListMultichainAccountsAction,\n AccountsControllerSelectedAccountChangeEvent,\n AccountsControllerSetSelectedAccountAction,\n} from '@metamask/accounts-controller';\nimport type { StateMetadata } from '@metamask/base-controller';\nimport {\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n type RestrictedMessenger,\n BaseController,\n} from '@metamask/base-controller';\nimport type { KeyringControllerGetStateAction } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { GetSnap as SnapControllerGetSnap } from '@metamask/snaps-controllers';\n\nimport type { AccountTreeWallet } from './AccountTreeWallet';\nimport type { WalletRule } from './rules';\nimport {\n EntropySourceWalletRule,\n SnapWalletRule,\n KeyringWalletRule,\n} from './rules';\n\nconst controllerName = 'AccountTreeController';\n\ntype AccountReverseMapping = {\n walletId: AccountWalletId;\n groupId: AccountGroupId;\n};\n\n// Do not export this one, we just use it to have a common type interface between group and wallet metadata.\ntype Metadata = {\n name: string;\n};\n\nexport type AccountWalletMetadata = Metadata;\n\nexport type AccountGroupMetadata = Metadata;\n\nexport type AccountGroupObject = {\n id: AccountGroupId;\n // Blockchain Accounts:\n accounts: AccountId[];\n metadata: AccountGroupMetadata;\n};\n\nexport type AccountWalletObject = {\n id: AccountWalletId;\n // Account groups OR Multichain accounts (once available).\n groups: {\n [groupId: AccountGroupId]: AccountGroupObject;\n };\n metadata: AccountWalletMetadata;\n};\n\nexport type AccountTreeControllerState = {\n accountTree: {\n wallets: {\n // Wallets:\n [walletId: AccountWalletId]: AccountWalletObject;\n };\n };\n selectedAccountGroup: AccountGroupId | '';\n};\n\nexport type AccountTreeControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AccountTreeControllerState\n>;\n\nexport type AccountTreeControllerSetSelectedAccountGroupAction = {\n type: `${typeof controllerName}:setSelectedAccountGroup`;\n handler: AccountTreeController['setSelectedAccountGroup'];\n};\n\nexport type AccountTreeControllerGetSelectedAccountGroupAction = {\n type: `${typeof controllerName}:getSelectedAccountGroup`;\n handler: AccountTreeController['getSelectedAccountGroup'];\n};\n\nexport type AllowedActions =\n | AccountsControllerGetAccountAction\n | AccountsControllerGetSelectedAccountAction\n | AccountsControllerListMultichainAccountsAction\n | AccountsControllerSetSelectedAccountAction\n | KeyringControllerGetStateAction\n | SnapControllerGetSnap;\n\nexport type AccountTreeControllerActions =\n | AccountTreeControllerGetStateAction\n | AccountTreeControllerSetSelectedAccountGroupAction\n | AccountTreeControllerGetSelectedAccountGroupAction;\n\nexport type AccountTreeControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AccountTreeControllerState\n>;\n\nexport type AllowedEvents =\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | AccountsControllerSelectedAccountChangeEvent;\n\nexport type AccountTreeControllerEvents = AccountTreeControllerStateChangeEvent;\n\nexport type AccountTreeControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AccountTreeControllerActions | AllowedActions,\n AccountTreeControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\nconst accountTreeControllerMetadata: StateMetadata<AccountTreeControllerState> =\n {\n accountTree: {\n persist: false, // We do re-recompute this state everytime.\n anonymous: false,\n },\n selectedAccountGroup: {\n persist: true,\n anonymous: false,\n },\n };\n\n/**\n * Gets default state of the `AccountTreeController`.\n *\n * @returns The default state of the `AccountTreeController`.\n */\nexport function getDefaultAccountTreeControllerState(): AccountTreeControllerState {\n return {\n accountTree: {\n wallets: {},\n },\n selectedAccountGroup: '',\n };\n}\n\nexport class AccountTreeController extends BaseController<\n typeof controllerName,\n AccountTreeControllerState,\n AccountTreeControllerMessenger\n> {\n readonly #reverse: Map<AccountId, AccountReverseMapping>;\n\n readonly #rules: WalletRule[];\n\n readonly #wallets: Map<AccountWalletId, AccountTreeWallet>;\n\n /**\n * Constructor for AccountTreeController.\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: AccountTreeControllerMessenger;\n state?: Partial<AccountTreeControllerState>;\n }) {\n super({\n messenger,\n name: controllerName,\n metadata: accountTreeControllerMetadata,\n state: {\n ...getDefaultAccountTreeControllerState(),\n ...state,\n },\n });\n this.#wallets = new Map();\n\n // Reverse map to allow fast node access from an account ID.\n this.#reverse = new Map();\n\n // Rules to apply to construct the wallets tree.\n this.#rules = [\n // 1. We group by entropy-source\n new EntropySourceWalletRule(this.messagingSystem),\n // 2. We group by Snap ID\n new SnapWalletRule(this.messagingSystem),\n // 3. We group by wallet type (this rule cannot fail and will group all non-matching accounts)\n new KeyringWalletRule(this.messagingSystem),\n ];\n\n this.messagingSystem.subscribe(\n 'AccountsController:accountAdded',\n (account) => {\n this.#handleAccountAdded(account);\n },\n );\n\n this.messagingSystem.subscribe(\n 'AccountsController:accountRemoved',\n (accountId) => {\n this.#handleAccountRemoved(accountId);\n },\n );\n\n this.messagingSystem.subscribe(\n 'AccountsController:selectedAccountChange',\n (account) => {\n this.#handleSelectedAccountChange(account);\n },\n );\n\n this.#registerMessageHandlers();\n }\n\n init() {\n const wallets: { [walletId: AccountWalletId]: AccountWalletObject } = {};\n\n // For now, we always re-compute all wallets, we do not re-use the existing state.\n for (const account of this.#listAccounts()) {\n this.#insert(wallets, account);\n }\n\n this.update((state) => {\n state.accountTree.wallets = wallets;\n\n if (state.selectedAccountGroup === '') {\n // No group is selected yet, re-sync with the AccountsController.\n state.selectedAccountGroup =\n this.#getDefaultSelectedAccountGroup(wallets);\n }\n });\n }\n\n /**\n * Initializes the selectedAccountGroup based on the currently selected account from AccountsController.\n *\n * @param wallets - Wallets object to use for fallback logic\n * @returns The default selected account group ID or empty string if none selected.\n */\n #getDefaultSelectedAccountGroup(wallets: {\n [walletId: AccountWalletId]: AccountWalletObject;\n }): AccountGroupId | '' {\n const selectedAccount = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n if (selectedAccount && selectedAccount.id) {\n const accountMapping = this.#reverse.get(selectedAccount.id);\n if (accountMapping) {\n const { groupId } = accountMapping;\n\n return groupId;\n }\n }\n\n // Default to the first wallet, first group in case of errors.\n return Object.values(Object.values(wallets)[0]?.groups ?? {})[0]?.id ?? '';\n }\n\n getWallet(id: AccountWalletId): AccountTreeWallet | undefined {\n return this.#wallets.get(id);\n }\n\n getWallets(): AccountTreeWallet[] {\n return Array.from(this.#wallets.values());\n }\n\n #handleAccountAdded(account: InternalAccount) {\n this.update((state) => {\n this.#insert(state.accountTree.wallets, account);\n });\n }\n\n #handleAccountRemoved(accountId: AccountId) {\n const found = this.#reverse.get(accountId);\n\n if (found) {\n const { walletId, groupId } = found;\n\n // Clean up the reverse mapping to prevent memory leaks\n this.#reverse.delete(accountId);\n\n this.update((state) => {\n const group = state.accountTree.wallets[walletId].groups[groupId];\n const { accounts } = group;\n\n const index = accounts.indexOf(accountId);\n if (index !== -1) {\n accounts.splice(index, 1);\n }\n\n // Check if we need to update selectedAccountGroup\n if (state.selectedAccountGroup === groupId && accounts.length === 0) {\n // The currently selected group is now empty, find a new group to select\n state.selectedAccountGroup = this.#findFirstNonEmptyGroup(\n state.accountTree.wallets,\n );\n }\n });\n }\n }\n\n #insert(\n wallets: { [walletId: AccountWalletId]: AccountWalletObject },\n account: InternalAccount,\n ) {\n for (const rule of this.#rules) {\n const match = rule.match(account);\n\n if (!match) {\n // No match for that rule, we go to the next one.\n continue;\n }\n\n const { wallet, group } = match;\n\n // Update in-memory wallet/group instances.\n this.#wallets.set(wallet.id, wallet);\n\n // Update controller's state.\n if (!wallets[wallet.id]) {\n wallets[wallet.id] = {\n id: wallet.id,\n groups: {\n [group.id]: {\n id: group.id,\n accounts: [],\n metadata: { name: group.getDefaultName() },\n },\n },\n metadata: {\n name: wallet.getDefaultName(),\n },\n };\n }\n wallets[wallet.id].groups[group.id].accounts.push(account.id);\n\n // Update the reverse mapping for this account.\n this.#reverse.set(account.id, {\n walletId: wallet.id,\n groupId: group.id,\n });\n\n return;\n }\n }\n\n #listAccounts(): InternalAccount[] {\n return this.messagingSystem.call(\n 'AccountsController:listMultichainAccounts',\n );\n }\n\n /**\n * Gets the currently selected account group ID.\n *\n * @returns The selected account group ID or empty string if none selected.\n */\n getSelectedAccountGroup(): AccountGroupId | '' {\n return this.state.selectedAccountGroup;\n }\n\n /**\n * Sets the selected account group and updates the AccountsController selectedAccount accordingly.\n *\n * @param accountGroupId - The account group ID to select.\n */\n setSelectedAccountGroup(accountGroupId: AccountGroupId): void {\n const currentSelectedGroup = this.state.selectedAccountGroup;\n\n // Idempotent check - if the same group is already selected, do nothing\n if (currentSelectedGroup === accountGroupId) {\n return;\n }\n\n // Find the first account in this group to select\n const accountToSelect = this.#getFirstAccountInGroup(accountGroupId);\n if (!accountToSelect) {\n throw new Error(`No accounts found in group: ${accountGroupId}`);\n }\n\n // Update our state first\n this.update((state) => {\n state.selectedAccountGroup = accountGroupId;\n });\n\n // Update AccountsController - this will trigger selectedAccountChange event,\n // but our handler is idempotent so it won't cause infinite loop\n this.messagingSystem.call(\n 'AccountsController:setSelectedAccount',\n accountToSelect,\n );\n }\n\n /**\n * Handles selected account change from AccountsController.\n * Updates selectedAccountGroup to match the selected account.\n *\n * @param account - The newly selected account.\n */\n #handleSelectedAccountChange(account: InternalAccount): void {\n const accountMapping = this.#reverse.get(account.id);\n if (!accountMapping) {\n // Account not in tree yet, might be during initialization\n return;\n }\n\n const { groupId } = accountMapping;\n const currentSelectedGroup = this.state.selectedAccountGroup;\n\n // Idempotent check - if the same group is already selected, do nothing\n if (currentSelectedGroup === groupId) {\n return;\n }\n\n // Update selectedAccountGroup to match the selected account\n this.update((state) => {\n state.selectedAccountGroup = groupId;\n });\n }\n\n /**\n * Gets the first account ID in the specified group.\n *\n * @param accountGroupId - The account group ID.\n * @returns The first account ID in the group, or undefined if no accounts found.\n */\n #getFirstAccountInGroup(\n accountGroupId: AccountGroupId,\n ): AccountId | undefined {\n for (const wallet of Object.values(\n this.state.accountTree.wallets,\n ) as AccountWalletObject[]) {\n if (Object.hasOwnProperty.call(wallet.groups, accountGroupId)) {\n const group = (wallet.groups as Record<string, AccountGroupObject>)[\n accountGroupId\n ];\n if (group && group.accounts.length > 0) {\n return group.accounts[0];\n }\n }\n }\n return undefined;\n }\n\n /**\n * Finds the first non-empty group in the given wallets object.\n *\n * @param wallets - The wallets object to search.\n * @returns The ID of the first non-empty group, or an empty string if no groups are found.\n */\n #findFirstNonEmptyGroup(wallets: {\n [walletId: AccountWalletId]: AccountWalletObject;\n }): AccountGroupId | '' {\n for (const wallet of Object.values(wallets)) {\n for (const group of Object.values(wallet.groups)) {\n if (group.accounts.length > 0) {\n return group.id;\n }\n }\n }\n return '';\n }\n\n /**\n * Registers message handlers for the AccountTreeController.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getSelectedAccountGroup`,\n this.getSelectedAccountGroup.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:setSelectedAccountGroup`,\n this.setSelectedAccountGroup.bind(this),\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AccountTreeController.cjs","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAA8D;AAG9D,+DAA2D;AAI3D,+DAAwD;AACxD,iDAA8C;AAC9C,iDAA8C;AAC9C,2CAAwC;AAQ3B,QAAA,cAAc,GAAG,uBAAuB,CAAC;AAEtD,MAAM,6BAA6B,GACjC;IACE,WAAW,EAAE;QACX,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEJ;;;;GAIG;AACH,SAAgB,oCAAoC;IAClD,OAAO;QACL,WAAW,EAAE;YACX,OAAO,EAAE,EAAE;SACZ;KACF,CAAC;AACJ,CAAC;AAND,oFAMC;AAiBD,MAAa,qBAAsB,SAAQ,gCAI1C;IAOC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,IAAI,EAAE,sBAAc;YACpB,QAAQ,EAAE,6BAA6B;YACvC,KAAK,EAAE;gBACL,GAAG,oCAAoC,EAAE;gBACzC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;;QA5BI,4DAAoD;QAEpD,+CAA0B;QAE1B,wDAAgE;QA0BvE,4DAA4D;QAC5D,uBAAA,IAAI,6CAAuB,IAAI,GAAG,EAAE,MAAA,CAAC;QAErC,gDAAgD;QAChD,uBAAA,IAAI,yCAAmB;YACrB,CAAC,mCAAqB,CAAC,OAAO,CAAC,EAAE,IAAI,qBAAW,C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;gBACvB,EAAE,EAAE,OAAO;gBACX,QAAQ,EAAE,EAAE;gBACZ,QAAQ,EAAE;oBACR,IAAI,EAAE,EAAE,EAAE,0BAA0B;iBACrC;aACF,CAAC;YACF,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;SAChC;QAED,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAEhC,+CAA+C;QAC/C,uBAAA,IAAI,iDAAoB,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE;YACvC,QAAQ,EAAE,MAAM,CAAC,EAAE;YACnB,OAAO,EAAE,KAAK,CAAC,EAAE;SAClB,CAAC,CAAC;QAEH,OAAO;KACR;AACH,CAAC;IAGC,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAC9B,2CAA2C,CAC5C,CAAC;AACJ,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletCategory } from '@metamask/account-api';\nimport type { AccountId } from '@metamask/accounts-controller';\nimport type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { AccountTreeRule } from './AccountTreeRule';\nimport { AccountTreeWallet } from './AccountTreeWallet';\nimport { EntropyRule } from './rules/entropy';\nimport { KeyringRule } from './rules/keyring';\nimport { SnapRule } from './rules/snap';\nimport type {\n AccountGroupObject,\n AccountTreeControllerMessenger,\n AccountTreeControllerState,\n AccountWalletObject,\n} from './types';\n\nexport const controllerName = 'AccountTreeController';\n\nconst accountTreeControllerMetadata: StateMetadata<AccountTreeControllerState> =\n {\n accountTree: {\n persist: false, // We do re-recompute this state everytime.\n anonymous: false,\n },\n };\n\n/**\n * Gets default state of the `AccountTreeController`.\n *\n * @returns The default state of the `AccountTreeController`.\n */\nexport function getDefaultAccountTreeControllerState(): AccountTreeControllerState {\n return {\n accountTree: {\n wallets: {},\n },\n };\n}\n\n/**\n * Context for an account.\n */\nexport type AccountContext = {\n /**\n * Wallet ID associated to that account.\n */\n walletId: AccountWalletId;\n\n /**\n * Account group ID associated to that account.\n */\n groupId: AccountGroupId;\n};\n\nexport class AccountTreeController extends BaseController<\n typeof controllerName,\n AccountTreeControllerState,\n AccountTreeControllerMessenger\n> {\n readonly #accountIdToContext: Map<AccountId, AccountContext>;\n\n readonly #rules: AccountTreeRule[];\n\n readonly #categoryToRule: Record<AccountWalletCategory, AccountTreeRule>;\n\n /**\n * Constructor for AccountTreeController.\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: AccountTreeControllerMessenger;\n state?: Partial<AccountTreeControllerState>;\n }) {\n super({\n messenger,\n name: controllerName,\n metadata: accountTreeControllerMetadata,\n state: {\n ...getDefaultAccountTreeControllerState(),\n ...state,\n },\n });\n\n // Reverse map to allow fast node access from an account ID.\n this.#accountIdToContext = new Map();\n\n // Rules to apply to construct the wallets tree.\n this.#categoryToRule = {\n [AccountWalletCategory.Entropy]: new EntropyRule(this.messagingSystem),\n [AccountWalletCategory.Snap]: new SnapRule(this.messagingSystem),\n [AccountWalletCategory.Keyring]: new KeyringRule(this.messagingSystem),\n } as const;\n this.#rules = [\n // 1. We group by entropy-source\n this.#categoryToRule[AccountWalletCategory.Entropy],\n // 2. We group by Snap ID\n this.#categoryToRule[AccountWalletCategory.Snap],\n // 3. We group by wallet type (this rule cannot fail and will group all non-matching accounts)\n this.#categoryToRule[AccountWalletCategory.Keyring],\n ];\n\n this.messagingSystem.subscribe(\n 'AccountsController:accountAdded',\n (account) => {\n this.#handleAccountAdded(account);\n },\n );\n\n this.messagingSystem.subscribe(\n 'AccountsController:accountRemoved',\n (accountId) => {\n this.#handleAccountRemoved(accountId);\n },\n );\n }\n\n init() {\n const wallets: AccountTreeControllerState['accountTree']['wallets'] = {};\n\n // For now, we always re-compute all wallets, we do not re-use the existing state.\n for (const account of this.#listAccounts()) {\n this.#insert(wallets, account);\n }\n\n // Once we have the account tree, we can compute the name.\n for (const wallet of Object.values(wallets)) {\n this.#renameAccountWalletIfNeeded(wallet);\n\n for (const group of Object.values(wallet.groups)) {\n this.#renameAccountGroupIfNeeded(wallet, group);\n }\n }\n\n this.update((state) => {\n state.accountTree.wallets = wallets;\n });\n }\n\n #renameAccountWalletIfNeeded(wallet: AccountWalletObject) {\n if (wallet.metadata.name) {\n return;\n }\n\n const rule = this.#categoryToRule[wallet.metadata.type];\n wallet.metadata.name = rule.getDefaultAccountWalletName(wallet);\n }\n\n #renameAccountGroupIfNeeded(\n wallet: AccountWalletObject,\n group: AccountGroupObject,\n ) {\n if (group.metadata.name) {\n return;\n }\n\n const rule = this.#categoryToRule[wallet.metadata.type];\n group.metadata.name = rule.getDefaultAccountGroupName(group);\n }\n\n getAccountWallet(walletId: AccountWalletId): AccountTreeWallet | undefined {\n const wallet = this.state.accountTree.wallets[walletId];\n if (!wallet) {\n return undefined;\n }\n\n return new AccountTreeWallet({ messenger: this.messagingSystem, wallet });\n }\n\n getAccountWallets(): AccountTreeWallet[] {\n return Object.values(this.state.accountTree.wallets).map((wallet) => {\n return new AccountTreeWallet({ messenger: this.messagingSystem, wallet });\n });\n }\n\n #handleAccountAdded(account: InternalAccount) {\n this.update((state) => {\n this.#insert(state.accountTree.wallets, account);\n\n const context = this.#accountIdToContext.get(account.id);\n if (context) {\n const { walletId, groupId } = context;\n\n const wallet = state.accountTree.wallets[walletId];\n if (wallet) {\n this.#renameAccountWalletIfNeeded(wallet);\n\n const group = wallet.groups[groupId];\n if (group) {\n this.#renameAccountGroupIfNeeded(wallet, group);\n }\n }\n }\n });\n }\n\n #handleAccountRemoved(accountId: AccountId) {\n const context = this.#accountIdToContext.get(accountId);\n\n if (context) {\n const { walletId, groupId } = context;\n\n this.update((state) => {\n const { accounts } =\n state.accountTree.wallets[walletId].groups[groupId];\n\n const index = accounts.indexOf(accountId);\n if (index !== -1) {\n accounts.splice(index, 1);\n }\n });\n\n // Clear reverse-mapping for that account.\n this.#accountIdToContext.delete(accountId);\n }\n }\n\n #insert(\n wallets: AccountTreeControllerState['accountTree']['wallets'],\n account: InternalAccount,\n ) {\n for (const rule of this.#rules) {\n const result = rule.match(account);\n\n if (!result) {\n // No match for that rule, we go to the next one.\n continue;\n }\n\n // Update controller's state.\n const walletId = result.wallet.id;\n let wallet = wallets[walletId];\n if (!wallet) {\n wallets[walletId] = {\n id: walletId,\n groups: {},\n metadata: {\n name: '', // Will get updated later.\n ...result.wallet.metadata,\n },\n };\n wallet = wallets[walletId];\n }\n\n const groupId = result.group.id;\n let group = wallet.groups[groupId];\n if (!group) {\n wallet.groups[groupId] = {\n id: groupId,\n accounts: [],\n metadata: {\n name: '', // Will get updated later.\n },\n };\n group = wallet.groups[groupId];\n }\n\n group.accounts.push(account.id);\n\n // Update the reverse mapping for this account.\n this.#accountIdToContext.set(account.id, {\n walletId: wallet.id,\n groupId: group.id,\n });\n\n return;\n }\n }\n\n #listAccounts(): InternalAccount[] {\n return this.messagingSystem.call(\n 'AccountsController:listMultichainAccounts',\n );\n }\n}\n"]}
|
|
@@ -1,56 +1,27 @@
|
|
|
1
1
|
import type { AccountGroupId, AccountWalletId } from "@metamask/account-api";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
import type { AccountTreeWallet } from "./AccountTreeWallet.cjs";
|
|
7
|
-
declare const controllerName = "AccountTreeController";
|
|
8
|
-
type Metadata = {
|
|
9
|
-
name: string;
|
|
10
|
-
};
|
|
11
|
-
export type AccountWalletMetadata = Metadata;
|
|
12
|
-
export type AccountGroupMetadata = Metadata;
|
|
13
|
-
export type AccountGroupObject = {
|
|
14
|
-
id: AccountGroupId;
|
|
15
|
-
accounts: AccountId[];
|
|
16
|
-
metadata: AccountGroupMetadata;
|
|
17
|
-
};
|
|
18
|
-
export type AccountWalletObject = {
|
|
19
|
-
id: AccountWalletId;
|
|
20
|
-
groups: {
|
|
21
|
-
[groupId: AccountGroupId]: AccountGroupObject;
|
|
22
|
-
};
|
|
23
|
-
metadata: AccountWalletMetadata;
|
|
24
|
-
};
|
|
25
|
-
export type AccountTreeControllerState = {
|
|
26
|
-
accountTree: {
|
|
27
|
-
wallets: {
|
|
28
|
-
[walletId: AccountWalletId]: AccountWalletObject;
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
selectedAccountGroup: AccountGroupId | '';
|
|
32
|
-
};
|
|
33
|
-
export type AccountTreeControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AccountTreeControllerState>;
|
|
34
|
-
export type AccountTreeControllerSetSelectedAccountGroupAction = {
|
|
35
|
-
type: `${typeof controllerName}:setSelectedAccountGroup`;
|
|
36
|
-
handler: AccountTreeController['setSelectedAccountGroup'];
|
|
37
|
-
};
|
|
38
|
-
export type AccountTreeControllerGetSelectedAccountGroupAction = {
|
|
39
|
-
type: `${typeof controllerName}:getSelectedAccountGroup`;
|
|
40
|
-
handler: AccountTreeController['getSelectedAccountGroup'];
|
|
41
|
-
};
|
|
42
|
-
export type AllowedActions = AccountsControllerGetAccountAction | AccountsControllerGetSelectedAccountAction | AccountsControllerListMultichainAccountsAction | AccountsControllerSetSelectedAccountAction | KeyringControllerGetStateAction | SnapControllerGetSnap;
|
|
43
|
-
export type AccountTreeControllerActions = AccountTreeControllerGetStateAction | AccountTreeControllerSetSelectedAccountGroupAction | AccountTreeControllerGetSelectedAccountGroupAction;
|
|
44
|
-
export type AccountTreeControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AccountTreeControllerState>;
|
|
45
|
-
export type AllowedEvents = AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent | AccountsControllerSelectedAccountChangeEvent;
|
|
46
|
-
export type AccountTreeControllerEvents = AccountTreeControllerStateChangeEvent;
|
|
47
|
-
export type AccountTreeControllerMessenger = RestrictedMessenger<typeof controllerName, AccountTreeControllerActions | AllowedActions, AccountTreeControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import { AccountTreeWallet } from "./AccountTreeWallet.cjs";
|
|
4
|
+
import type { AccountTreeControllerMessenger, AccountTreeControllerState } from "./types.cjs";
|
|
5
|
+
export declare const controllerName = "AccountTreeController";
|
|
48
6
|
/**
|
|
49
7
|
* Gets default state of the `AccountTreeController`.
|
|
50
8
|
*
|
|
51
9
|
* @returns The default state of the `AccountTreeController`.
|
|
52
10
|
*/
|
|
53
11
|
export declare function getDefaultAccountTreeControllerState(): AccountTreeControllerState;
|
|
12
|
+
/**
|
|
13
|
+
* Context for an account.
|
|
14
|
+
*/
|
|
15
|
+
export type AccountContext = {
|
|
16
|
+
/**
|
|
17
|
+
* Wallet ID associated to that account.
|
|
18
|
+
*/
|
|
19
|
+
walletId: AccountWalletId;
|
|
20
|
+
/**
|
|
21
|
+
* Account group ID associated to that account.
|
|
22
|
+
*/
|
|
23
|
+
groupId: AccountGroupId;
|
|
24
|
+
};
|
|
54
25
|
export declare class AccountTreeController extends BaseController<typeof controllerName, AccountTreeControllerState, AccountTreeControllerMessenger> {
|
|
55
26
|
#private;
|
|
56
27
|
/**
|
|
@@ -65,20 +36,7 @@ export declare class AccountTreeController extends BaseController<typeof control
|
|
|
65
36
|
state?: Partial<AccountTreeControllerState>;
|
|
66
37
|
});
|
|
67
38
|
init(): void;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Gets the currently selected account group ID.
|
|
72
|
-
*
|
|
73
|
-
* @returns The selected account group ID or empty string if none selected.
|
|
74
|
-
*/
|
|
75
|
-
getSelectedAccountGroup(): AccountGroupId | '';
|
|
76
|
-
/**
|
|
77
|
-
* Sets the selected account group and updates the AccountsController selectedAccount accordingly.
|
|
78
|
-
*
|
|
79
|
-
* @param accountGroupId - The account group ID to select.
|
|
80
|
-
*/
|
|
81
|
-
setSelectedAccountGroup(accountGroupId: AccountGroupId): void;
|
|
39
|
+
getAccountWallet(walletId: AccountWalletId): AccountTreeWallet | undefined;
|
|
40
|
+
getAccountWallets(): AccountTreeWallet[];
|
|
82
41
|
}
|
|
83
|
-
export {};
|
|
84
42
|
//# sourceMappingURL=AccountTreeController.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTreeController.d.cts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;
|
|
1
|
+
{"version":3,"file":"AccountTreeController.d.cts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAI7E,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAI3D,OAAO,EAAE,iBAAiB,EAAE,gCAA4B;AAIxD,OAAO,KAAK,EAEV,8BAA8B,EAC9B,0BAA0B,EAE3B,oBAAgB;AAEjB,eAAO,MAAM,cAAc,0BAA0B,CAAC;AAUtD;;;;GAIG;AACH,wBAAgB,oCAAoC,IAAI,0BAA0B,CAMjF;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;OAEG;IACH,QAAQ,EAAE,eAAe,CAAC;IAE1B;;OAEG;IACH,OAAO,EAAE,cAAc,CAAC;CACzB,CAAC;AAEF,qBAAa,qBAAsB,SAAQ,cAAc,CACvD,OAAO,cAAc,EACrB,0BAA0B,EAC1B,8BAA8B,CAC/B;;IAOC;;;;;;OAMG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,8BAA8B,CAAC;QAC1C,KAAK,CAAC,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC;KAC7C;IA4CD,IAAI;IA2CJ,gBAAgB,CAAC,QAAQ,EAAE,eAAe,GAAG,iBAAiB,GAAG,SAAS;IAS1E,iBAAiB,IAAI,iBAAiB,EAAE;CAyGzC"}
|
|
@@ -1,56 +1,27 @@
|
|
|
1
1
|
import type { AccountGroupId, AccountWalletId } from "@metamask/account-api";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
import type { AccountTreeWallet } from "./AccountTreeWallet.mjs";
|
|
7
|
-
declare const controllerName = "AccountTreeController";
|
|
8
|
-
type Metadata = {
|
|
9
|
-
name: string;
|
|
10
|
-
};
|
|
11
|
-
export type AccountWalletMetadata = Metadata;
|
|
12
|
-
export type AccountGroupMetadata = Metadata;
|
|
13
|
-
export type AccountGroupObject = {
|
|
14
|
-
id: AccountGroupId;
|
|
15
|
-
accounts: AccountId[];
|
|
16
|
-
metadata: AccountGroupMetadata;
|
|
17
|
-
};
|
|
18
|
-
export type AccountWalletObject = {
|
|
19
|
-
id: AccountWalletId;
|
|
20
|
-
groups: {
|
|
21
|
-
[groupId: AccountGroupId]: AccountGroupObject;
|
|
22
|
-
};
|
|
23
|
-
metadata: AccountWalletMetadata;
|
|
24
|
-
};
|
|
25
|
-
export type AccountTreeControllerState = {
|
|
26
|
-
accountTree: {
|
|
27
|
-
wallets: {
|
|
28
|
-
[walletId: AccountWalletId]: AccountWalletObject;
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
selectedAccountGroup: AccountGroupId | '';
|
|
32
|
-
};
|
|
33
|
-
export type AccountTreeControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AccountTreeControllerState>;
|
|
34
|
-
export type AccountTreeControllerSetSelectedAccountGroupAction = {
|
|
35
|
-
type: `${typeof controllerName}:setSelectedAccountGroup`;
|
|
36
|
-
handler: AccountTreeController['setSelectedAccountGroup'];
|
|
37
|
-
};
|
|
38
|
-
export type AccountTreeControllerGetSelectedAccountGroupAction = {
|
|
39
|
-
type: `${typeof controllerName}:getSelectedAccountGroup`;
|
|
40
|
-
handler: AccountTreeController['getSelectedAccountGroup'];
|
|
41
|
-
};
|
|
42
|
-
export type AllowedActions = AccountsControllerGetAccountAction | AccountsControllerGetSelectedAccountAction | AccountsControllerListMultichainAccountsAction | AccountsControllerSetSelectedAccountAction | KeyringControllerGetStateAction | SnapControllerGetSnap;
|
|
43
|
-
export type AccountTreeControllerActions = AccountTreeControllerGetStateAction | AccountTreeControllerSetSelectedAccountGroupAction | AccountTreeControllerGetSelectedAccountGroupAction;
|
|
44
|
-
export type AccountTreeControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AccountTreeControllerState>;
|
|
45
|
-
export type AllowedEvents = AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent | AccountsControllerSelectedAccountChangeEvent;
|
|
46
|
-
export type AccountTreeControllerEvents = AccountTreeControllerStateChangeEvent;
|
|
47
|
-
export type AccountTreeControllerMessenger = RestrictedMessenger<typeof controllerName, AccountTreeControllerActions | AllowedActions, AccountTreeControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import { AccountTreeWallet } from "./AccountTreeWallet.mjs";
|
|
4
|
+
import type { AccountTreeControllerMessenger, AccountTreeControllerState } from "./types.mjs";
|
|
5
|
+
export declare const controllerName = "AccountTreeController";
|
|
48
6
|
/**
|
|
49
7
|
* Gets default state of the `AccountTreeController`.
|
|
50
8
|
*
|
|
51
9
|
* @returns The default state of the `AccountTreeController`.
|
|
52
10
|
*/
|
|
53
11
|
export declare function getDefaultAccountTreeControllerState(): AccountTreeControllerState;
|
|
12
|
+
/**
|
|
13
|
+
* Context for an account.
|
|
14
|
+
*/
|
|
15
|
+
export type AccountContext = {
|
|
16
|
+
/**
|
|
17
|
+
* Wallet ID associated to that account.
|
|
18
|
+
*/
|
|
19
|
+
walletId: AccountWalletId;
|
|
20
|
+
/**
|
|
21
|
+
* Account group ID associated to that account.
|
|
22
|
+
*/
|
|
23
|
+
groupId: AccountGroupId;
|
|
24
|
+
};
|
|
54
25
|
export declare class AccountTreeController extends BaseController<typeof controllerName, AccountTreeControllerState, AccountTreeControllerMessenger> {
|
|
55
26
|
#private;
|
|
56
27
|
/**
|
|
@@ -65,20 +36,7 @@ export declare class AccountTreeController extends BaseController<typeof control
|
|
|
65
36
|
state?: Partial<AccountTreeControllerState>;
|
|
66
37
|
});
|
|
67
38
|
init(): void;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Gets the currently selected account group ID.
|
|
72
|
-
*
|
|
73
|
-
* @returns The selected account group ID or empty string if none selected.
|
|
74
|
-
*/
|
|
75
|
-
getSelectedAccountGroup(): AccountGroupId | '';
|
|
76
|
-
/**
|
|
77
|
-
* Sets the selected account group and updates the AccountsController selectedAccount accordingly.
|
|
78
|
-
*
|
|
79
|
-
* @param accountGroupId - The account group ID to select.
|
|
80
|
-
*/
|
|
81
|
-
setSelectedAccountGroup(accountGroupId: AccountGroupId): void;
|
|
39
|
+
getAccountWallet(walletId: AccountWalletId): AccountTreeWallet | undefined;
|
|
40
|
+
getAccountWallets(): AccountTreeWallet[];
|
|
82
41
|
}
|
|
83
|
-
export {};
|
|
84
42
|
//# sourceMappingURL=AccountTreeController.d.mts.map
|