@metamask-previews/account-tree-controller 7.6.1-preview-9efd0b754 → 7.6.1-preview-4e64b66e8
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.
- package/CHANGELOG.md +13 -0
- package/dist/AccountTreeController-method-action-types.cjs.map +1 -1
- package/dist/AccountTreeController-method-action-types.d.cts +34 -1
- package/dist/AccountTreeController-method-action-types.d.cts.map +1 -1
- package/dist/AccountTreeController-method-action-types.d.mts +34 -1
- package/dist/AccountTreeController-method-action-types.d.mts.map +1 -1
- package/dist/AccountTreeController-method-action-types.mjs.map +1 -1
- package/dist/AccountTreeController.cjs +53 -1
- package/dist/AccountTreeController.cjs.map +1 -1
- package/dist/AccountTreeController.d.cts +29 -0
- package/dist/AccountTreeController.d.cts.map +1 -1
- package/dist/AccountTreeController.d.mts +29 -0
- package/dist/AccountTreeController.d.mts.map +1 -1
- package/dist/AccountTreeController.mjs +53 -1
- package/dist/AccountTreeController.mjs.map +1 -1
- package/dist/index.cjs +10 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +6 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -1
- package/dist/state/export.cjs +221 -0
- package/dist/state/export.cjs.map +1 -0
- package/dist/state/export.d.cts +40 -0
- package/dist/state/export.d.cts.map +1 -0
- package/dist/state/export.d.mts +40 -0
- package/dist/state/export.d.mts.map +1 -0
- package/dist/state/export.mjs +215 -0
- package/dist/state/export.mjs.map +1 -0
- package/dist/state/id-map.cjs +65 -0
- package/dist/state/id-map.cjs.map +1 -0
- package/dist/state/id-map.d.cts +41 -0
- package/dist/state/id-map.d.cts.map +1 -0
- package/dist/state/id-map.d.mts +41 -0
- package/dist/state/id-map.d.mts.map +1 -0
- package/dist/state/id-map.mjs +61 -0
- package/dist/state/id-map.mjs.map +1 -0
- package/dist/state/import.cjs +285 -0
- package/dist/state/import.cjs.map +1 -0
- package/dist/state/import.d.cts +29 -0
- package/dist/state/import.d.cts.map +1 -0
- package/dist/state/import.d.mts +29 -0
- package/dist/state/import.d.mts.map +1 -0
- package/dist/state/import.mjs +281 -0
- package/dist/state/import.mjs.map +1 -0
- package/dist/state/payload.cjs +169 -0
- package/dist/state/payload.cjs.map +1 -0
- package/dist/state/payload.d.cts +288 -0
- package/dist/state/payload.d.cts.map +1 -0
- package/dist/state/payload.d.mts +288 -0
- package/dist/state/payload.d.mts.map +1 -0
- package/dist/state/payload.mjs +162 -0
- package/dist/state/payload.mjs.map +1 -0
- package/dist/state/snapshot.cjs +202 -0
- package/dist/state/snapshot.cjs.map +1 -0
- package/dist/state/snapshot.d.cts +120 -0
- package/dist/state/snapshot.d.cts.map +1 -0
- package/dist/state/snapshot.d.mts +120 -0
- package/dist/state/snapshot.d.mts.map +1 -0
- package/dist/state/snapshot.mjs +198 -0
- package/dist/state/snapshot.mjs.map +1 -0
- package/dist/state/utils.cjs +68 -0
- package/dist/state/utils.cjs.map +1 -0
- package/dist/state/utils.d.cts +49 -0
- package/dist/state/utils.d.cts.map +1 -0
- package/dist/state/utils.d.mts +49 -0
- package/dist/state/utils.d.mts.map +1 -0
- package/dist/state/utils.mjs +61 -0
- package/dist/state/utils.mjs.map +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +23 -4
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +23 -4
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { AccountWalletType } from "@metamask/account-api";
|
|
2
|
+
import { HdKeyring } from "@metamask/eth-hd-keyring/v2";
|
|
3
|
+
import { EthAccountType } from "@metamask/keyring-api";
|
|
4
|
+
import { KeyringTypes } from "@metamask/keyring-controller";
|
|
5
|
+
import { IdMap } from "./id-map.mjs";
|
|
6
|
+
import { AccountWalletPayloadType, AccountWalletPrivateKeyEncoding, toWalletPayloadId, toGroupPayloadId } from "./payload.mjs";
|
|
7
|
+
import { AccountTreeSnapshot } from "./snapshot.mjs";
|
|
8
|
+
import { encodeBytes } from "./utils.mjs";
|
|
9
|
+
/**
|
|
10
|
+
* Returns `true` if `wallet` is an HD entropy wallet ({@link AccountWalletEntropyObject}).
|
|
11
|
+
*
|
|
12
|
+
* @param wallet - The wallet object to test.
|
|
13
|
+
* @returns Type predicate narrowing to {@link AccountWalletEntropyObject}.
|
|
14
|
+
*/
|
|
15
|
+
export function isMnemonicWalletObject(wallet) {
|
|
16
|
+
return wallet.type === AccountWalletType.Entropy;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Returns `true` if `wallet` is an imported private-key wallet
|
|
20
|
+
* ({@link AccountWalletKeyringObject} with keyring type {@link KeyringTypes.simple}).
|
|
21
|
+
*
|
|
22
|
+
* @param wallet - The wallet object to test.
|
|
23
|
+
* @returns Type predicate narrowing to {@link AccountWalletKeyringObject}.
|
|
24
|
+
*/
|
|
25
|
+
export function isPrivateKeyWalletObject(wallet) {
|
|
26
|
+
return (wallet.type === AccountWalletType.Keyring &&
|
|
27
|
+
wallet.metadata.keyring.type === KeyringTypes.simple);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Exports a single entropy (HD) wallet object as an {@link AccountWalletMnemonicPayload}.
|
|
31
|
+
*
|
|
32
|
+
* Calls `KeyringController:withKeyringV2Unsafe` to derive the stable entropy source ID
|
|
33
|
+
* via {@link HdKeyring.toEntropySourceId} and, when `includeSecrets` is `true`, to read
|
|
34
|
+
* the raw mnemonic bytes.
|
|
35
|
+
*
|
|
36
|
+
* @param context - Export context.
|
|
37
|
+
* @param walletObj - The local entropy wallet to export.
|
|
38
|
+
* @param includeSecrets - When `true`, the BIP-39 mnemonic is included in the payload.
|
|
39
|
+
* @param idMap - ID map to populate with local↔payload ID pairs for this wallet and its groups.
|
|
40
|
+
* @returns The mnemonic wallet payload entry.
|
|
41
|
+
* @throws If `includeSecrets` is `true` but the mnemonic cannot be read from the keyring.
|
|
42
|
+
*/
|
|
43
|
+
async function exportMnemonicWalletObject(context, walletObj, includeSecrets, idMap) {
|
|
44
|
+
const result = await context.messenger.call('KeyringController:withKeyringV2Unsafe',
|
|
45
|
+
// The local wallet entropy ID is the keyring ID.
|
|
46
|
+
{ id: walletObj.metadata.entropy.id }, async ({ keyring }) => {
|
|
47
|
+
const hdKeyring = keyring;
|
|
48
|
+
const includeMnemonic = includeSecrets &&
|
|
49
|
+
hdKeyring.mnemonic !== null &&
|
|
50
|
+
hdKeyring.mnemonic !== undefined;
|
|
51
|
+
return {
|
|
52
|
+
// Compute the stable entropy source ID from the keyring's mnemonic (BIP-39 seed).
|
|
53
|
+
entropySourceId: await hdKeyring.toEntropySourceId(),
|
|
54
|
+
// No need to include the mnemonic here if we're not exporting secrets.
|
|
55
|
+
mnemonic: includeMnemonic ? hdKeyring.mnemonic : undefined,
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
const { entropySourceId, mnemonic } = result;
|
|
59
|
+
// We use the stable entropy source ID as the payload wallet ID, rather than the local wallet ID, to
|
|
60
|
+
// ensure that the exported snapshot is stable across different installations and sessions.
|
|
61
|
+
const wallet = {
|
|
62
|
+
type: AccountWalletPayloadType.Mnemonic,
|
|
63
|
+
id: toWalletPayloadId(entropySourceId),
|
|
64
|
+
metadata: { name: walletObj.metadata.name },
|
|
65
|
+
groups: [],
|
|
66
|
+
};
|
|
67
|
+
idMap.add(walletObj.id, wallet.id);
|
|
68
|
+
for (const groupObj of Object.values(walletObj.groups)) {
|
|
69
|
+
const { groupIndex } = groupObj.metadata.entropy;
|
|
70
|
+
const group = {
|
|
71
|
+
id: toGroupPayloadId(wallet.id, groupIndex),
|
|
72
|
+
groupIndex,
|
|
73
|
+
metadata: {
|
|
74
|
+
name: groupObj.metadata.name,
|
|
75
|
+
pinned: groupObj.metadata.pinned,
|
|
76
|
+
hidden: groupObj.metadata.hidden,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
idMap.add(groupObj.id, group.id);
|
|
80
|
+
wallet.groups.push(group);
|
|
81
|
+
}
|
|
82
|
+
// Sort the groups by their `groupIndex` to ensure a stable order in the exported payload.
|
|
83
|
+
wallet.groups.sort((group, otherGroup) => group.groupIndex - otherGroup.groupIndex);
|
|
84
|
+
// Defensive check: group indices must be contiguous starting at 0. This should never happen
|
|
85
|
+
// in practice since groups are only ever appended, but guards against future regressions
|
|
86
|
+
// producing a payload that silently fails validation on import.
|
|
87
|
+
for (let i = 0; i < wallet.groups.length; i++) {
|
|
88
|
+
if (wallet.groups[i]?.groupIndex !== i) {
|
|
89
|
+
throw new Error('Found non-contiguous groups in mnemonic wallet');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (includeSecrets) {
|
|
93
|
+
if (mnemonic === undefined) {
|
|
94
|
+
throw new Error(`Failed to export mnemonic for wallet ${wallet.id}`);
|
|
95
|
+
}
|
|
96
|
+
wallet.value = encodeBytes(mnemonic);
|
|
97
|
+
}
|
|
98
|
+
return wallet;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Exports a single simple-keyring wallet object as an {@link AccountWalletPrivateKeyPayload}.
|
|
102
|
+
*
|
|
103
|
+
* All groups from the wallet are merged into the `'private-key'` singleton payload entry.
|
|
104
|
+
* When `includeSecrets` is `true`, each group's private key is exported via
|
|
105
|
+
* `KeyringController:withKeyringV2`.
|
|
106
|
+
*
|
|
107
|
+
* @param context - Export context.
|
|
108
|
+
* @param walletObj - The local simple-keyring wallet to export.
|
|
109
|
+
* @param includeSecrets - When `true`, private keys are included in the payload.
|
|
110
|
+
* @param idMap - ID map to populate with local↔payload ID pairs for this wallet and its groups.
|
|
111
|
+
* @returns The private-key wallet payload entry.
|
|
112
|
+
* @throws If `includeSecrets` is `true` but a private key cannot be exported for an account.
|
|
113
|
+
*/
|
|
114
|
+
async function exportPrivateKeyWalletObject(context, walletObj, includeSecrets, idMap) {
|
|
115
|
+
// We use a singleton wallet ID for private keys.
|
|
116
|
+
const wallet = {
|
|
117
|
+
type: AccountWalletPayloadType.PrivateKey,
|
|
118
|
+
id: toWalletPayloadId(AccountWalletPayloadType.PrivateKey),
|
|
119
|
+
metadata: { name: walletObj.metadata.name },
|
|
120
|
+
groups: [],
|
|
121
|
+
};
|
|
122
|
+
for (const groupObj of Object.values(walletObj.groups)) {
|
|
123
|
+
const accountId = groupObj.accounts[0];
|
|
124
|
+
if (!accountId) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const account = context.messenger.call('AccountsController:getAccount', accountId);
|
|
128
|
+
if (!account) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const { address } = account;
|
|
132
|
+
let exported;
|
|
133
|
+
if (includeSecrets) {
|
|
134
|
+
const result = await context.messenger.call('KeyringController:withKeyringV2', { address }, async ({ keyring }) => {
|
|
135
|
+
if (!keyring.exportAccount) {
|
|
136
|
+
throw new Error(`Keyring for account ${accountId} does not support exportAccount`);
|
|
137
|
+
}
|
|
138
|
+
return keyring.exportAccount(accountId, {
|
|
139
|
+
type: 'private-key',
|
|
140
|
+
encoding: AccountWalletPrivateKeyEncoding.Hexadecimal,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
exported = result;
|
|
144
|
+
}
|
|
145
|
+
const group = {
|
|
146
|
+
id: toGroupPayloadId(wallet.id, address),
|
|
147
|
+
metadata: {
|
|
148
|
+
name: groupObj.metadata.name,
|
|
149
|
+
pinned: groupObj.metadata.pinned,
|
|
150
|
+
hidden: groupObj.metadata.hidden,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
if (includeSecrets) {
|
|
154
|
+
if (!exported) {
|
|
155
|
+
throw new Error(`Failed to export private key for account ${accountId}`);
|
|
156
|
+
}
|
|
157
|
+
group.value = {
|
|
158
|
+
privateKey: encodeBytes(new TextEncoder().encode(exported.privateKey)),
|
|
159
|
+
encoding: exported.encoding,
|
|
160
|
+
type: EthAccountType.Eoa,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
idMap.add(groupObj.id, group.id);
|
|
164
|
+
wallet.groups.push(group);
|
|
165
|
+
}
|
|
166
|
+
return wallet;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Builds an {@link AccountTreeSnapshot} from the current controller state.
|
|
170
|
+
*
|
|
171
|
+
* Iterates over all wallets in the tree:
|
|
172
|
+
* - {@link AccountWalletType.Entropy} (HD) wallets -> `'mnemonic'` payload entries.
|
|
173
|
+
* - {@link AccountWalletType.Keyring} wallets of type `simple` -> `'private-key'` payload entries.
|
|
174
|
+
* - Snap wallets and hardware keyrings are skipped in v1.
|
|
175
|
+
*
|
|
176
|
+
* @param context - Export context providing state and messenger access.
|
|
177
|
+
* @param options - Export options.
|
|
178
|
+
* @returns A promise that resolves to the built snapshot.
|
|
179
|
+
* @throws If the vault is locked.
|
|
180
|
+
*/
|
|
181
|
+
export async function exportState(context, options = {}) {
|
|
182
|
+
const state = context.getState();
|
|
183
|
+
const includeSecrets = options.includeSecrets ?? false;
|
|
184
|
+
const { isUnlocked } = context.messenger.call('KeyringController:getState');
|
|
185
|
+
if (!isUnlocked) {
|
|
186
|
+
throw new Error('Cannot export account tree when vault is locked');
|
|
187
|
+
}
|
|
188
|
+
const idMap = new IdMap();
|
|
189
|
+
const entries = [];
|
|
190
|
+
let privateKeyWallet;
|
|
191
|
+
for (const walletObj of Object.values(state.accountTree.wallets)) {
|
|
192
|
+
if (isMnemonicWalletObject(walletObj)) {
|
|
193
|
+
entries.push(await exportMnemonicWalletObject(context, walletObj, includeSecrets, idMap));
|
|
194
|
+
}
|
|
195
|
+
else if (isPrivateKeyWalletObject(walletObj)) {
|
|
196
|
+
const exported = await exportPrivateKeyWalletObject(context, walletObj, includeSecrets, idMap);
|
|
197
|
+
if (privateKeyWallet) {
|
|
198
|
+
privateKeyWallet.groups.push(...exported.groups);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
privateKeyWallet = exported;
|
|
202
|
+
// Register only once, since all private keys are exported into the same wallet entry.
|
|
203
|
+
idMap.add(walletObj.id, exported.id);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
// AccountWalletType.Snap and hardware keyrings: skipped for now.
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (privateKeyWallet) {
|
|
211
|
+
entries.push(privateKeyWallet);
|
|
212
|
+
}
|
|
213
|
+
return new AccountTreeSnapshot(entries, idMap);
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=export.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"export.mjs","sourceRoot":"","sources":["../../src/state/export.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAC1D,OAAO,EAAE,SAAS,EAAE,oCAAoC;AACxD,OAAO,EAAE,cAAc,EAAE,8BAA8B;AAEvD,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAW5D,OAAO,EAAE,KAAK,EAAE,qBAAoB;AASpC,OAAO,EACL,wBAAwB,EACxB,+BAA+B,EAC/B,iBAAiB,EACjB,gBAAgB,EACjB,sBAAqB;AACtB,OAAO,EAAE,mBAAmB,EAAE,uBAAsB;AACpD,OAAO,EAAE,WAAW,EAAE,oBAAmB;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,MAA2B;IAE3B,OAAO,MAAM,CAAC,IAAI,KAAK,iBAAiB,CAAC,OAAO,CAAC;AACnD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,wBAAwB,CACtC,MAA2B;IAE3B,OAAO,CACL,MAAM,CAAC,IAAI,KAAK,iBAAiB,CAAC,OAAO;QACzC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAK,YAAY,CAAC,MAAM,CACrD,CAAC;AACJ,CAAC;AAQD;;;;;;;;;;;;;GAaG;AACH,KAAK,UA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AC,CAAC;IACrE,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC;IAC1B,MAAM,OAAO,GAA6B,EAAE,CAAC;IAC7C,IAAI,gBAA4D,CAAC;IAEjE,KAAK,MAAM,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;QACjE,IAAI,sBAAsB,CAAC,SAAS,CAAC,EAAE,CAAC;YACtC,OAAO,CAAC,IAAI,CACV,MAAM,0BAA0B,CAC9B,OAAO,EACP,SAAS,EACT,cAAc,EACd,KAAK,CACN,CACF,CAAC;QACJ,CAAC;aAAM,IAAI,wBAAwB,CAAC,SAAS,CAAC,EAAE,CAAC;YAC/C,MAAM,QAAQ,GAAG,MAAM,4BAA4B,CACjD,OAAO,EACP,SAAS,EACT,cAAc,EACd,KAAK,CACN,CAAC;YAEF,IAAI,gBAAgB,EAAE,CAAC;gBACrB,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;YACnD,CAAC;iBAAM,CAAC;gBACN,gBAAgB,GAAG,QAAQ,CAAC;gBAE5B,sFAAsF;gBACtF,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC;YACvC,CAAC;QACH,CAAC;aAAM,CAAC;YACN,iEAAiE;QACnE,CAAC;IACH,CAAC;IAED,IAAI,gBAAgB,EAAE,CAAC;QACrB,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,IAAI,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import { AccountWalletType } from '@metamask/account-api';\nimport { HdKeyring } from '@metamask/eth-hd-keyring/v2';\nimport { EthAccountType } from '@metamask/keyring-api';\nimport { PrivateKeyExportedAccount } from '@metamask/keyring-api/v2';\nimport { KeyringTypes } from '@metamask/keyring-controller';\n\nimport type {\n AccountTreeControllerMessenger,\n AccountTreeControllerState,\n} from '../types.js';\nimport type { AccountWalletObject } from '../wallet.js';\nimport {\n AccountWalletEntropyObject,\n AccountWalletKeyringObject,\n} from '../wallet.js';\nimport { IdMap } from './id-map.js';\nimport type {\n AccountTreeWalletEntry,\n AccountWalletMnemonicGroupEntry,\n AccountWalletMnemonicPayload,\n AccountWalletPrivateKeyGroupEntry,\n AccountWalletPrivateKeyPayload,\n ExportStateOptions,\n} from './payload.js';\nimport {\n AccountWalletPayloadType,\n AccountWalletPrivateKeyEncoding,\n toWalletPayloadId,\n toGroupPayloadId,\n} from './payload.js';\nimport { AccountTreeSnapshot } from './snapshot.js';\nimport { encodeBytes } from './utils.js';\n\n/**\n * Returns `true` if `wallet` is an HD entropy wallet ({@link AccountWalletEntropyObject}).\n *\n * @param wallet - The wallet object to test.\n * @returns Type predicate narrowing to {@link AccountWalletEntropyObject}.\n */\nexport function isMnemonicWalletObject(\n wallet: AccountWalletObject,\n): wallet is AccountWalletEntropyObject {\n return wallet.type === AccountWalletType.Entropy;\n}\n\n/**\n * Returns `true` if `wallet` is an imported private-key wallet\n * ({@link AccountWalletKeyringObject} with keyring type {@link KeyringTypes.simple}).\n *\n * @param wallet - The wallet object to test.\n * @returns Type predicate narrowing to {@link AccountWalletKeyringObject}.\n */\nexport function isPrivateKeyWalletObject(\n wallet: AccountWalletObject,\n): wallet is AccountWalletKeyringObject {\n return (\n wallet.type === AccountWalletType.Keyring &&\n wallet.metadata.keyring.type === KeyringTypes.simple\n );\n}\n\n/** Context required by {@link exportState}. */\nexport type ExportContext = {\n getState: () => AccountTreeControllerState;\n messenger: AccountTreeControllerMessenger;\n};\n\n/**\n * Exports a single entropy (HD) wallet object as an {@link AccountWalletMnemonicPayload}.\n *\n * Calls `KeyringController:withKeyringV2Unsafe` to derive the stable entropy source ID\n * via {@link HdKeyring.toEntropySourceId} and, when `includeSecrets` is `true`, to read\n * the raw mnemonic bytes.\n *\n * @param context - Export context.\n * @param walletObj - The local entropy wallet to export.\n * @param includeSecrets - When `true`, the BIP-39 mnemonic is included in the payload.\n * @param idMap - ID map to populate with local↔payload ID pairs for this wallet and its groups.\n * @returns The mnemonic wallet payload entry.\n * @throws If `includeSecrets` is `true` but the mnemonic cannot be read from the keyring.\n */\nasync function exportMnemonicWalletObject(\n context: ExportContext,\n walletObj: AccountWalletEntropyObject,\n includeSecrets: boolean,\n idMap: IdMap,\n): Promise<AccountWalletMnemonicPayload> {\n const result = await context.messenger.call(\n 'KeyringController:withKeyringV2Unsafe',\n // The local wallet entropy ID is the keyring ID.\n { id: walletObj.metadata.entropy.id },\n async ({ keyring }) => {\n const hdKeyring = keyring as HdKeyring;\n const includeMnemonic =\n includeSecrets &&\n hdKeyring.mnemonic !== null &&\n hdKeyring.mnemonic !== undefined;\n\n return {\n // Compute the stable entropy source ID from the keyring's mnemonic (BIP-39 seed).\n entropySourceId: await hdKeyring.toEntropySourceId(),\n // No need to include the mnemonic here if we're not exporting secrets.\n mnemonic: includeMnemonic ? hdKeyring.mnemonic : undefined,\n };\n },\n );\n const { entropySourceId, mnemonic } = result as {\n entropySourceId: string;\n mnemonic?: Uint8Array;\n };\n\n // We use the stable entropy source ID as the payload wallet ID, rather than the local wallet ID, to\n // ensure that the exported snapshot is stable across different installations and sessions.\n const wallet: AccountWalletMnemonicPayload = {\n type: AccountWalletPayloadType.Mnemonic,\n id: toWalletPayloadId(entropySourceId),\n metadata: { name: walletObj.metadata.name },\n groups: [],\n };\n\n idMap.add(walletObj.id, wallet.id);\n\n for (const groupObj of Object.values(walletObj.groups)) {\n const { groupIndex } = groupObj.metadata.entropy;\n\n const group: AccountWalletMnemonicGroupEntry = {\n id: toGroupPayloadId(wallet.id, groupIndex),\n groupIndex,\n metadata: {\n name: groupObj.metadata.name,\n pinned: groupObj.metadata.pinned,\n hidden: groupObj.metadata.hidden,\n },\n };\n\n idMap.add(groupObj.id, group.id);\n\n wallet.groups.push(group);\n }\n\n // Sort the groups by their `groupIndex` to ensure a stable order in the exported payload.\n wallet.groups.sort(\n (group, otherGroup) => group.groupIndex - otherGroup.groupIndex,\n );\n\n // Defensive check: group indices must be contiguous starting at 0. This should never happen\n // in practice since groups are only ever appended, but guards against future regressions\n // producing a payload that silently fails validation on import.\n for (let i = 0; i < wallet.groups.length; i++) {\n if (wallet.groups[i]?.groupIndex !== i) {\n throw new Error('Found non-contiguous groups in mnemonic wallet');\n }\n }\n\n if (includeSecrets) {\n if (mnemonic === undefined) {\n throw new Error(`Failed to export mnemonic for wallet ${wallet.id}`);\n }\n\n wallet.value = encodeBytes(mnemonic);\n }\n\n return wallet;\n}\n\n/**\n * Exports a single simple-keyring wallet object as an {@link AccountWalletPrivateKeyPayload}.\n *\n * All groups from the wallet are merged into the `'private-key'` singleton payload entry.\n * When `includeSecrets` is `true`, each group's private key is exported via\n * `KeyringController:withKeyringV2`.\n *\n * @param context - Export context.\n * @param walletObj - The local simple-keyring wallet to export.\n * @param includeSecrets - When `true`, private keys are included in the payload.\n * @param idMap - ID map to populate with local↔payload ID pairs for this wallet and its groups.\n * @returns The private-key wallet payload entry.\n * @throws If `includeSecrets` is `true` but a private key cannot be exported for an account.\n */\nasync function exportPrivateKeyWalletObject(\n context: ExportContext,\n walletObj: AccountWalletKeyringObject,\n includeSecrets: boolean,\n idMap: IdMap,\n): Promise<AccountWalletPrivateKeyPayload> {\n // We use a singleton wallet ID for private keys.\n const wallet: AccountWalletPrivateKeyPayload = {\n type: AccountWalletPayloadType.PrivateKey,\n id: toWalletPayloadId(AccountWalletPayloadType.PrivateKey),\n metadata: { name: walletObj.metadata.name },\n groups: [],\n };\n\n for (const groupObj of Object.values(walletObj.groups)) {\n const accountId = groupObj.accounts[0];\n if (!accountId) {\n continue;\n }\n const account = context.messenger.call(\n 'AccountsController:getAccount',\n accountId,\n );\n if (!account) {\n continue;\n }\n\n const { address } = account;\n\n let exported: PrivateKeyExportedAccount | undefined;\n if (includeSecrets) {\n const result = await context.messenger.call(\n 'KeyringController:withKeyringV2',\n { address },\n async ({ keyring }) => {\n if (!keyring.exportAccount) {\n throw new Error(\n `Keyring for account ${accountId} does not support exportAccount`,\n );\n }\n\n return keyring.exportAccount(accountId, {\n type: 'private-key',\n encoding: AccountWalletPrivateKeyEncoding.Hexadecimal,\n });\n },\n );\n\n exported = result as PrivateKeyExportedAccount;\n }\n\n const group: AccountWalletPrivateKeyGroupEntry = {\n id: toGroupPayloadId(wallet.id, address),\n metadata: {\n name: groupObj.metadata.name,\n pinned: groupObj.metadata.pinned,\n hidden: groupObj.metadata.hidden,\n },\n };\n\n if (includeSecrets) {\n if (!exported) {\n throw new Error(\n `Failed to export private key for account ${accountId}`,\n );\n }\n group.value = {\n privateKey: encodeBytes(new TextEncoder().encode(exported.privateKey)),\n encoding: exported.encoding,\n type: EthAccountType.Eoa,\n };\n }\n\n idMap.add(groupObj.id, group.id);\n\n wallet.groups.push(group);\n }\n\n return wallet;\n}\n\n/**\n * Builds an {@link AccountTreeSnapshot} from the current controller state.\n *\n * Iterates over all wallets in the tree:\n * - {@link AccountWalletType.Entropy} (HD) wallets -> `'mnemonic'` payload entries.\n * - {@link AccountWalletType.Keyring} wallets of type `simple` -> `'private-key'` payload entries.\n * - Snap wallets and hardware keyrings are skipped in v1.\n *\n * @param context - Export context providing state and messenger access.\n * @param options - Export options.\n * @returns A promise that resolves to the built snapshot.\n * @throws If the vault is locked.\n */\nexport async function exportState(\n context: ExportContext,\n options: ExportStateOptions = {},\n): Promise<AccountTreeSnapshot> {\n const state = context.getState();\n\n const includeSecrets = options.includeSecrets ?? false;\n const { isUnlocked } = context.messenger.call('KeyringController:getState');\n if (!isUnlocked) {\n throw new Error('Cannot export account tree when vault is locked');\n }\n\n const idMap = new IdMap();\n const entries: AccountTreeWalletEntry[] = [];\n let privateKeyWallet: AccountWalletPrivateKeyPayload | undefined;\n\n for (const walletObj of Object.values(state.accountTree.wallets)) {\n if (isMnemonicWalletObject(walletObj)) {\n entries.push(\n await exportMnemonicWalletObject(\n context,\n walletObj,\n includeSecrets,\n idMap,\n ),\n );\n } else if (isPrivateKeyWalletObject(walletObj)) {\n const exported = await exportPrivateKeyWalletObject(\n context,\n walletObj,\n includeSecrets,\n idMap,\n );\n\n if (privateKeyWallet) {\n privateKeyWallet.groups.push(...exported.groups);\n } else {\n privateKeyWallet = exported;\n\n // Register only once, since all private keys are exported into the same wallet entry.\n idMap.add(walletObj.id, exported.id);\n }\n } else {\n // AccountWalletType.Snap and hardware keyrings: skipped for now.\n }\n }\n\n if (privateKeyWallet) {\n entries.push(privateKeyWallet);\n }\n\n return new AccountTreeSnapshot(entries, idMap);\n}\n"]}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _IdMap_localToPayload, _IdMap_payloadToLocal;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.IdMap = void 0;
|
|
10
|
+
const utils_1 = require("@metamask/utils");
|
|
11
|
+
/**
|
|
12
|
+
* Bidirectional map between local controller IDs and portable payload IDs.
|
|
13
|
+
*
|
|
14
|
+
* Populated during {@link exportState} so that callers can bridge between
|
|
15
|
+
* device-local wallet/group IDs and the stable cross-device IDs that appear
|
|
16
|
+
* in a serialized {@link AccountTreePayload}.
|
|
17
|
+
*/
|
|
18
|
+
class IdMap {
|
|
19
|
+
/**
|
|
20
|
+
* @param entries - Optional seed pairs `[localId, payloadId]` to pre-populate the map.
|
|
21
|
+
*/
|
|
22
|
+
constructor(entries = []) {
|
|
23
|
+
_IdMap_localToPayload.set(this, new Map());
|
|
24
|
+
_IdMap_payloadToLocal.set(this, new Map());
|
|
25
|
+
for (const [localId, payloadId] of entries) {
|
|
26
|
+
this.add(localId, payloadId);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Registers a local↔payload ID pair.
|
|
31
|
+
*
|
|
32
|
+
* @param localId - Local controller wallet or group ID.
|
|
33
|
+
* @param payloadId - Corresponding portable payload ID.
|
|
34
|
+
*/
|
|
35
|
+
add(localId, payloadId) {
|
|
36
|
+
// Each local ID maps to exactly one payload ID and vice versa: wallet and
|
|
37
|
+
// group IDs are unique by construction, so a duplicate signals a bug in the
|
|
38
|
+
// caller (e.g. exportState visiting the same node twice).
|
|
39
|
+
(0, utils_1.assert)(!__classPrivateFieldGet(this, _IdMap_localToPayload, "f").has(localId), `Local ID already registered: ${localId}`);
|
|
40
|
+
(0, utils_1.assert)(!__classPrivateFieldGet(this, _IdMap_payloadToLocal, "f").has(payloadId), `Payload ID already registered: ${payloadId}`);
|
|
41
|
+
__classPrivateFieldGet(this, _IdMap_localToPayload, "f").set(localId, payloadId);
|
|
42
|
+
__classPrivateFieldGet(this, _IdMap_payloadToLocal, "f").set(payloadId, localId);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Returns the portable payload ID for a given local ID.
|
|
46
|
+
*
|
|
47
|
+
* @param localId - Local controller wallet or group ID.
|
|
48
|
+
* @returns The payload ID, or `undefined` if not registered.
|
|
49
|
+
*/
|
|
50
|
+
getPayloadId(localId) {
|
|
51
|
+
return __classPrivateFieldGet(this, _IdMap_localToPayload, "f").get(localId);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns the local controller ID for a given payload ID.
|
|
55
|
+
*
|
|
56
|
+
* @param payloadId - Portable payload ID.
|
|
57
|
+
* @returns The local wallet or group ID, or `undefined` if not registered.
|
|
58
|
+
*/
|
|
59
|
+
getLocalId(payloadId) {
|
|
60
|
+
return __classPrivateFieldGet(this, _IdMap_payloadToLocal, "f").get(payloadId);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.IdMap = IdMap;
|
|
64
|
+
_IdMap_localToPayload = new WeakMap(), _IdMap_payloadToLocal = new WeakMap();
|
|
65
|
+
//# sourceMappingURL=id-map.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-map.cjs","sourceRoot":"","sources":["../../src/state/id-map.ts"],"names":[],"mappings":";;;;;;;;;AACA,2CAAyC;AAUzC;;;;;;GAMG;AACH,MAAa,KAAK;IAKhB;;OAEG;IACH,YAAY,UAAsD,EAAE;QAP3D,gCAA2C,IAAI,GAAG,EAAE,EAAC;QAErD,gCAA2C,IAAI,GAAG,EAAE,EAAC;QAM5D,KAAK,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,IAAI,OAAO,EAAE,CAAC;YAC3C,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,OAAgB,EAAE,SAAoB;QACxC,0EAA0E;QAC1E,4EAA4E;QAC5E,0DAA0D;QAC1D,IAAA,cAAM,EACJ,CAAC,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,EAClC,gCAAgC,OAAO,EAAE,CAC1C,CAAC;QACF,IAAA,cAAM,EACJ,CAAC,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,SAAS,CAAC,EACpC,kCAAkC,SAAS,EAAE,CAC9C,CAAC;QACF,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAC7C,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;OAKG;IACH,YAAY,CAAC,OAAgB;QAC3B,OAAO,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,SAAoB;QAC7B,OAAO,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC7C,CAAC;CACF;AAvDD,sBAuDC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { assert } from '@metamask/utils';\n\nimport type {\n AccountGroupPayloadId,\n AccountWalletPayloadId,\n} from './payload.js';\n\ntype LocalId = AccountWalletId | AccountGroupId;\ntype PayloadId = AccountWalletPayloadId | AccountGroupPayloadId;\n\n/**\n * Bidirectional map between local controller IDs and portable payload IDs.\n *\n * Populated during {@link exportState} so that callers can bridge between\n * device-local wallet/group IDs and the stable cross-device IDs that appear\n * in a serialized {@link AccountTreePayload}.\n */\nexport class IdMap {\n readonly #localToPayload: Map<LocalId, PayloadId> = new Map();\n\n readonly #payloadToLocal: Map<PayloadId, LocalId> = new Map();\n\n /**\n * @param entries - Optional seed pairs `[localId, payloadId]` to pre-populate the map.\n */\n constructor(entries: [localId: LocalId, payloadId: PayloadId][] = []) {\n for (const [localId, payloadId] of entries) {\n this.add(localId, payloadId);\n }\n }\n\n /**\n * Registers a local↔payload ID pair.\n *\n * @param localId - Local controller wallet or group ID.\n * @param payloadId - Corresponding portable payload ID.\n */\n add(localId: LocalId, payloadId: PayloadId): void {\n // Each local ID maps to exactly one payload ID and vice versa: wallet and\n // group IDs are unique by construction, so a duplicate signals a bug in the\n // caller (e.g. exportState visiting the same node twice).\n assert(\n !this.#localToPayload.has(localId),\n `Local ID already registered: ${localId}`,\n );\n assert(\n !this.#payloadToLocal.has(payloadId),\n `Payload ID already registered: ${payloadId}`,\n );\n this.#localToPayload.set(localId, payloadId);\n this.#payloadToLocal.set(payloadId, localId);\n }\n\n /**\n * Returns the portable payload ID for a given local ID.\n *\n * @param localId - Local controller wallet or group ID.\n * @returns The payload ID, or `undefined` if not registered.\n */\n getPayloadId(localId: LocalId): PayloadId | undefined {\n return this.#localToPayload.get(localId);\n }\n\n /**\n * Returns the local controller ID for a given payload ID.\n *\n * @param payloadId - Portable payload ID.\n * @returns The local wallet or group ID, or `undefined` if not registered.\n */\n getLocalId(payloadId: PayloadId): LocalId | undefined {\n return this.#payloadToLocal.get(payloadId);\n }\n}\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { AccountGroupId, AccountWalletId } from "@metamask/account-api";
|
|
2
|
+
import type { AccountGroupPayloadId, AccountWalletPayloadId } from "./payload.cjs";
|
|
3
|
+
type LocalId = AccountWalletId | AccountGroupId;
|
|
4
|
+
type PayloadId = AccountWalletPayloadId | AccountGroupPayloadId;
|
|
5
|
+
/**
|
|
6
|
+
* Bidirectional map between local controller IDs and portable payload IDs.
|
|
7
|
+
*
|
|
8
|
+
* Populated during {@link exportState} so that callers can bridge between
|
|
9
|
+
* device-local wallet/group IDs and the stable cross-device IDs that appear
|
|
10
|
+
* in a serialized {@link AccountTreePayload}.
|
|
11
|
+
*/
|
|
12
|
+
export declare class IdMap {
|
|
13
|
+
#private;
|
|
14
|
+
/**
|
|
15
|
+
* @param entries - Optional seed pairs `[localId, payloadId]` to pre-populate the map.
|
|
16
|
+
*/
|
|
17
|
+
constructor(entries?: [localId: LocalId, payloadId: PayloadId][]);
|
|
18
|
+
/**
|
|
19
|
+
* Registers a local↔payload ID pair.
|
|
20
|
+
*
|
|
21
|
+
* @param localId - Local controller wallet or group ID.
|
|
22
|
+
* @param payloadId - Corresponding portable payload ID.
|
|
23
|
+
*/
|
|
24
|
+
add(localId: LocalId, payloadId: PayloadId): void;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the portable payload ID for a given local ID.
|
|
27
|
+
*
|
|
28
|
+
* @param localId - Local controller wallet or group ID.
|
|
29
|
+
* @returns The payload ID, or `undefined` if not registered.
|
|
30
|
+
*/
|
|
31
|
+
getPayloadId(localId: LocalId): PayloadId | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the local controller ID for a given payload ID.
|
|
34
|
+
*
|
|
35
|
+
* @param payloadId - Portable payload ID.
|
|
36
|
+
* @returns The local wallet or group ID, or `undefined` if not registered.
|
|
37
|
+
*/
|
|
38
|
+
getLocalId(payloadId: PayloadId): LocalId | undefined;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
41
|
+
//# sourceMappingURL=id-map.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-map.d.cts","sourceRoot":"","sources":["../../src/state/id-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EACV,qBAAqB,EACrB,sBAAsB,EACvB,sBAAqB;AAEtB,KAAK,OAAO,GAAG,eAAe,GAAG,cAAc,CAAC;AAChD,KAAK,SAAS,GAAG,sBAAsB,GAAG,qBAAqB,CAAC;AAEhE;;;;;;GAMG;AACH,qBAAa,KAAK;;IAKhB;;OAEG;gBACS,OAAO,GAAE,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,EAAO;IAMpE;;;;;OAKG;IACH,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,GAAG,IAAI;IAgBjD;;;;;OAKG;IACH,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS;IAIrD;;;;;OAKG;IACH,UAAU,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,GAAG,SAAS;CAGtD"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { AccountGroupId, AccountWalletId } from "@metamask/account-api";
|
|
2
|
+
import type { AccountGroupPayloadId, AccountWalletPayloadId } from "./payload.mjs";
|
|
3
|
+
type LocalId = AccountWalletId | AccountGroupId;
|
|
4
|
+
type PayloadId = AccountWalletPayloadId | AccountGroupPayloadId;
|
|
5
|
+
/**
|
|
6
|
+
* Bidirectional map between local controller IDs and portable payload IDs.
|
|
7
|
+
*
|
|
8
|
+
* Populated during {@link exportState} so that callers can bridge between
|
|
9
|
+
* device-local wallet/group IDs and the stable cross-device IDs that appear
|
|
10
|
+
* in a serialized {@link AccountTreePayload}.
|
|
11
|
+
*/
|
|
12
|
+
export declare class IdMap {
|
|
13
|
+
#private;
|
|
14
|
+
/**
|
|
15
|
+
* @param entries - Optional seed pairs `[localId, payloadId]` to pre-populate the map.
|
|
16
|
+
*/
|
|
17
|
+
constructor(entries?: [localId: LocalId, payloadId: PayloadId][]);
|
|
18
|
+
/**
|
|
19
|
+
* Registers a local↔payload ID pair.
|
|
20
|
+
*
|
|
21
|
+
* @param localId - Local controller wallet or group ID.
|
|
22
|
+
* @param payloadId - Corresponding portable payload ID.
|
|
23
|
+
*/
|
|
24
|
+
add(localId: LocalId, payloadId: PayloadId): void;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the portable payload ID for a given local ID.
|
|
27
|
+
*
|
|
28
|
+
* @param localId - Local controller wallet or group ID.
|
|
29
|
+
* @returns The payload ID, or `undefined` if not registered.
|
|
30
|
+
*/
|
|
31
|
+
getPayloadId(localId: LocalId): PayloadId | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the local controller ID for a given payload ID.
|
|
34
|
+
*
|
|
35
|
+
* @param payloadId - Portable payload ID.
|
|
36
|
+
* @returns The local wallet or group ID, or `undefined` if not registered.
|
|
37
|
+
*/
|
|
38
|
+
getLocalId(payloadId: PayloadId): LocalId | undefined;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
41
|
+
//# sourceMappingURL=id-map.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-map.d.mts","sourceRoot":"","sources":["../../src/state/id-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EACV,qBAAqB,EACrB,sBAAsB,EACvB,sBAAqB;AAEtB,KAAK,OAAO,GAAG,eAAe,GAAG,cAAc,CAAC;AAChD,KAAK,SAAS,GAAG,sBAAsB,GAAG,qBAAqB,CAAC;AAEhE;;;;;;GAMG;AACH,qBAAa,KAAK;;IAKhB;;OAEG;gBACS,OAAO,GAAE,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,EAAO;IAMpE;;;;;OAKG;IACH,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,GAAG,IAAI;IAgBjD;;;;;OAKG;IACH,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS;IAIrD;;;;;OAKG;IACH,UAAU,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,GAAG,SAAS;CAGtD"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
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");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _IdMap_localToPayload, _IdMap_payloadToLocal;
|
|
7
|
+
import { assert } from "@metamask/utils";
|
|
8
|
+
/**
|
|
9
|
+
* Bidirectional map between local controller IDs and portable payload IDs.
|
|
10
|
+
*
|
|
11
|
+
* Populated during {@link exportState} so that callers can bridge between
|
|
12
|
+
* device-local wallet/group IDs and the stable cross-device IDs that appear
|
|
13
|
+
* in a serialized {@link AccountTreePayload}.
|
|
14
|
+
*/
|
|
15
|
+
export class IdMap {
|
|
16
|
+
/**
|
|
17
|
+
* @param entries - Optional seed pairs `[localId, payloadId]` to pre-populate the map.
|
|
18
|
+
*/
|
|
19
|
+
constructor(entries = []) {
|
|
20
|
+
_IdMap_localToPayload.set(this, new Map());
|
|
21
|
+
_IdMap_payloadToLocal.set(this, new Map());
|
|
22
|
+
for (const [localId, payloadId] of entries) {
|
|
23
|
+
this.add(localId, payloadId);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Registers a local↔payload ID pair.
|
|
28
|
+
*
|
|
29
|
+
* @param localId - Local controller wallet or group ID.
|
|
30
|
+
* @param payloadId - Corresponding portable payload ID.
|
|
31
|
+
*/
|
|
32
|
+
add(localId, payloadId) {
|
|
33
|
+
// Each local ID maps to exactly one payload ID and vice versa: wallet and
|
|
34
|
+
// group IDs are unique by construction, so a duplicate signals a bug in the
|
|
35
|
+
// caller (e.g. exportState visiting the same node twice).
|
|
36
|
+
assert(!__classPrivateFieldGet(this, _IdMap_localToPayload, "f").has(localId), `Local ID already registered: ${localId}`);
|
|
37
|
+
assert(!__classPrivateFieldGet(this, _IdMap_payloadToLocal, "f").has(payloadId), `Payload ID already registered: ${payloadId}`);
|
|
38
|
+
__classPrivateFieldGet(this, _IdMap_localToPayload, "f").set(localId, payloadId);
|
|
39
|
+
__classPrivateFieldGet(this, _IdMap_payloadToLocal, "f").set(payloadId, localId);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns the portable payload ID for a given local ID.
|
|
43
|
+
*
|
|
44
|
+
* @param localId - Local controller wallet or group ID.
|
|
45
|
+
* @returns The payload ID, or `undefined` if not registered.
|
|
46
|
+
*/
|
|
47
|
+
getPayloadId(localId) {
|
|
48
|
+
return __classPrivateFieldGet(this, _IdMap_localToPayload, "f").get(localId);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns the local controller ID for a given payload ID.
|
|
52
|
+
*
|
|
53
|
+
* @param payloadId - Portable payload ID.
|
|
54
|
+
* @returns The local wallet or group ID, or `undefined` if not registered.
|
|
55
|
+
*/
|
|
56
|
+
getLocalId(payloadId) {
|
|
57
|
+
return __classPrivateFieldGet(this, _IdMap_payloadToLocal, "f").get(payloadId);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
_IdMap_localToPayload = new WeakMap(), _IdMap_payloadToLocal = new WeakMap();
|
|
61
|
+
//# sourceMappingURL=id-map.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-map.mjs","sourceRoot":"","sources":["../../src/state/id-map.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAUzC;;;;;;GAMG;AACH,MAAM,OAAO,KAAK;IAKhB;;OAEG;IACH,YAAY,UAAsD,EAAE;QAP3D,gCAA2C,IAAI,GAAG,EAAE,EAAC;QAErD,gCAA2C,IAAI,GAAG,EAAE,EAAC;QAM5D,KAAK,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,IAAI,OAAO,EAAE,CAAC;YAC3C,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,OAAgB,EAAE,SAAoB;QACxC,0EAA0E;QAC1E,4EAA4E;QAC5E,0DAA0D;QAC1D,MAAM,CACJ,CAAC,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,EAClC,gCAAgC,OAAO,EAAE,CAC1C,CAAC;QACF,MAAM,CACJ,CAAC,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,SAAS,CAAC,EACpC,kCAAkC,SAAS,EAAE,CAC9C,CAAC;QACF,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAC7C,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;OAKG;IACH,YAAY,CAAC,OAAgB;QAC3B,OAAO,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,SAAoB;QAC7B,OAAO,uBAAA,IAAI,6BAAgB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC7C,CAAC;CACF","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { assert } from '@metamask/utils';\n\nimport type {\n AccountGroupPayloadId,\n AccountWalletPayloadId,\n} from './payload.js';\n\ntype LocalId = AccountWalletId | AccountGroupId;\ntype PayloadId = AccountWalletPayloadId | AccountGroupPayloadId;\n\n/**\n * Bidirectional map between local controller IDs and portable payload IDs.\n *\n * Populated during {@link exportState} so that callers can bridge between\n * device-local wallet/group IDs and the stable cross-device IDs that appear\n * in a serialized {@link AccountTreePayload}.\n */\nexport class IdMap {\n readonly #localToPayload: Map<LocalId, PayloadId> = new Map();\n\n readonly #payloadToLocal: Map<PayloadId, LocalId> = new Map();\n\n /**\n * @param entries - Optional seed pairs `[localId, payloadId]` to pre-populate the map.\n */\n constructor(entries: [localId: LocalId, payloadId: PayloadId][] = []) {\n for (const [localId, payloadId] of entries) {\n this.add(localId, payloadId);\n }\n }\n\n /**\n * Registers a local↔payload ID pair.\n *\n * @param localId - Local controller wallet or group ID.\n * @param payloadId - Corresponding portable payload ID.\n */\n add(localId: LocalId, payloadId: PayloadId): void {\n // Each local ID maps to exactly one payload ID and vice versa: wallet and\n // group IDs are unique by construction, so a duplicate signals a bug in the\n // caller (e.g. exportState visiting the same node twice).\n assert(\n !this.#localToPayload.has(localId),\n `Local ID already registered: ${localId}`,\n );\n assert(\n !this.#payloadToLocal.has(payloadId),\n `Payload ID already registered: ${payloadId}`,\n );\n this.#localToPayload.set(localId, payloadId);\n this.#payloadToLocal.set(payloadId, localId);\n }\n\n /**\n * Returns the portable payload ID for a given local ID.\n *\n * @param localId - Local controller wallet or group ID.\n * @returns The payload ID, or `undefined` if not registered.\n */\n getPayloadId(localId: LocalId): PayloadId | undefined {\n return this.#localToPayload.get(localId);\n }\n\n /**\n * Returns the local controller ID for a given payload ID.\n *\n * @param payloadId - Portable payload ID.\n * @returns The local wallet or group ID, or `undefined` if not registered.\n */\n getLocalId(payloadId: PayloadId): LocalId | undefined {\n return this.#payloadToLocal.get(payloadId);\n }\n}\n"]}
|