@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.
Files changed (90) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/AccountTreeController.cjs +30 -137
  3. package/dist/AccountTreeController.cjs.map +1 -1
  4. package/dist/AccountTreeController.d.cts +10 -38
  5. package/dist/AccountTreeController.d.cts.map +1 -1
  6. package/dist/AccountTreeController.d.mts +10 -38
  7. package/dist/AccountTreeController.d.mts.map +1 -1
  8. package/dist/AccountTreeController.mjs +29 -133
  9. package/dist/AccountTreeController.mjs.map +1 -1
  10. package/dist/AccountTreeGroup.cjs +58 -0
  11. package/dist/AccountTreeGroup.cjs.map +1 -0
  12. package/dist/AccountTreeGroup.d.cts +31 -0
  13. package/dist/AccountTreeGroup.d.cts.map +1 -0
  14. package/dist/AccountTreeGroup.d.mts +31 -0
  15. package/dist/AccountTreeGroup.d.mts.map +1 -0
  16. package/dist/AccountTreeGroup.mjs +54 -0
  17. package/dist/AccountTreeGroup.mjs.map +1 -0
  18. package/dist/AccountTreeWallet.cjs +50 -0
  19. package/dist/AccountTreeWallet.cjs.map +1 -0
  20. package/dist/AccountTreeWallet.d.cts +38 -0
  21. package/dist/AccountTreeWallet.d.cts.map +1 -0
  22. package/dist/AccountTreeWallet.d.mts +38 -0
  23. package/dist/AccountTreeWallet.d.mts.map +1 -0
  24. package/dist/AccountTreeWallet.mjs +46 -0
  25. package/dist/AccountTreeWallet.mjs.map +1 -0
  26. package/dist/index.cjs +1 -7
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +4 -2
  29. package/dist/index.d.cts.map +1 -1
  30. package/dist/index.d.mts +4 -2
  31. package/dist/index.d.mts.map +1 -1
  32. package/dist/index.mjs +1 -1
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/rules/EntropySourceWalletRule.cjs +100 -0
  35. package/dist/rules/EntropySourceWalletRule.cjs.map +1 -0
  36. package/dist/rules/EntropySourceWalletRule.d.cts +19 -0
  37. package/dist/rules/EntropySourceWalletRule.d.cts.map +1 -0
  38. package/dist/rules/EntropySourceWalletRule.d.mts +19 -0
  39. package/dist/rules/EntropySourceWalletRule.d.mts.map +1 -0
  40. package/dist/rules/EntropySourceWalletRule.mjs +95 -0
  41. package/dist/rules/EntropySourceWalletRule.mjs.map +1 -0
  42. package/dist/rules/KeyringWalletRule.cjs +99 -0
  43. package/dist/rules/KeyringWalletRule.cjs.map +1 -0
  44. package/dist/rules/KeyringWalletRule.d.cts +18 -0
  45. package/dist/rules/KeyringWalletRule.d.cts.map +1 -0
  46. package/dist/rules/KeyringWalletRule.d.mts +18 -0
  47. package/dist/rules/KeyringWalletRule.d.mts.map +1 -0
  48. package/dist/rules/KeyringWalletRule.mjs +94 -0
  49. package/dist/rules/KeyringWalletRule.mjs.map +1 -0
  50. package/dist/rules/SnapWalletRule.cjs +70 -0
  51. package/dist/rules/SnapWalletRule.cjs.map +1 -0
  52. package/dist/rules/SnapWalletRule.d.cts +10 -0
  53. package/dist/rules/SnapWalletRule.d.cts.map +1 -0
  54. package/dist/rules/SnapWalletRule.d.mts +10 -0
  55. package/dist/rules/SnapWalletRule.d.mts.map +1 -0
  56. package/dist/rules/SnapWalletRule.mjs +66 -0
  57. package/dist/rules/SnapWalletRule.mjs.map +1 -0
  58. package/dist/rules/WalletRule.cjs +13 -0
  59. package/dist/rules/WalletRule.cjs.map +1 -0
  60. package/dist/rules/WalletRule.d.cts +37 -0
  61. package/dist/rules/WalletRule.d.cts.map +1 -0
  62. package/dist/rules/WalletRule.d.mts +37 -0
  63. package/dist/rules/WalletRule.d.mts.map +1 -0
  64. package/dist/rules/WalletRule.mjs +9 -0
  65. package/dist/rules/WalletRule.mjs.map +1 -0
  66. package/dist/rules/index.cjs +21 -0
  67. package/dist/rules/index.cjs.map +1 -0
  68. package/dist/rules/index.d.cts +5 -0
  69. package/dist/rules/index.d.cts.map +1 -0
  70. package/dist/rules/index.d.mts +5 -0
  71. package/dist/rules/index.d.mts.map +1 -0
  72. package/dist/rules/index.mjs +5 -0
  73. package/dist/rules/index.mjs.map +1 -0
  74. package/dist/rules/utils.cjs +15 -0
  75. package/dist/rules/utils.cjs.map +1 -0
  76. package/dist/rules/utils.d.cts +11 -0
  77. package/dist/rules/utils.d.cts.map +1 -0
  78. package/dist/rules/utils.d.mts +11 -0
  79. package/dist/rules/utils.d.mts.map +1 -0
  80. package/dist/rules/utils.mjs +11 -0
  81. package/dist/rules/utils.mjs.map +1 -0
  82. package/package.json +3 -1
  83. package/dist/names.cjs +0 -46
  84. package/dist/names.cjs.map +0 -1
  85. package/dist/names.d.cts +0 -9
  86. package/dist/names.d.cts.map +0 -1
  87. package/dist/names.d.mts +0 -9
  88. package/dist/names.d.mts.map +0 -1
  89. package/dist/names.mjs +0 -42
  90. package/dist/names.mjs.map +0 -1
