@metamask-previews/account-tree-controller 0.4.0-preview-d90711c → 0.4.0-preview-ea7474e2
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 +8 -0
- package/dist/AccountTreeController.cjs +30 -137
- package/dist/AccountTreeController.cjs.map +1 -1
- package/dist/AccountTreeController.d.cts +10 -38
- package/dist/AccountTreeController.d.cts.map +1 -1
- package/dist/AccountTreeController.d.mts +10 -38
- package/dist/AccountTreeController.d.mts.map +1 -1
- package/dist/AccountTreeController.mjs +29 -133
- package/dist/AccountTreeController.mjs.map +1 -1
- package/dist/AccountTreeGroup.cjs +58 -0
- package/dist/AccountTreeGroup.cjs.map +1 -0
- package/dist/AccountTreeGroup.d.cts +31 -0
- package/dist/AccountTreeGroup.d.cts.map +1 -0
- package/dist/AccountTreeGroup.d.mts +31 -0
- package/dist/AccountTreeGroup.d.mts.map +1 -0
- package/dist/AccountTreeGroup.mjs +54 -0
- package/dist/AccountTreeGroup.mjs.map +1 -0
- package/dist/AccountTreeWallet.cjs +50 -0
- package/dist/AccountTreeWallet.cjs.map +1 -0
- package/dist/AccountTreeWallet.d.cts +38 -0
- package/dist/AccountTreeWallet.d.cts.map +1 -0
- package/dist/AccountTreeWallet.d.mts +38 -0
- package/dist/AccountTreeWallet.d.mts.map +1 -0
- package/dist/AccountTreeWallet.mjs +46 -0
- package/dist/AccountTreeWallet.mjs.map +1 -0
- package/dist/index.cjs +1 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/rules/EntropySourceWalletRule.cjs +100 -0
- package/dist/rules/EntropySourceWalletRule.cjs.map +1 -0
- package/dist/rules/EntropySourceWalletRule.d.cts +19 -0
- package/dist/rules/EntropySourceWalletRule.d.cts.map +1 -0
- package/dist/rules/EntropySourceWalletRule.d.mts +19 -0
- package/dist/rules/EntropySourceWalletRule.d.mts.map +1 -0
- package/dist/rules/EntropySourceWalletRule.mjs +95 -0
- package/dist/rules/EntropySourceWalletRule.mjs.map +1 -0
- package/dist/rules/KeyringWalletRule.cjs +99 -0
- package/dist/rules/KeyringWalletRule.cjs.map +1 -0
- package/dist/rules/KeyringWalletRule.d.cts +18 -0
- package/dist/rules/KeyringWalletRule.d.cts.map +1 -0
- package/dist/rules/KeyringWalletRule.d.mts +18 -0
- package/dist/rules/KeyringWalletRule.d.mts.map +1 -0
- package/dist/rules/KeyringWalletRule.mjs +94 -0
- package/dist/rules/KeyringWalletRule.mjs.map +1 -0
- package/dist/rules/SnapWalletRule.cjs +70 -0
- package/dist/rules/SnapWalletRule.cjs.map +1 -0
- package/dist/rules/SnapWalletRule.d.cts +10 -0
- package/dist/rules/SnapWalletRule.d.cts.map +1 -0
- package/dist/rules/SnapWalletRule.d.mts +10 -0
- package/dist/rules/SnapWalletRule.d.mts.map +1 -0
- package/dist/rules/SnapWalletRule.mjs +66 -0
- package/dist/rules/SnapWalletRule.mjs.map +1 -0
- package/dist/rules/WalletRule.cjs +13 -0
- package/dist/rules/WalletRule.cjs.map +1 -0
- package/dist/rules/WalletRule.d.cts +37 -0
- package/dist/rules/WalletRule.d.cts.map +1 -0
- package/dist/rules/WalletRule.d.mts +37 -0
- package/dist/rules/WalletRule.d.mts.map +1 -0
- package/dist/rules/WalletRule.mjs +9 -0
- package/dist/rules/WalletRule.mjs.map +1 -0
- package/dist/rules/index.cjs +21 -0
- package/dist/rules/index.cjs.map +1 -0
- package/dist/rules/index.d.cts +5 -0
- package/dist/rules/index.d.cts.map +1 -0
- package/dist/rules/index.d.mts +5 -0
- package/dist/rules/index.d.mts.map +1 -0
- package/dist/rules/index.mjs +5 -0
- package/dist/rules/index.mjs.map +1 -0
- package/dist/rules/utils.cjs +15 -0
- package/dist/rules/utils.cjs.map +1 -0
- package/dist/rules/utils.d.cts +11 -0
- package/dist/rules/utils.d.cts.map +1 -0
- package/dist/rules/utils.d.mts +11 -0
- package/dist/rules/utils.d.mts.map +1 -0
- package/dist/rules/utils.mjs +11 -0
- package/dist/rules/utils.mjs.map +1 -0
- package/package.json +3 -1
- package/dist/names.cjs +0 -46
- package/dist/names.cjs.map +0 -1
- package/dist/names.d.cts +0 -9
- package/dist/names.d.cts.map +0 -1
- package/dist/names.d.mts +0 -9
- package/dist/names.d.mts.map +0 -1
- package/dist/names.mjs +0 -42
- package/dist/names.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,11 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
+
- **BREAKING:** Add `@metamask/account-api` peer dependency ([#6115](https://github.com/MetaMask/core/pull/6115))
|
|
13
|
+
- **BREAKING:** Types `AccountWallet` and `AccountGroup` have been respectively renamed to `AccountWalletObject` and `AccountGroupObject` ([#6115](https://github.com/MetaMask/core/pull/6115))
|
|
14
|
+
- Those names are now used by the `@metamask/account-api` package to define higher-level interfaces.
|
|
12
15
|
- **BREAKING:** Bump peer dependency `@metamask/snaps-controllers` from `^12.0.0` to `^14.0.0` ([#6035](https://github.com/MetaMask/core/pull/6035))
|
|
13
16
|
- Bump `@metamask/snaps-sdk` from `^7.1.0` to `^9.0.0` ([#6035](https://github.com/MetaMask/core/pull/6035))
|
|
14
17
|
- Bump `@metamask/snaps-utils` from `^9.4.0` to `^11.0.0` ([#6035](https://github.com/MetaMask/core/pull/6035))
|
|
15
18
|
- Properly export `AccountWalletCategory` constant and conversion functions ([#6062](https://github.com/MetaMask/core/pull/6062))
|
|
16
19
|
|
|
20
|
+
### Removed
|
|
21
|
+
|
|
22
|
+
- **BREAKING:** No longer export `AccountWalletCategory`, `toAccountWalletId`, `toAccountGroupId` and `toDefaultAccountGroupId` ([#6115](https://github.com/MetaMask/core/pull/6115))
|
|
23
|
+
- You should now import them from the `@metamask/account-api` package (peer dependency).
|
|
24
|
+
|
|
17
25
|
## [0.4.0]
|
|
18
26
|
|
|
19
27
|
### Changed
|
|
@@ -10,20 +10,12 @@ 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, _AccountTreeController_reverse, _AccountTreeController_rules, _AccountTreeController_handleAccountAdded, _AccountTreeController_handleAccountRemoved,
|
|
13
|
+
var _AccountTreeController_instances, _AccountTreeController_reverse, _AccountTreeController_rules, _AccountTreeController_wallets, _AccountTreeController_handleAccountAdded, _AccountTreeController_handleAccountRemoved, _AccountTreeController_insert, _AccountTreeController_listAccounts;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.AccountTreeController = exports.
|
|
15
|
+
exports.AccountTreeController = exports.getDefaultAccountTreeControllerState = void 0;
|
|
16
16
|
const base_controller_1 = require("@metamask/base-controller");
|
|
17
|
-
const
|
|
18
|
-
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
19
|
-
const names_1 = require("./names.cjs");
|
|
17
|
+
const rules_1 = require("./rules/index.cjs");
|
|
20
18
|
const controllerName = 'AccountTreeController';
|
|
21
|
-
var AccountWalletCategory;
|
|
22
|
-
(function (AccountWalletCategory) {
|
|
23
|
-
AccountWalletCategory["Entropy"] = "entropy";
|
|
24
|
-
AccountWalletCategory["Keyring"] = "keyring";
|
|
25
|
-
AccountWalletCategory["Snap"] = "snap";
|
|
26
|
-
})(AccountWalletCategory || (exports.AccountWalletCategory = AccountWalletCategory = {}));
|
|
27
19
|
const accountTreeControllerMetadata = {
|
|
28
20
|
accountTree: {
|
|
29
21
|
persist: false,
|
|
@@ -43,41 +35,6 @@ function getDefaultAccountTreeControllerState() {
|
|
|
43
35
|
};
|
|
44
36
|
}
|
|
45
37
|
exports.getDefaultAccountTreeControllerState = getDefaultAccountTreeControllerState;
|
|
46
|
-
// TODO: For now we use this for the 2nd-level of the tree until we implements proper multichain accounts.
|
|
47
|
-
exports.DEFAULT_ACCOUNT_GROUP_UNIQUE_ID = 'default'; // This might need to be re-evaluated based on new structure
|
|
48
|
-
exports.DEFAULT_ACCOUNT_GROUP_NAME = 'Default';
|
|
49
|
-
/**
|
|
50
|
-
* Convert a unique ID to a wallet ID for a given category.
|
|
51
|
-
*
|
|
52
|
-
* @param category - A wallet category.
|
|
53
|
-
* @param id - A unique ID.
|
|
54
|
-
* @returns A wallet ID.
|
|
55
|
-
*/
|
|
56
|
-
function toAccountWalletId(category, id) {
|
|
57
|
-
return `${category}:${id}`;
|
|
58
|
-
}
|
|
59
|
-
exports.toAccountWalletId = toAccountWalletId;
|
|
60
|
-
/**
|
|
61
|
-
* Convert a wallet ID and a unique ID, to a group ID.
|
|
62
|
-
*
|
|
63
|
-
* @param walletId - A wallet ID.
|
|
64
|
-
* @param id - A unique ID.
|
|
65
|
-
* @returns A group ID.
|
|
66
|
-
*/
|
|
67
|
-
function toAccountGroupId(walletId, id) {
|
|
68
|
-
return `${walletId}:${id}`;
|
|
69
|
-
}
|
|
70
|
-
exports.toAccountGroupId = toAccountGroupId;
|
|
71
|
-
/**
|
|
72
|
-
* Convert a wallet ID to the default group ID.
|
|
73
|
-
*
|
|
74
|
-
* @param walletId - A wallet ID.
|
|
75
|
-
* @returns The default group ID.
|
|
76
|
-
*/
|
|
77
|
-
function toDefaultAccountGroupId(walletId) {
|
|
78
|
-
return toAccountGroupId(walletId, exports.DEFAULT_ACCOUNT_GROUP_UNIQUE_ID);
|
|
79
|
-
}
|
|
80
|
-
exports.toDefaultAccountGroupId = toDefaultAccountGroupId;
|
|
81
38
|
class AccountTreeController extends base_controller_1.BaseController {
|
|
82
39
|
/**
|
|
83
40
|
* Constructor for AccountTreeController.
|
|
@@ -99,16 +56,18 @@ class AccountTreeController extends base_controller_1.BaseController {
|
|
|
99
56
|
_AccountTreeController_instances.add(this);
|
|
100
57
|
_AccountTreeController_reverse.set(this, void 0);
|
|
101
58
|
_AccountTreeController_rules.set(this, void 0);
|
|
59
|
+
_AccountTreeController_wallets.set(this, void 0);
|
|
60
|
+
__classPrivateFieldSet(this, _AccountTreeController_wallets, new Map(), "f");
|
|
102
61
|
// Reverse map to allow fast node access from an account ID.
|
|
103
62
|
__classPrivateFieldSet(this, _AccountTreeController_reverse, new Map(), "f");
|
|
104
63
|
// Rules to apply to construct the wallets tree.
|
|
105
64
|
__classPrivateFieldSet(this, _AccountTreeController_rules, [
|
|
106
65
|
// 1. We group by entropy-source
|
|
107
|
-
|
|
66
|
+
new rules_1.EntropySourceWalletRule(this.messagingSystem),
|
|
108
67
|
// 2. We group by Snap ID
|
|
109
|
-
|
|
68
|
+
new rules_1.SnapWalletRule(this.messagingSystem),
|
|
110
69
|
// 3. We group by wallet type (this rule cannot fail and will group all non-matching accounts)
|
|
111
|
-
|
|
70
|
+
new rules_1.KeyringWalletRule(this.messagingSystem),
|
|
112
71
|
], "f");
|
|
113
72
|
this.messagingSystem.subscribe('AccountsController:accountAdded', (account) => {
|
|
114
73
|
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_handleAccountAdded).call(this, account);
|
|
@@ -127,9 +86,15 @@ class AccountTreeController extends base_controller_1.BaseController {
|
|
|
127
86
|
state.accountTree.wallets = wallets;
|
|
128
87
|
});
|
|
129
88
|
}
|
|
89
|
+
getWallet(id) {
|
|
90
|
+
return __classPrivateFieldGet(this, _AccountTreeController_wallets, "f").get(id);
|
|
91
|
+
}
|
|
92
|
+
getWallets() {
|
|
93
|
+
return Array.from(__classPrivateFieldGet(this, _AccountTreeController_wallets, "f").values());
|
|
94
|
+
}
|
|
130
95
|
}
|
|
131
96
|
exports.AccountTreeController = AccountTreeController;
|
|
132
|
-
_AccountTreeController_reverse = new WeakMap(), _AccountTreeController_rules = new WeakMap(), _AccountTreeController_instances = new WeakSet(), _AccountTreeController_handleAccountAdded = function _AccountTreeController_handleAccountAdded(account) {
|
|
97
|
+
_AccountTreeController_reverse = new WeakMap(), _AccountTreeController_rules = new WeakMap(), _AccountTreeController_wallets = new WeakMap(), _AccountTreeController_instances = new WeakSet(), _AccountTreeController_handleAccountAdded = function _AccountTreeController_handleAccountAdded(account) {
|
|
133
98
|
this.update((state) => {
|
|
134
99
|
__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_insert).call(this, state.accountTree.wallets, account);
|
|
135
100
|
});
|
|
@@ -145,109 +110,37 @@ _AccountTreeController_reverse = new WeakMap(), _AccountTreeController_rules = n
|
|
|
145
110
|
}
|
|
146
111
|
});
|
|
147
112
|
}
|
|
148
|
-
}, _AccountTreeController_hasKeyringType = function _AccountTreeController_hasKeyringType(account, type) {
|
|
149
|
-
return account.metadata.keyring.type === type;
|
|
150
|
-
}, _AccountTreeController_matchGroupByEntropySource = function _AccountTreeController_matchGroupByEntropySource(account) {
|
|
151
|
-
let entropySource;
|
|
152
|
-
if (__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_hasKeyringType).call(this, account, keyring_controller_1.KeyringTypes.hd)) {
|
|
153
|
-
// TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?
|
|
154
|
-
if (!account.options.entropySource) {
|
|
155
|
-
console.warn("! Found an HD account with no entropy source: account won't be associated to its wallet");
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
entropySource = account.options.entropySource;
|
|
159
|
-
}
|
|
160
|
-
// TODO: For now, we're not checking if the Snap is a preinstalled one, and we probably should...
|
|
161
|
-
if (__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_hasKeyringType).call(this, account, keyring_controller_1.KeyringTypes.snap) &&
|
|
162
|
-
account.metadata.snap?.enabled) {
|
|
163
|
-
// Not all Snaps have an entropy-source and options are not typed yet, so we have to check manually here.
|
|
164
|
-
if (account.options.entropySource) {
|
|
165
|
-
// We blindly trust the `entropySource` for now, but it could be wrong since it comes from a Snap.
|
|
166
|
-
entropySource = account.options.entropySource;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (!entropySource) {
|
|
170
|
-
return undefined;
|
|
171
|
-
}
|
|
172
|
-
// We check if we can get the name for that entropy source, if not this means this entropy does not match
|
|
173
|
-
// any HD keyrings, thus, is invalid (this account will be grouped by another rule).
|
|
174
|
-
const entropySourceName = __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getEntropySourceName).call(this, entropySource);
|
|
175
|
-
if (!entropySourceName) {
|
|
176
|
-
console.warn('! Tried to name a wallet using an unknown entropy, this should not be possible.');
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
179
|
-
return {
|
|
180
|
-
category: AccountWalletCategory.Entropy,
|
|
181
|
-
id: toAccountWalletId(AccountWalletCategory.Entropy, entropySource),
|
|
182
|
-
name: entropySourceName,
|
|
183
|
-
};
|
|
184
|
-
}, _AccountTreeController_matchGroupBySnapId = function _AccountTreeController_matchGroupBySnapId(account) {
|
|
185
|
-
if (__classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_hasKeyringType).call(this, account, keyring_controller_1.KeyringTypes.snap) &&
|
|
186
|
-
account.metadata.snap &&
|
|
187
|
-
account.metadata.snap.enabled) {
|
|
188
|
-
const { id } = account.metadata.snap;
|
|
189
|
-
return {
|
|
190
|
-
category: AccountWalletCategory.Snap,
|
|
191
|
-
id: toAccountWalletId(AccountWalletCategory.Snap, id),
|
|
192
|
-
name: __classPrivateFieldGet(this, _AccountTreeController_instances, "m", _AccountTreeController_getSnapName).call(this, id),
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
return undefined;
|
|
196
|
-
}, _AccountTreeController_matchGroupByKeyringType = function _AccountTreeController_matchGroupByKeyringType(account) {
|
|
197
|
-
const { type } = account.metadata.keyring;
|
|
198
|
-
return {
|
|
199
|
-
category: AccountWalletCategory.Keyring,
|
|
200
|
-
id: toAccountWalletId(AccountWalletCategory.Keyring, type),
|
|
201
|
-
name: (0, names_1.getAccountWalletNameFromKeyringType)(type),
|
|
202
|
-
};
|
|
203
|
-
}, _AccountTreeController_getSnapName = function _AccountTreeController_getSnapName(snapId) {
|
|
204
|
-
const snap = this.messagingSystem.call('SnapController:get', snapId);
|
|
205
|
-
const snapName = snap
|
|
206
|
-
? // TODO: Handle localization here, but that's a "client thing", so we don't have a `core` controller
|
|
207
|
-
// to refer to.
|
|
208
|
-
snap.manifest.proposedName
|
|
209
|
-
: (0, snaps_utils_1.stripSnapPrefix)(snapId);
|
|
210
|
-
return snapName;
|
|
211
|
-
}, _AccountTreeController_getEntropySourceName = function _AccountTreeController_getEntropySourceName(entropySource) {
|
|
212
|
-
const { keyrings } = this.messagingSystem.call('KeyringController:getState');
|
|
213
|
-
const index = keyrings
|
|
214
|
-
.filter((keyring) => keyring.type === keyring_controller_1.KeyringTypes.hd)
|
|
215
|
-
.findIndex((keyring) => keyring.metadata.id === entropySource);
|
|
216
|
-
if (index === -1) {
|
|
217
|
-
return undefined;
|
|
218
|
-
}
|
|
219
|
-
return `Wallet ${index + 1}`; // Use human indexing.
|
|
220
113
|
}, _AccountTreeController_insert = function _AccountTreeController_insert(wallets, account) {
|
|
221
114
|
for (const rule of __classPrivateFieldGet(this, _AccountTreeController_rules, "f")) {
|
|
222
|
-
const match = rule(account);
|
|
115
|
+
const match = rule.match(account);
|
|
223
116
|
if (!match) {
|
|
224
117
|
// No match for that rule, we go to the next one.
|
|
225
118
|
continue;
|
|
226
119
|
}
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (!wallets[
|
|
232
|
-
wallets[
|
|
233
|
-
id:
|
|
120
|
+
const { wallet, group } = match;
|
|
121
|
+
// Update in-memory wallet/group instances.
|
|
122
|
+
__classPrivateFieldGet(this, _AccountTreeController_wallets, "f").set(wallet.id, wallet);
|
|
123
|
+
// Update controller's state.
|
|
124
|
+
if (!wallets[wallet.id]) {
|
|
125
|
+
wallets[wallet.id] = {
|
|
126
|
+
id: wallet.id,
|
|
234
127
|
groups: {
|
|
235
|
-
[
|
|
236
|
-
id:
|
|
128
|
+
[group.id]: {
|
|
129
|
+
id: group.id,
|
|
237
130
|
accounts: [],
|
|
238
|
-
metadata: { name:
|
|
131
|
+
metadata: { name: group.getDefaultName() },
|
|
239
132
|
},
|
|
240
133
|
},
|
|
241
134
|
metadata: {
|
|
242
|
-
name:
|
|
135
|
+
name: wallet.getDefaultName(),
|
|
243
136
|
},
|
|
244
137
|
};
|
|
245
138
|
}
|
|
246
|
-
wallets[
|
|
139
|
+
wallets[wallet.id].groups[group.id].accounts.push(account.id);
|
|
247
140
|
// Update the reverse mapping for this account.
|
|
248
141
|
__classPrivateFieldGet(this, _AccountTreeController_reverse, "f").set(account.id, {
|
|
249
|
-
walletId,
|
|
250
|
-
groupId,
|
|
142
|
+
walletId: wallet.id,
|
|
143
|
+
groupId: group.id,
|
|
251
144
|
});
|
|
252
145
|
return;
|
|
253
146
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTreeController.cjs","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,+DAKmC;AAEnC,qEAA4D;AAI5D,uDAAwD;AAExD,uCAA8D;AAE9D,MAAM,cAAc,GAAG,uBAAuB,CAAC;AAE/C,IAAY,qBAIX;AAJD,WAAY,qBAAqB;IAC/B,4CAAmB,CAAA;IACnB,4CAAmB,CAAA;IACnB,sCAAa,CAAA;AACf,CAAC,EAJW,qBAAqB,qCAArB,qBAAqB,QAIhC;AAqFD,MAAM,6BAA6B,GACjC;IACE,WAAW,EAAE;QACX,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEJ;;;;GAIG;AACH,SAAgB,oCAAoC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E;YACtB,OAAO,CAAC,QAAQ,CAAC,GAAG;gBAClB,EAAE,EAAE,QAAQ;gBACZ,MAAM,EAAE;oBACN,CAAC,OAAO,CAAC,EAAE;wBACT,EAAE,EAAE,OAAO;wBACX,QAAQ,EAAE,EAAE;wBACZ,QAAQ,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;qBAC9B;iBACF;gBACD,QAAQ,EAAE;oBACR,IAAI,EAAE,UAAU;iBACjB;aACF,CAAC;SACH;QACD,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAE5D,+CAA+C;QAC/C,uBAAA,IAAI,sCAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE;YAC5B,QAAQ;YACR,OAAO;SACR,CAAC,CAAC;QAEH,OAAO;KACR;AACH,CAAC;IAGC,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAC9B,2CAA2C,CAC5C,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountId,\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerListMultichainAccountsAction,\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 { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { GetSnap as SnapControllerGetSnap } from '@metamask/snaps-controllers';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { stripSnapPrefix } from '@metamask/snaps-utils';\n\nimport { getAccountWalletNameFromKeyringType } from './names';\n\nconst controllerName = 'AccountTreeController';\n\nexport enum AccountWalletCategory {\n Entropy = 'entropy',\n Keyring = 'keyring',\n Snap = 'snap',\n}\n\ntype AccountTreeRuleMatch = {\n category: AccountWalletCategory;\n id: AccountWalletId;\n name: string;\n};\n\ntype AccountTreeRuleFunction = (\n account: InternalAccount,\n) => AccountTreeRuleMatch | undefined;\n\ntype AccountReverseMapping = {\n walletId: AccountWalletId;\n groupId: AccountGroupId;\n};\n\nexport type AccountWalletId = `${AccountWalletCategory}:${string}`;\nexport type AccountGroupId = `${AccountWalletId}:${string}`;\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 AccountGroup = {\n id: AccountGroupId;\n // Blockchain Accounts:\n accounts: AccountId[];\n metadata: AccountGroupMetadata;\n};\n\nexport type AccountWallet = {\n id: AccountWalletId;\n // Account groups OR Multichain accounts (once available).\n groups: {\n [groupId: AccountGroupId]: AccountGroup;\n };\n metadata: AccountWalletMetadata;\n};\n\nexport type AccountTreeControllerState = {\n accountTree: {\n wallets: {\n // Wallets:\n [walletId: AccountWalletId]: AccountWallet;\n };\n };\n};\n\nexport type AccountTreeControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AccountTreeControllerState\n>;\n\nexport type AllowedActions =\n | AccountsControllerListMultichainAccountsAction\n | KeyringControllerGetStateAction\n | SnapControllerGetSnap;\n\nexport type AccountTreeControllerActions = never;\n\nexport type AccountTreeControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AccountTreeControllerState\n>;\n\nexport type AllowedEvents =\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent;\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 };\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// TODO: For now we use this for the 2nd-level of the tree until we implements proper multichain accounts.\nexport const DEFAULT_ACCOUNT_GROUP_UNIQUE_ID: string = 'default'; // This might need to be re-evaluated based on new structure\nexport const DEFAULT_ACCOUNT_GROUP_NAME: string = 'Default';\n\n/**\n * Convert a unique ID to a wallet ID for a given category.\n *\n * @param category - A wallet category.\n * @param id - A unique ID.\n * @returns A wallet ID.\n */\nexport function toAccountWalletId(\n category: AccountWalletCategory,\n id: string,\n): AccountWalletId {\n return `${category}:${id}`;\n}\n\n/**\n * Convert a wallet ID and a unique ID, to a group ID.\n *\n * @param walletId - A wallet ID.\n * @param id - A unique ID.\n * @returns A group ID.\n */\nexport function toAccountGroupId(\n walletId: AccountWalletId,\n id: string,\n): AccountGroupId {\n return `${walletId}:${id}`;\n}\n\n/**\n * Convert a wallet ID to the default group ID.\n *\n * @param walletId - A wallet ID.\n * @returns The default group ID.\n */\nexport function toDefaultAccountGroupId(\n walletId: AccountWalletId,\n): AccountGroupId {\n return toAccountGroupId(walletId, DEFAULT_ACCOUNT_GROUP_UNIQUE_ID);\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: AccountTreeRuleFunction[];\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.#reverse = new Map();\n\n // Rules to apply to construct the wallets tree.\n this.#rules = [\n // 1. We group by entropy-source\n (account: InternalAccount) => this.#matchGroupByEntropySource(account),\n // 2. We group by Snap ID\n (account: InternalAccount) => this.#matchGroupBySnapId(account),\n // 3. We group by wallet type (this rule cannot fail and will group all non-matching accounts)\n (account: InternalAccount) => this.#matchGroupByKeyringType(account),\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 = {};\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 }\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 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 }\n\n #hasKeyringType(account: InternalAccount, type: KeyringTypes): boolean {\n return account.metadata.keyring.type === (type as string);\n }\n\n #matchGroupByEntropySource(\n account: InternalAccount,\n ): AccountTreeRuleMatch | undefined {\n let entropySource: string | undefined;\n\n if (this.#hasKeyringType(account, KeyringTypes.hd)) {\n // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?\n if (!account.options.entropySource) {\n console.warn(\n \"! Found an HD account with no entropy source: account won't be associated to its wallet\",\n );\n return undefined;\n }\n\n entropySource = account.options.entropySource as string;\n }\n\n // TODO: For now, we're not checking if the Snap is a preinstalled one, and we probably should...\n if (\n this.#hasKeyringType(account, KeyringTypes.snap) &&\n account.metadata.snap?.enabled\n ) {\n // Not all Snaps have an entropy-source and options are not typed yet, so we have to check manually here.\n if (account.options.entropySource) {\n // We blindly trust the `entropySource` for now, but it could be wrong since it comes from a Snap.\n entropySource = account.options.entropySource as string;\n }\n }\n\n if (!entropySource) {\n return undefined;\n }\n\n // We check if we can get the name for that entropy source, if not this means this entropy does not match\n // any HD keyrings, thus, is invalid (this account will be grouped by another rule).\n const entropySourceName = this.#getEntropySourceName(entropySource);\n if (!entropySourceName) {\n console.warn(\n '! Tried to name a wallet using an unknown entropy, this should not be possible.',\n );\n return undefined;\n }\n\n return {\n category: AccountWalletCategory.Entropy,\n id: toAccountWalletId(AccountWalletCategory.Entropy, entropySource),\n name: entropySourceName,\n };\n }\n\n #matchGroupBySnapId(\n account: InternalAccount,\n ): AccountTreeRuleMatch | undefined {\n if (\n this.#hasKeyringType(account, KeyringTypes.snap) &&\n account.metadata.snap &&\n account.metadata.snap.enabled\n ) {\n const { id } = account.metadata.snap;\n\n return {\n category: AccountWalletCategory.Snap,\n id: toAccountWalletId(AccountWalletCategory.Snap, id),\n name: this.#getSnapName(id as SnapId),\n };\n }\n\n return undefined;\n }\n\n #matchGroupByKeyringType(\n account: InternalAccount,\n ): AccountTreeRuleMatch | undefined {\n const { type } = account.metadata.keyring;\n\n return {\n category: AccountWalletCategory.Keyring,\n id: toAccountWalletId(AccountWalletCategory.Keyring, type),\n name: getAccountWalletNameFromKeyringType(type as KeyringTypes),\n };\n }\n\n #getSnapName(snapId: SnapId): string {\n const snap = this.messagingSystem.call('SnapController:get', snapId);\n const snapName = snap\n ? // TODO: Handle localization here, but that's a \"client thing\", so we don't have a `core` controller\n // to refer to.\n snap.manifest.proposedName\n : stripSnapPrefix(snapId);\n\n return snapName;\n }\n\n #getEntropySourceName(entropySource: string): string | undefined {\n const { keyrings } = this.messagingSystem.call(\n 'KeyringController:getState',\n );\n\n const index = keyrings\n .filter((keyring) => keyring.type === (KeyringTypes.hd as string))\n .findIndex((keyring) => keyring.metadata.id === entropySource);\n\n if (index === -1) {\n return undefined;\n }\n\n return `Wallet ${index + 1}`; // Use human indexing.\n }\n\n #insert(\n wallets: { [walletId: AccountWalletId]: AccountWallet },\n account: InternalAccount,\n ) {\n for (const rule of this.#rules) {\n const match = rule(account);\n\n if (!match) {\n // No match for that rule, we go to the next one.\n continue;\n }\n\n const walletId = match.id;\n const walletName = match.name;\n const groupId = toDefaultAccountGroupId(walletId); // Use a single-group for now until multichain accounts is supported.\n const groupName = DEFAULT_ACCOUNT_GROUP_NAME;\n\n if (!wallets[walletId]) {\n wallets[walletId] = {\n id: walletId,\n groups: {\n [groupId]: {\n id: groupId,\n accounts: [],\n metadata: { name: groupName },\n },\n },\n metadata: {\n name: walletName,\n },\n };\n }\n wallets[walletId].groups[groupId].accounts.push(account.id);\n\n // Update the reverse mapping for this account.\n this.#reverse.set(account.id, {\n walletId,\n groupId,\n });\n\n return;\n }\n }\n\n #listAccounts(): InternalAccount[] {\n return this.messagingSystem.call(\n 'AccountsController:listMultichainAccounts',\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AccountTreeController.cjs","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AASA,+DAKmC;AAOnC,6CAIiB;AAEjB,MAAM,cAAc,GAAG,uBAAuB,CAAC;AAyE/C,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;AAED,MAAa,qBAAsB,SAAQ,gCAI1C;IAOC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,C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AC,OAAO,CAAC,CAAC;QAElC,IAAI,CAAC,KAAK,EAAE;YACV,iDAAiD;YACjD,SAAS;SACV;QAED,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;QAEhC,2CAA2C;QAC3C,uBAAA,IAAI,sCAAS,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;QAErC,6BAA6B;QAC7B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;YACvB,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG;gBACnB,EAAE,EAAE,MAAM,CAAC,EAAE;gBACb,MAAM,EAAE;oBACN,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE;wBACV,EAAE,EAAE,KAAK,CAAC,EAAE;wBACZ,QAAQ,EAAE,EAAE;wBACZ,QAAQ,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,cAAc,EAAE,EAAE;qBAC3C;iBACF;gBACD,QAAQ,EAAE;oBACR,IAAI,EAAE,MAAM,CAAC,cAAc,EAAE;iBAC9B;aACF,CAAC;SACH;QACD,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAE9D,+CAA+C;QAC/C,uBAAA,IAAI,sCAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE;YAC5B,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 type {\n AccountId,\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetAccountAction,\n AccountsControllerListMultichainAccountsAction,\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};\n\nexport type AccountTreeControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AccountTreeControllerState\n>;\n\nexport type AllowedActions =\n | AccountsControllerGetAccountAction\n | AccountsControllerListMultichainAccountsAction\n | KeyringControllerGetStateAction\n | SnapControllerGetSnap;\n\nexport type AccountTreeControllerActions = never;\n\nexport type AccountTreeControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AccountTreeControllerState\n>;\n\nexport type AllowedEvents =\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent;\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 };\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\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\n init() {\n const 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 this.update((state) => {\n state.accountTree.wallets = wallets;\n });\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 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 }\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"]}
|
|
@@ -1,41 +1,36 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AccountGroupId, AccountWalletId } from "@metamask/account-api";
|
|
2
|
+
import type { AccountId, AccountsControllerAccountAddedEvent, AccountsControllerAccountRemovedEvent, AccountsControllerGetAccountAction, AccountsControllerListMultichainAccountsAction } from "@metamask/accounts-controller";
|
|
2
3
|
import { type ControllerGetStateAction, type ControllerStateChangeEvent, type RestrictedMessenger, BaseController } from "@metamask/base-controller";
|
|
3
4
|
import type { KeyringControllerGetStateAction } from "@metamask/keyring-controller";
|
|
4
5
|
import type { GetSnap as SnapControllerGetSnap } from "@metamask/snaps-controllers";
|
|
6
|
+
import type { AccountTreeWallet } from "./AccountTreeWallet.cjs";
|
|
5
7
|
declare const controllerName = "AccountTreeController";
|
|
6
|
-
export declare enum AccountWalletCategory {
|
|
7
|
-
Entropy = "entropy",
|
|
8
|
-
Keyring = "keyring",
|
|
9
|
-
Snap = "snap"
|
|
10
|
-
}
|
|
11
|
-
export type AccountWalletId = `${AccountWalletCategory}:${string}`;
|
|
12
|
-
export type AccountGroupId = `${AccountWalletId}:${string}`;
|
|
13
8
|
type Metadata = {
|
|
14
9
|
name: string;
|
|
15
10
|
};
|
|
16
11
|
export type AccountWalletMetadata = Metadata;
|
|
17
12
|
export type AccountGroupMetadata = Metadata;
|
|
18
|
-
export type
|
|
13
|
+
export type AccountGroupObject = {
|
|
19
14
|
id: AccountGroupId;
|
|
20
15
|
accounts: AccountId[];
|
|
21
16
|
metadata: AccountGroupMetadata;
|
|
22
17
|
};
|
|
23
|
-
export type
|
|
18
|
+
export type AccountWalletObject = {
|
|
24
19
|
id: AccountWalletId;
|
|
25
20
|
groups: {
|
|
26
|
-
[groupId: AccountGroupId]:
|
|
21
|
+
[groupId: AccountGroupId]: AccountGroupObject;
|
|
27
22
|
};
|
|
28
23
|
metadata: AccountWalletMetadata;
|
|
29
24
|
};
|
|
30
25
|
export type AccountTreeControllerState = {
|
|
31
26
|
accountTree: {
|
|
32
27
|
wallets: {
|
|
33
|
-
[walletId: AccountWalletId]:
|
|
28
|
+
[walletId: AccountWalletId]: AccountWalletObject;
|
|
34
29
|
};
|
|
35
30
|
};
|
|
36
31
|
};
|
|
37
32
|
export type AccountTreeControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AccountTreeControllerState>;
|
|
38
|
-
export type AllowedActions = AccountsControllerListMultichainAccountsAction | KeyringControllerGetStateAction | SnapControllerGetSnap;
|
|
33
|
+
export type AllowedActions = AccountsControllerGetAccountAction | AccountsControllerListMultichainAccountsAction | KeyringControllerGetStateAction | SnapControllerGetSnap;
|
|
39
34
|
export type AccountTreeControllerActions = never;
|
|
40
35
|
export type AccountTreeControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AccountTreeControllerState>;
|
|
41
36
|
export type AllowedEvents = AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent;
|
|
@@ -47,31 +42,6 @@ export type AccountTreeControllerMessenger = RestrictedMessenger<typeof controll
|
|
|
47
42
|
* @returns The default state of the `AccountTreeController`.
|
|
48
43
|
*/
|
|
49
44
|
export declare function getDefaultAccountTreeControllerState(): AccountTreeControllerState;
|
|
50
|
-
export declare const DEFAULT_ACCOUNT_GROUP_UNIQUE_ID: string;
|
|
51
|
-
export declare const DEFAULT_ACCOUNT_GROUP_NAME: string;
|
|
52
|
-
/**
|
|
53
|
-
* Convert a unique ID to a wallet ID for a given category.
|
|
54
|
-
*
|
|
55
|
-
* @param category - A wallet category.
|
|
56
|
-
* @param id - A unique ID.
|
|
57
|
-
* @returns A wallet ID.
|
|
58
|
-
*/
|
|
59
|
-
export declare function toAccountWalletId(category: AccountWalletCategory, id: string): AccountWalletId;
|
|
60
|
-
/**
|
|
61
|
-
* Convert a wallet ID and a unique ID, to a group ID.
|
|
62
|
-
*
|
|
63
|
-
* @param walletId - A wallet ID.
|
|
64
|
-
* @param id - A unique ID.
|
|
65
|
-
* @returns A group ID.
|
|
66
|
-
*/
|
|
67
|
-
export declare function toAccountGroupId(walletId: AccountWalletId, id: string): AccountGroupId;
|
|
68
|
-
/**
|
|
69
|
-
* Convert a wallet ID to the default group ID.
|
|
70
|
-
*
|
|
71
|
-
* @param walletId - A wallet ID.
|
|
72
|
-
* @returns The default group ID.
|
|
73
|
-
*/
|
|
74
|
-
export declare function toDefaultAccountGroupId(walletId: AccountWalletId): AccountGroupId;
|
|
75
45
|
export declare class AccountTreeController extends BaseController<typeof controllerName, AccountTreeControllerState, AccountTreeControllerMessenger> {
|
|
76
46
|
#private;
|
|
77
47
|
/**
|
|
@@ -86,6 +56,8 @@ export declare class AccountTreeController extends BaseController<typeof control
|
|
|
86
56
|
state?: Partial<AccountTreeControllerState>;
|
|
87
57
|
});
|
|
88
58
|
init(): void;
|
|
59
|
+
getWallet(id: AccountWalletId): AccountTreeWallet | undefined;
|
|
60
|
+
getWallets(): AccountTreeWallet[];
|
|
89
61
|
}
|
|
90
62
|
export {};
|
|
91
63
|
//# sourceMappingURL=AccountTreeController.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTreeController.d.cts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,mCAAmC,EACnC,qCAAqC,EACrC,8CAA8C,EAC/C,sCAAsC;AAEvC,OAAO,EACL,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,cAAc,EACf,kCAAkC;AACnC,OAAO,KAAK,EAAE,+BAA+B,EAAE,qCAAqC;
|
|
1
|
+
{"version":3,"file":"AccountTreeController.d.cts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAC7E,OAAO,KAAK,EACV,SAAS,EACT,mCAAmC,EACnC,qCAAqC,EACrC,kCAAkC,EAClC,8CAA8C,EAC/C,sCAAsC;AAEvC,OAAO,EACL,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,K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mC,GACnC,qCAAqC,CAAC;AAE1C,MAAM,MAAM,2BAA2B,GAAG,qCAAqC,CAAC;AAEhF,MAAM,MAAM,8BAA8B,GAAG,mBAAmB,CAC9D,OAAO,cAAc,EACrB,4BAA4B,GAAG,cAAc,EAC7C,2BAA2B,GAAG,aAAa,EAC3C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAUF;;;;GAIG;AACH,wBAAgB,oCAAoC,IAAI,0BAA0B,CAMjF;AAED,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;IAwCD,IAAI;IAaJ,SAAS,CAAC,EAAE,EAAE,eAAe,GAAG,iBAAiB,GAAG,SAAS;IAI7D,UAAU,IAAI,iBAAiB,EAAE;CA6ElC"}
|
|
@@ -1,41 +1,36 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AccountGroupId, AccountWalletId } from "@metamask/account-api";
|
|
2
|
+
import type { AccountId, AccountsControllerAccountAddedEvent, AccountsControllerAccountRemovedEvent, AccountsControllerGetAccountAction, AccountsControllerListMultichainAccountsAction } from "@metamask/accounts-controller";
|
|
2
3
|
import { type ControllerGetStateAction, type ControllerStateChangeEvent, type RestrictedMessenger, BaseController } from "@metamask/base-controller";
|
|
3
4
|
import type { KeyringControllerGetStateAction } from "@metamask/keyring-controller";
|
|
4
5
|
import type { GetSnap as SnapControllerGetSnap } from "@metamask/snaps-controllers";
|
|
6
|
+
import type { AccountTreeWallet } from "./AccountTreeWallet.mjs";
|
|
5
7
|
declare const controllerName = "AccountTreeController";
|
|
6
|
-
export declare enum AccountWalletCategory {
|
|
7
|
-
Entropy = "entropy",
|
|
8
|
-
Keyring = "keyring",
|
|
9
|
-
Snap = "snap"
|
|
10
|
-
}
|
|
11
|
-
export type AccountWalletId = `${AccountWalletCategory}:${string}`;
|
|
12
|
-
export type AccountGroupId = `${AccountWalletId}:${string}`;
|
|
13
8
|
type Metadata = {
|
|
14
9
|
name: string;
|
|
15
10
|
};
|
|
16
11
|
export type AccountWalletMetadata = Metadata;
|
|
17
12
|
export type AccountGroupMetadata = Metadata;
|
|
18
|
-
export type
|
|
13
|
+
export type AccountGroupObject = {
|
|
19
14
|
id: AccountGroupId;
|
|
20
15
|
accounts: AccountId[];
|
|
21
16
|
metadata: AccountGroupMetadata;
|
|
22
17
|
};
|
|
23
|
-
export type
|
|
18
|
+
export type AccountWalletObject = {
|
|
24
19
|
id: AccountWalletId;
|
|
25
20
|
groups: {
|
|
26
|
-
[groupId: AccountGroupId]:
|
|
21
|
+
[groupId: AccountGroupId]: AccountGroupObject;
|
|
27
22
|
};
|
|
28
23
|
metadata: AccountWalletMetadata;
|
|
29
24
|
};
|
|
30
25
|
export type AccountTreeControllerState = {
|
|
31
26
|
accountTree: {
|
|
32
27
|
wallets: {
|
|
33
|
-
[walletId: AccountWalletId]:
|
|
28
|
+
[walletId: AccountWalletId]: AccountWalletObject;
|
|
34
29
|
};
|
|
35
30
|
};
|
|
36
31
|
};
|
|
37
32
|
export type AccountTreeControllerGetStateAction = ControllerGetStateAction<typeof controllerName, AccountTreeControllerState>;
|
|
38
|
-
export type AllowedActions = AccountsControllerListMultichainAccountsAction | KeyringControllerGetStateAction | SnapControllerGetSnap;
|
|
33
|
+
export type AllowedActions = AccountsControllerGetAccountAction | AccountsControllerListMultichainAccountsAction | KeyringControllerGetStateAction | SnapControllerGetSnap;
|
|
39
34
|
export type AccountTreeControllerActions = never;
|
|
40
35
|
export type AccountTreeControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AccountTreeControllerState>;
|
|
41
36
|
export type AllowedEvents = AccountsControllerAccountAddedEvent | AccountsControllerAccountRemovedEvent;
|
|
@@ -47,31 +42,6 @@ export type AccountTreeControllerMessenger = RestrictedMessenger<typeof controll
|
|
|
47
42
|
* @returns The default state of the `AccountTreeController`.
|
|
48
43
|
*/
|
|
49
44
|
export declare function getDefaultAccountTreeControllerState(): AccountTreeControllerState;
|
|
50
|
-
export declare const DEFAULT_ACCOUNT_GROUP_UNIQUE_ID: string;
|
|
51
|
-
export declare const DEFAULT_ACCOUNT_GROUP_NAME: string;
|
|
52
|
-
/**
|
|
53
|
-
* Convert a unique ID to a wallet ID for a given category.
|
|
54
|
-
*
|
|
55
|
-
* @param category - A wallet category.
|
|
56
|
-
* @param id - A unique ID.
|
|
57
|
-
* @returns A wallet ID.
|
|
58
|
-
*/
|
|
59
|
-
export declare function toAccountWalletId(category: AccountWalletCategory, id: string): AccountWalletId;
|
|
60
|
-
/**
|
|
61
|
-
* Convert a wallet ID and a unique ID, to a group ID.
|
|
62
|
-
*
|
|
63
|
-
* @param walletId - A wallet ID.
|
|
64
|
-
* @param id - A unique ID.
|
|
65
|
-
* @returns A group ID.
|
|
66
|
-
*/
|
|
67
|
-
export declare function toAccountGroupId(walletId: AccountWalletId, id: string): AccountGroupId;
|
|
68
|
-
/**
|
|
69
|
-
* Convert a wallet ID to the default group ID.
|
|
70
|
-
*
|
|
71
|
-
* @param walletId - A wallet ID.
|
|
72
|
-
* @returns The default group ID.
|
|
73
|
-
*/
|
|
74
|
-
export declare function toDefaultAccountGroupId(walletId: AccountWalletId): AccountGroupId;
|
|
75
45
|
export declare class AccountTreeController extends BaseController<typeof controllerName, AccountTreeControllerState, AccountTreeControllerMessenger> {
|
|
76
46
|
#private;
|
|
77
47
|
/**
|
|
@@ -86,6 +56,8 @@ export declare class AccountTreeController extends BaseController<typeof control
|
|
|
86
56
|
state?: Partial<AccountTreeControllerState>;
|
|
87
57
|
});
|
|
88
58
|
init(): void;
|
|
59
|
+
getWallet(id: AccountWalletId): AccountTreeWallet | undefined;
|
|
60
|
+
getWallets(): AccountTreeWallet[];
|
|
89
61
|
}
|
|
90
62
|
export {};
|
|
91
63
|
//# sourceMappingURL=AccountTreeController.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountTreeController.d.mts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,mCAAmC,EACnC,qCAAqC,EACrC,8CAA8C,EAC/C,sCAAsC;AAEvC,OAAO,EACL,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,cAAc,EACf,kCAAkC;AACnC,OAAO,KAAK,EAAE,+BAA+B,EAAE,qCAAqC;
|
|
1
|
+
{"version":3,"file":"AccountTreeController.d.mts","sourceRoot":"","sources":["../src/AccountTreeController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAC7E,OAAO,KAAK,EACV,SAAS,EACT,mCAAmC,EACnC,qCAAqC,EACrC,kCAAkC,EAClC,8CAA8C,EAC/C,sCAAsC;AAEvC,OAAO,EACL,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAC/B,KAAK,mBAAmB,EACxB,cAAc,EACf,kCAAkC;AACnC,OAAO,KAAK,EAAE,+BAA+B,EAAE,qCAAqC;AAEpF,OAAO,KAAK,EAAE,OAAO,IAAI,qBAAqB,EAAE,oCAAoC;AAEpF,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAQ7D,QAAA,MAAM,cAAc,0BAA0B,CAAC;AAQ/C,KAAK,QAAQ,GAAG;IACd,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC;AAE7C,MAAM,MAAM,oBAAoB,GAAG,QAAQ,CAAC;AAE5C,MAAM,MAAM,kBAAkB,GAAG;IAC/B,EAAE,EAAE,cAAc,CAAC;IAEnB,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtB,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