Commit 299edd8
authored
refactor(account-tree-controller): add type field for each tree node + improve typing overall (#6214)
## Explanation
Add new `type` field for each wallets/groups.
This is used as a "tag", so proper node types can be inferred by the
compiler.
e.g:
```ts
// Using wallet as root node:
if (wallet.type === AccountWalletType.Entropy) {
console.log(wallet.metadata.entropy.id);
// This `wallet` is narrowed down to `AccountWalletEntropyObject`, it knows that
// `wallet.groups` are `AccountGroupMultichainAccountObject`, so we can use
// the `groupIndex` safely here too.
for (const group of Object.values(wallet.groups)) {
console.log(group.metadata.entropy.groupIndex);
}
}
if (wallet.type === AccountWalletType.Snap) {
console.log(wallet.metadata.snap.id);
}
if (wallet.type === AccountWalletType.Keyring) {
console.log(wallet.metadata.keyring.type);
}
// Using group as root node:
if (group.type === AccountGroupType.SingleAccount) {
// No real metadata for this, however, we enforced the `accounts` to be
// a `[AccountId]` tuple, so it is safe to do:
console.log(group.accounts[0]);
// And this should not build:
console.log(group.accounts[1]); // ERROR: Tuple type '[string]' of length '1' has no element at index '1'.
}
if (group.type === AccountGroupType.MultichainAccount) {
console.log(group.metadata.groupIndex);
// For multichain accounts group we have "at least 1" account, but we could have more.
console.log(group.accounts[0]);
console.log(group.accounts[1]); // Assuming we have 2 accounts here.
}
```
## References
N/A
## Changelog
N/A
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs),
highlighting breaking changes as necessary
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes1 parent 19316e4 commit 299edd8
18 files changed
Lines changed: 847 additions & 712 deletions
File tree
- packages
- account-tree-controller
- src
- rules
- multichain-account-service
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
10 | 17 | | |
11 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
12 | 22 | | |
13 | 23 | | |
14 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
0 commit comments