@@ -0,0 +1,38 @@
1
+ import { type AccountGroupId, type AccountWallet, type AccountWalletCategory, type AccountWalletId } from "@metamask/account-api";
2
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
3
+ import { type AccountTreeControllerMessenger } from "./AccountTreeController.mjs";
4
+ import type { AccountTreeGroup } from "./AccountTreeGroup.mjs";
5
+ import { MutableAccountTreeGroup } from "./AccountTreeGroup.mjs";
6
+ /**
7
+ * Account wallet coming from the {@link AccountTreeController}.
8
+ */
9
+ export type AccountTreeWallet = {
10
+ /**
11
+ * Gets account tree group for a given ID.
12
+ *
13
+ * @returns Account tree group.
14
+ */
15
+ getAccountGroup(groupId: AccountGroupId): AccountTreeGroup | undefined;
16
+ /**
17
+ * Gets all account tree groups.
18
+ *
19
+ * @returns Account tree groups.
20
+ */
21
+ getAccountGroups(): AccountTreeGroup[];
22
+ /**
23
+ * Gets the default name for that account wallet.
24
+ */
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;
37
+ }
38
+ //# sourceMappingURL=AccountTreeWallet.d.mts.map
@@ -0,0 +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"}
@@ -0,0 +1,46 @@
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 _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");
24
+ }
25
+ getAccountGroup(groupId) {
26
+ return __classPrivateFieldGet(this, _MutableAccountTreeWallet_groups, "f").get(groupId);
27
+ }
28
+ getAccountGroups() {
29
+ return Array.from(__classPrivateFieldGet(this, _MutableAccountTreeWallet_groups, "f").values()); // TODO: Should we avoid the copy here?
30
+ }
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);
40
+ }
41
+ group.addAccount(account);
42
+ return group;
43
+ }
44
+ }
45
+ _MutableAccountTreeWallet_groups = new WeakMap();
46
+ //# sourceMappingURL=AccountTreeWallet.mjs.map
@@ -0,0 +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"]}
package/dist/index.cjs CHANGED
@@ -1,13 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_ACCOUNT_GROUP_UNIQUE_ID = exports.DEFAULT_ACCOUNT_GROUP_NAME = exports.toDefaultAccountGroupId = exports.toAccountWalletId = exports.toAccountGroupId = exports.getDefaultAccountTreeControllerState = exports.AccountTreeController = exports.AccountWalletCategory = void 0;
3
+ exports.getDefaultAccountTreeControllerState = exports.AccountTreeController = void 0;
4
4
  var AccountTreeController_1 = require("./AccountTreeController.cjs");
5
- Object.defineProperty(exports, "AccountWalletCategory", { enumerable: true, get: function () { return AccountTreeController_1.AccountWalletCategory; } });
6
5
  Object.defineProperty(exports, "AccountTreeController", { enumerable: true, get: function () { return AccountTreeController_1.AccountTreeController; } });
7
6
  Object.defineProperty(exports, "getDefaultAccountTreeControllerState", { enumerable: true, get: function () { return AccountTreeController_1.getDefaultAccountTreeControllerState; } });
