Skip to content

Commit 299edd8

Browse files
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 changes
1 parent 19316e4 commit 299edd8

18 files changed

Lines changed: 847 additions & 712 deletions

packages/account-tree-controller/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add `group.type` tag ([#6214](https://github.com/MetaMask/core/pull/6214))
13+
- This `type` can be used as a tag to strongly-type (tagged-union) the `AccountGroupObject`.
14+
- Add `group.metadata` metadata object ([#6214](https://github.com/MetaMask/core/pull/6214))
15+
- Given the `group.type` you will now have access to specific metadata information (e.g. `groupIndex` for multichain account groups)
16+
1017
### Changed
1118

19+
- **BREAKING:** Bump peer dependency `@metamask/account-api` from `^0.3.0` to `^0.5.0` ([#6214](https://github.com/MetaMask/core/pull/6214))
20+
- **BREAKING:** Move `wallet.metadata.type` tag to `wallet` node ([#6214](https://github.com/MetaMask/core/pull/6214))
21+
- This `type` can be used as a tag to strongly-type (tagged-union) the `AccountWalletObject`.
1222
- Defaults to the EVM account from a group when using `setSelectedAccountGroup` ([#6208](https://github.com/MetaMask/core/pull/6208))
1323
- In case no EVM accounts are found in a group (which should not be possible), it will defaults to the first account of that group.
1424

packages/account-tree-controller/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"lodash": "^4.17.21"
5454
},
5555
"devDependencies": {
56-
"@metamask/account-api": "^0.3.0",
56+
"@metamask/account-api": "^0.5.0",
5757
"@metamask/accounts-controller": "^32.0.1",
5858
"@metamask/auto-changelog": "^3.4.4",
5959
"@metamask/keyring-api": "^19.0.0",
@@ -70,7 +70,7 @@
7070
"webextension-polyfill": "^0.12.0"
7171
},
7272
"peerDependencies": {
73-
"@metamask/account-api": "^0.3.0",
73+
"@metamask/account-api": "^0.5.0",
7474
"@metamask/accounts-controller": "^32.0.0",
7575
"@metamask/keyring-controller": "^22.0.0",
7676
"@metamask/providers": "^22.0.0",

0 commit comments

Comments
 (0)