@metamask-previews/account-tree-controller 0.6.0-preview-e0bc4b4b → 0.6.0-preview-9b5151c

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.
Files changed (114) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/AccountTreeController.cjs +86 -25
  3. package/dist/AccountTreeController.cjs.map +1 -1
  4. package/dist/AccountTreeController.d.cts +5 -3
  5. package/dist/AccountTreeController.d.cts.map +1 -1
  6. package/dist/AccountTreeController.d.mts +5 -3
  7. package/dist/AccountTreeController.d.mts.map +1 -1
  8. package/dist/AccountTreeController.mjs +87 -26
  9. package/dist/AccountTreeController.mjs.map +1 -1
  10. package/dist/AccountTreeGroup.cjs +51 -35
  11. package/dist/AccountTreeGroup.cjs.map +1 -1
  12. package/dist/AccountTreeGroup.d.cts +15 -22
  13. package/dist/AccountTreeGroup.d.cts.map +1 -1
  14. package/dist/AccountTreeGroup.d.mts +15 -22
  15. package/dist/AccountTreeGroup.d.mts.map +1 -1
  16. package/dist/AccountTreeGroup.mjs +49 -33
  17. package/dist/AccountTreeGroup.mjs.map +1 -1
  18. package/dist/AccountTreeWallet.cjs +61 -27
  19. package/dist/AccountTreeWallet.cjs.map +1 -1
  20. package/dist/AccountTreeWallet.d.cts +24 -17
  21. package/dist/AccountTreeWallet.d.cts.map +1 -1
  22. package/dist/AccountTreeWallet.d.mts +24 -17
  23. package/dist/AccountTreeWallet.d.mts.map +1 -1
  24. package/dist/AccountTreeWallet.mjs +60 -26
  25. package/dist/AccountTreeWallet.mjs.map +1 -1
  26. package/dist/rules/entropy.cjs +83 -0
  27. package/dist/rules/entropy.cjs.map +1 -0
  28. package/dist/rules/entropy.d.cts +15 -0
  29. package/dist/rules/entropy.d.cts.map +1 -0
  30. package/dist/rules/entropy.d.mts +15 -0
  31. package/dist/rules/entropy.d.mts.map +1 -0
  32. package/dist/rules/entropy.mjs +79 -0
  33. package/dist/rules/entropy.mjs.map +1 -0
  34. package/dist/rules/index.cjs +4 -4
  35. package/dist/rules/index.cjs.map +1 -1
  36. package/dist/rules/index.d.cts +4 -4
  37. package/dist/rules/index.d.cts.map +1 -1
  38. package/dist/rules/index.d.mts +4 -4
  39. package/dist/rules/index.d.mts.map +1 -1
  40. package/dist/rules/index.mjs +4 -4
  41. package/dist/rules/index.mjs.map +1 -1
  42. package/dist/rules/keyring.cjs +80 -0
  43. package/dist/rules/keyring.cjs.map +1 -0
  44. package/dist/rules/keyring.d.cts +20 -0
  45. package/dist/rules/keyring.d.cts.map +1 -0
  46. package/dist/rules/keyring.d.mts +20 -0
  47. package/dist/rules/keyring.d.mts.map +1 -0
  48. package/dist/rules/keyring.mjs +75 -0
  49. package/dist/rules/keyring.mjs.map +1 -0
  50. package/dist/rules/rule.cjs +10 -0
  51. package/dist/rules/rule.cjs.map +1 -0
  52. package/dist/rules/rule.d.cts +42 -0
  53. package/dist/rules/rule.d.cts.map +1 -0
  54. package/dist/rules/rule.d.mts +42 -0
  55. package/dist/rules/rule.d.mts.map +1 -0
  56. package/dist/rules/rule.mjs +6 -0
  57. package/dist/rules/rule.mjs.map +1 -0
  58. package/dist/rules/snap.cjs +53 -0
  59. package/dist/rules/snap.cjs.map +1 -0
  60. package/dist/rules/snap.d.cts +23 -0
  61. package/dist/rules/snap.d.cts.map +1 -0
  62. package/dist/rules/snap.d.mts +23 -0
  63. package/dist/rules/snap.d.mts.map +1 -0
  64. package/dist/rules/snap.mjs +49 -0
  65. package/dist/rules/snap.mjs.map +1 -0
  66. package/dist/types.cjs +3 -0
  67. package/dist/types.cjs.map +1 -0
  68. package/dist/types.d.cts +15 -0
  69. package/dist/types.d.cts.map +1 -0
  70. package/dist/types.d.mts +15 -0
  71. package/dist/types.d.mts.map +1 -0
  72. package/dist/types.mjs +2 -0
  73. package/dist/types.mjs.map +1 -0
  74. package/package.json +3 -3
  75. package/dist/rules/EntropySourceWalletRule.cjs +0 -100
  76. package/dist/rules/EntropySourceWalletRule.cjs.map +0 -1
  77. package/dist/rules/EntropySourceWalletRule.d.cts +0 -19
  78. package/dist/rules/EntropySourceWalletRule.d.cts.map +0 -1
  79. package/dist/rules/EntropySourceWalletRule.d.mts +0 -19
  80. package/dist/rules/EntropySourceWalletRule.d.mts.map +0 -1
  81. package/dist/rules/EntropySourceWalletRule.mjs +0 -95
  82. package/dist/rules/EntropySourceWalletRule.mjs.map +0 -1
  83. package/dist/rules/KeyringWalletRule.cjs +0 -99
  84. package/dist/rules/KeyringWalletRule.cjs.map +0 -1
  85. package/dist/rules/KeyringWalletRule.d.cts +0 -18
  86. package/dist/rules/KeyringWalletRule.d.cts.map +0 -1
  87. package/dist/rules/KeyringWalletRule.d.mts +0 -18
  88. package/dist/rules/KeyringWalletRule.d.mts.map +0 -1
  89. package/dist/rules/KeyringWalletRule.mjs +0 -94
  90. package/dist/rules/KeyringWalletRule.mjs.map +0 -1
  91. package/dist/rules/SnapWalletRule.cjs +0 -70
  92. package/dist/rules/SnapWalletRule.cjs.map +0 -1
  93. package/dist/rules/SnapWalletRule.d.cts +0 -10
  94. package/dist/rules/SnapWalletRule.d.cts.map +0 -1
  95. package/dist/rules/SnapWalletRule.d.mts +0 -10
  96. package/dist/rules/SnapWalletRule.d.mts.map +0 -1
  97. package/dist/rules/SnapWalletRule.mjs +0 -66
  98. package/dist/rules/SnapWalletRule.mjs.map +0 -1
  99. package/dist/rules/WalletRule.cjs +0 -13
  100. package/dist/rules/WalletRule.cjs.map +0 -1
  101. package/dist/rules/WalletRule.d.cts +0 -37
  102. package/dist/rules/WalletRule.d.cts.map +0 -1
  103. package/dist/rules/WalletRule.d.mts +0 -37
  104. package/dist/rules/WalletRule.d.mts.map +0 -1
  105. package/dist/rules/WalletRule.mjs +0 -9
  106. package/dist/rules/WalletRule.mjs.map +0 -1
  107. package/dist/rules/utils.cjs +0 -15
  108. package/dist/rules/utils.cjs.map +0 -1
  109. package/dist/rules/utils.d.cts +0 -11
  110. package/dist/rules/utils.d.cts.map +0 -1
  111. package/dist/rules/utils.d.mts +0 -11
  112. package/dist/rules/utils.d.mts.map +0 -1
  113. package/dist/rules/utils.mjs +0 -11
  114. package/dist/rules/utils.mjs.map +0 -1
