@metamask-previews/keyring-controller 25.2.0-preview-6b4f746 → 25.2.0-preview-6e596eb
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 +0 -10
- package/dist/KeyringController-method-action-types.cjs.map +1 -1
- package/dist/KeyringController-method-action-types.d.cts +1 -67
- package/dist/KeyringController-method-action-types.d.cts.map +1 -1
- package/dist/KeyringController-method-action-types.d.mts +1 -67
- package/dist/KeyringController-method-action-types.d.mts.map +1 -1
- package/dist/KeyringController-method-action-types.mjs.map +1 -1
- package/dist/KeyringController.cjs +39 -206
- package/dist/KeyringController.cjs.map +1 -1
- package/dist/KeyringController.d.cts +1 -92
- package/dist/KeyringController.d.cts.map +1 -1
- package/dist/KeyringController.d.mts +1 -92
- package/dist/KeyringController.d.mts.map +1 -1
- package/dist/KeyringController.mjs +37 -207
- package/dist/KeyringController.mjs.map +1 -1
- package/dist/constants.cjs +0 -1
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +1 -2
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +1 -2
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +0 -1
- package/dist/constants.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders,
|
|
12
|
+
var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_encryptionKey, _KeyringController_getKeyringForAccount, _KeyringController_findKeyringIndexForAccount, _KeyringController_assertNoUnsafeDirectKeyringAccess, _KeyringController_registerMessageHandlers, _KeyringController_selectKeyring, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_createNewVaultWithKeyring, _KeyringController_deriveAndSetEncryptionKey, _KeyringController_setEncryptionKey, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_getSessionState, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_isNewEncryptionAvailable, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_assertNoDuplicateAccounts, _KeyringController_setUnlocked, _KeyringController_assertIsUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
|
|
13
13
|
function $importDefault(module) {
|
|
14
14
|
if (module?.__esModule) {
|
|
15
15
|
return module.default;
|
|
@@ -18,9 +18,9 @@ function $importDefault(module) {
|
|
|
18
18
|
}
|
|
19
19
|
import { isValidPrivate, getBinarySize } from "@ethereumjs/util";
|
|
20
20
|
import { BaseController } from "@metamask/base-controller";
|
|
21
|
-
import { HdKeyring
|
|
21
|
+
import { HdKeyring } from "@metamask/eth-hd-keyring";
|
|
22
22
|
import { normalize as ethNormalize } from "@metamask/eth-sig-util";
|
|
23
|
-
import SimpleKeyring
|
|
23
|
+
import SimpleKeyring from "@metamask/eth-simple-keyring";
|
|
24
24
|
import { add0x, assertIsStrictHexString, bytesToHex, hasProperty, hexToBytes, isObject, isStrictHexString, isValidHexAddress, isValidJson, remove0x } from "@metamask/utils";
|
|
25
25
|
import { Mutex } from "async-mutex";
|
|
26
26
|
import $Wallet from "ethereumjs-wallet";
|
|
@@ -51,8 +51,6 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
51
51
|
'addNewAccount',
|
|
52
52
|
'withKeyring',
|
|
53
53
|
'withKeyringUnsafe',
|
|
54
|
-
'withKeyringV2',
|
|
55
|
-
'withKeyringV2Unsafe',
|
|
56
54
|
'addNewKeyring',
|
|
57
55
|
'createNewVaultAndKeychain',
|
|
58
56
|
'createNewVaultAndRestore',
|
|
@@ -129,17 +127,6 @@ const defaultKeyringBuilders = [
|
|
|
129
127
|
keyringBuilderFactory(SimpleKeyring),
|
|
130
128
|
keyringBuilderFactory(HdKeyring),
|
|
131
129
|
];
|
|
132
|
-
const hdKeyringV2Builder = Object.assign((keyring, metadata) => new HdKeyringV2({
|
|
133
|
-
legacyKeyring: keyring,
|
|
134
|
-
entropySource: metadata.id,
|
|
135
|
-
}), { type: KeyringTypes.hd });
|
|
136
|
-
const simpleKeyringV2Builder = Object.assign((keyring) => new SimpleKeyringV2({
|
|
137
|
-
legacyKeyring: keyring,
|
|
138
|
-
}), { type: KeyringTypes.simple });
|
|
139
|
-
const defaultKeyringV2Builders = [
|
|
140
|
-
simpleKeyringV2Builder,
|
|
141
|
-
hdKeyringV2Builder,
|
|
142
|
-
];
|
|
143
130
|
export const getDefaultKeyringState = () => {
|
|
144
131
|
return {
|
|
145
132
|
isUnlocked: false,
|
|
@@ -264,7 +251,7 @@ export class KeyringController extends BaseController {
|
|
|
264
251
|
* @param options.state - Initial state to set on this controller.
|
|
265
252
|
*/
|
|
266
253
|
constructor(options) {
|
|
267
|
-
const { encryptor, keyringBuilders,
|
|
254
|
+
const { encryptor, keyringBuilders, messenger, state } = options;
|
|
268
255
|
super({
|
|
269
256
|
name,
|
|
270
257
|
metadata: {
|
|
@@ -309,7 +296,6 @@ export class KeyringController extends BaseController {
|
|
|
309
296
|
_KeyringController_controllerOperationMutex.set(this, new Mutex());
|
|
310
297
|
_KeyringController_vaultOperationMutex.set(this, new Mutex());
|
|
311
298
|
_KeyringController_keyringBuilders.set(this, void 0);
|
|
312
|
-
_KeyringController_keyringV2Builders.set(this, void 0);
|
|
313
299
|
_KeyringController_encryptor.set(this, void 0);
|
|
314
300
|
_KeyringController_keyrings.set(this, void 0);
|
|
315
301
|
_KeyringController_unsupportedKeyrings.set(this, void 0);
|
|
@@ -317,9 +303,6 @@ export class KeyringController extends BaseController {
|
|
|
317
303
|
__classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
|
|
318
304
|
? keyringBuilders.concat(defaultKeyringBuilders)
|
|
319
305
|
: defaultKeyringBuilders, "f");
|
|
320
|
-
__classPrivateFieldSet(this, _KeyringController_keyringV2Builders, keyringV2Builders
|
|
321
|
-
? keyringV2Builders.concat(defaultKeyringV2Builders)
|
|
322
|
-
: defaultKeyringV2Builders, "f");
|
|
323
306
|
__classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
|
|
324
307
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
325
308
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -564,7 +547,9 @@ export class KeyringController extends BaseController {
|
|
|
564
547
|
*/
|
|
565
548
|
getKeyringsByType(type) {
|
|
566
549
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
567
|
-
return __classPrivateFieldGet(this,
|
|
550
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")
|
|
551
|
+
.filter(({ keyring }) => keyring.type === type)
|
|
552
|
+
.map(({ keyring }) => keyring);
|
|
568
553
|
}
|
|
569
554
|
/**
|
|
570
555
|
* Persist all serialized keyrings in the vault.
|
|
@@ -653,7 +638,7 @@ export class KeyringController extends BaseController {
|
|
|
653
638
|
if (keyringIndex === -1) {
|
|
654
639
|
throw new KeyringControllerError(KeyringControllerErrorMessage.NoKeyring);
|
|
655
640
|
}
|
|
656
|
-
const { keyring
|
|
641
|
+
const { keyring } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
657
642
|
const isPrimaryKeyring = keyringIndex === 0;
|
|
658
643
|
const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
|
|
659
644
|
// Primary keyring should never be removed, so we need to keep at least one account in it
|
|
@@ -674,7 +659,7 @@ export class KeyringController extends BaseController {
|
|
|
674
659
|
await keyring.removeAccount(address);
|
|
675
660
|
if (shouldRemoveKeyring) {
|
|
676
661
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").splice(keyringIndex, 1);
|
|
677
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring
|
|
662
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
678
663
|
}
|
|
679
664
|
});
|
|
680
665
|
this.messenger.publish(`${name}:accountRemoved`, address);
|
|
@@ -1032,124 +1017,17 @@ export class KeyringController extends BaseController {
|
|
|
1032
1017
|
metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
|
|
1033
1018
|
}), keyring);
|
|
1034
1019
|
}
|
|
1035
|
-
/**
|
|
1036
|
-
* Select a keyring, wrap it in a `KeyringV2` adapter, and execute
|
|
1037
|
-
* the given operation with the wrapped keyring as a mutually
|
|
1038
|
-
* exclusive atomic operation.
|
|
1039
|
-
*
|
|
1040
|
-
* We re-wrap the keyring in a `KeyringV2` adapter on each invocation,
|
|
1041
|
-
* since V2 wrappers are ephemeral adapters created on-the-fly, and cheap to create.
|
|
1042
|
-
*
|
|
1043
|
-
* The method automatically persists changes at the end of the
|
|
1044
|
-
* function execution, or rolls back the changes if an error
|
|
1045
|
-
* is thrown.
|
|
1046
|
-
*
|
|
1047
|
-
* A `KeyringV2Builder` for the selected keyring's type must exist
|
|
1048
|
-
* (either as a default or registered via the `keyringV2Builders`
|
|
1049
|
-
* constructor option); otherwise an error is thrown.
|
|
1050
|
-
*
|
|
1051
|
-
* Selection is performed against the V1 keyrings in `#keyrings`, since
|
|
1052
|
-
* V2 wrappers are ephemeral adapters created on-the-fly.
|
|
1053
|
-
*
|
|
1054
|
-
* @param selector - Keyring selector object.
|
|
1055
|
-
* @param operation - Function to execute with the wrapped V2 keyring.
|
|
1056
|
-
* @returns Promise resolving to the result of the function execution.
|
|
1057
|
-
* @template CallbackResult - The type of the value resolved by the callback function.
|
|
1058
|
-
*/
|
|
1059
|
-
async withKeyringV2(selector, operation) {
|
|
1060
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1061
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
1062
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1063
|
-
v2: true,
|
|
1064
|
-
selector,
|
|
1065
|
-
});
|
|
1066
|
-
if (!entry) {
|
|
1067
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
|
|
1068
|
-
}
|
|
1069
|
-
if (!entry.keyringV2) {
|
|
1070
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1071
|
-
}
|
|
1072
|
-
const { metadata } = entry;
|
|
1073
|
-
const keyring = entry.keyringV2;
|
|
1074
|
-
const result = await operation({
|
|
1075
|
-
keyring,
|
|
1076
|
-
metadata,
|
|
1077
|
-
});
|
|
1078
|
-
if (Object.is(result, keyring)) {
|
|
1079
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
|
|
1080
|
-
}
|
|
1081
|
-
return result;
|
|
1082
|
-
});
|
|
1083
|
-
}
|
|
1084
|
-
/**
|
|
1085
|
-
* Select a keyring, wrap it in a `KeyringV2` adapter, and execute
|
|
1086
|
-
* the given read-only operation **without** acquiring the controller's
|
|
1087
|
-
* mutual exclusion lock.
|
|
1088
|
-
*
|
|
1089
|
-
* ## When to use this method
|
|
1090
|
-
*
|
|
1091
|
-
* This method is an escape hatch for read-only access to keyring data that
|
|
1092
|
-
* is immutable once the keyring is initialized. A typical safe use case is
|
|
1093
|
-
* reading immutable fields from a `KeyringV2` adapter: data that is set
|
|
1094
|
-
* during initialization and never mutated afterwards.
|
|
1095
|
-
*
|
|
1096
|
-
* ## Why it is "unsafe"
|
|
1097
|
-
*
|
|
1098
|
-
* The "unsafe" designation mirrors the semantics of `unsafe { }` blocks in
|
|
1099
|
-
* Rust: the method itself does not enforce thread-safety guarantees. By
|
|
1100
|
-
* calling this method the **caller** explicitly takes responsibility for
|
|
1101
|
-
* ensuring that:
|
|
1102
|
-
*
|
|
1103
|
-
* - The operation is **read-only** — no state is mutated.
|
|
1104
|
-
* - The data being read is **immutable** after the keyring is initialized,
|
|
1105
|
-
* so concurrent locked operations cannot alter it while this callback
|
|
1106
|
-
* runs.
|
|
1107
|
-
*
|
|
1108
|
-
* Do **not** use this method to:
|
|
1109
|
-
* - Mutate keyring state (add accounts, sign, etc.) — use `withKeyringV2`.
|
|
1110
|
-
* - Read mutable fields that could change during concurrent operations.
|
|
1111
|
-
*
|
|
1112
|
-
* @param selector - Keyring selector object.
|
|
1113
|
-
* @param operation - Read-only function to execute with the wrapped V2 keyring.
|
|
1114
|
-
* @returns Promise resolving to the result of the function execution.
|
|
1115
|
-
* @template SelectedKeyring - The type of the selected V2 keyring.
|
|
1116
|
-
* @template CallbackResult - The type of the value resolved by the callback function.
|
|
1117
|
-
*/
|
|
1118
|
-
async withKeyringV2Unsafe(selector, operation) {
|
|
1119
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1120
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1121
|
-
v2: true,
|
|
1122
|
-
selector,
|
|
1123
|
-
});
|
|
1124
|
-
if (!entry) {
|
|
1125
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
|
|
1126
|
-
}
|
|
1127
|
-
if (!entry.keyringV2) {
|
|
1128
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1129
|
-
}
|
|
1130
|
-
const { metadata } = entry;
|
|
1131
|
-
const keyring = entry.keyringV2;
|
|
1132
|
-
const result = await operation({ keyring, metadata });
|
|
1133
|
-
if (Object.is(result, keyring)) {
|
|
1134
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
|
|
1135
|
-
}
|
|
1136
|
-
return result;
|
|
1137
|
-
}
|
|
1138
1020
|
async getAccountKeyringType(account) {
|
|
1139
1021
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1140
1022
|
const keyring = (await this.getKeyringForAccount(account));
|
|
1141
1023
|
return keyring.type;
|
|
1142
1024
|
}
|
|
1143
1025
|
}
|
|
1144
|
-
_KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(),
|
|
1145
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1146
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, account);
|
|
1147
|
-
return entry?.keyring;
|
|
1148
|
-
}, _KeyringController_getKeyringEntryForAccount = async function _KeyringController_getKeyringEntryForAccount(account) {
|
|
1026
|
+
_KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_encryptionKey = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_getKeyringForAccount = async function _KeyringController_getKeyringForAccount(account) {
|
|
1149
1027
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1150
1028
|
const keyringIndex = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_findKeyringIndexForAccount).call(this, account);
|
|
1151
1029
|
if (keyringIndex > -1) {
|
|
1152
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
1030
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex].keyring;
|
|
1153
1031
|
}
|
|
1154
1032
|
return undefined;
|
|
1155
1033
|
}, _KeyringController_findKeyringIndexForAccount = async function _KeyringController_findKeyringIndexForAccount(account) {
|
|
@@ -1157,9 +1035,6 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1157
1035
|
const address = account.toLowerCase();
|
|
1158
1036
|
const accountsPerKeyring = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(({ keyring }) => keyring.getAccounts()));
|
|
1159
1037
|
return accountsPerKeyring.findIndex((accounts) => accounts.map((a) => a.toLowerCase()).includes(address));
|
|
1160
|
-
}, _KeyringController_getKeyringEntriesByType = function _KeyringController_getKeyringEntriesByType(type) {
|
|
1161
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1162
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter(({ keyring }) => keyring.type === type);
|
|
1163
1038
|
}, _KeyringController_assertNoUnsafeDirectKeyringAccess = function _KeyringController_assertNoUnsafeDirectKeyringAccess(value, keyring) {
|
|
1164
1039
|
if (Object.is(value, keyring)) {
|
|
1165
1040
|
// Access to a keyring instance outside of controller safeguards
|
|
@@ -1171,60 +1046,32 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1171
1046
|
return value;
|
|
1172
1047
|
}, _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
|
|
1173
1048
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
1174
|
-
},
|
|
1049
|
+
}, _KeyringController_selectKeyring =
|
|
1175
1050
|
/**
|
|
1176
|
-
* Select a keyring
|
|
1051
|
+
* Select a keyring using a selector without acquiring the controller lock.
|
|
1177
1052
|
*
|
|
1178
|
-
* @param
|
|
1179
|
-
* @
|
|
1180
|
-
* @param options.selector - Keyring selector object.
|
|
1181
|
-
* @returns The selected keyring entry, or `undefined` if no match is found.
|
|
1053
|
+
* @param selector - Keyring selector object.
|
|
1054
|
+
* @returns The selected keyring, or `undefined` if no match is found.
|
|
1182
1055
|
* @template SelectedKeyring - The expected type of the selected keyring.
|
|
1183
|
-
* @template SelectedKeyringV2 - The expected type of the selected keyring (v2).
|
|
1184
1056
|
*/
|
|
1185
|
-
async function
|
|
1186
|
-
let
|
|
1057
|
+
async function _KeyringController_selectKeyring(selector) {
|
|
1058
|
+
let keyring;
|
|
1187
1059
|
if ('address' in selector) {
|
|
1188
|
-
|
|
1060
|
+
keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringForAccount).call(this, selector.address));
|
|
1189
1061
|
}
|
|
1190
1062
|
else if ('type' in selector) {
|
|
1191
|
-
|
|
1063
|
+
keyring = this.getKeyringsByType(selector.type)[selector.index ?? 0];
|
|
1192
1064
|
}
|
|
1193
1065
|
else if ('id' in selector) {
|
|
1194
|
-
|
|
1066
|
+
keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
|
|
1195
1067
|
}
|
|
1196
1068
|
else if ('filter' in selector) {
|
|
1197
|
-
|
|
1198
|
-
// If v2, then we'll use the v2 selector which expects a `KeyringV2` instance.
|
|
1199
|
-
if (v2) {
|
|
1200
|
-
// However, some keyrings do not have a v2 wrapper, so we just skip them.
|
|
1201
|
-
if (!keyringV2) {
|
|
1202
|
-
return false;
|
|
1203
|
-
}
|
|
1204
|
-
return selector.filter(keyringV2, metadata);
|
|
1205
|
-
}
|
|
1206
|
-
return selector.filter(keyring, metadata);
|
|
1207
|
-
});
|
|
1069
|
+
keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring: filteredKeyring, metadata }) => selector.filter(filteredKeyring, metadata))?.keyring;
|
|
1208
1070
|
}
|
|
1209
|
-
return
|
|
1210
|
-
}, _KeyringController_selectKeyring =
|
|
1211
|
-
/**
|
|
1212
|
-
* Select a keyring using a selector without acquiring the controller lock.
|
|
1213
|
-
*
|
|
1214
|
-
* @param selector - Keyring selector object.
|
|
1215
|
-
* @returns The selected keyring, or `undefined` if no match is found.
|
|
1216
|
-
* @template SelectedKeyring - The expected type of the selected keyring.
|
|
1217
|
-
*/
|
|
1218
|
-
async function _KeyringController_selectKeyring(selector) {
|
|
1219
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1220
|
-
v2: false,
|
|
1221
|
-
selector,
|
|
1222
|
-
});
|
|
1223
|
-
return entry?.keyring;
|
|
1071
|
+
return keyring;
|
|
1224
1072
|
}, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
|
|
1225
|
-
return __classPrivateFieldGet(this,
|
|
1226
|
-
|
|
1227
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId);
|
|
1073
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId)
|
|
1074
|
+
?.keyring;
|
|
1228
1075
|
}, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
|
|
1229
1076
|
if (!keyringId) {
|
|
1230
1077
|
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
|
|
@@ -1238,8 +1085,6 @@ async function _KeyringController_selectKeyring(selector) {
|
|
|
1238
1085
|
return keyringWithMetadata.metadata;
|
|
1239
1086
|
}, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
|
|
1240
1087
|
return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
|
|
1241
|
-
}, _KeyringController_getKeyringV2BuilderForType = function _KeyringController_getKeyringV2BuilderForType(type) {
|
|
1242
|
-
return __classPrivateFieldGet(this, _KeyringController_keyringV2Builders, "f").find((builder) => builder.type === type);
|
|
1243
1088
|
}, _KeyringController_createNewVaultWithKeyring =
|
|
1244
1089
|
/**
|
|
1245
1090
|
* Create new vault with an initial keyring
|
|
@@ -1563,8 +1408,8 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
|
1563
1408
|
* @throws If the keyring includes duplicated accounts.
|
|
1564
1409
|
*/
|
|
1565
1410
|
async function _KeyringController_newKeyring(type, data) {
|
|
1566
|
-
const
|
|
1567
|
-
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring,
|
|
1411
|
+
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1412
|
+
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, metadata: getDefaultKeyringMetadata() });
|
|
1568
1413
|
return keyring;
|
|
1569
1414
|
}, _KeyringController_createKeyring =
|
|
1570
1415
|
/**
|
|
@@ -1581,13 +1426,11 @@ async function _KeyringController_newKeyring(type, data) {
|
|
|
1581
1426
|
*
|
|
1582
1427
|
* @param type - The type of keyring to add.
|
|
1583
1428
|
* @param data - Keyring initialization options.
|
|
1584
|
-
* @param metadata - Keyring metadata if available.
|
|
1585
1429
|
* @returns The new keyring.
|
|
1586
1430
|
* @throws If the keyring includes duplicated accounts.
|
|
1587
1431
|
*/
|
|
1588
|
-
async function _KeyringController_createKeyring(type, data
|
|
1432
|
+
async function _KeyringController_createKeyring(type, data) {
|
|
1589
1433
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1590
|
-
const keyringMetadata = metadata ?? getDefaultKeyringMetadata();
|
|
1591
1434
|
const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
|
|
1592
1435
|
if (!keyringBuilder) {
|
|
1593
1436
|
throw new KeyringControllerError(`${KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
|
|
@@ -1611,13 +1454,7 @@ async function _KeyringController_createKeyring(type, data, metadata) {
|
|
|
1611
1454
|
await keyring.generateRandomMnemonic();
|
|
1612
1455
|
await keyring.addAccounts(1);
|
|
1613
1456
|
}
|
|
1614
|
-
|
|
1615
|
-
const keyringBuilderV2 = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringV2BuilderForType).call(this, type);
|
|
1616
|
-
let keyringV2;
|
|
1617
|
-
if (keyringBuilderV2) {
|
|
1618
|
-
keyringV2 = keyringBuilderV2(keyring, keyringMetadata);
|
|
1619
|
-
}
|
|
1620
|
-
return { keyring, keyringV2, metadata: keyringMetadata };
|
|
1457
|
+
return keyring;
|
|
1621
1458
|
}, _KeyringController_clearKeyrings =
|
|
1622
1459
|
/**
|
|
1623
1460
|
* Remove all managed keyrings, destroying all their
|
|
@@ -1625,8 +1462,8 @@ async function _KeyringController_createKeyring(type, data, metadata) {
|
|
|
1625
1462
|
*/
|
|
1626
1463
|
async function _KeyringController_clearKeyrings() {
|
|
1627
1464
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1628
|
-
for (const { keyring
|
|
1629
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring
|
|
1465
|
+
for (const { keyring } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
|
|
1466
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
1630
1467
|
}
|
|
1631
1468
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
1632
1469
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -1642,8 +1479,11 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1642
1479
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1643
1480
|
try {
|
|
1644
1481
|
const { type, data, metadata: serializedMetadata } = serialized;
|
|
1645
|
-
|
|
1646
|
-
|
|
1482
|
+
const oldState = JSON.stringify(data);
|
|
1483
|
+
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1484
|
+
const newState = JSON.stringify(await keyring.serialize());
|
|
1485
|
+
let hasChanged = oldState !== newState;
|
|
1486
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
|
|
1647
1487
|
// If metadata is missing, assume the data is from an installation before we had
|
|
1648
1488
|
// keyring metadata.
|
|
1649
1489
|
let metadata = serializedMetadata;
|
|
@@ -1651,19 +1491,13 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1651
1491
|
hasChanged = true;
|
|
1652
1492
|
metadata = getDefaultKeyringMetadata();
|
|
1653
1493
|
}
|
|
1654
|
-
const oldState = JSON.stringify(data);
|
|
1655
|
-
const { keyring, keyringV2 } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data, metadata);
|
|
1656
|
-
const newState = JSON.stringify(await keyring.serialize());
|
|
1657
|
-
hasChanged || (hasChanged = oldState !== newState);
|
|
1658
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
|
|
1659
1494
|
// The keyring is added to the keyrings array only if it's successfully restored
|
|
1660
1495
|
// and the metadata is successfully added to the controller
|
|
1661
1496
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
|
|
1662
1497
|
keyring,
|
|
1663
|
-
keyringV2,
|
|
1664
1498
|
metadata,
|
|
1665
1499
|
});
|
|
1666
|
-
return { keyring,
|
|
1500
|
+
return { keyring, metadata, hasChanged };
|
|
1667
1501
|
}
|
|
1668
1502
|
catch (error) {
|
|
1669
1503
|
console.error(error);
|
|
@@ -1679,13 +1513,9 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1679
1513
|
* clears the keyring bridge iframe from the DOM.
|
|
1680
1514
|
*
|
|
1681
1515
|
* @param keyring - The keyring to destroy.
|
|
1682
|
-
* @param keyringV2 - The keyring v2 to destroy (if any).
|
|
1683
1516
|
*/
|
|
1684
|
-
async function _KeyringController_destroyKeyring(keyring
|
|
1517
|
+
async function _KeyringController_destroyKeyring(keyring) {
|
|
1685
1518
|
await keyring.destroy?.();
|
|
1686
|
-
if (keyringV2) {
|
|
1687
|
-
await keyringV2.destroy?.();
|
|
1688
|
-
}
|
|
1689
1519
|
}, _KeyringController_assertNoDuplicateAccounts =
|
|
1690
1520
|
/**
|
|
1691
1521
|
* Assert that there are no duplicate accounts in the keyrings.
|