8
- Object.defineProperty(exports, "toAccountGroupId", { enumerable: true, get: function () { return AccountTreeController_1.toAccountGroupId; } });
9
- Object.defineProperty(exports, "toAccountWalletId", { enumerable: true, get: function () { return AccountTreeController_1.toAccountWalletId; } });
10
- Object.defineProperty(exports, "toDefaultAccountGroupId", { enumerable: true, get: function () { return AccountTreeController_1.toDefaultAccountGroupId; } });
11
- Object.defineProperty(exports, "DEFAULT_ACCOUNT_GROUP_NAME", { enumerable: true, get: function () { return AccountTreeController_1.DEFAULT_ACCOUNT_GROUP_NAME; } });
12
- Object.defineProperty(exports, "DEFAULT_ACCOUNT_GROUP_UNIQUE_ID", { enumerable: true, get: function () { return AccountTreeController_1.DEFAULT_ACCOUNT_GROUP_UNIQUE_ID; } });
13
7
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAcA,qEASiC;AAR/B,8HAAA,qBAAqB,OAAA;AACrB,8HAAA,qBAAqB,OAAA;AACrB,6IAAA,oCAAoC,OAAA;AACpC,yHAAA,gBAAgB,OAAA;AAChB,0HAAA,iBAAiB,OAAA;AACjB,gIAAA,uBAAuB,OAAA;AACvB,mIAAA,0BAA0B,OAAA;AAC1B,wIAAA,+BAA+B,OAAA","sourcesContent":["export type {\n AccountTreeControllerState,\n AccountTreeControllerGetStateAction,\n AccountTreeControllerActions,\n AccountTreeControllerStateChangeEvent,\n AccountTreeControllerEvents,\n AccountTreeControllerMessenger,\n AccountWallet,\n AccountWalletId,\n AccountWalletMetadata,\n AccountGroup,\n AccountGroupId,\n AccountGroupMetadata,\n} from './AccountTreeController';\nexport {\n AccountWalletCategory,\n AccountTreeController,\n getDefaultAccountTreeControllerState,\n toAccountGroupId,\n toAccountWalletId,\n toDefaultAccountGroupId,\n DEFAULT_ACCOUNT_GROUP_NAME,\n DEFAULT_ACCOUNT_GROUP_UNIQUE_ID,\n} from './AccountTreeController';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAYA,qEAGiC;AAF/B,8HAAA,qBAAqB,OAAA;AACrB,6IAAA,oCAAoC,OAAA","sourcesContent":["export type {\n AccountTreeControllerState,\n AccountTreeControllerGetStateAction,\n AccountTreeControllerActions,\n AccountTreeControllerStateChangeEvent,\n AccountTreeControllerEvents,\n AccountTreeControllerMessenger,\n AccountWalletObject,\n AccountWalletMetadata,\n AccountGroupObject,\n AccountGroupMetadata,\n} from './AccountTreeController';\nexport {\n AccountTreeController,\n getDefaultAccountTreeControllerState,\n} from './AccountTreeController';\nexport type { AccountTreeWallet } from './AccountTreeWallet';\nexport type { AccountTreeGroup } from './AccountTreeGroup';\n"]}
package/dist/index.d.cts CHANGED
@@ -1,3 +1,5 @@
1
- export type { AccountTreeControllerState, AccountTreeControllerGetStateAction, AccountTreeControllerActions, AccountTreeControllerStateChangeEvent, AccountTreeControllerEvents, AccountTreeControllerMessenger, AccountWallet, AccountWalletId, AccountWalletMetadata, AccountGroup, AccountGroupId, AccountGroupMetadata, } from "./AccountTreeController.cjs";
2
- export { AccountWalletCategory, AccountTreeController, getDefaultAccountTreeControllerState, toAccountGroupId, toAccountWalletId, toDefaultAccountGroupId, DEFAULT_ACCOUNT_GROUP_NAME, DEFAULT_ACCOUNT_GROUP_UNIQUE_ID, } from "./AccountTreeController.cjs";
1
+ export type { AccountTreeControllerState, AccountTreeControllerGetStateAction, AccountTreeControllerActions, AccountTreeControllerStateChangeEvent, AccountTreeControllerEvents, AccountTreeControllerMessenger, AccountWalletObject, AccountWalletMetadata, AccountGroupObject, AccountGroupMetadata, } from "./AccountTreeController.cjs";
2
+ export { AccountTreeController, getDefaultAccountTreeControllerState, } from "./AccountTreeController.cjs";
3
+ export type { AccountTreeWallet } from "./AccountTreeWallet.cjs";
4
+ export type { AccountTreeGroup } from "./AccountTreeGroup.cjs";
3
5
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,aAAa,EACb,eAAe,EACf,qBAAqB,EACrB,YAAY,EACZ,cAAc,EACd,oBAAoB,GACrB,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,qBAAqB,EACrB,oCAAoC,EACpC,gBAAgB,EAChB,iBAAiB,EACjB,uBAAuB,EACvB,0BAA0B,EAC1B,+BAA+B,GAChC,oCAAgC"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,oBAAoB,GACrB,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,oCAAoC,GACrC,oCAAgC;AACjC,YAAY,EAAE,iBAAiB,EAAE,gCAA4B;AAC7D,YAAY,EAAE,gBAAgB,EAAE,+BAA2B"}
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
- export type { AccountTreeControllerState, AccountTreeControllerGetStateAction, AccountTreeControllerActions, AccountTreeControllerStateChangeEvent, AccountTreeControllerEvents, AccountTreeControllerMessenger, AccountWallet, AccountWalletId, AccountWalletMetadata, AccountGroup, AccountGroupId, AccountGroupMetadata, } from "./AccountTreeController.mjs";
2
- export { AccountWalletCategory, AccountTreeController, getDefaultAccountTreeControllerState, toAccountGroupId, toAccountWalletId, toDefaultAccountGroupId, DEFAULT_ACCOUNT_GROUP_NAME, DEFAULT_ACCOUNT_GROUP_UNIQUE_ID, } from "./AccountTreeController.mjs";
1
+ export type { AccountTreeControllerState, AccountTreeControllerGetStateAction, AccountTreeControllerActions, AccountTreeControllerStateChangeEvent, AccountTreeControllerEvents, AccountTreeControllerMessenger, AccountWalletObject, AccountWalletMetadata, AccountGroupObject, AccountGroupMetadata, } from "./AccountTreeController.mjs";
2
+ export { AccountTreeController, getDefaultAccountTreeControllerState, } from "./AccountTreeController.mjs";
3
+ export type { AccountTreeWallet } from "./AccountTreeWallet.mjs";
4
+ export type { AccountTreeGroup } from "./AccountTreeGroup.mjs";
3
5
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,aAAa,EACb,eAAe,EACf,qBAAqB,EACrB,YAAY,EACZ,cAAc,EACd,oBAAoB,GACrB,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,qBAAqB,EACrB,oCAAoC,EACpC,gBAAgB,EAChB,iBAAiB,EACjB,uBAAuB,EACvB,0BAA0B,EAC1B,+BAA+B,GAChC,oCAAgC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,oBAAoB,GACrB,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,oCAAoC,GACrC,oCAAgC;AACjC,YAAY,EAAE,iBAAiB,EAAE,gCAA4B;AAC7D,YAAY,EAAE,gBAAgB,EAAE,+BAA2B"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { AccountWalletCategory, AccountTreeController, getDefaultAccountTreeControllerState, toAccountGroupId, toAccountWalletId, toDefaultAccountGroupId, DEFAULT_ACCOUNT_GROUP_NAME, DEFAULT_ACCOUNT_GROUP_UNIQUE_ID } from "./AccountTreeController.mjs";
1
+ export { AccountTreeController, getDefaultAccountTreeControllerState } from "./AccountTreeController.mjs";
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EACL,qBAAqB,EACrB,qBAAqB,EACrB,oCAAoC,EACpC,gBAAgB,EAChB,iBAAiB,EACjB,uBAAuB,EACvB,0BAA0B,EAC1B,+BAA+B,EAChC,oCAAgC","sourcesContent":["export type {\n AccountTreeControllerState,\n AccountTreeControllerGetStateAction,\n AccountTreeControllerActions,\n AccountTreeControllerStateChangeEvent,\n AccountTreeControllerEvents,\n AccountTreeControllerMessenger,\n AccountWallet,\n AccountWalletId,\n AccountWalletMetadata,\n AccountGroup,\n AccountGroupId,\n AccountGroupMetadata,\n} from './AccountTreeController';\nexport {\n AccountWalletCategory,\n AccountTreeController,\n getDefaultAccountTreeControllerState,\n toAccountGroupId,\n toAccountWalletId,\n toDefaultAccountGroupId,\n DEFAULT_ACCOUNT_GROUP_NAME,\n DEFAULT_ACCOUNT_GROUP_UNIQUE_ID,\n} from './AccountTreeController';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAYA,OAAO,EACL,qBAAqB,EACrB,oCAAoC,EACrC,oCAAgC","sourcesContent":["export type {\n AccountTreeControllerState,\n AccountTreeControllerGetStateAction,\n AccountTreeControllerActions,\n AccountTreeControllerStateChangeEvent,\n AccountTreeControllerEvents,\n AccountTreeControllerMessenger,\n AccountWalletObject,\n AccountWalletMetadata,\n AccountGroupObject,\n AccountGroupMetadata,\n} from './AccountTreeController';\nexport {\n AccountTreeController,\n getDefaultAccountTreeControllerState,\n} from './AccountTreeController';\nexport type { AccountTreeWallet } from './AccountTreeWallet';\nexport type { AccountTreeGroup } from './AccountTreeGroup';\n"]}
@@ -0,0 +1,100 @@
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 _EntropySourceWalletRule_wallets;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.EntropySourceWalletRule = exports.EntropySourceWallet = void 0;
16
+ const account_api_1 = require("@metamask/account-api");
17
+ const keyring_controller_1 = require("@metamask/keyring-controller");
18
+ const utils_1 = require("./utils.cjs");
19
+ const WalletRule_1 = require("./WalletRule.cjs");
20
+ const AccountTreeWallet_1 = require("../AccountTreeWallet.cjs");
21
+ class EntropySourceWallet extends AccountTreeWallet_1.MutableAccountTreeWallet {
22
+ constructor(messenger, entropySource) {
23
+ super(messenger, account_api_1.AccountWalletCategory.Entropy, entropySource);
24
+ this.entropySource = entropySource;
25
+ }
26
+ static toAccountWalletId(entropySource) {
27
+ return (0, account_api_1.toAccountWalletId)(account_api_1.AccountWalletCategory.Entropy, entropySource);
28
+ }
29
+ static getEntropySourceIndex(keyrings, entropySource) {
30
+ return keyrings
31
+ .filter((keyring) => keyring.type === keyring_controller_1.KeyringTypes.hd)
32
+ .findIndex((keyring) => keyring.metadata.id === entropySource);
33
+ }
34
+ getDefaultName() {
35
+ const { keyrings } = this.messenger.call('KeyringController:getState');
36
+ const index = EntropySourceWallet.getEntropySourceIndex(keyrings, this.entropySource);
37
+ if (index === -1) {
38
+ // NOTE: This should never really fail, as we checked for this precondition
39
+ // during rule matching.
40
+ throw new Error('Unable to get index for entropy source');
41
+ }
42
+ return `Wallet ${index + 1}`; // Use human indexing.
43
+ }
44
+ }
45
+ exports.EntropySourceWallet = EntropySourceWallet;
46
+ class EntropySourceWalletRule extends WalletRule_1.BaseWalletRule {
47
+ constructor(messenger) {
48
+ super(messenger);
49
+ _EntropySourceWalletRule_wallets.set(this, void 0);
50
+ __classPrivateFieldSet(this, _EntropySourceWalletRule_wallets, new Map(), "f");
51
+ }
52
+ match(account) {
53
+ let entropySource;
54
+ if ((0, utils_1.hasKeyringType)(account, keyring_controller_1.KeyringTypes.hd)) {
55
+ // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?
56
+ if (!account.options.entropySource) {
57
+ console.warn("! Found an HD account with no entropy source: account won't be associated to its wallet");
58
+ return undefined;
59
+ }
60
+ entropySource = account.options.entropySource;
61
+ }
62
+ // TODO: For now, we're not checking if the Snap is a preinstalled one, and we probably should...
63
+ if ((0, utils_1.hasKeyringType)(account, keyring_controller_1.KeyringTypes.snap) &&
64
+ account.metadata.snap?.enabled) {
65
+ // Not all Snaps have an entropy-source and options are not typed yet, so we have to check manually here.
66
+ if (account.options.entropySource) {
67
+ // We blindly trust the `entropySource` for now, but it could be wrong since it comes from a Snap.
68
+ entropySource = account.options.entropySource;
69
+ }
70
+ }
71
+ if (!entropySource) {
72
+ return undefined;
73
+ }
74
+ // NOTE: We make this check now, so that we are guaranteed that `getDefaultName` will never fail if we
75
+ // pass that point:
76
+ // ------------------------------------------------------------------------------------------------------
77
+ // We check if we can get the name for that entropy source, if not this means this entropy does not match
78
+ // any HD keyrings, thus, is invalid (this account will be grouped by another rule).
79
+ const { keyrings } = this.messenger.call('KeyringController:getState');
80
+ if (EntropySourceWallet.getEntropySourceIndex(keyrings, entropySource) === -1) {
81
+ console.warn('! Tried to name a wallet using an unknown entropy, this should not be possible.');
82
+ return undefined;
83
+ }
84
+ // Check if a wallet already exists for that entropy source.
85
+ let wallet = __classPrivateFieldGet(this, _EntropySourceWalletRule_wallets, "f").get(EntropySourceWallet.toAccountWalletId(entropySource));
86
+ if (!wallet) {
87
+ wallet = new EntropySourceWallet(this.messenger, entropySource);
88
+ __classPrivateFieldGet(this, _EntropySourceWalletRule_wallets, "f").set(wallet.id, wallet);
89
+ }
90
+ // This will automatically creates the group if it's missing.
91
+ const group = wallet.addAccount(account);
92
+ return {
93
+ wallet,
94
+ group,
95
+ };
96
+ }
97
+ }
98
+ exports.EntropySourceWalletRule = EntropySourceWalletRule;
99
+ _EntropySourceWalletRule_wallets = new WeakMap();
100
+ //# sourceMappingURL=EntropySourceWalletRule.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EntropySourceWalletRule.cjs","sourceRoot":"","sources":["../../src/rules/EntropySourceWalletRule.ts"],"names":[],"mappings":"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","sourcesContent":["import type { AccountWalletId } from '@metamask/account-api';\nimport {\n AccountWalletCategory,\n toAccountWalletId,\n} from '@metamask/account-api';\nimport type { KeyringObject } from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { hasKeyringType } from './utils';\nimport type { WalletRuleMatch } from './WalletRule';\nimport { BaseWalletRule } from './WalletRule';\nimport type { AccountTreeControllerMessenger } from '../AccountTreeController';\nimport { MutableAccountTreeWallet } from '../AccountTreeWallet';\n\nexport class EntropySourceWallet extends MutableAccountTreeWallet {\n readonly entropySource: string;\n\n constructor(\n messenger: AccountTreeControllerMessenger,\n entropySource: string,\n ) {\n super(messenger, AccountWalletCategory.Entropy, entropySource);\n this.entropySource = entropySource;\n }\n\n static toAccountWalletId(entropySource: string) {\n return toAccountWalletId(AccountWalletCategory.Entropy, entropySource);\n }\n\n static getEntropySourceIndex(\n keyrings: KeyringObject[],\n entropySource: string,\n ) {\n return keyrings\n .filter((keyring) => keyring.type === (KeyringTypes.hd as string))\n .findIndex((keyring) => keyring.metadata.id === entropySource);\n }\n\n getDefaultName(): string {\n const { keyrings } = this.messenger.call('KeyringController:getState');\n\n const index = EntropySourceWallet.getEntropySourceIndex(\n keyrings,\n this.entropySource,\n );\n if (index === -1) {\n // NOTE: This should never really fail, as we checked for this precondition\n // during rule matching.\n throw new Error('Unable to get index for entropy source');\n }\n\n return `Wallet ${index + 1}`; // Use human indexing.\n }\n}\n\nexport class EntropySourceWalletRule extends BaseWalletRule {\n readonly #wallets: Map<AccountWalletId, EntropySourceWallet>;\n\n constructor(messenger: AccountTreeControllerMessenger) {\n super(messenger);\n\n this.#wallets = new Map();\n }\n\n match(account: InternalAccount): WalletRuleMatch | undefined {\n let entropySource: string | undefined;\n\n if (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 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 // NOTE: We make this check now, so that we are guaranteed that `getDefaultName` will never fail if we\n // pass that point:\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 { keyrings } = this.messenger.call('KeyringController:getState');\n if (\n EntropySourceWallet.getEntropySourceIndex(keyrings, entropySource) === -1\n ) {\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 // Check if a wallet already exists for that entropy source.\n let wallet = this.#wallets.get(\n EntropySourceWallet.toAccountWalletId(entropySource),\n );\n if (!wallet) {\n wallet = new EntropySourceWallet(this.messenger, entropySource);\n this.#wallets.set(wallet.id, wallet);\n }\n\n // This will automatically creates the group if it's missing.\n const group = wallet.addAccount(account);\n\n return {\n wallet,\n group,\n };\n }\n}\n"]}
@@ -0,0 +1,19 @@
1
+ import type { KeyringObject } from "@metamask/keyring-controller";
2
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
3
+ import type { WalletRuleMatch } from "./WalletRule.cjs";
4
+ import { BaseWalletRule } from "./WalletRule.cjs";
5
+ import type { AccountTreeControllerMessenger } from "../AccountTreeController.cjs";
6
+ import { MutableAccountTreeWallet } from "../AccountTreeWallet.cjs";
7
+ export declare class EntropySourceWallet extends MutableAccountTreeWallet {
8
+ readonly entropySource: string;
9
+ constructor(messenger: AccountTreeControllerMessenger, entropySource: string);
10
+ static toAccountWalletId(entropySource: string): `entropy:${string}` | `keyring:${string}` | `snap:${string}`;
11
+ static getEntropySourceIndex(keyrings: KeyringObject[], entropySource: string): number;
12
+ getDefaultName(): string;
13
+ }
14
+ export declare class EntropySourceWalletRule extends BaseWalletRule {
15
+ #private;
16
+ constructor(messenger: AccountTreeControllerMessenger);
17
+ match(account: InternalAccount): WalletRuleMatch | undefined;
18
+ }
19
+ //# sourceMappingURL=EntropySourceWalletRule.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EntropySourceWalletRule.d.cts","sourceRoot":"","sources":["../../src/rules/EntropySourceWalletRule.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,qCAAqC;AAElE,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAGtE,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,EAAE,cAAc,EAAE,yBAAqB;AAC9C,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,EAAE,wBAAwB,EAAE,iCAA6B;AAEhE,qBAAa,mBAAoB,SAAQ,wBAAwB;IAC/D,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;gBAG7B,SAAS,EAAE,8BAA8B,EACzC,aAAa,EAAE,MAAM;IAMvB,MAAM,CAAC,iBAAiB,CAAC,aAAa,EAAE,MAAM;IAI9C,MAAM,CAAC,qBAAqB,CAC1B,QAAQ,EAAE,aAAa,EAAE,EACzB,aAAa,EAAE,MAAM;IAOvB,cAAc,IAAI,MAAM;CAezB;AAED,qBAAa,uBAAwB,SAAQ,cAAc;;gBAG7C,SAAS,EAAE,8BAA8B;IAMrD,KAAK,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe,GAAG,SAAS;CA+D7D"}
@@ -0,0 +1,19 @@
1
+ import type { KeyringObject } from "@metamask/keyring-controller";
2
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
3
+ import type { WalletRuleMatch } from "./WalletRule.mjs";
4
+ import { BaseWalletRule } from "./WalletRule.mjs";
5
+ import type { AccountTreeControllerMessenger } from "../AccountTreeController.mjs";
6
+ import { MutableAccountTreeWallet } from "../AccountTreeWallet.mjs";
7
+ export declare class EntropySourceWallet extends MutableAccountTreeWallet {
8
+ readonly entropySource: string;
9
+ constructor(messenger: AccountTreeControllerMessenger, entropySource: string);
10
+ static toAccountWalletId(entropySource: string): `entropy:${string}` | `keyring:${string}` | `snap:${string}`;
11
+ static getEntropySourceIndex(keyrings: KeyringObject[], entropySource: string): number;
12
+ getDefaultName(): string;
13
+ }
14
+ export declare class EntropySourceWalletRule extends BaseWalletRule {
15
+ #private;
16
+ constructor(messenger: AccountTreeControllerMessenger);
17
+ match(account: InternalAccount): WalletRuleMatch | undefined;
18
+ }
19
+ //# sourceMappingURL=EntropySourceWalletRule.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EntropySourceWalletRule.d.mts","sourceRoot":"","sources":["../../src/rules/EntropySourceWalletRule.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,qCAAqC;AAElE,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAGtE,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,EAAE,cAAc,EAAE,yBAAqB;AAC9C,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,EAAE,wBAAwB,EAAE,iCAA6B;AAEhE,qBAAa,mBAAoB,SAAQ,wBAAwB;IAC/D,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;gBAG7B,SAAS,EAAE,8BAA8B,EACzC,aAAa,EAAE,MAAM;IAMvB,MAAM,CAAC,iBAAiB,CAAC,aAAa,EAAE,MAAM;IAI9C,MAAM,CAAC,qBAAqB,CAC1B,QAAQ,EAAE,aAAa,EAAE,EACzB,aAAa,EAAE,MAAM;IAOvB,cAAc,IAAI,MAAM;CAezB;AAED,qBAAa,uBAAwB,SAAQ,cAAc;;gBAG7C,SAAS,EAAE,8BAA8B;IAMrD,KAAK,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe,GAAG,SAAS;CA+D7D"}
@@ -0,0 +1,95 @@
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 _EntropySourceWalletRule_wallets;
13
+ import { AccountWalletCategory, toAccountWalletId } from "@metamask/account-api";
14
+ import { KeyringTypes } from "@metamask/keyring-controller";
15
+ import { hasKeyringType } from "./utils.mjs";
16
+ import { BaseWalletRule } from "./WalletRule.mjs";
17
+ import { MutableAccountTreeWallet } from "../AccountTreeWallet.mjs";
18
+ export class EntropySourceWallet extends MutableAccountTreeWallet {
19
+ constructor(messenger, entropySource) {
20
+ super(messenger, AccountWalletCategory.Entropy, entropySource);
21
+ this.entropySource = entropySource;
22
+ }
23
+ static toAccountWalletId(entropySource) {
24
+ return toAccountWalletId(AccountWalletCategory.Entropy, entropySource);
25
+ }
26
+ static getEntropySourceIndex(keyrings, entropySource) {
27
+ return keyrings
28
+ .filter((keyring) => keyring.type === KeyringTypes.hd)
29
+ .findIndex((keyring) => keyring.metadata.id === entropySource);
30
+ }
31
+ getDefaultName() {
32
+ const { keyrings } = this.messenger.call('KeyringController:getState');
33
+ const index = EntropySourceWallet.getEntropySourceIndex(keyrings, this.entropySource);
34
+ if (index === -1) {
35
+ // NOTE: This should never really fail, as we checked for this precondition
36
+ // during rule matching.
37
+ throw new Error('Unable to get index for entropy source');
38
+ }
39
+ return `Wallet ${index + 1}`; // Use human indexing.
40
+ }
41
+ }
42
+ export class EntropySourceWalletRule extends BaseWalletRule {
43
+ constructor(messenger) {
44
+ super(messenger);
45
+ _EntropySourceWalletRule_wallets.set(this, void 0);
46
+ __classPrivateFieldSet(this, _EntropySourceWalletRule_wallets, new Map(), "f");
47
+ }
48
+ match(account) {
49
+ let entropySource;
50
+ if (hasKeyringType(account, KeyringTypes.hd)) {
51
+ // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?
52
+ if (!account.options.entropySource) {
53
+ console.warn("! Found an HD account with no entropy source: account won't be associated to its wallet");
54
+ return undefined;
55
+ }
56
+ entropySource = account.options.entropySource;
57
+ }
58
+ // TODO: For now, we're not checking if the Snap is a preinstalled one, and we probably should...
59
+ if (hasKeyringType(account, KeyringTypes.snap) &&
60
+ account.metadata.snap?.enabled) {
61
+ // Not all Snaps have an entropy-source and options are not typed yet, so we have to check manually here.
62
+ if (account.options.entropySource) {
63
+ // We blindly trust the `entropySource` for now, but it could be wrong since it comes from a Snap.
64
+ entropySource = account.options.entropySource;
65
+ }
66
+ }
67
+ if (!entropySource) {
68
+ return undefined;
69
+ }
70
+ // NOTE: We make this check now, so that we are guaranteed that `getDefaultName` will never fail if we
71
+ // pass that point:
72
+ // ------------------------------------------------------------------------------------------------------
73
+ // We check if we can get the name for that entropy source, if not this means this entropy does not match
74
+ // any HD keyrings, thus, is invalid (this account will be grouped by another rule).
75
+ const { keyrings } = this.messenger.call('KeyringController:getState');
76
+ if (EntropySourceWallet.getEntropySourceIndex(keyrings, entropySource) === -1) {
77
+ console.warn('! Tried to name a wallet using an unknown entropy, this should not be possible.');
78
+ return undefined;
79
+ }
80
+ // Check if a wallet already exists for that entropy source.
81
+ let wallet = __classPrivateFieldGet(this, _EntropySourceWalletRule_wallets, "f").get(EntropySourceWallet.toAccountWalletId(entropySource));
82
+ if (!wallet) {
83
+ wallet = new EntropySourceWallet(this.messenger, entropySource);
84
+ __classPrivateFieldGet(this, _EntropySourceWalletRule_wallets, "f").set(wallet.id, wallet);
85
+ }
86
+ // This will automatically creates the group if it's missing.
87
+ const group = wallet.addAccount(account);
88
+ return {
89
+ wallet,
90
+ group,
91
+ };
92
+ }
93
+ }
94
+ _EntropySourceWalletRule_wallets = new WeakMap();
95
+ //# sourceMappingURL=EntropySourceWalletRule.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EntropySourceWalletRule.mjs","sourceRoot":"","sources":["../../src/rules/EntropySourceWalletRule.ts"],"names":[],"mappings":"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","sourcesContent":["import type { AccountWalletId } from '@metamask/account-api';\nimport {\n AccountWalletCategory,\n toAccountWalletId,\n} from '@metamask/account-api';\nimport type { KeyringObject } from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { hasKeyringType } from './utils';\nimport type { WalletRuleMatch } from './WalletRule';\nimport { BaseWalletRule } from './WalletRule';\nimport type { AccountTreeControllerMessenger } from '../AccountTreeController';\nimport { MutableAccountTreeWallet } from '../AccountTreeWallet';\n\nexport class EntropySourceWallet extends MutableAccountTreeWallet {\n readonly entropySource: string;\n\n constructor(\n messenger: AccountTreeControllerMessenger,\n entropySource: string,\n ) {\n super(messenger, AccountWalletCategory.Entropy, entropySource);\n this.entropySource = entropySource;\n }\n\n static toAccountWalletId(entropySource: string) {\n return toAccountWalletId(AccountWalletCategory.Entropy, entropySource);\n }\n\n static getEntropySourceIndex(\n keyrings: KeyringObject[],\n entropySource: string,\n ) {\n return keyrings\n .filter((keyring) => keyring.type === (KeyringTypes.hd as string))\n .findIndex((keyring) => keyring.metadata.id === entropySource);\n }\n\n getDefaultName(): string {\n const { keyrings } = this.messenger.call('KeyringController:getState');\n\n const index = EntropySourceWallet.getEntropySourceIndex(\n keyrings,\n this.entropySource,\n );\n if (index === -1) {\n // NOTE: This should never really fail, as we checked for this precondition\n // during rule matching.\n throw new Error('Unable to get index for entropy source');\n }\n\n return `Wallet ${index + 1}`; // Use human indexing.\n }\n}\n\nexport class EntropySourceWalletRule extends BaseWalletRule {\n readonly #wallets: Map<AccountWalletId, EntropySourceWallet>;\n\n constructor(messenger: AccountTreeControllerMessenger) {\n super(messenger);\n\n this.#wallets = new Map();\n }\n\n match(account: InternalAccount): WalletRuleMatch | undefined {\n let entropySource: string | undefined;\n\n if (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 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 // NOTE: We make this check now, so that we are guaranteed that `getDefaultName` will never fail if we\n // pass that point:\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 { keyrings } = this.messenger.call('KeyringController:getState');\n if (\n EntropySourceWallet.getEntropySourceIndex(keyrings, entropySource) === -1\n ) {\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 // Check if a wallet already exists for that entropy source.\n let wallet = this.#wallets.get(\n EntropySourceWallet.toAccountWalletId(entropySource),\n );\n if (!wallet) {\n wallet = new EntropySourceWallet(this.messenger, entropySource);\n this.#wallets.set(wallet.id, wallet);\n }\n\n // This will automatically creates the group if it's missing.\n const group = wallet.addAccount(account);\n\n return {\n wallet,\n group,\n };\n }\n}\n"]}
@@ -0,0 +1,99 @@
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 _KeyringWalletRule_wallets;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.KeyringWalletRule = exports.getAccountWalletNameFromKeyringType = void 0;
16
+ const account_api_1 = require("@metamask/account-api");
17
+ const keyring_controller_1 = require("@metamask/keyring-controller");
18
+ const WalletRule_1 = require("./WalletRule.cjs");
19
+ const AccountTreeWallet_1 = require("../AccountTreeWallet.cjs");
20
+ /**
21
+ * Get wallet name from a keyring type.
22
+ *
23
+ * @param type - Keyring's type.
24
+ * @returns Wallet name.
25
+ */
26
+ function getAccountWalletNameFromKeyringType(type) {
27
+ switch (type) {
28
+ case keyring_controller_1.KeyringTypes.simple: {
29
+ return 'Imported accounts';
30
+ }
31
+ case keyring_controller_1.KeyringTypes.trezor: {
32
+ return 'Trezor';
33
+ }
34
+ case keyring_controller_1.KeyringTypes.oneKey: {
35
+ return 'OneKey';
36
+ }
37
+ case keyring_controller_1.KeyringTypes.ledger: {
38
+ return 'Ledger';
39
+ }
40
+ case keyring_controller_1.KeyringTypes.lattice: {
41
+ return 'Lattice';
42
+ }
43
+ case keyring_controller_1.KeyringTypes.qr: {
44
+ return 'QR';
45
+ }
46
+ // Those keyrings should never really be used in such context since they
47
+ // should be used by other grouping rules.
48
+ case keyring_controller_1.KeyringTypes.hd: {
49
+ return 'HD Wallet';
50
+ }
51
+ case keyring_controller_1.KeyringTypes.snap: {
52
+ return 'Snap Wallet';
53
+ }
54
+ // ------------------------------------------------------------------------
55
+ default: {
56
+ return 'Unknown';
57
+ }
58
+ }
59
+ }
60
+ exports.getAccountWalletNameFromKeyringType = getAccountWalletNameFromKeyringType;
61
+ class KeyringTypeWallet extends AccountTreeWallet_1.MutableAccountTreeWallet {
62
+ constructor(messenger, type) {
63
+ super(messenger, account_api_1.AccountWalletCategory.Keyring, type);
64
+ this.type = type;
65
+ }
66
+ static toAccountWalletId(type) {
67
+ return (0, account_api_1.toAccountWalletId)(account_api_1.AccountWalletCategory.Keyring, type);
68
+ }
69
+ getDefaultName() {
70
+ return getAccountWalletNameFromKeyringType(this.type);
71
+ }
72
+ }
73
+ class KeyringWalletRule extends WalletRule_1.BaseWalletRule {
74
+ constructor(messenger) {
75
+ super(messenger);
76
+ _KeyringWalletRule_wallets.set(this, void 0);
77
+ __classPrivateFieldSet(this, _KeyringWalletRule_wallets, new Map(), "f");
78
+ }
79
+ match(account) {
80
+ const { type } = account.metadata.keyring;
81
+ // We assume that `type` is really a `KeyringTypes`.
82
+ const keyringType = type;
83
+ // Check if a wallet already exists for that keyring type.
84
+ let wallet = __classPrivateFieldGet(this, _KeyringWalletRule_wallets, "f").get(KeyringTypeWallet.toAccountWalletId(keyringType));
85
+ if (!wallet) {
86
+ wallet = new KeyringTypeWallet(this.messenger, keyringType);
87
+ __classPrivateFieldGet(this, _KeyringWalletRule_wallets, "f").set(wallet.id, wallet);
88
+ }
89
+ // This will automatically creates the group if it's missing.
90
+ const group = wallet.addAccount(account);
91
+ return {
92
+ wallet,
93
+ group,
94
+ };
95
+ }
96
+ }
97
+ exports.KeyringWalletRule = KeyringWalletRule;
98
+ _KeyringWalletRule_wallets = new WeakMap();
99
+ //# sourceMappingURL=KeyringWalletRule.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringWalletRule.cjs","sourceRoot":"","sources":["../../src/rules/KeyringWalletRule.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAG+B;AAC/B,qEAA4D;AAI5D,iDAA8C;AAE9C,gEAAgE;AAEhE;;;;;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;YACpB,OAAO,IAAI,CAAC;SACb;QACD,wEAAwE;QACxE,0CAA0C;QAC1C,KAAK,iCAAY,CAAC,EAAE,CAAC,CAAC;YACpB,OAAO,WAAW,CAAC;SACpB;QACD,KAAK,iCAAY,CAAC,IAAI,CAAC,CAAC;YACtB,OAAO,aAAa,CAAC;SACtB;QACD,2EAA2E;QAC3E,OAAO,CAAC,CAAC;YACP,OAAO,SAAS,CAAC;SAClB;KACF;AACH,CAAC;AAjCD,kFAiCC;AAED,MAAM,iBAAkB,SAAQ,4CAAwB;IAGtD,YAAY,SAAyC,EAAE,IAAkB;QACvE,KAAK,CAAC,SAAS,EAAE,mCAAqB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,MAAM,CAAC,iBAAiB,CAAC,IAAkB;QACzC,OAAO,IAAA,+BAAiB,EAAC,mCAAqB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAChE,CAAC;IAED,cAAc;QACZ,OAAO,mCAAmC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxD,CAAC;CACF;AAED,MAAa,iBAAkB,SAAQ,2BAAc;IAGnD,YAAY,SAAyC;QACnD,KAAK,CAAC,SAAS,CAAC,CAAC;QAHV,6CAAkD;QAKzD,uBAAA,IAAI,8BAAY,IAAI,GAAG,EAAE,MAAA,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,OAAwB;QAC5B,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;QAC1C,oDAAoD;QACpD,MAAM,WAAW,GAAG,IAAoB,CAAC;QAEzC,0DAA0D;QAC1D,IAAI,MAAM,GAAG,uBAAA,IAAI,kCAAS,CAAC,GAAG,CAC5B,iBAAiB,CAAC,iBAAiB,CAAC,WAAW,CAAC,CACjD,CAAC;QACF,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,GAAG,IAAI,iBAAiB,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;YAC5D,uBAAA,IAAI,kCAAS,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;SACtC;QAED,6DAA6D;QAC7D,MAAM,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAEzC,OAAO;YACL,MAAM;YACN,KAAK;SACN,CAAC;IACJ,CAAC;CACF;AA/BD,8CA+BC","sourcesContent":["import type { AccountWalletId } from '@metamask/account-api';\nimport {\n AccountWalletCategory,\n toAccountWalletId,\n} from '@metamask/account-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { WalletRuleMatch } from './WalletRule';\nimport { BaseWalletRule } from './WalletRule';\nimport type { AccountTreeControllerMessenger } from '../AccountTreeController';\nimport { MutableAccountTreeWallet } from '../AccountTreeWallet';\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\nclass KeyringTypeWallet extends MutableAccountTreeWallet {\n readonly type: KeyringTypes;\n\n constructor(messenger: AccountTreeControllerMessenger, type: KeyringTypes) {\n super(messenger, AccountWalletCategory.Keyring, type);\n this.type = type;\n }\n\n static toAccountWalletId(type: KeyringTypes) {\n return toAccountWalletId(AccountWalletCategory.Keyring, type);\n }\n\n getDefaultName(): string {\n return getAccountWalletNameFromKeyringType(this.type);\n }\n}\n\nexport class KeyringWalletRule extends BaseWalletRule {\n readonly #wallets: Map<AccountWalletId, KeyringTypeWallet>;\n\n constructor(messenger: AccountTreeControllerMessenger) {\n super(messenger);\n\n this.#wallets = new Map();\n }\n\n match(account: InternalAccount): WalletRuleMatch | undefined {\n const { type } = account.metadata.keyring;\n // We assume that `type` is really a `KeyringTypes`.\n const keyringType = type as KeyringTypes;\n\n // Check if a wallet already exists for that keyring type.\n let wallet = this.#wallets.get(\n KeyringTypeWallet.toAccountWalletId(keyringType),\n );\n if (!wallet) {\n wallet = new KeyringTypeWallet(this.messenger, keyringType);\n this.#wallets.set(wallet.id, wallet);\n }\n\n // This will automatically creates the group if it's missing.\n const group = wallet.addAccount(account);\n\n return {\n wallet,\n group,\n };\n }\n}\n"]}
@@ -0,0 +1,18 @@
1
+ import { KeyringTypes } from "@metamask/keyring-controller";
2
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
3
+ import type { WalletRuleMatch } from "./WalletRule.cjs";
4
+ import { BaseWalletRule } from "./WalletRule.cjs";
5
+ import type { AccountTreeControllerMessenger } from "../AccountTreeController.cjs";
6
+ /**
7
+ * Get wallet name from a keyring type.
8
+ *
9
+ * @param type - Keyring's type.
10
+ * @returns Wallet name.
11
+ */
12
+ export declare function getAccountWalletNameFromKeyringType(type: KeyringTypes): "Imported accounts" | "Trezor" | "OneKey" | "Ledger" | "Lattice" | "QR" | "HD Wallet" | "Snap Wallet" | "Unknown";
13
+ export declare class KeyringWalletRule extends BaseWalletRule {
14
+ #private;
15
+ constructor(messenger: AccountTreeControllerMessenger);
16
+ match(account: InternalAccount): WalletRuleMatch | undefined;
17
+ }
18
+ //# sourceMappingURL=KeyringWalletRule.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringWalletRule.d.cts","sourceRoot":"","sources":["../../src/rules/KeyringWalletRule.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,EAAE,cAAc,EAAE,yBAAqB;AAC9C,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAG/E;;;;;GAKG;AACH,wBAAgB,mCAAmC,CAAC,IAAI,EAAE,YAAY,qHAiCrE;AAmBD,qBAAa,iBAAkB,SAAQ,cAAc;;gBAGvC,SAAS,EAAE,8BAA8B;IAMrD,KAAK,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe,GAAG,SAAS;CAsB7D"}
@@ -0,0 +1,18 @@
1
+ import { KeyringTypes } from "@metamask/keyring-controller";
2
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
3
+ import type { WalletRuleMatch } from "./WalletRule.mjs";
4
+ import { BaseWalletRule } from "./WalletRule.mjs";
5
+ import type { AccountTreeControllerMessenger } from "../AccountTreeController.mjs";
6
+ /**
7
+ * Get wallet name from a keyring type.
8
+ *
9
+ * @param type - Keyring's type.
10
+ * @returns Wallet name.
11
+ */
12
+ export declare function getAccountWalletNameFromKeyringType(type: KeyringTypes): "Imported accounts" | "Trezor" | "OneKey" | "Ledger" | "Lattice" | "QR" | "HD Wallet" | "Snap Wallet" | "Unknown";
13
+ export declare class KeyringWalletRule extends BaseWalletRule {
14
+ #private;
15
+ constructor(messenger: AccountTreeControllerMessenger);
16
+ match(account: InternalAccount): WalletRuleMatch | undefined;
17
+ }
18
+ //# sourceMappingURL=KeyringWalletRule.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringWalletRule.d.mts","sourceRoot":"","sources":["../../src/rules/KeyringWalletRule.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,eAAe,EAAE,yBAAqB;AACpD,OAAO,EAAE,cAAc,EAAE,yBAAqB;AAC9C,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAG/E;;;;;GAKG;AACH,wBAAgB,mCAAmC,CAAC,IAAI,EAAE,YAAY,qHAiCrE;AAmBD,qBAAa,iBAAkB,SAAQ,cAAc;;gBAGvC,SAAS,EAAE,8BAA8B;IAMrD,KAAK,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe,GAAG,SAAS;CAsB7D"}