@metamask-previews/account-tree-controller 7.6.1-preview-58a4a8b71 → 7.6.1-preview-4c8a5dfcb

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 (70) hide show
  1. package/CHANGELOG.md +1 -5
  2. package/dist/AccountTreeController-method-action-types.cjs.map +1 -1
  3. package/dist/AccountTreeController-method-action-types.d.cts +1 -34
  4. package/dist/AccountTreeController-method-action-types.d.cts.map +1 -1
  5. package/dist/AccountTreeController-method-action-types.d.mts +1 -34
  6. package/dist/AccountTreeController-method-action-types.d.mts.map +1 -1
  7. package/dist/AccountTreeController-method-action-types.mjs.map +1 -1
  8. package/dist/AccountTreeController.cjs +0 -42
  9. package/dist/AccountTreeController.cjs.map +1 -1
  10. package/dist/AccountTreeController.d.cts +0 -29
  11. package/dist/AccountTreeController.d.cts.map +1 -1
  12. package/dist/AccountTreeController.d.mts +0 -29
  13. package/dist/AccountTreeController.d.mts.map +1 -1
  14. package/dist/AccountTreeController.mjs +0 -42
  15. package/dist/AccountTreeController.mjs.map +1 -1
  16. package/dist/index.cjs +1 -10
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +1 -6
  19. package/dist/index.d.cts.map +1 -1
  20. package/dist/index.d.mts +1 -6
  21. package/dist/index.d.mts.map +1 -1
  22. package/dist/index.mjs +0 -3
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/types.cjs.map +1 -1
  25. package/dist/types.d.cts +3 -3
  26. package/dist/types.d.cts.map +1 -1
  27. package/dist/types.d.mts +3 -3
  28. package/dist/types.d.mts.map +1 -1
  29. package/dist/types.mjs.map +1 -1
  30. package/package.json +1 -3
  31. package/dist/state/export.cjs +0 -210
  32. package/dist/state/export.cjs.map +0 -1
  33. package/dist/state/export.d.cts +0 -40
  34. package/dist/state/export.d.cts.map +0 -1
  35. package/dist/state/export.d.mts +0 -40
  36. package/dist/state/export.d.mts.map +0 -1
  37. package/dist/state/export.mjs +0 -204
  38. package/dist/state/export.mjs.map +0 -1
  39. package/dist/state/id-map.cjs +0 -59
  40. package/dist/state/id-map.cjs.map +0 -1
  41. package/dist/state/id-map.d.cts +0 -41
  42. package/dist/state/id-map.d.cts.map +0 -1
  43. package/dist/state/id-map.d.mts +0 -41
  44. package/dist/state/id-map.d.mts.map +0 -1
  45. package/dist/state/id-map.mjs +0 -55
  46. package/dist/state/id-map.mjs.map +0 -1
  47. package/dist/state/import.cjs +0 -233
  48. package/dist/state/import.cjs.map +0 -1
  49. package/dist/state/import.d.cts +0 -29
  50. package/dist/state/import.d.cts.map +0 -1
  51. package/dist/state/import.d.mts +0 -29
  52. package/dist/state/import.d.mts.map +0 -1
  53. package/dist/state/import.mjs +0 -229
  54. package/dist/state/import.mjs.map +0 -1
  55. package/dist/state/payload.cjs +0 -159
  56. package/dist/state/payload.cjs.map +0 -1
  57. package/dist/state/payload.d.cts +0 -287
  58. package/dist/state/payload.d.cts.map +0 -1
  59. package/dist/state/payload.d.mts +0 -287
  60. package/dist/state/payload.d.mts.map +0 -1
  61. package/dist/state/payload.mjs +0 -152
  62. package/dist/state/payload.mjs.map +0 -1
  63. package/dist/state/snapshot.cjs +0 -203
  64. package/dist/state/snapshot.cjs.map +0 -1
  65. package/dist/state/snapshot.d.cts +0 -111
  66. package/dist/state/snapshot.d.cts.map +0 -1
  67. package/dist/state/snapshot.d.mts +0 -111
  68. package/dist/state/snapshot.d.mts.map +0 -1
  69. package/dist/state/snapshot.mjs +0 -199
  70. package/dist/state/snapshot.mjs.map +0 -1