@@ -1,15 +1,25 @@
1
- import { type AccountGroupId, type AccountWallet, type AccountWalletCategory, type AccountWalletId } from "@metamask/account-api";
1
+ import type { AccountWalletCategory, AccountWalletId } from "@metamask/account-api";
2
+ import { type AccountGroupId, type AccountWallet } from "@metamask/account-api";
2
3
  import type { InternalAccount } from "@metamask/keyring-internal-api";
4
+ import type { AccountWalletObject } from "./AccountTreeController.mjs";
3
5
  import { type AccountTreeControllerMessenger } from "./AccountTreeController.mjs";
4
- import type { AccountTreeGroup } from "./AccountTreeGroup.mjs";
5
- import { MutableAccountTreeGroup } from "./AccountTreeGroup.mjs";
6
+ import { AccountTreeGroup } from "./AccountTreeGroup.mjs";
6
7
  /**
7
8
  * Account wallet coming from the {@link AccountTreeController}.
8
9
  */
9
- export type AccountTreeWallet = {
10
+ export declare class AccountTreeWallet implements AccountWallet<InternalAccount> {
11
+ #private;
12
+ constructor({ messenger, wallet, }: {
13
+ messenger: AccountTreeControllerMessenger;
14
+ wallet: AccountWalletObject;
15
+ });
16
+ get id(): AccountWalletId;
17
+ get category(): AccountWalletCategory;
18
+ get name(): string;
10
19
  /**
11
20
  * Gets account tree group for a given ID.
12
21
  *
22
+ * @param groupId - Group ID.
13
23
  * @returns Account tree group.
14
24
  */
15
25
  getAccountGroup(groupId: AccountGroupId): AccountTreeGroup | undefined;
@@ -20,19 +30,16 @@ export type AccountTreeWallet = {
20
30
  */
21
31
  getAccountGroups(): AccountTreeGroup[];
22
32
  /**
23
- * Gets the default name for that account wallet.
33
+ * Gets any underlying account.
34
+ *
35
+ * This can be useful if the accounts from this wallet have been grouped some
36
+ * common criteria, so you expect some commonly defined information on any of
37
+ * those accounts.
38
+ *
39
+ * @throws If the wallet has no account group (which should not be possible).
40
+ * @returns Any account that share the same information based on the grouping
41
+ * rule that has been used.
24
42
  */
25
- getDefaultName(): string;
26
- } & AccountWallet<InternalAccount>;
27
- export declare abstract class MutableAccountTreeWallet implements AccountTreeWallet {
28
- #private;
29
- readonly id: AccountWalletId;
30
- readonly category: AccountWalletCategory;
31
- readonly messenger: AccountTreeControllerMessenger;
32
- constructor(messenger: AccountTreeControllerMessenger, category: AccountWalletCategory, id: string);
33
- getAccountGroup(groupId: AccountGroupId): AccountTreeGroup | undefined;
34
- getAccountGroups(): AccountTreeGroup[];
35
- addAccount(account: InternalAccount): MutableAccountTreeGroup;
36
- abstract getDefaultName(): string;
43
+ getAnyAccount(): InternalAccount;
37
44
  }
38
45
  //# sourceMappingURL=AccountTreeWallet.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AccountTreeWallet.d.mts","sourceRoot":"","sources":["../src/AccountTreeWallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,qBAAqB,EAC1B,KAAK,eAAe,EACrB,8BAA8B;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,EAAE,KAAK,8BAA8B,EAAE,oCAAgC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,+BAA2B;AAC3D,OAAO,EAAE,uBAAuB,EAAE,+BAA2B;AAE7D;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;;OAIG;IACH,eAAe,CAAC,OAAO,EAAE,cAAc,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAEvE;;;;OAIG;IACH,gBAAgB,IAAI,gBAAgB,EAAE,CAAC;IAEvC;;OAEG;IACH,cAAc,IAAI,MAAM,CAAC;CAC1B,GAAG,aAAa,CAAC,eAAe,CAAC,CAAC;AAInC,8BAAsB,wBAAyB,YAAW,iBAAiB;;IACzE,QAAQ,CAAC,EAAE,EAAE,eAAe,CAAC;IAE7B,QAAQ,CAAC,QAAQ,EAAE,qBAAqB,CAAC;IAEzC,QAAQ,CAAC,SAAS,EAAE,8BAA8B,CAAC;gBAKjD,SAAS,EAAE,8BAA8B,EACzC,QAAQ,EAAE,qBAAqB,EAC/B,EAAE,EAAE,MAAM;IASZ,eAAe,CAAC,OAAO,EAAE,cAAc,GAAG,gBAAgB,GAAG,SAAS;IAItE,gBAAgB,IAAI,gBAAgB,EAAE;IAKtC,UAAU,CAAC,OAAO,EAAE,eAAe,GAAG,uBAAuB;IAgB7D,QAAQ,CAAC,cAAc,IAAI,MAAM;CAClC"}
1
+ {"version":3,"file":"AccountTreeWallet.d.mts","sourceRoot":"","sources":["../src/AccountTreeWallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,eAAe,EAChB,8BAA8B;AAC/B,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,8BAA8B;AAChF,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,mBAAmB,EAAE,oCAAgC;AACnE,OAAO,EAAE,KAAK,8BAA8B,EAAE,oCAAgC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,+BAA2B;AAEtD;;GAEG;AACH,qBAAa,iBAAkB,YAAW,aAAa,CAAC,eAAe,CAAC;;gBAO1D,EACV,SAAS,EACT,MAAM,GACP,EAAE;QACD,SAAS,EAAE,8BAA8B,CAAC;QAC1C,MAAM,EAAE,mBAAmB,CAAC;KAC7B;IAiBD,IAAI,EAAE,IAAI,eAAe,CAExB;IAED,IAAI,QAAQ,IAAI,qBAAqB,CAEpC;IAED,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;;;;OAKG;IACH,eAAe,CAAC,OAAO,EAAE,cAAc,GAAG,gBAAgB,GAAG,SAAS;IAItE;;;;OAIG;IACH,gBAAgB,IAAI,gBAAgB,EAAE;IAItC;;;;;;;;;;OAUG;IACH,aAAa,IAAI,eAAe;CASjC"}
@@ -9,38 +9,72 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  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");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _MutableAccountTreeWallet_groups;
13
- import { toAccountGroupId, toAccountWalletId, DEFAULT_ACCOUNT_GROUP_UNIQUE_ID } from "@metamask/account-api";
14
- import { MutableAccountTreeGroup } from "./AccountTreeGroup.mjs";
15
- // This class is meant to be used internally by every rules. It exposes mutable operations
16
- // which should not leak outside of this package.
17
- export class MutableAccountTreeWallet {
18
- constructor(messenger, category, id) {
19
- _MutableAccountTreeWallet_groups.set(this, void 0);
20
- this.id = toAccountWalletId(category, id);
21
- this.category = category;
22
- this.messenger = messenger;
23
- __classPrivateFieldSet(this, _MutableAccountTreeWallet_groups, new Map(), "f");
12
+ var _AccountTreeWallet_messenger, _AccountTreeWallet_wallet, _AccountTreeWallet_groups;
13
+ import { AccountTreeGroup } from "./AccountTreeGroup.mjs";
14
+ /**
15
+ * Account wallet coming from the {@link AccountTreeController}.
16
+ */
17
+ export class AccountTreeWallet {
18
+ constructor({ messenger, wallet, }) {
19
+ _AccountTreeWallet_messenger.set(this, void 0);
20
+ _AccountTreeWallet_wallet.set(this, void 0);
21
+ _AccountTreeWallet_groups.set(this, void 0);
22
+ __classPrivateFieldSet(this, _AccountTreeWallet_messenger, messenger, "f");
23
+ __classPrivateFieldSet(this, _AccountTreeWallet_wallet, wallet, "f");
24
+ __classPrivateFieldSet(this, _AccountTreeWallet_groups, new Map(), "f");
25
+ for (const [groupId, group] of Object.entries(__classPrivateFieldGet(this, _AccountTreeWallet_wallet, "f").groups)) {
26
+ __classPrivateFieldGet(this, _AccountTreeWallet_groups, "f").set(groupId, new AccountTreeGroup({
27
+ messenger: __classPrivateFieldGet(this, _AccountTreeWallet_messenger, "f"),
28
+ wallet: this,
29
+ group,
30
+ }));
31
+ }
32
+ }
33
+ get id() {
34
+ return __classPrivateFieldGet(this, _AccountTreeWallet_wallet, "f").id;
35
+ }
36
+ get category() {
37
+ return __classPrivateFieldGet(this, _AccountTreeWallet_wallet, "f").category;
38
+ }
39
+ get name() {
40
+ return __classPrivateFieldGet(this, _AccountTreeWallet_wallet, "f").metadata.name;
24
41
  }
42
+ /**
43
+ * Gets account tree group for a given ID.
44
+ *
45
+ * @param groupId - Group ID.
46
+ * @returns Account tree group.
47
+ */
25
48
  getAccountGroup(groupId) {
26
- return __classPrivateFieldGet(this, _MutableAccountTreeWallet_groups, "f").get(groupId);
49
+ return __classPrivateFieldGet(this, _AccountTreeWallet_groups, "f").get(groupId);
27
50
  }
51
+ /**
52
+ * Gets all account tree groups.
53
+ *
54
+ * @returns Account tree groups.
55
+ */
28
56
  getAccountGroups() {
29
- return Array.from(__classPrivateFieldGet(this, _MutableAccountTreeWallet_groups, "f").values()); // TODO: Should we avoid the copy here?
57
+ return Array.from(__classPrivateFieldGet(this, _AccountTreeWallet_groups, "f").values());
30
58
  }
31
- // NOTE: This method SHOULD BE overriden if a rule need to group things differently.
32
- addAccount(account) {
33
- const id = DEFAULT_ACCOUNT_GROUP_UNIQUE_ID;
34
- // Use a single-group by default.
35
- let group = __classPrivateFieldGet(this, _MutableAccountTreeWallet_groups, "f").get(toAccountGroupId(this.id, id));
36
- if (!group) {
37
- // We create the account group and attach it to this wallet.
38
- group = new MutableAccountTreeGroup(this.messenger, this, id);
39
- __classPrivateFieldGet(this, _MutableAccountTreeWallet_groups, "f").set(group.id, group);
59
+ /**
60
+ * Gets any underlying account.
61
+ *
62
+ * This can be useful if the accounts from this wallet have been grouped some
63
+ * common criteria, so you expect some commonly defined information on any of
64
+ * those accounts.
65
+ *
66
+ * @throws If the wallet has no account group (which should not be possible).
67
+ * @returns Any account that share the same information based on the grouping
68
+ * rule that has been used.
69
+ */
70
+ getAnyAccount() {
71
+ if (__classPrivateFieldGet(this, _AccountTreeWallet_groups, "f").size === 0) {
72
+ throw new Error('Wallet contains no account group');
40
73
  }
41
- group.addAccount(account);
42
- return group;
74
+ // It cannot be `undefined`, we checked the size before.
75
+ const group = __classPrivateFieldGet(this, _AccountTreeWallet_groups, "f").values().next().value;
76
+ return group.getAnyAccount();
43
77
  }
44
78
  }
45
- _MutableAccountTreeWallet_groups = new WeakMap();
79
+ _AccountTreeWallet_messenger = new WeakMap(), _AccountTreeWallet_wallet = new WeakMap(), _AccountTreeWallet_groups = new WeakMap();
46
80
  //# sourceMappingURL=AccountTreeWallet.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"AccountTreeWallet.mjs","sourceRoot":"","sources":["../src/AccountTreeWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,+BAA+B,EAKhC,8BAA8B;AAK/B,OAAO,EAAE,uBAAuB,EAAE,+BAA2B;AA0B7D,0FAA0F;AAC1F,iDAAiD;AACjD,MAAM,OAAgB,wBAAwB;IAS5C,YACE,SAAyC,EACzC,QAA+B,EAC/B,EAAU;QALH,mDAAsD;QAO7D,IAAI,CAAC,EAAE,GAAG,iBAAiB,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAC1C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAE3B,uBAAA,IAAI,oCAAW,IAAI,GAAG,EAAE,MAAA,CAAC;IAC3B,CAAC;IAED,eAAe,CAAC,OAAuB;QACrC,OAAO,uBAAA,IAAI,wCAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAED,gBAAgB;QACd,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,wCAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,uCAAuC;IACnF,CAAC;IAED,oFAAoF;IACpF,UAAU,CAAC,OAAwB;QACjC,MAAM,EAAE,GAAG,+BAA+B,CAAC;QAE3C,iCAAiC;QACjC,IAAI,KAAK,GAAG,uBAAA,IAAI,wCAAQ,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;QAC5D,IAAI,CAAC,KAAK,EAAE;YACV,4DAA4D;YAC5D,KAAK,GAAG,IAAI,uBAAuB,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;YAC9D,uBAAA,IAAI,wCAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;SACnC;QAED,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAE1B,OAAO,KAAK,CAAC;IACf,CAAC;CAGF","sourcesContent":["import {\n toAccountGroupId,\n toAccountWalletId,\n DEFAULT_ACCOUNT_GROUP_UNIQUE_ID,\n type AccountGroupId,\n type AccountWallet,\n type AccountWalletCategory,\n type AccountWalletId,\n} from '@metamask/account-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { type AccountTreeControllerMessenger } from './AccountTreeController';\nimport type { AccountTreeGroup } from './AccountTreeGroup';\nimport { MutableAccountTreeGroup } from './AccountTreeGroup';\n\n/**\n * Account wallet coming from the {@link AccountTreeController}.\n */\nexport type AccountTreeWallet = {\n /**\n * Gets account tree group for a given ID.\n *\n * @returns Account tree group.\n */\n getAccountGroup(groupId: AccountGroupId): AccountTreeGroup | undefined;\n\n /**\n * Gets all account tree groups.\n *\n * @returns Account tree groups.\n */\n getAccountGroups(): AccountTreeGroup[];\n\n /**\n * Gets the default name for that account wallet.\n */\n getDefaultName(): string;\n} & AccountWallet<InternalAccount>;\n\n// This class is meant to be used internally by every rules. It exposes mutable operations\n// which should not leak outside of this package.\nexport abstract class MutableAccountTreeWallet implements AccountTreeWallet {\n readonly id: AccountWalletId;\n\n readonly category: AccountWalletCategory;\n\n readonly messenger: AccountTreeControllerMessenger;\n\n readonly #groups: Map<AccountGroupId, MutableAccountTreeGroup>;\n\n constructor(\n messenger: AccountTreeControllerMessenger,\n category: AccountWalletCategory,\n id: string,\n ) {\n this.id = toAccountWalletId(category, id);\n this.category = category;\n this.messenger = messenger;\n\n this.#groups = new Map();\n }\n\n getAccountGroup(groupId: AccountGroupId): AccountTreeGroup | undefined {\n return this.#groups.get(groupId);\n }\n\n getAccountGroups(): AccountTreeGroup[] {\n return Array.from(this.#groups.values()); // TODO: Should we avoid the copy here?\n }\n\n // NOTE: This method SHOULD BE overriden if a rule need to group things differently.\n addAccount(account: InternalAccount): MutableAccountTreeGroup {\n const id = DEFAULT_ACCOUNT_GROUP_UNIQUE_ID;\n\n // Use a single-group by default.\n let group = this.#groups.get(toAccountGroupId(this.id, id));\n if (!group) {\n // We create the account group and attach it to this wallet.\n group = new MutableAccountTreeGroup(this.messenger, this, id);\n this.#groups.set(group.id, group);\n }\n\n group.addAccount(account);\n\n return group;\n }\n\n abstract getDefaultName(): string;\n}\n"]}
1
+ {"version":3,"file":"AccountTreeWallet.mjs","sourceRoot":"","sources":["../src/AccountTreeWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;AASA,OAAO,EAAE,gBAAgB,EAAE,+BAA2B;AAEtD;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAO5B,YAAY,EACV,SAAS,EACT,MAAM,GAIP;QAZQ,+CAA2C;QAE3C,4CAA6B;QAE7B,4CAA+C;QAStD,uBAAA,IAAI,gCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,6BAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,6BAAW,IAAI,GAAG,EAAE,MAAA,CAAC;QAEzB,KAAK,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,uBAAA,IAAI,iCAAQ,CAAC,MAAM,CAAC,EAAE;YAClE,uBAAA,IAAI,iCAAQ,CAAC,GAAG,CACd,OAAyB,EACzB,IAAI,gBAAgB,CAAC;gBACnB,SAAS,EAAE,uBAAA,IAAI,oCAAW;gBAC1B,MAAM,EAAE,IAAI;gBACZ,KAAK;aACN,CAAC,CACH,CAAC;SACH;IACH,CAAC;IAED,IAAI,EAAE;QACJ,OAAO,uBAAA,IAAI,iCAAQ,CAAC,EAAE,CAAC;IACzB,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,iCAAQ,CAAC,QAAQ,CAAC;IAC/B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,iCAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;IACpC,CAAC;IAED;;;;;OAKG;IACH,eAAe,CAAC,OAAuB;QACrC,OAAO,uBAAA,IAAI,iCAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,gBAAgB;QACd,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,iCAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;OAUG;IACH,aAAa;QACX,IAAI,uBAAA,IAAI,iCAAQ,CAAC,IAAI,KAAK,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;SACrD;QAED,wDAAwD;QACxD,MAAM,KAAK,GAAG,uBAAA,IAAI,iCAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAyB,CAAC;QACrE,OAAO,KAAK,CAAC,aAAa,EAAE,CAAC;IAC/B,CAAC;CACF","sourcesContent":["import type {\n AccountWalletCategory,\n AccountWalletId,\n} from '@metamask/account-api';\nimport { type AccountGroupId, type AccountWallet } from '@metamask/account-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { AccountWalletObject } from './AccountTreeController';\nimport { type AccountTreeControllerMessenger } from './AccountTreeController';\nimport { AccountTreeGroup } from './AccountTreeGroup';\n\n/**\n * Account wallet coming from the {@link AccountTreeController}.\n */\nexport class AccountTreeWallet implements AccountWallet<InternalAccount> {\n readonly #messenger: AccountTreeControllerMessenger;\n\n readonly #wallet: AccountWalletObject;\n\n readonly #groups: Map<AccountGroupId, AccountTreeGroup>;\n\n constructor({\n messenger,\n wallet,\n }: {\n messenger: AccountTreeControllerMessenger;\n wallet: AccountWalletObject;\n }) {\n this.#messenger = messenger;\n this.#wallet = wallet;\n this.#groups = new Map();\n\n for (const [groupId, group] of Object.entries(this.#wallet.groups)) {\n this.#groups.set(\n groupId as AccountGroupId,\n new AccountTreeGroup({\n messenger: this.#messenger,\n wallet: this,\n group,\n }),\n );\n }\n }\n\n get id(): AccountWalletId {\n return this.#wallet.id;\n }\n\n get category(): AccountWalletCategory {\n return this.#wallet.category;\n }\n\n get name(): string {\n return this.#wallet.metadata.name;\n }\n\n /**\n * Gets account tree group for a given ID.\n *\n * @param groupId - Group ID.\n * @returns Account tree group.\n */\n getAccountGroup(groupId: AccountGroupId): AccountTreeGroup | undefined {\n return this.#groups.get(groupId);\n }\n\n /**\n * Gets all account tree groups.\n *\n * @returns Account tree groups.\n */\n getAccountGroups(): AccountTreeGroup[] {\n return Array.from(this.#groups.values());\n }\n\n /**\n * Gets any underlying account.\n *\n * This can be useful if the accounts from this wallet have been grouped some\n * common criteria, so you expect some commonly defined information on any of\n * those accounts.\n *\n * @throws If the wallet has no account group (which should not be possible).\n * @returns Any account that share the same information based on the grouping\n * rule that has been used.\n */\n getAnyAccount(): InternalAccount {\n if (this.#groups.size === 0) {\n throw new Error('Wallet contains no account group');\n }\n\n // It cannot be `undefined`, we checked the size before.\n const group = this.#groups.values().next().value as AccountTreeGroup;\n return group.getAnyAccount();\n }\n}\n"]}
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _EntropyRule_instances, _EntropyRule_entropySourcesToIndex, _EntropyRule_syncEntropySources;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.EntropyRule = void 0;
16
+ const account_api_1 = require("@metamask/account-api");
17
+ const keyring_api_1 = require("@metamask/keyring-api");
18
+ const rule_1 = require("./rule.cjs");
19
+ /**
20
+ * Select keyrings from keyring controller state.
21
+ *
22
+ * @param state - The keyring controller state.
23
+ * @returns The keyrings.
24
+ */
25
+ function selectKeyringControllerKeyrings(state) {
26
+ return state.keyrings;
27
+ }
28
+ class EntropyRule extends rule_1.Rule {
29
+ constructor(messenger) {
30
+ super(messenger);
31
+ _EntropyRule_instances.add(this);
32
+ this.category = account_api_1.AccountWalletCategory.Entropy;
33
+ _EntropyRule_entropySourcesToIndex.set(this, void 0);
34
+ __classPrivateFieldSet(this, _EntropyRule_entropySourcesToIndex, new Map(), "f");
35
+ __classPrivateFieldGet(this, _EntropyRule_instances, "m", _EntropyRule_syncEntropySources).call(this);
36
+ }
37
+ match(account) {
38
+ if (!(0, account_api_1.isBip44Account)(account)) {
39
+ return undefined;
40
+ }
41
+ const entropySource = account.options.entropy.id;
42
+ const entropySourceIndex = __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").get(entropySource);
43
+ if (entropySourceIndex === undefined) {
44
+ console.warn(`! Found an unknown entropy ID: "${entropySource}", account "${account.id}" won't be grouped by entropy.`);
45
+ return undefined;
46
+ }
47
+ const walletId = (0, account_api_1.toMultichainAccountWalletId)(account.options.entropy.id);
48
+ const groupId = (0, account_api_1.toMultichainAccountId)(walletId, account.options.entropy.groupIndex);
49
+ return {
50
+ walletId,
51
+ groupId,
52
+ };
53
+ }
54
+ getDefaultAccountWalletName(wallet) {
55
+ // Precondition: This method is invoked only if there was a match for
56
+ // a BIP-44 account, so we can type-cast here.
57
+ const account = wallet.getAnyAccount();
58
+ const entropySource = account.options.entropy.id;
59
+ const entropySourceIndex = __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").get(entropySource); // This has to be defined, we checked it during the `match` above.
60
+ return `Wallet ${entropySourceIndex + 1}`; // Use human indexing (starts at 1).
61
+ }
62
+ getDefaultAccountGroupName(group) {
63
+ // EVM account name has a highest priority.
64
+ const accounts = group.getAccounts();
65
+ const evmAccount = accounts.find((account) => (0, keyring_api_1.isEvmAccountType)(account.type));
66
+ if (evmAccount) {
67
+ return evmAccount.metadata.name;
68
+ }
69
+ // We should always have an account, since this function will be called only
70
+ // if an account got a match.
71
+ return accounts[0].metadata.name;
72
+ }
73
+ }
74
+ exports.EntropyRule = EntropyRule;
75
+ _EntropyRule_entropySourcesToIndex = new WeakMap(), _EntropyRule_instances = new WeakSet(), _EntropyRule_syncEntropySources = function _EntropyRule_syncEntropySources() {
76
+ __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").clear();
77
+ const state = this.messenger.call('KeyringController:getState');
78
+ state.keyrings.forEach((keyring, index) => __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").set(keyring.metadata.id, index));
79
+ this.messenger.subscribe('KeyringController:stateChange', (keyrings) => {
80
+ keyrings.forEach((keyring, index) => __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").set(keyring.metadata.id, index));
81
+ }, selectKeyringControllerKeyrings);
82
+ };
83
+ //# sourceMappingURL=entropy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entropy.cjs","sourceRoot":"","sources":["../../src/rules/entropy.ts"],"names":[],"mappings":";;;;;;;;;;;;;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AAE,EAAE,KAAK,CAAC,CAC5D,CAAC;IACJ,CAAC,EACD,+BAA+B,CAChC,CAAC;AACJ,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport {\n AccountWalletCategory,\n isBip44Account,\n toMultichainAccountId,\n toMultichainAccountWalletId,\n} from '@metamask/account-api';\nimport { isEvmAccountType, type EntropySourceId } from '@metamask/keyring-api';\nimport type { KeyringControllerState } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { Rule } from './rule';\nimport type { AccountTreeGroup, AccountTreeWallet } from '..';\nimport type { AccountTreeControllerMessenger } from '../AccountTreeController';\nimport type { AccountContext } from '../types';\n\n/**\n * Select keyrings from keyring controller state.\n *\n * @param state - The keyring controller state.\n * @returns The keyrings.\n */\nfunction selectKeyringControllerKeyrings(state: KeyringControllerState) {\n return state.keyrings;\n}\n\nexport class EntropyRule extends Rule {\n readonly category = AccountWalletCategory.Entropy;\n\n readonly #entropySourcesToIndex: Map<EntropySourceId, number>;\n\n constructor(messenger: AccountTreeControllerMessenger) {\n super(messenger);\n\n this.#entropySourcesToIndex = new Map();\n this.#syncEntropySources();\n }\n\n #syncEntropySources() {\n this.#entropySourcesToIndex.clear();\n\n const state = this.messenger.call('KeyringController:getState');\n state.keyrings.forEach((keyring, index) =>\n this.#entropySourcesToIndex.set(keyring.metadata.id, index),\n );\n\n this.messenger.subscribe(\n 'KeyringController:stateChange',\n (keyrings) => {\n keyrings.forEach((keyring, index) =>\n this.#entropySourcesToIndex.set(keyring.metadata.id, index),\n );\n },\n selectKeyringControllerKeyrings,\n );\n }\n\n match(account: InternalAccount): AccountContext | undefined {\n if (!isBip44Account(account)) {\n return undefined;\n }\n\n const entropySource = account.options.entropy.id;\n const entropySourceIndex = this.#entropySourcesToIndex.get(entropySource);\n if (entropySourceIndex === undefined) {\n console.warn(\n `! Found an unknown entropy ID: \"${entropySource}\", account \"${account.id}\" won't be grouped by entropy.`,\n );\n return undefined;\n }\n\n const walletId = toMultichainAccountWalletId(account.options.entropy.id);\n const groupId = toMultichainAccountId(\n walletId,\n account.options.entropy.groupIndex,\n );\n\n return {\n walletId,\n groupId,\n };\n }\n\n getDefaultAccountWalletName(wallet: AccountTreeWallet): string {\n // Precondition: This method is invoked only if there was a match for\n // a BIP-44 account, so we can type-cast here.\n const account = wallet.getAnyAccount() as Bip44Account<InternalAccount>;\n\n const entropySource = account.options.entropy.id;\n const entropySourceIndex = this.#entropySourcesToIndex.get(\n entropySource,\n ) as number; // This has to be defined, we checked it during the `match` above.\n\n return `Wallet ${entropySourceIndex + 1}`; // Use human indexing (starts at 1).\n }\n\n getDefaultAccountGroupName(group: AccountTreeGroup): string {\n // EVM account name has a highest priority.\n const accounts = group.getAccounts();\n const evmAccount = accounts.find((account) =>\n isEvmAccountType(account.type),\n );\n if (evmAccount) {\n return evmAccount.metadata.name;\n }\n\n // We should always have an account, since this function will be called only\n // if an account got a match.\n return accounts[0].metadata.name;\n }\n}\n"]}
@@ -0,0 +1,15 @@
1
+ import { AccountWalletCategory } from "@metamask/account-api";
2
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
3
+ import { Rule } from "./rule.cjs";
4
+ import type { AccountTreeGroup, AccountTreeWallet } from "../index.cjs";
5
+ import type { AccountTreeControllerMessenger } from "../AccountTreeController.cjs";
6
+ import type { AccountContext } from "../types.cjs";
7
+ export declare class EntropyRule extends Rule {
8
+ #private;
9
+ readonly category = AccountWalletCategory.Entropy;
10
+ constructor(messenger: AccountTreeControllerMessenger);
11
+ match(account: InternalAccount): AccountContext | undefined;
12
+ getDefaultAccountWalletName(wallet: AccountTreeWallet): string;
13
+ getDefaultAccountGroupName(group: AccountTreeGroup): string;
14
+ }
15
+ //# sourceMappingURL=entropy.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entropy.d.cts","sourceRoot":"","sources":["../../src/rules/entropy.ts"],"names":[],"mappings":"AACA,OAAO,EACL,qBAAqB,EAItB,8BAA8B;AAG/B,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,EAAE,IAAI,EAAE,mBAAe;AAC9B,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,qBAAW;AAC9D,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAiB;AAY/C,qBAAa,WAAY,SAAQ,IAAI;;IACnC,QAAQ,CAAC,QAAQ,iCAAiC;gBAItC,SAAS,EAAE,8BAA8B;IA0BrD,KAAK,CAAC,OAAO,EAAE,eAAe,GAAG,cAAc,GAAG,SAAS;IA0B3D,2BAA2B,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM;IAa9D,0BAA0B,CAAC,KAAK,EAAE,gBAAgB,GAAG,MAAM;CAc5D"}
@@ -0,0 +1,15 @@
1
+ import { AccountWalletCategory } from "@metamask/account-api";
2
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
3
+ import { Rule } from "./rule.mjs";
4
+ import type { AccountTreeGroup, AccountTreeWallet } from "../index.mjs";
5
+ import type { AccountTreeControllerMessenger } from "../AccountTreeController.mjs";
6
+ import type { AccountContext } from "../types.mjs";
7
+ export declare class EntropyRule extends Rule {
8
+ #private;
9
+ readonly category = AccountWalletCategory.Entropy;
10
+ constructor(messenger: AccountTreeControllerMessenger);
11
+ match(account: InternalAccount): AccountContext | undefined;
12
+ getDefaultAccountWalletName(wallet: AccountTreeWallet): string;
13
+ getDefaultAccountGroupName(group: AccountTreeGroup): string;
14
+ }
15
+ //# sourceMappingURL=entropy.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entropy.d.mts","sourceRoot":"","sources":["../../src/rules/entropy.ts"],"names":[],"mappings":"AACA,OAAO,EACL,qBAAqB,EAItB,8BAA8B;AAG/B,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,EAAE,IAAI,EAAE,mBAAe;AAC9B,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,qBAAW;AAC9D,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAiB;AAY/C,qBAAa,WAAY,SAAQ,IAAI;;IACnC,QAAQ,CAAC,QAAQ,iCAAiC;gBAItC,SAAS,EAAE,8BAA8B;IA0BrD,KAAK,CAAC,OAAO,EAAE,eAAe,GAAG,cAAc,GAAG,SAAS;IA0B3D,2BAA2B,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM;IAa9D,0BAA0B,CAAC,KAAK,EAAE,gBAAgB,GAAG,MAAM;CAc5D"}
@@ -0,0 +1,79 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _EntropyRule_instances, _EntropyRule_entropySourcesToIndex, _EntropyRule_syncEntropySources;
13
+ import { AccountWalletCategory, isBip44Account, toMultichainAccountId, toMultichainAccountWalletId } from "@metamask/account-api";
14
+ import { isEvmAccountType } from "@metamask/keyring-api";
15
+ import { Rule } from "./rule.mjs";
16
+ /**
17
+ * Select keyrings from keyring controller state.
18
+ *
19
+ * @param state - The keyring controller state.
20
+ * @returns The keyrings.
21
+ */
22
+ function selectKeyringControllerKeyrings(state) {
23
+ return state.keyrings;
24
+ }
25
+ export class EntropyRule extends Rule {
26
+ constructor(messenger) {
27
+ super(messenger);
28
+ _EntropyRule_instances.add(this);
29
+ this.category = AccountWalletCategory.Entropy;
30
+ _EntropyRule_entropySourcesToIndex.set(this, void 0);
31
+ __classPrivateFieldSet(this, _EntropyRule_entropySourcesToIndex, new Map(), "f");
32
+ __classPrivateFieldGet(this, _EntropyRule_instances, "m", _EntropyRule_syncEntropySources).call(this);
33
+ }
34
+ match(account) {
35
+ if (!isBip44Account(account)) {
36
+ return undefined;
37
+ }
38
+ const entropySource = account.options.entropy.id;
39
+ const entropySourceIndex = __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").get(entropySource);
40
+ if (entropySourceIndex === undefined) {
41
+ console.warn(`! Found an unknown entropy ID: "${entropySource}", account "${account.id}" won't be grouped by entropy.`);
42
+ return undefined;
43
+ }
44
+ const walletId = toMultichainAccountWalletId(account.options.entropy.id);
45
+ const groupId = toMultichainAccountId(walletId, account.options.entropy.groupIndex);
46
+ return {
47
+ walletId,
48
+ groupId,
49
+ };
50
+ }
51
+ getDefaultAccountWalletName(wallet) {
52
+ // Precondition: This method is invoked only if there was a match for
53
+ // a BIP-44 account, so we can type-cast here.
54
+ const account = wallet.getAnyAccount();
55
+ const entropySource = account.options.entropy.id;
56
+ const entropySourceIndex = __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").get(entropySource); // This has to be defined, we checked it during the `match` above.
57
+ return `Wallet ${entropySourceIndex + 1}`; // Use human indexing (starts at 1).
58
+ }
59
+ getDefaultAccountGroupName(group) {
60
+ // EVM account name has a highest priority.
61
+ const accounts = group.getAccounts();
62
+ const evmAccount = accounts.find((account) => isEvmAccountType(account.type));
63
+ if (evmAccount) {
64
+ return evmAccount.metadata.name;
65
+ }
66
+ // We should always have an account, since this function will be called only
67
+ // if an account got a match.
68
+ return accounts[0].metadata.name;
69
+ }
70
+ }
71
+ _EntropyRule_entropySourcesToIndex = new WeakMap(), _EntropyRule_instances = new WeakSet(), _EntropyRule_syncEntropySources = function _EntropyRule_syncEntropySources() {
72
+ __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").clear();
73
+ const state = this.messenger.call('KeyringController:getState');
74
+ state.keyrings.forEach((keyring, index) => __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").set(keyring.metadata.id, index));
75
+ this.messenger.subscribe('KeyringController:stateChange', (keyrings) => {
76
+ keyrings.forEach((keyring, index) => __classPrivateFieldGet(this, _EntropyRule_entropySourcesToIndex, "f").set(keyring.metadata.id, index));
77
+ }, selectKeyringControllerKeyrings);
78
+ };
79
+ //# sourceMappingURL=entropy.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entropy.mjs","sourceRoot":"","sources":["../../src/rules/entropy.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EACL,qBAAqB,EACrB,cAAc,EACd,qBAAqB,EACrB,2BAA2B,EAC5B,8BAA8B;AAC/B,OAAO,EAAE,gBAAgB,EAAwB,8BAA8B;AAI/E,OAAO,EAAE,IAAI,EAAE,mBAAe;AAK9B;;;;;GAKG;AACH,SAAS,+BAA+B,CAAC,KAA6B;IACpE,OAAO,KAAK,CAAC,QAAQ,CAAC;AACxB,CAAC;AAED,MAAM,OAAO,WAAY,SAAQ,IAAI;IAKnC,YAAY,SAAyC;QACnD,KAAK,CAAC,SAAS,CAAC,CAAC;;QALV,aAAQ,GAAG,qBAAqB,CAAC,OAAO,CAAC;QAEzC,qDAAqD;QAK5D,uBAAA,IAAI,sCAA0B,IAAI,GAAG,EAAE,MAAA,CAAC;QACxC,uBAAA,IAAI,+DAAoB,MAAxB,IAAI,CAAsB,CAAC;IAC7B,CAAC;IAqBD,KAAK,CAAC,OAAwB;QAC5B,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE;YAC5B,OAAO,SAAS,CAAC;SAClB;QAED,MAAM,aAAa,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QAC5E,6BAA6B;QAC7B,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;IACnC,CAAC;CACF;;IAvEG,uBAAA,IAAI,0CAAuB,CAAC,KAAK,EAAE,CAAC;IAEpC,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;IAChE,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CACxC,uBAAA,IAAI,0CAAuB,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC,CAC5D,CAAC;IAEF,IAAI,CAAC,SAAS,CAAC,SAAS,CACtB,+BAA+B,EAC/B,CAAC,QAAQ,EAAE,EAAE;QACX,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAClC,uBAAA,IAAI,0CAAuB,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC,CAC5D,CAAC;IACJ,CAAC,EACD,+BAA+B,CAChC,CAAC;AACJ,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport {\n AccountWalletCategory,\n isBip44Account,\n toMultichainAccountId,\n toMultichainAccountWalletId,\n} from '@metamask/account-api';\nimport { isEvmAccountType, type EntropySourceId } from '@metamask/keyring-api';\nimport type { KeyringControllerState } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { Rule } from './rule';\nimport type { AccountTreeGroup, AccountTreeWallet } from '..';\nimport type { AccountTreeControllerMessenger } from '../AccountTreeController';\nimport type { AccountContext } from '../types';\n\n/**\n * Select keyrings from keyring controller state.\n *\n * @param state - The keyring controller state.\n * @returns The keyrings.\n */\nfunction selectKeyringControllerKeyrings(state: KeyringControllerState) {\n return state.keyrings;\n}\n\nexport class EntropyRule extends Rule {\n readonly category = AccountWalletCategory.Entropy;\n\n readonly #entropySourcesToIndex: Map<EntropySourceId, number>;\n\n constructor(messenger: AccountTreeControllerMessenger) {\n super(messenger);\n\n this.#entropySourcesToIndex = new Map();\n this.#syncEntropySources();\n }\n\n #syncEntropySources() {\n this.#entropySourcesToIndex.clear();\n\n const state = this.messenger.call('KeyringController:getState');\n state.keyrings.forEach((keyring, index) =>\n this.#entropySourcesToIndex.set(keyring.metadata.id, index),\n );\n\n this.messenger.subscribe(\n 'KeyringController:stateChange',\n (keyrings) => {\n keyrings.forEach((keyring, index) =>\n this.#entropySourcesToIndex.set(keyring.metadata.id, index),\n );\n },\n selectKeyringControllerKeyrings,\n );\n }\n\n match(account: InternalAccount): AccountContext | undefined {\n if (!isBip44Account(account)) {\n return undefined;\n }\n\n const entropySource = account.options.entropy.id;\n const entropySourceIndex = this.#entropySourcesToIndex.get(entropySource);\n if (entropySourceIndex === undefined) {\n console.warn(\n `! Found an unknown entropy ID: \"${entropySource}\", account \"${account.id}\" won't be grouped by entropy.`,\n );\n return undefined;\n }\n\n const walletId = toMultichainAccountWalletId(account.options.entropy.id);\n const groupId = toMultichainAccountId(\n walletId,\n account.options.entropy.groupIndex,\n );\n\n return {\n walletId,\n groupId,\n };\n }\n\n getDefaultAccountWalletName(wallet: AccountTreeWallet): string {\n // Precondition: This method is invoked only if there was a match for\n // a BIP-44 account, so we can type-cast here.\n const account = wallet.getAnyAccount() as Bip44Account<InternalAccount>;\n\n const entropySource = account.options.entropy.id;\n const entropySourceIndex = this.#entropySourcesToIndex.get(\n entropySource,\n ) as number; // This has to be defined, we checked it during the `match` above.\n\n return `Wallet ${entropySourceIndex + 1}`; // Use human indexing (starts at 1).\n }\n\n getDefaultAccountGroupName(group: AccountTreeGroup): string {\n // EVM account name has a highest priority.\n const accounts = group.getAccounts();\n const evmAccount = accounts.find((account) =>\n isEvmAccountType(account.type),\n );\n if (evmAccount) {\n return evmAccount.metadata.name;\n }\n\n // We should always have an account, since this function will be called only\n // if an account got a match.\n return accounts[0].metadata.name;\n }\n}\n"]}
@@ -14,8 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./WalletRule.cjs"), exports);
18
- __exportStar(require("./EntropySourceWalletRule.cjs"), exports);
19
- __exportStar(require("./SnapWalletRule.cjs"), exports);
20
- __exportStar(require("./KeyringWalletRule.cjs"), exports);
17
+ __exportStar(require("./rule.cjs"), exports);
18
+ __exportStar(require("./entropy.cjs"), exports);
19
+ __exportStar(require("./snap.cjs"), exports);
20
+ __exportStar(require("./keyring.cjs"), exports);
21
21
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAA6B;AAC7B,gEAA0C;AAC1C,uDAAiC;AACjC,0DAAoC","sourcesContent":["export * from './WalletRule';\nexport * from './EntropySourceWalletRule';\nexport * from './SnapWalletRule';\nexport * from './KeyringWalletRule';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAAuB;AACvB,gDAA0B;AAC1B,6CAAuB;AACvB,gDAA0B","sourcesContent":["export * from './rule';\nexport * from './entropy';\nexport * from './snap';\nexport * from './keyring';\n"]}
@@ -1,5 +1,5 @@
1
- export * from "./WalletRule.cjs";
2
- export * from "./EntropySourceWalletRule.cjs";
3
- export * from "./SnapWalletRule.cjs";
4
- export * from "./KeyringWalletRule.cjs";
1
+ export * from "./rule.cjs";
2
+ export * from "./entropy.cjs";
3
+ export * from "./snap.cjs";
4
+ export * from "./keyring.cjs";
5
5
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,8CAA0C;AAC1C,qCAAiC;AACjC,wCAAoC"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,2BAAuB;AACvB,8BAA0B;AAC1B,2BAAuB;AACvB,8BAA0B"}
@@ -1,5 +1,5 @@
1
- export * from "./WalletRule.mjs";
2
- export * from "./EntropySourceWalletRule.mjs";
3
- export * from "./SnapWalletRule.mjs";
4
- export * from "./KeyringWalletRule.mjs";
1
+ export * from "./rule.mjs";
2
+ export * from "./entropy.mjs";
3
+ export * from "./snap.mjs";
4
+ export * from "./keyring.mjs";
5
5
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,8CAA0C;AAC1C,qCAAiC;AACjC,wCAAoC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,2BAAuB;AACvB,8BAA0B;AAC1B,2BAAuB;AACvB,8BAA0B"}
@@ -1,5 +1,5 @@
1
- export * from "./WalletRule.mjs";
2
- export * from "./EntropySourceWalletRule.mjs";
3
- export * from "./SnapWalletRule.mjs";
4
- export * from "./KeyringWalletRule.mjs";
1
+ export * from "./rule.mjs";
2
+ export * from "./entropy.mjs";
3
+ export * from "./snap.mjs";
4
+ export * from "./keyring.mjs";
5
5
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,iCAA6B;AAC7B,8CAA0C;AAC1C,qCAAiC;AACjC,wCAAoC","sourcesContent":["export * from './WalletRule';\nexport * from './EntropySourceWalletRule';\nexport * from './SnapWalletRule';\nexport * from './KeyringWalletRule';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,2BAAuB;AACvB,8BAA0B;AAC1B,2BAAuB;AACvB,8BAA0B","sourcesContent":["export * from './rule';\nexport * from './entropy';\nexport * from './snap';\nexport * from './keyring';\n"]}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KeyringRule = exports.getAccountWalletNameFromKeyringType = void 0;
4
+ const account_api_1 = require("@metamask/account-api");
5
+ const keyring_controller_1 = require("@metamask/keyring-controller");
6
+ const rule_1 = require("./rule.cjs");
7
+ /**
8
+ * Get wallet name from a keyring type.
9
+ *
10
+ * @param type - Keyring's type.
11
+ * @returns Wallet name.
12
+ */
13
+ function getAccountWalletNameFromKeyringType(type) {
14
+ switch (type) {
15
+ case keyring_controller_1.KeyringTypes.simple: {
16
+ return 'Imported accounts';
17
+ }
18
+ case keyring_controller_1.KeyringTypes.trezor: {
19
+ return 'Trezor';
20
+ }
21
+ case keyring_controller_1.KeyringTypes.oneKey: {
22
+ return 'OneKey';
23
+ }
24
+ case keyring_controller_1.KeyringTypes.ledger: {
25
+ return 'Ledger';
26
+ }
27
+ case keyring_controller_1.KeyringTypes.lattice: {
28
+ return 'Lattice';
29
+ }
30
+ case keyring_controller_1.KeyringTypes.qr: {
31
+ return 'QR';
32
+ }
33
+ // Those keyrings should never really be used in such context since they
34
+ // should be used by other grouping rules.
35
+ case keyring_controller_1.KeyringTypes.hd: {
36
+ return 'HD Wallet';
37
+ }
38
+ case keyring_controller_1.KeyringTypes.snap: {
39
+ return 'Snap Wallet';
40
+ }
41
+ // ------------------------------------------------------------------------
42
+ default: {
43
+ return 'Unknown';
44
+ }
45
+ }
46
+ }
47
+ exports.getAccountWalletNameFromKeyringType = getAccountWalletNameFromKeyringType;
48
+ class KeyringRule extends rule_1.Rule {
49
+ constructor() {
50
+ super(...arguments);
51
+ this.category = account_api_1.AccountWalletCategory.Keyring;
52
+ }
53
+ match(account) {
54
+ // We assume that `type` is really a `KeyringTypes`.
55
+ const type = account.metadata.keyring.type;
56
+ const walletId = (0, account_api_1.toAccountWalletId)(this.category, type);
57
+ const groupId = (0, account_api_1.toAccountGroupId)(walletId, account.address);
58
+ // This rule cannot fail.
59
+ return {
60
+ walletId,
61
+ groupId,
62
+ };
63
+ }
64
+ getDefaultAccountWalletName(wallet) {
65
+ // Precondition: This method is invoked only if there was a match for
66
+ // this rule.
67
+ const account = wallet.getAnyAccount();
68
+ return getAccountWalletNameFromKeyringType(account.metadata.keyring.type);
69
+ }
70
+ getDefaultAccountGroupName(group) {
71
+ // Precondition: This method is invoked only if there was a match for
72
+ // this rule. Also, each of those account groups should contain
73
+ // only 1 account.
74
+ const account = group.getOnlyAccount();
75
+ // We only have 1 account for this kind of rule.
76
+ return account.metadata.name;
77
+ }
78
+ }
79
+ exports.KeyringRule = KeyringRule;
80
+ //# sourceMappingURL=keyring.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyring.cjs","sourceRoot":"","sources":["../../src/rules/keyring.ts"],"names":[],"mappings":";;;AAAA,uDAI+B;AAC/B,qEAA4D;AAI5D,qCAA8B;AAG9B;;;;;GAKG;AACH,SAAgB,mCAAmC,CAAC,IAAkB;IACpE,QAAQ,IAAI,EAAE;QACZ,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,mBAAmB,CAAC;SAC5B;QACD,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,iCAAY,CAAC,MAAM,CAAC,CAAC;YACxB,OAAO,QAAQ,CAAC;SACjB;QACD,KAAK,iCAAY,CAAC,OAAO,CAAC,CAAC;YACzB,OAAO,SAAS,CAAC;SAClB;QACD,KAAK,iCAAY,CAAC,EAAE,CAAC,CAAC;YACp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AccountWalletId,\n} from '@metamask/account-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { AccountContext } from 'src/types';\n\nimport { Rule } from './rule';\nimport type { AccountTreeGroup, AccountTreeWallet } from '..';\n\n/**\n * Get wallet name from a keyring type.\n *\n * @param type - Keyring's type.\n * @returns Wallet name.\n */\nexport function getAccountWalletNameFromKeyringType(type: KeyringTypes) {\n switch (type) {\n case KeyringTypes.simple: {\n return 'Imported accounts';\n }\n case KeyringTypes.trezor: {\n return 'Trezor';\n }\n case KeyringTypes.oneKey: {\n return 'OneKey';\n }\n case KeyringTypes.ledger: {\n return 'Ledger';\n }\n case KeyringTypes.lattice: {\n return 'Lattice';\n }\n case KeyringTypes.qr: {\n return 'QR';\n }\n // Those keyrings should never really be used in such context since they\n // should be used by other grouping rules.\n case KeyringTypes.hd: {\n return 'HD Wallet';\n }\n case KeyringTypes.snap: {\n return 'Snap Wallet';\n }\n // ------------------------------------------------------------------------\n default: {\n return 'Unknown';\n }\n }\n}\n\nexport class KeyringRule extends Rule {\n readonly category = AccountWalletCategory.Keyring;\n\n match(account: InternalAccount): AccountContext | undefined {\n // We assume that `type` is really a `KeyringTypes`.\n const type = account.metadata.keyring.type as KeyringTypes;\n\n const walletId = toAccountWalletId(this.category, type);\n const groupId = toAccountGroupId(walletId, account.address);\n\n // This rule cannot fail.\n return {\n walletId,\n groupId,\n };\n }\n\n getDefaultAccountWalletName(wallet: AccountTreeWallet): string {\n // Precondition: This method is invoked only if there was a match for\n // this rule.\n const account = wallet.getAnyAccount();\n\n return getAccountWalletNameFromKeyringType(\n account.metadata.keyring.type as KeyringTypes,\n );\n }\n\n getDefaultAccountGroupName(group: AccountTreeGroup): string {\n // Precondition: This method is invoked only if there was a match for\n // this rule. Also, each of those account groups should contain\n // only 1 account.\n const account = group.getOnlyAccount();\n\n // We only have 1 account for this kind of rule.\n return account.metadata.name;\n }\n}\n"]}