@@ -1,203 +0,0 @@
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 _AccountTreeSnapshot_entries, _AccountTreeSnapshot_idMap;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.AccountTreeSnapshot = void 0;
16
- const payload_js_1 = require("./payload.cjs");
17
- /**
18
- * Recursively freezes a value and its nested properties.
19
- *
20
- * @param value - Value to freeze.
21
- * @returns The frozen value.
22
- */
23
- function deepFreeze(value) {
24
- if (value === null || typeof value !== 'object') {
25
- return value;
26
- }
27
- Object.freeze(value);
28
- for (const nested of Object.values(value)) {
29
- deepFreeze(nested);
30
- }
31
- return value;
32
- }
33
- /**
34
- * Immutable value object returned by {@link AccountTreeController.exportState}.
35
- *
36
- * Construct with {@link AccountTreeController.exportState},
37
- * {@link AccountTreeSnapshot.deserialize}, or `new AccountTreeSnapshot(...)`
38
- * for tests and advanced use.
39
- *
40
- * Wallet and group entries are deep-cloned and deep-frozen once in the
41
- * constructor. Filtering predicates receive those read-only views directly;
42
- * each filter method returns a new snapshot that repeats the process for its
43
- * result.
44
- *
45
- * An optional ID map (local ↔ payload) may be supplied when bridging between
46
- * internal controller IDs and the stable cross-device IDs in the serialized
47
- * payload. The map covers the original export and is preserved unchanged
48
- * through filtering until {@link serialize}. Omit it when deterministic IDs
49
- * make {@link toLocalId} / {@link toPayloadId} unnecessary.
50
- */
51
- class AccountTreeSnapshot {
52
- /**
53
- * @param entries - Wallet entries in the snapshot.
54
- * @param idMap - Optional local ↔ payload ID map from export.
55
- */
56
- constructor(entries, idMap) {
57
- _AccountTreeSnapshot_entries.set(this, void 0);
58
- _AccountTreeSnapshot_idMap.set(this, void 0);
59
- __classPrivateFieldSet(this, _AccountTreeSnapshot_entries, deepFreeze(structuredClone(entries)), "f");
60
- __classPrivateFieldSet(this, _AccountTreeSnapshot_idMap, idMap, "f");
61
- }
62
- /**
63
- * Returns a new snapshot containing only the wallets for which
64
- * `predicate` returns `true`.
65
- *
66
- * When filtering by wallet ID, compare against stable payload IDs from
67
- * {@link serialize} or convert local IDs with {@link toPayloadId} first.
68
- *
69
- * @param predicate - Function called with each deeply read-only wallet entry.
70
- * @returns A filtered snapshot.
71
- */
72
- filterWallets(predicate) {
73
- const filteredEntries = __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f").filter((entry) => predicate(entry));
74
- return new AccountTreeSnapshot(filteredEntries, __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f"));
75
- }
76
- /**
77
- * Filters groups within one wallet. Other wallets are left unchanged.
78
- *
79
- * Throws if `walletId` does not identify a wallet in the snapshot.
80
- * Removes the wallet if no groups remain after filtering — this prevents a
81
- * mnemonic wallet with zero selected groups from still transferring its secret.
82
- *
83
- * @param walletId - Stable payload wallet ID to filter groups within.
84
- * @param predicate - Function called with each deeply read-only group entry.
85
- * @returns A filtered snapshot.
86
- * @throws If `walletId` is not present in the snapshot.
87
- */
88
- filterGroups(walletId, predicate) {
89
- const walletIndex = __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f").findIndex((entry) => entry.id === walletId);
90
- if (walletIndex === -1) {
91
- throw new Error(`Cannot filter groups: wallet "${walletId}" not found in snapshot`);
92
- }
93
- const wallet = __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f")[walletIndex];
94
- const filteredGroups = wallet.groups.filter((group) => predicate(group));
95
- const filteredEntries = [...__classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f")];
96
- if (filteredGroups.length === 0) {
97
- filteredEntries.splice(walletIndex, 1);
98
- }
99
- else if (wallet.type === 'mnemonic') {
100
- filteredEntries[walletIndex] = {
101
- ...wallet,
102
- groups: filteredGroups,
103
- };
104
- }
105
- else {
106
- filteredEntries[walletIndex] = {
107
- ...wallet,
108
- groups: filteredGroups,
109
- };
110
- }
111
- return new AccountTreeSnapshot(filteredEntries, __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f"));
112
- }
113
- /**
114
- * Filters groups across every wallet.
115
- *
116
- * The parent wallet is provided as context to the predicate. Removes any
117
- * wallet with no remaining groups after filtering.
118
- *
119
- * @param predicate - Function called with each group and its parent wallet.
120
- * @returns A filtered snapshot.
121
- */
122
- filterAllGroups(predicate) {
123
- const filteredEntries = [];
124
- for (const wallet of __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f")) {
125
- const filteredGroups = wallet.groups.filter((group) => predicate(group, wallet));
126
- if (filteredGroups.length === 0) {
127
- continue;
128
- }
129
- if (wallet.type === 'mnemonic') {
130
- filteredEntries.push({
131
- ...wallet,
132
- groups: filteredGroups,
133
- });
134
- }
135
- else {
136
- filteredEntries.push({
137
- ...wallet,
138
- groups: filteredGroups,
139
- });
140
- }
141
- }
142
- return new AccountTreeSnapshot(filteredEntries, __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f"));
143
- }
144
- /**
145
- * Converts a payload ID (wallet or group) to the corresponding local
146
- * `AccountTreeController` ID.
147
- *
148
- * The map reflects the original export, not the wallets/groups currently
149
- * retained in this snapshot after filtering.
150
- *
151
- * @param payloadId - Stable cross-device wallet or group payload ID.
152
- * @returns The local controller ID, or `undefined` if not found or no ID map is present.
153
- */
154
- toLocalId(payloadId) {
155
- return __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f")?.getLocalId(payloadId);
156
- }
157
- /**
158
- * Converts a local `AccountTreeController` ID (wallet or group) to its
159
- * stable cross-device payload ID.
160
- *
161
- * The map reflects the original export, not the wallets/groups currently
162
- * retained in this snapshot after filtering.
163
- *
164
- * @param localId - Local controller wallet or group ID.
165
- * @returns The payload ID, or `undefined` if not found or no ID map is present.
166
- */
167
- toPayloadId(localId) {
168
- return __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f")?.getPayloadId(localId);
169
- }
170
- /**
171
- * Serializes the snapshot to a versioned state envelope wrapping the {@link AccountTreePayload}.
172
- *
173
- * Returns the constructor-frozen wallet tree without copying it again.
174
- *
175
- * @returns The versioned payload envelope.
176
- */
177
- serialize() {
178
- return { version: payload_js_1.migrations.version, data: { wallets: __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f") } };
179
- }
180
- /**
181
- * Validates a raw value as an {@link AccountTreePayload}, running any
182
- * necessary version migrations, and returns an immutable snapshot.
183
- *
184
- * This is the entry point for untrusted serialized data. Unsupported schema
185
- * versions and wallet types fail closed with an error instead of returning a
186
- * partial snapshot.
187
- *
188
- * The returned snapshot has no ID map — {@link toLocalId} / {@link toPayloadId}
189
- * return `undefined`. Pass an {@link IdMap} to the constructor when you need
190
- * the map.
191
- *
192
- * @param raw - Unknown value to parse.
193
- * @returns A validated snapshot.
194
- * @throws If `raw` is not a valid payload or its version is unsupported.
195
- */
196
- static async deserialize(raw) {
197
- const payload = await (0, payload_js_1.migrate)(raw);
198
- return new AccountTreeSnapshot(payload.wallets);
199
- }
200
- }
201
- exports.AccountTreeSnapshot = AccountTreeSnapshot;
202
- _AccountTreeSnapshot_entries = new WeakMap(), _AccountTreeSnapshot_idMap = new WeakMap();
203
- //# sourceMappingURL=snapshot.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"snapshot.cjs","sourceRoot":"","sources":["../../src/state/snapshot.ts"],"names":[],"mappings":"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","sourcesContent":["import type { VersionedState } from '@metamask/keyring-sdk';\n\nimport type { IdMap } from './id-map.js';\nimport type {\n AccountGroupPayloadId,\n AccountTreePayload,\n AccountTreeSnapshotGroup,\n AccountTreeSnapshotWallet,\n AccountTreeWalletEntry,\n AccountWalletMnemonicGroupEntry,\n AccountWalletMnemonicPayload,\n AccountWalletPayloadId,\n AccountWalletPrivateKeyGroupEntry,\n AccountWalletPrivateKeyPayload,\n} from './payload.js';\nimport { migrations, migrate } from './payload.js';\n\n/**\n * Recursively freezes a value and its nested properties.\n *\n * @param value - Value to freeze.\n * @returns The frozen value.\n */\nfunction deepFreeze<T>(value: T): T {\n if (value === null || typeof value !== 'object') {\n return value;\n }\n\n Object.freeze(value);\n\n for (const nested of Object.values(value)) {\n deepFreeze(nested);\n }\n\n return value;\n}\n\n/**\n * Immutable value object returned by {@link AccountTreeController.exportState}.\n *\n * Construct with {@link AccountTreeController.exportState},\n * {@link AccountTreeSnapshot.deserialize}, or `new AccountTreeSnapshot(...)`\n * for tests and advanced use.\n *\n * Wallet and group entries are deep-cloned and deep-frozen once in the\n * constructor. Filtering predicates receive those read-only views directly;\n * each filter method returns a new snapshot that repeats the process for its\n * result.\n *\n * An optional ID map (local ↔ payload) may be supplied when bridging between\n * internal controller IDs and the stable cross-device IDs in the serialized\n * payload. The map covers the original export and is preserved unchanged\n * through filtering until {@link serialize}. Omit it when deterministic IDs\n * make {@link toLocalId} / {@link toPayloadId} unnecessary.\n */\nexport class AccountTreeSnapshot {\n readonly #entries: AccountTreeWalletEntry[];\n\n readonly #idMap: IdMap | undefined;\n\n /**\n * @param entries - Wallet entries in the snapshot.\n * @param idMap - Optional local ↔ payload ID map from export.\n */\n constructor(entries: AccountTreeWalletEntry[], idMap?: IdMap) {\n this.#entries = deepFreeze(structuredClone(entries));\n this.#idMap = idMap;\n }\n\n /**\n * Returns a new snapshot containing only the wallets for which\n * `predicate` returns `true`.\n *\n * When filtering by wallet ID, compare against stable payload IDs from\n * {@link serialize} or convert local IDs with {@link toPayloadId} first.\n *\n * @param predicate - Function called with each deeply read-only wallet entry.\n * @returns A filtered snapshot.\n */\n filterWallets(\n predicate: (wallet: AccountTreeSnapshotWallet) => boolean,\n ): AccountTreeSnapshot {\n const filteredEntries = this.#entries.filter((entry) =>\n predicate(entry as AccountTreeSnapshotWallet),\n );\n\n return new AccountTreeSnapshot(filteredEntries, this.#idMap);\n }\n\n /**\n * Filters groups within one wallet. Other wallets are left unchanged.\n *\n * Throws if `walletId` does not identify a wallet in the snapshot.\n * Removes the wallet if no groups remain after filtering — this prevents a\n * mnemonic wallet with zero selected groups from still transferring its secret.\n *\n * @param walletId - Stable payload wallet ID to filter groups within.\n * @param predicate - Function called with each deeply read-only group entry.\n * @returns A filtered snapshot.\n * @throws If `walletId` is not present in the snapshot.\n */\n filterGroups(\n walletId: AccountWalletPayloadId,\n predicate: (group: AccountTreeSnapshotGroup) => boolean,\n ): AccountTreeSnapshot {\n const walletIndex = this.#entries.findIndex(\n (entry) => entry.id === walletId,\n );\n if (walletIndex === -1) {\n throw new Error(\n `Cannot filter groups: wallet \"${walletId}\" not found in snapshot`,\n );\n }\n\n const wallet = this.#entries[walletIndex];\n\n const filteredGroups = wallet.groups.filter((group) =>\n predicate(group as AccountTreeSnapshotGroup),\n );\n\n const filteredEntries = [...this.#entries];\n if (filteredGroups.length === 0) {\n filteredEntries.splice(walletIndex, 1);\n } else if (wallet.type === 'mnemonic') {\n filteredEntries[walletIndex] = {\n ...wallet,\n groups: filteredGroups as AccountWalletMnemonicGroupEntry[],\n };\n } else {\n filteredEntries[walletIndex] = {\n ...wallet,\n groups: filteredGroups as AccountWalletPrivateKeyGroupEntry[],\n };\n }\n\n return new AccountTreeSnapshot(filteredEntries, this.#idMap);\n }\n\n /**\n * Filters groups across every wallet.\n *\n * The parent wallet is provided as context to the predicate. Removes any\n * wallet with no remaining groups after filtering.\n *\n * @param predicate - Function called with each group and its parent wallet.\n * @returns A filtered snapshot.\n */\n filterAllGroups(\n predicate: (\n group: AccountTreeSnapshotGroup,\n wallet: AccountTreeSnapshotWallet,\n ) => boolean,\n ): AccountTreeSnapshot {\n const filteredEntries: AccountTreeWalletEntry[] = [];\n\n for (const wallet of this.#entries) {\n const filteredGroups = wallet.groups.filter((group) =>\n predicate(\n group as AccountTreeSnapshotGroup,\n wallet as AccountTreeSnapshotWallet,\n ),\n );\n\n if (filteredGroups.length === 0) {\n continue;\n }\n\n if (wallet.type === 'mnemonic') {\n filteredEntries.push({\n ...wallet,\n groups: filteredGroups as AccountWalletMnemonicGroupEntry[],\n });\n } else {\n filteredEntries.push({\n ...wallet,\n groups: filteredGroups as AccountWalletPrivateKeyGroupEntry[],\n });\n }\n }\n\n return new AccountTreeSnapshot(filteredEntries, this.#idMap);\n }\n\n /**\n * Converts a payload ID (wallet or group) to the corresponding local\n * `AccountTreeController` ID.\n *\n * The map reflects the original export, not the wallets/groups currently\n * retained in this snapshot after filtering.\n *\n * @param payloadId - Stable cross-device wallet or group payload ID.\n * @returns The local controller ID, or `undefined` if not found or no ID map is present.\n */\n toLocalId(\n payloadId: AccountWalletPayloadId | AccountGroupPayloadId,\n ): ReturnType<IdMap['getLocalId']> {\n return this.#idMap?.getLocalId(payloadId);\n }\n\n /**\n * Converts a local `AccountTreeController` ID (wallet or group) to its\n * stable cross-device payload ID.\n *\n * The map reflects the original export, not the wallets/groups currently\n * retained in this snapshot after filtering.\n *\n * @param localId - Local controller wallet or group ID.\n * @returns The payload ID, or `undefined` if not found or no ID map is present.\n */\n toPayloadId(\n localId: Parameters<IdMap['add']>[0],\n ): ReturnType<IdMap['getPayloadId']> {\n return this.#idMap?.getPayloadId(localId);\n }\n\n /**\n * Serializes the snapshot to a versioned state envelope wrapping the {@link AccountTreePayload}.\n *\n * Returns the constructor-frozen wallet tree without copying it again.\n *\n * @returns The versioned payload envelope.\n */\n serialize(): VersionedState<AccountTreePayload> {\n return { version: migrations.version, data: { wallets: this.#entries } };\n }\n\n /**\n * Validates a raw value as an {@link AccountTreePayload}, running any\n * necessary version migrations, and returns an immutable snapshot.\n *\n * This is the entry point for untrusted serialized data. Unsupported schema\n * versions and wallet types fail closed with an error instead of returning a\n * partial snapshot.\n *\n * The returned snapshot has no ID map — {@link toLocalId} / {@link toPayloadId}\n * return `undefined`. Pass an {@link IdMap} to the constructor when you need\n * the map.\n *\n * @param raw - Unknown value to parse.\n * @returns A validated snapshot.\n * @throws If `raw` is not a valid payload or its version is unsupported.\n */\n static async deserialize(raw: unknown): Promise<AccountTreeSnapshot> {\n const payload = await migrate(raw);\n return new AccountTreeSnapshot(payload.wallets);\n }\n}\n"]}
@@ -1,111 +0,0 @@
1
- import type { VersionedState } from "@metamask/keyring-sdk";
2
- import type { IdMap } from "./id-map.cjs";
3
- import type { AccountGroupPayloadId, AccountTreePayload, AccountTreeSnapshotGroup, AccountTreeSnapshotWallet, AccountTreeWalletEntry, AccountWalletPayloadId } from "./payload.cjs";
4
- /**
5
- * Immutable value object returned by {@link AccountTreeController.exportState}.
6
- *
7
- * Construct with {@link AccountTreeController.exportState},
8
- * {@link AccountTreeSnapshot.deserialize}, or `new AccountTreeSnapshot(...)`
9
- * for tests and advanced use.
10
- *
11
- * Wallet and group entries are deep-cloned and deep-frozen once in the
12
- * constructor. Filtering predicates receive those read-only views directly;
13
- * each filter method returns a new snapshot that repeats the process for its
14
- * result.
15
- *
16
- * An optional ID map (local ↔ payload) may be supplied when bridging between
17
- * internal controller IDs and the stable cross-device IDs in the serialized
18
- * payload. The map covers the original export and is preserved unchanged
19
- * through filtering until {@link serialize}. Omit it when deterministic IDs
20
- * make {@link toLocalId} / {@link toPayloadId} unnecessary.
21
- */
22
- export declare class AccountTreeSnapshot {
23
- #private;
24
- /**
25
- * @param entries - Wallet entries in the snapshot.
26
- * @param idMap - Optional local ↔ payload ID map from export.
27
- */
28
- constructor(entries: AccountTreeWalletEntry[], idMap?: IdMap);
29
- /**
30
- * Returns a new snapshot containing only the wallets for which
31
- * `predicate` returns `true`.
32
- *
33
- * When filtering by wallet ID, compare against stable payload IDs from
34
- * {@link serialize} or convert local IDs with {@link toPayloadId} first.
35
- *
36
- * @param predicate - Function called with each deeply read-only wallet entry.
37
- * @returns A filtered snapshot.
38
- */
39
- filterWallets(predicate: (wallet: AccountTreeSnapshotWallet) => boolean): AccountTreeSnapshot;
40
- /**
41
- * Filters groups within one wallet. Other wallets are left unchanged.
42
- *
43
- * Throws if `walletId` does not identify a wallet in the snapshot.
44
- * Removes the wallet if no groups remain after filtering — this prevents a
45
- * mnemonic wallet with zero selected groups from still transferring its secret.
46
- *
47
- * @param walletId - Stable payload wallet ID to filter groups within.
48
- * @param predicate - Function called with each deeply read-only group entry.
49
- * @returns A filtered snapshot.
50
- * @throws If `walletId` is not present in the snapshot.
51
- */
52
- filterGroups(walletId: AccountWalletPayloadId, predicate: (group: AccountTreeSnapshotGroup) => boolean): AccountTreeSnapshot;
53
- /**
54
- * Filters groups across every wallet.
55
- *
56
- * The parent wallet is provided as context to the predicate. Removes any
57
- * wallet with no remaining groups after filtering.
58
- *
59
- * @param predicate - Function called with each group and its parent wallet.
60
- * @returns A filtered snapshot.
61
- */
62
- filterAllGroups(predicate: (group: AccountTreeSnapshotGroup, wallet: AccountTreeSnapshotWallet) => boolean): AccountTreeSnapshot;
63
- /**
64
- * Converts a payload ID (wallet or group) to the corresponding local
65
- * `AccountTreeController` ID.
66
- *
67
- * The map reflects the original export, not the wallets/groups currently
68
- * retained in this snapshot after filtering.
69
- *
70
- * @param payloadId - Stable cross-device wallet or group payload ID.
71
- * @returns The local controller ID, or `undefined` if not found or no ID map is present.
72
- */
73
- toLocalId(payloadId: AccountWalletPayloadId | AccountGroupPayloadId): ReturnType<IdMap['getLocalId']>;
74
- /**
75
- * Converts a local `AccountTreeController` ID (wallet or group) to its
76
- * stable cross-device payload ID.
77
- *
78
- * The map reflects the original export, not the wallets/groups currently
79
- * retained in this snapshot after filtering.
80
- *
81
- * @param localId - Local controller wallet or group ID.
82
- * @returns The payload ID, or `undefined` if not found or no ID map is present.
83
- */
84
- toPayloadId(localId: Parameters<IdMap['add']>[0]): ReturnType<IdMap['getPayloadId']>;
85
- /**
86
- * Serializes the snapshot to a versioned state envelope wrapping the {@link AccountTreePayload}.
87
- *
88
- * Returns the constructor-frozen wallet tree without copying it again.
89
- *
90
- * @returns The versioned payload envelope.
91
- */
92
- serialize(): VersionedState<AccountTreePayload>;
93
- /**
94
- * Validates a raw value as an {@link AccountTreePayload}, running any
95
- * necessary version migrations, and returns an immutable snapshot.
96
- *
97
- * This is the entry point for untrusted serialized data. Unsupported schema
98
- * versions and wallet types fail closed with an error instead of returning a
99
- * partial snapshot.
100
- *
101
- * The returned snapshot has no ID map — {@link toLocalId} / {@link toPayloadId}
102
- * return `undefined`. Pass an {@link IdMap} to the constructor when you need
103
- * the map.
104
- *
105
- * @param raw - Unknown value to parse.
106
- * @returns A validated snapshot.
107
- * @throws If `raw` is not a valid payload or its version is unsupported.
108
- */
109
- static deserialize(raw: unknown): Promise<AccountTreeSnapshot>;
110
- }
111
- //# sourceMappingURL=snapshot.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"snapshot.d.cts","sourceRoot":"","sources":["../../src/state/snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA8B;AAE5D,OAAO,KAAK,EAAE,KAAK,EAAE,qBAAoB;AACzC,OAAO,KAAK,EACV,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,EACxB,yBAAyB,EACzB,sBAAsB,EAGtB,sBAAsB,EAGvB,sBAAqB;AAuBtB;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,mBAAmB;;IAK9B;;;OAGG;gBACS,OAAO,EAAE,sBAAsB,EAAE,EAAE,KAAK,CAAC,EAAE,KAAK;IAK5D;;;;;;;;;OASG;IACH,aAAa,CACX,SAAS,EAAE,CAAC,MAAM,EAAE,yBAAyB,KAAK,OAAO,GACxD,mBAAmB;IAQtB;;;;;;;;;;;OAWG;IACH,YAAY,CACV,QAAQ,EAAE,sBAAsB,EAChC,SAAS,EAAE,CAAC,KAAK,EAAE,wBAAwB,KAAK,OAAO,GACtD,mBAAmB;IAkCtB;;;;;;;;OAQG;IACH,eAAe,CACb,SAAS,EAAE,CACT,KAAK,EAAE,wBAAwB,EAC/B,MAAM,EAAE,yBAAyB,KAC9B,OAAO,GACX,mBAAmB;IA+BtB;;;;;;;;;OASG;IACH,SAAS,CACP,SAAS,EAAE,sBAAsB,GAAG,qBAAqB,GACxD,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAIlC;;;;;;;;;OASG;IACH,WAAW,CACT,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GACnC,UAAU,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;IAIpC;;;;;;OAMG;IACH,SAAS,IAAI,cAAc,CAAC,kBAAkB,CAAC;IAI/C;;;;;;;;;;;;;;;OAeG;WACU,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,mBAAmB,CAAC;CAIrE"}
@@ -1,111 +0,0 @@
1
- import type { VersionedState } from "@metamask/keyring-sdk";
2
- import type { IdMap } from "./id-map.mjs";
3
- import type { AccountGroupPayloadId, AccountTreePayload, AccountTreeSnapshotGroup, AccountTreeSnapshotWallet, AccountTreeWalletEntry, AccountWalletPayloadId } from "./payload.mjs";
4
- /**
5
- * Immutable value object returned by {@link AccountTreeController.exportState}.
6
- *
7
- * Construct with {@link AccountTreeController.exportState},
8
- * {@link AccountTreeSnapshot.deserialize}, or `new AccountTreeSnapshot(...)`
9
- * for tests and advanced use.
10
- *
11
- * Wallet and group entries are deep-cloned and deep-frozen once in the
12
- * constructor. Filtering predicates receive those read-only views directly;
13
- * each filter method returns a new snapshot that repeats the process for its
14
- * result.
15
- *
16
- * An optional ID map (local ↔ payload) may be supplied when bridging between
17
- * internal controller IDs and the stable cross-device IDs in the serialized
18
- * payload. The map covers the original export and is preserved unchanged
19
- * through filtering until {@link serialize}. Omit it when deterministic IDs
20
- * make {@link toLocalId} / {@link toPayloadId} unnecessary.
21
- */
22
- export declare class AccountTreeSnapshot {
23
- #private;
24
- /**
25
- * @param entries - Wallet entries in the snapshot.
26
- * @param idMap - Optional local ↔ payload ID map from export.
27
- */
28
- constructor(entries: AccountTreeWalletEntry[], idMap?: IdMap);
29
- /**
30
- * Returns a new snapshot containing only the wallets for which
31
- * `predicate` returns `true`.
32
- *
33
- * When filtering by wallet ID, compare against stable payload IDs from
34
- * {@link serialize} or convert local IDs with {@link toPayloadId} first.
35
- *
36
- * @param predicate - Function called with each deeply read-only wallet entry.
37
- * @returns A filtered snapshot.
38
- */
39
- filterWallets(predicate: (wallet: AccountTreeSnapshotWallet) => boolean): AccountTreeSnapshot;
40
- /**
41
- * Filters groups within one wallet. Other wallets are left unchanged.
42
- *
43
- * Throws if `walletId` does not identify a wallet in the snapshot.
44
- * Removes the wallet if no groups remain after filtering — this prevents a
45
- * mnemonic wallet with zero selected groups from still transferring its secret.
46
- *
47
- * @param walletId - Stable payload wallet ID to filter groups within.
48
- * @param predicate - Function called with each deeply read-only group entry.
49
- * @returns A filtered snapshot.
50
- * @throws If `walletId` is not present in the snapshot.
51
- */
52
- filterGroups(walletId: AccountWalletPayloadId, predicate: (group: AccountTreeSnapshotGroup) => boolean): AccountTreeSnapshot;
53
- /**
54
- * Filters groups across every wallet.
55
- *
56
- * The parent wallet is provided as context to the predicate. Removes any
57
- * wallet with no remaining groups after filtering.
58
- *
59
- * @param predicate - Function called with each group and its parent wallet.
60
- * @returns A filtered snapshot.
61
- */
62
- filterAllGroups(predicate: (group: AccountTreeSnapshotGroup, wallet: AccountTreeSnapshotWallet) => boolean): AccountTreeSnapshot;
63
- /**
64
- * Converts a payload ID (wallet or group) to the corresponding local
65
- * `AccountTreeController` ID.
66
- *
67
- * The map reflects the original export, not the wallets/groups currently
68
- * retained in this snapshot after filtering.
69
- *
70
- * @param payloadId - Stable cross-device wallet or group payload ID.
71
- * @returns The local controller ID, or `undefined` if not found or no ID map is present.
72
- */
73
- toLocalId(payloadId: AccountWalletPayloadId | AccountGroupPayloadId): ReturnType<IdMap['getLocalId']>;
74
- /**
75
- * Converts a local `AccountTreeController` ID (wallet or group) to its
76
- * stable cross-device payload ID.
77
- *
78
- * The map reflects the original export, not the wallets/groups currently
79
- * retained in this snapshot after filtering.
80
- *
81
- * @param localId - Local controller wallet or group ID.
82
- * @returns The payload ID, or `undefined` if not found or no ID map is present.
83
- */
84
- toPayloadId(localId: Parameters<IdMap['add']>[0]): ReturnType<IdMap['getPayloadId']>;
85
- /**
86
- * Serializes the snapshot to a versioned state envelope wrapping the {@link AccountTreePayload}.
87
- *
88
- * Returns the constructor-frozen wallet tree without copying it again.
89
- *
90
- * @returns The versioned payload envelope.
91
- */
92
- serialize(): VersionedState<AccountTreePayload>;
93
- /**
94
- * Validates a raw value as an {@link AccountTreePayload}, running any
95
- * necessary version migrations, and returns an immutable snapshot.
96
- *
97
- * This is the entry point for untrusted serialized data. Unsupported schema
98
- * versions and wallet types fail closed with an error instead of returning a
99
- * partial snapshot.
100
- *
101
- * The returned snapshot has no ID map — {@link toLocalId} / {@link toPayloadId}
102
- * return `undefined`. Pass an {@link IdMap} to the constructor when you need
103
- * the map.
104
- *
105
- * @param raw - Unknown value to parse.
106
- * @returns A validated snapshot.
107
- * @throws If `raw` is not a valid payload or its version is unsupported.
108
- */
109
- static deserialize(raw: unknown): Promise<AccountTreeSnapshot>;
110
- }
111
- //# sourceMappingURL=snapshot.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"snapshot.d.mts","sourceRoot":"","sources":["../../src/state/snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA8B;AAE5D,OAAO,KAAK,EAAE,KAAK,EAAE,qBAAoB;AACzC,OAAO,KAAK,EACV,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,EACxB,yBAAyB,EACzB,sBAAsB,EAGtB,sBAAsB,EAGvB,sBAAqB;AAuBtB;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,mBAAmB;;IAK9B;;;OAGG;gBACS,OAAO,EAAE,sBAAsB,EAAE,EAAE,KAAK,CAAC,EAAE,KAAK;IAK5D;;;;;;;;;OASG;IACH,aAAa,CACX,SAAS,EAAE,CAAC,MAAM,EAAE,yBAAyB,KAAK,OAAO,GACxD,mBAAmB;IAQtB;;;;;;;;;;;OAWG;IACH,YAAY,CACV,QAAQ,EAAE,sBAAsB,EAChC,SAAS,EAAE,CAAC,KAAK,EAAE,wBAAwB,KAAK,OAAO,GACtD,mBAAmB;IAkCtB;;;;;;;;OAQG;IACH,eAAe,CACb,SAAS,EAAE,CACT,KAAK,EAAE,wBAAwB,EAC/B,MAAM,EAAE,yBAAyB,KAC9B,OAAO,GACX,mBAAmB;IA+BtB;;;;;;;;;OASG;IACH,SAAS,CACP,SAAS,EAAE,sBAAsB,GAAG,qBAAqB,GACxD,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAIlC;;;;;;;;;OASG;IACH,WAAW,CACT,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GACnC,UAAU,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;IAIpC;;;;;;OAMG;IACH,SAAS,IAAI,cAAc,CAAC,kBAAkB,CAAC;IAI/C;;;;;;;;;;;;;;;OAeG;WACU,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,mBAAmB,CAAC;CAIrE"}
@@ -1,199 +0,0 @@
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 _AccountTreeSnapshot_entries, _AccountTreeSnapshot_idMap;
13
- import { migrations, migrate } from "./payload.mjs";
14
- /**
15
- * Recursively freezes a value and its nested properties.
16
- *
17
- * @param value - Value to freeze.
18
- * @returns The frozen value.
19
- */
20
- function deepFreeze(value) {
21
- if (value === null || typeof value !== 'object') {
22
- return value;
23
- }
24
- Object.freeze(value);
25
- for (const nested of Object.values(value)) {
26
- deepFreeze(nested);
27
- }
28
- return value;
29
- }
30
- /**
31
- * Immutable value object returned by {@link AccountTreeController.exportState}.
32
- *
33
- * Construct with {@link AccountTreeController.exportState},
34
- * {@link AccountTreeSnapshot.deserialize}, or `new AccountTreeSnapshot(...)`
35
- * for tests and advanced use.
36
- *
37
- * Wallet and group entries are deep-cloned and deep-frozen once in the
38
- * constructor. Filtering predicates receive those read-only views directly;
39
- * each filter method returns a new snapshot that repeats the process for its
40
- * result.
41
- *
42
- * An optional ID map (local ↔ payload) may be supplied when bridging between
43
- * internal controller IDs and the stable cross-device IDs in the serialized
44
- * payload. The map covers the original export and is preserved unchanged
45
- * through filtering until {@link serialize}. Omit it when deterministic IDs
46
- * make {@link toLocalId} / {@link toPayloadId} unnecessary.
47
- */
48
- export class AccountTreeSnapshot {
49
- /**
50
- * @param entries - Wallet entries in the snapshot.
51
- * @param idMap - Optional local ↔ payload ID map from export.
52
- */
53
- constructor(entries, idMap) {
54
- _AccountTreeSnapshot_entries.set(this, void 0);
55
- _AccountTreeSnapshot_idMap.set(this, void 0);
56
- __classPrivateFieldSet(this, _AccountTreeSnapshot_entries, deepFreeze(structuredClone(entries)), "f");
57
- __classPrivateFieldSet(this, _AccountTreeSnapshot_idMap, idMap, "f");
58
- }
59
- /**
60
- * Returns a new snapshot containing only the wallets for which
61
- * `predicate` returns `true`.
62
- *
63
- * When filtering by wallet ID, compare against stable payload IDs from
64
- * {@link serialize} or convert local IDs with {@link toPayloadId} first.
65
- *
66
- * @param predicate - Function called with each deeply read-only wallet entry.
67
- * @returns A filtered snapshot.
68
- */
69
- filterWallets(predicate) {
70
- const filteredEntries = __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f").filter((entry) => predicate(entry));
71
- return new AccountTreeSnapshot(filteredEntries, __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f"));
72
- }
73
- /**
74
- * Filters groups within one wallet. Other wallets are left unchanged.
75
- *
76
- * Throws if `walletId` does not identify a wallet in the snapshot.
77
- * Removes the wallet if no groups remain after filtering — this prevents a
78
- * mnemonic wallet with zero selected groups from still transferring its secret.
79
- *
80
- * @param walletId - Stable payload wallet ID to filter groups within.
81
- * @param predicate - Function called with each deeply read-only group entry.
82
- * @returns A filtered snapshot.
83
- * @throws If `walletId` is not present in the snapshot.
84
- */
85
- filterGroups(walletId, predicate) {
86
- const walletIndex = __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f").findIndex((entry) => entry.id === walletId);
87
- if (walletIndex === -1) {
88
- throw new Error(`Cannot filter groups: wallet "${walletId}" not found in snapshot`);
89
- }
90
- const wallet = __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f")[walletIndex];
91
- const filteredGroups = wallet.groups.filter((group) => predicate(group));
92
- const filteredEntries = [...__classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f")];
93
- if (filteredGroups.length === 0) {
94
- filteredEntries.splice(walletIndex, 1);
95
- }
96
- else if (wallet.type === 'mnemonic') {
97
- filteredEntries[walletIndex] = {
98
- ...wallet,
99
- groups: filteredGroups,
100
- };
101
- }
102
- else {
103
- filteredEntries[walletIndex] = {
104
- ...wallet,
105
- groups: filteredGroups,
106
- };
107
- }
108
- return new AccountTreeSnapshot(filteredEntries, __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f"));
109
- }
110
- /**
111
- * Filters groups across every wallet.
112
- *
113
- * The parent wallet is provided as context to the predicate. Removes any
114
- * wallet with no remaining groups after filtering.
115
- *
116
- * @param predicate - Function called with each group and its parent wallet.
117
- * @returns A filtered snapshot.
118
- */
119
- filterAllGroups(predicate) {
120
- const filteredEntries = [];
121
- for (const wallet of __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f")) {
122
- const filteredGroups = wallet.groups.filter((group) => predicate(group, wallet));
123
- if (filteredGroups.length === 0) {
124
- continue;
125
- }
126
- if (wallet.type === 'mnemonic') {
127
- filteredEntries.push({
128
- ...wallet,
129
- groups: filteredGroups,
130
- });
131
- }
132
- else {
133
- filteredEntries.push({
134
- ...wallet,
135
- groups: filteredGroups,
136
- });
137
- }
138
- }
139
- return new AccountTreeSnapshot(filteredEntries, __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f"));
140
- }
141
- /**
142
- * Converts a payload ID (wallet or group) to the corresponding local
143
- * `AccountTreeController` ID.
144
- *
145
- * The map reflects the original export, not the wallets/groups currently
146
- * retained in this snapshot after filtering.
147
- *
148
- * @param payloadId - Stable cross-device wallet or group payload ID.
149
- * @returns The local controller ID, or `undefined` if not found or no ID map is present.
150
- */
151
- toLocalId(payloadId) {
152
- return __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f")?.getLocalId(payloadId);
153
- }
154
- /**
155
- * Converts a local `AccountTreeController` ID (wallet or group) to its
156
- * stable cross-device payload ID.
157
- *
158
- * The map reflects the original export, not the wallets/groups currently
159
- * retained in this snapshot after filtering.
160
- *
161
- * @param localId - Local controller wallet or group ID.
162
- * @returns The payload ID, or `undefined` if not found or no ID map is present.
163
- */
164
- toPayloadId(localId) {
165
- return __classPrivateFieldGet(this, _AccountTreeSnapshot_idMap, "f")?.getPayloadId(localId);
166
- }
167
- /**
168
- * Serializes the snapshot to a versioned state envelope wrapping the {@link AccountTreePayload}.
169
- *
170
- * Returns the constructor-frozen wallet tree without copying it again.
171
- *
172
- * @returns The versioned payload envelope.
173
- */
174
- serialize() {
175
- return { version: migrations.version, data: { wallets: __classPrivateFieldGet(this, _AccountTreeSnapshot_entries, "f") } };
176
- }
177
- /**
178
- * Validates a raw value as an {@link AccountTreePayload}, running any
179
- * necessary version migrations, and returns an immutable snapshot.
180
- *
181
- * This is the entry point for untrusted serialized data. Unsupported schema
182
- * versions and wallet types fail closed with an error instead of returning a
183
- * partial snapshot.
184
- *
185
- * The returned snapshot has no ID map — {@link toLocalId} / {@link toPayloadId}
186
- * return `undefined`. Pass an {@link IdMap} to the constructor when you need
187
- * the map.
188
- *
189
- * @param raw - Unknown value to parse.
190
- * @returns A validated snapshot.
191
- * @throws If `raw` is not a valid payload or its version is unsupported.
192
- */
193
- static async deserialize(raw) {
194
- const payload = await migrate(raw);
195
- return new AccountTreeSnapshot(payload.wallets);
196
- }
197
- }
198
- _AccountTreeSnapshot_entries = new WeakMap(), _AccountTreeSnapshot_idMap = new WeakMap();
199
- //# sourceMappingURL=snapshot.mjs.map