@metamask-previews/keyring-controller 25.2.0-preview-b800567ef → 25.2.0-preview-60aa3d02f
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 -13
- package/dist/KeyringController-method-action-types.cjs.map +1 -1
- package/dist/KeyringController-method-action-types.d.cts +1 -64
- package/dist/KeyringController-method-action-types.d.cts.map +1 -1
- package/dist/KeyringController-method-action-types.d.mts +1 -64
- 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 +66 -214
- package/dist/KeyringController.cjs.map +1 -1
- package/dist/KeyringController.d.cts +1 -89
- package/dist/KeyringController.d.cts.map +1 -1
- package/dist/KeyringController.d.mts +1 -89
- package/dist/KeyringController.d.mts.map +1 -1
- package/dist/KeyringController.mjs +65 -216
- 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 +17 -17
|
@@ -33,14 +33,17 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
33
33
|
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");
|
|
34
34
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
35
35
|
};
|
|
36
|
-
var
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
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;
|
|
37
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
41
|
exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
|
|
39
42
|
const util_1 = require("@ethereumjs/util");
|
|
40
43
|
const base_controller_1 = require("@metamask/base-controller");
|
|
41
44
|
const eth_hd_keyring_1 = require("@metamask/eth-hd-keyring");
|
|
42
45
|
const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
43
|
-
const eth_simple_keyring_1 =
|
|
46
|
+
const eth_simple_keyring_1 = __importDefault(require("@metamask/eth-simple-keyring"));
|
|
44
47
|
const utils_1 = require("@metamask/utils");
|
|
45
48
|
const async_mutex_1 = require("async-mutex");
|
|
46
49
|
const ethereumjs_wallet_1 = __importStar(require("ethereumjs-wallet"));
|
|
@@ -68,8 +71,6 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
68
71
|
'addNewAccount',
|
|
69
72
|
'withKeyring',
|
|
70
73
|
'withKeyringUnsafe',
|
|
71
|
-
'withKeyringV2',
|
|
72
|
-
'withKeyringV2Unsafe',
|
|
73
74
|
'addNewKeyring',
|
|
74
75
|
'createNewVaultAndKeychain',
|
|
75
76
|
'createNewVaultAndRestore',
|
|
@@ -148,17 +149,6 @@ const defaultKeyringBuilders = [
|
|
|
148
149
|
keyringBuilderFactory(eth_simple_keyring_1.default),
|
|
149
150
|
keyringBuilderFactory(eth_hd_keyring_1.HdKeyring),
|
|
150
151
|
];
|
|
151
|
-
const hdKeyringV2Builder = Object.assign((keyring, metadata) => new eth_hd_keyring_1.HdKeyringV2({
|
|
152
|
-
legacyKeyring: keyring,
|
|
153
|
-
entropySource: metadata.id,
|
|
154
|
-
}), { type: KeyringTypes.hd });
|
|
155
|
-
const simpleKeyringV2Builder = Object.assign((keyring) => new eth_simple_keyring_1.SimpleKeyringV2({
|
|
156
|
-
legacyKeyring: keyring,
|
|
157
|
-
}), { type: KeyringTypes.simple });
|
|
158
|
-
const defaultKeyringV2Builders = [
|
|
159
|
-
simpleKeyringV2Builder,
|
|
160
|
-
hdKeyringV2Builder,
|
|
161
|
-
];
|
|
162
152
|
const getDefaultKeyringState = () => {
|
|
163
153
|
return {
|
|
164
154
|
isUnlocked: false,
|
|
@@ -284,7 +274,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
284
274
|
* @param options.state - Initial state to set on this controller.
|
|
285
275
|
*/
|
|
286
276
|
constructor(options) {
|
|
287
|
-
const { encryptor, keyringBuilders,
|
|
277
|
+
const { encryptor, keyringBuilders, messenger, state } = options;
|
|
288
278
|
super({
|
|
289
279
|
name,
|
|
290
280
|
metadata: {
|
|
@@ -329,7 +319,6 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
329
319
|
_KeyringController_controllerOperationMutex.set(this, new async_mutex_1.Mutex());
|
|
330
320
|
_KeyringController_vaultOperationMutex.set(this, new async_mutex_1.Mutex());
|
|
331
321
|
_KeyringController_keyringBuilders.set(this, void 0);
|
|
332
|
-
_KeyringController_keyringV2Builders.set(this, void 0);
|
|
333
322
|
_KeyringController_encryptor.set(this, void 0);
|
|
334
323
|
_KeyringController_keyrings.set(this, void 0);
|
|
335
324
|
_KeyringController_unsupportedKeyrings.set(this, void 0);
|
|
@@ -337,9 +326,6 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
337
326
|
__classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
|
|
338
327
|
? keyringBuilders.concat(defaultKeyringBuilders)
|
|
339
328
|
: defaultKeyringBuilders, "f");
|
|
340
|
-
__classPrivateFieldSet(this, _KeyringController_keyringV2Builders, keyringV2Builders
|
|
341
|
-
? keyringV2Builders.concat(defaultKeyringV2Builders)
|
|
342
|
-
: defaultKeyringV2Builders, "f");
|
|
343
329
|
__classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
|
|
344
330
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
345
331
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -584,7 +570,9 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
584
570
|
*/
|
|
585
571
|
getKeyringsByType(type) {
|
|
586
572
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
587
|
-
return __classPrivateFieldGet(this,
|
|
573
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")
|
|
574
|
+
.filter(({ keyring }) => keyring.type === type)
|
|
575
|
+
.map(({ keyring }) => keyring);
|
|
588
576
|
}
|
|
589
577
|
/**
|
|
590
578
|
* Persist all serialized keyrings in the vault.
|
|
@@ -673,7 +661,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
673
661
|
if (keyringIndex === -1) {
|
|
674
662
|
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.NoKeyring);
|
|
675
663
|
}
|
|
676
|
-
const { keyring
|
|
664
|
+
const { keyring } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
677
665
|
const isPrimaryKeyring = keyringIndex === 0;
|
|
678
666
|
const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
|
|
679
667
|
// Primary keyring should never be removed, so we need to keep at least one account in it
|
|
@@ -694,7 +682,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
694
682
|
await keyring.removeAccount(address);
|
|
695
683
|
if (shouldRemoveKeyring) {
|
|
696
684
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").splice(keyringIndex, 1);
|
|
697
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring
|
|
685
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
698
686
|
}
|
|
699
687
|
});
|
|
700
688
|
this.messenger.publish(`${name}:accountRemoved`, address);
|
|
@@ -907,7 +895,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
907
895
|
* @returns Promise resolving when the operation completes.
|
|
908
896
|
*/
|
|
909
897
|
async submitEncryptionKey(encryptionKey, encryptionSalt) {
|
|
910
|
-
const {
|
|
898
|
+
const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
911
899
|
const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, {
|
|
912
900
|
encryptionKey,
|
|
913
901
|
encryptionSalt,
|
|
@@ -919,7 +907,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
919
907
|
// if new metadata has been generated during login, we
|
|
920
908
|
// can attempt to upgrade the vault.
|
|
921
909
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
922
|
-
if (
|
|
910
|
+
if (newMetadata) {
|
|
923
911
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
|
|
924
912
|
}
|
|
925
913
|
});
|
|
@@ -950,17 +938,17 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
950
938
|
* @returns Promise resolving when the operation completes.
|
|
951
939
|
*/
|
|
952
940
|
async submitPassword(password) {
|
|
953
|
-
const {
|
|
941
|
+
const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
954
942
|
const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, { password });
|
|
955
943
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
|
|
956
944
|
return result;
|
|
957
945
|
});
|
|
958
946
|
try {
|
|
959
947
|
// If there are stronger encryption params available, or
|
|
960
|
-
// if
|
|
948
|
+
// if new metadata has been generated during login, we
|
|
961
949
|
// can attempt to upgrade the vault.
|
|
962
950
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
963
|
-
if (
|
|
951
|
+
if (newMetadata || __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_isNewEncryptionAvailable).call(this)) {
|
|
964
952
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveAndSetEncryptionKey).call(this, password, {
|
|
965
953
|
// If the vault is being upgraded, we want to ignore the metadata
|
|
966
954
|
// that is already in the vault, so we can effectively
|
|
@@ -992,20 +980,17 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
992
980
|
}) {
|
|
993
981
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
994
982
|
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
995
|
-
let
|
|
996
|
-
|
|
997
|
-
selector,
|
|
998
|
-
});
|
|
999
|
-
if (!entry && 'type' in selector && options.createIfMissing) {
|
|
1000
|
-
const newKeyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
|
|
1001
|
-
entry = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring }) => keyring === newKeyring);
|
|
983
|
+
let keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyring).call(this, selector);
|
|
984
|
+
if (!keyring && 'type' in selector && options.createIfMissing) {
|
|
985
|
+
keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
|
|
1002
986
|
}
|
|
1003
|
-
if (!
|
|
987
|
+
if (!keyring) {
|
|
1004
988
|
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
|
|
1005
989
|
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
990
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
991
|
+
keyring,
|
|
992
|
+
metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
|
|
993
|
+
}), keyring);
|
|
1009
994
|
});
|
|
1010
995
|
}
|
|
1011
996
|
/**
|
|
@@ -1044,109 +1029,16 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
1044
1029
|
*/
|
|
1045
1030
|
async withKeyringUnsafe(selector, operation) {
|
|
1046
1031
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1047
|
-
const
|
|
1048
|
-
if (!
|
|
1032
|
+
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyring).call(this, selector);
|
|
1033
|
+
if (!keyring) {
|
|
1049
1034
|
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
|
|
1050
1035
|
}
|
|
1051
|
-
const { metadata } = entry;
|
|
1052
|
-
const keyring = entry.keyring;
|
|
1053
1036
|
// Even if this method is "unsafe", we still want to prevent returning
|
|
1054
1037
|
// the keyring directly.
|
|
1055
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
* the given operation with the wrapped keyring as a mutually
|
|
1060
|
-
* exclusive atomic operation.
|
|
1061
|
-
*
|
|
1062
|
-
* The cached `KeyringV2` adapter is retrieved from the keyring
|
|
1063
|
-
* entry.
|
|
1064
|
-
*
|
|
1065
|
-
* A `KeyringV2Builder` for the selected keyring's type must exist
|
|
1066
|
-
* (either as a default or registered via the `keyringV2Builders`
|
|
1067
|
-
* constructor option); otherwise an error is thrown.
|
|
1068
|
-
*
|
|
1069
|
-
* The method automatically persists changes at the end of the
|
|
1070
|
-
* function execution, or rolls back the changes if an error
|
|
1071
|
-
* is thrown.
|
|
1072
|
-
*
|
|
1073
|
-
* @param selector - Keyring selector object.
|
|
1074
|
-
* @param operation - Function to execute with the wrapped V2 keyring.
|
|
1075
|
-
* @returns Promise resolving to the result of the function execution.
|
|
1076
|
-
* @template CallbackResult - The type of the value resolved by the callback function.
|
|
1077
|
-
*/
|
|
1078
|
-
async withKeyringV2(selector, operation) {
|
|
1079
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1080
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
1081
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1082
|
-
v2: true,
|
|
1083
|
-
selector,
|
|
1084
|
-
});
|
|
1085
|
-
if (!entry) {
|
|
1086
|
-
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
|
|
1087
|
-
}
|
|
1088
|
-
if (!entry.keyringV2) {
|
|
1089
|
-
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1090
|
-
}
|
|
1091
|
-
const { metadata } = entry;
|
|
1092
|
-
const keyring = entry.keyringV2;
|
|
1093
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
1094
|
-
keyring,
|
|
1095
|
-
metadata,
|
|
1096
|
-
}), keyring);
|
|
1097
|
-
});
|
|
1098
|
-
}
|
|
1099
|
-
/**
|
|
1100
|
-
* Select a keyring, wrap it in a `KeyringV2` adapter, and execute
|
|
1101
|
-
* the given read-only operation **without** acquiring the controller's
|
|
1102
|
-
* mutual exclusion lock.
|
|
1103
|
-
*
|
|
1104
|
-
* ## When to use this method
|
|
1105
|
-
*
|
|
1106
|
-
* This method is an escape hatch for read-only access to keyring data that
|
|
1107
|
-
* is immutable once the keyring is initialized. A typical safe use case is
|
|
1108
|
-
* reading immutable fields from a `KeyringV2` adapter: data that is set
|
|
1109
|
-
* during initialization and never mutated afterwards.
|
|
1110
|
-
*
|
|
1111
|
-
* ## Why it is "unsafe"
|
|
1112
|
-
*
|
|
1113
|
-
* The "unsafe" designation mirrors the semantics of `unsafe { }` blocks in
|
|
1114
|
-
* Rust: the method itself does not enforce thread-safety guarantees. By
|
|
1115
|
-
* calling this method the **caller** explicitly takes responsibility for
|
|
1116
|
-
* ensuring that:
|
|
1117
|
-
*
|
|
1118
|
-
* - The operation is **read-only** — no state is mutated.
|
|
1119
|
-
* - The data being read is **immutable** after the keyring is initialized,
|
|
1120
|
-
* so concurrent locked operations cannot alter it while this callback
|
|
1121
|
-
* runs.
|
|
1122
|
-
*
|
|
1123
|
-
* Do **not** use this method to:
|
|
1124
|
-
* - Mutate keyring state (add accounts, sign, etc.) — use `withKeyringV2`.
|
|
1125
|
-
* - Read mutable fields that could change during concurrent operations.
|
|
1126
|
-
*
|
|
1127
|
-
* @param selector - Keyring selector object.
|
|
1128
|
-
* @param operation - Read-only function to execute with the wrapped V2 keyring.
|
|
1129
|
-
* @returns Promise resolving to the result of the function execution.
|
|
1130
|
-
* @template SelectedKeyring - The type of the selected V2 keyring.
|
|
1131
|
-
* @template CallbackResult - The type of the value resolved by the callback function.
|
|
1132
|
-
*/
|
|
1133
|
-
async withKeyringV2Unsafe(selector, operation) {
|
|
1134
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1135
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1136
|
-
v2: true,
|
|
1137
|
-
selector,
|
|
1138
|
-
});
|
|
1139
|
-
if (!entry) {
|
|
1140
|
-
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
|
|
1141
|
-
}
|
|
1142
|
-
if (!entry.keyringV2) {
|
|
1143
|
-
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1144
|
-
}
|
|
1145
|
-
const { metadata } = entry;
|
|
1146
|
-
const keyring = entry.keyringV2;
|
|
1147
|
-
// Even if this method is "unsafe", we still want to prevent returning
|
|
1148
|
-
// the keyring directly.
|
|
1149
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({ keyring, metadata }), keyring);
|
|
1038
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
1039
|
+
keyring,
|
|
1040
|
+
metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
|
|
1041
|
+
}), keyring);
|
|
1150
1042
|
}
|
|
1151
1043
|
async getAccountKeyringType(account) {
|
|
1152
1044
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
@@ -1155,15 +1047,11 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
1155
1047
|
}
|
|
1156
1048
|
}
|
|
1157
1049
|
exports.KeyringController = KeyringController;
|
|
1158
|
-
_KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(),
|
|
1159
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1160
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, account);
|
|
1161
|
-
return entry?.keyring;
|
|
1162
|
-
}, _KeyringController_getKeyringEntryForAccount = async function _KeyringController_getKeyringEntryForAccount(account) {
|
|
1050
|
+
_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) {
|
|
1163
1051
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1164
1052
|
const keyringIndex = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_findKeyringIndexForAccount).call(this, account);
|
|
1165
1053
|
if (keyringIndex > -1) {
|
|
1166
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
1054
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex].keyring;
|
|
1167
1055
|
}
|
|
1168
1056
|
return undefined;
|
|
1169
1057
|
}, _KeyringController_findKeyringIndexForAccount = async function _KeyringController_findKeyringIndexForAccount(account) {
|
|
@@ -1171,9 +1059,6 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1171
1059
|
const address = account.toLowerCase();
|
|
1172
1060
|
const accountsPerKeyring = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(({ keyring }) => keyring.getAccounts()));
|
|
1173
1061
|
return accountsPerKeyring.findIndex((accounts) => accounts.map((a) => a.toLowerCase()).includes(address));
|
|
1174
|
-
}, _KeyringController_getKeyringEntriesByType = function _KeyringController_getKeyringEntriesByType(type) {
|
|
1175
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1176
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter(({ keyring }) => keyring.type === type);
|
|
1177
1062
|
}, _KeyringController_assertNoUnsafeDirectKeyringAccess = function _KeyringController_assertNoUnsafeDirectKeyringAccess(value, keyring) {
|
|
1178
1063
|
if (Object.is(value, keyring)) {
|
|
1179
1064
|
// Access to a keyring instance outside of controller safeguards
|
|
@@ -1185,46 +1070,32 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1185
1070
|
return value;
|
|
1186
1071
|
}, _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
|
|
1187
1072
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
1188
|
-
},
|
|
1073
|
+
}, _KeyringController_selectKeyring =
|
|
1189
1074
|
/**
|
|
1190
|
-
* Select a keyring
|
|
1075
|
+
* Select a keyring using a selector without acquiring the controller lock.
|
|
1191
1076
|
*
|
|
1192
|
-
* @param
|
|
1193
|
-
* @
|
|
1194
|
-
* @param options.selector - Keyring selector object.
|
|
1195
|
-
* @returns The selected keyring entry, or `undefined` if no match is found.
|
|
1077
|
+
* @param selector - Keyring selector object.
|
|
1078
|
+
* @returns The selected keyring, or `undefined` if no match is found.
|
|
1196
1079
|
* @template SelectedKeyring - The expected type of the selected keyring.
|
|
1197
|
-
* @template SelectedKeyringV2 - The expected type of the selected keyring (v2).
|
|
1198
1080
|
*/
|
|
1199
|
-
async function
|
|
1200
|
-
let
|
|
1081
|
+
async function _KeyringController_selectKeyring(selector) {
|
|
1082
|
+
let keyring;
|
|
1201
1083
|
if ('address' in selector) {
|
|
1202
|
-
|
|
1084
|
+
keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringForAccount).call(this, selector.address));
|
|
1203
1085
|
}
|
|
1204
1086
|
else if ('type' in selector) {
|
|
1205
|
-
|
|
1087
|
+
keyring = this.getKeyringsByType(selector.type)[selector.index ?? 0];
|
|
1206
1088
|
}
|
|
1207
1089
|
else if ('id' in selector) {
|
|
1208
|
-
|
|
1090
|
+
keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
|
|
1209
1091
|
}
|
|
1210
1092
|
else if ('filter' in selector) {
|
|
1211
|
-
|
|
1212
|
-
// If v2, then we'll use the v2 selector which expects a `KeyringV2` instance.
|
|
1213
|
-
if (v2) {
|
|
1214
|
-
// However, some keyrings do not have a v2 wrapper, so we just skip them.
|
|
1215
|
-
if (!keyringV2) {
|
|
1216
|
-
return false;
|
|
1217
|
-
}
|
|
1218
|
-
return selector.filter(keyringV2, metadata);
|
|
1219
|
-
}
|
|
1220
|
-
return selector.filter(keyring, metadata);
|
|
1221
|
-
});
|
|
1093
|
+
keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring: filteredKeyring, metadata }) => selector.filter(filteredKeyring, metadata))?.keyring;
|
|
1222
1094
|
}
|
|
1223
|
-
return
|
|
1095
|
+
return keyring;
|
|
1224
1096
|
}, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
|
|
1225
|
-
return __classPrivateFieldGet(this,
|
|
1226
|
-
|
|
1227
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId);
|
|
1097
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId)
|
|
1098
|
+
?.keyring;
|
|
1228
1099
|
}, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
|
|
1229
1100
|
if (!keyringId) {
|
|
1230
1101
|
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
|
|
@@ -1238,8 +1109,6 @@ async function _KeyringController_selectKeyringEntry({ v2, selector, }) {
|
|
|
1238
1109
|
return keyringWithMetadata.metadata;
|
|
1239
1110
|
}, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
|
|
1240
1111
|
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
1112
|
}, _KeyringController_createNewVaultWithKeyring =
|
|
1244
1113
|
/**
|
|
1245
1114
|
* Create new vault with an initial keyring
|
|
@@ -1427,18 +1296,18 @@ async function _KeyringController_getSessionState() {
|
|
|
1427
1296
|
async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings) {
|
|
1428
1297
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
|
|
1429
1298
|
const keyrings = [];
|
|
1430
|
-
let
|
|
1299
|
+
let newMetadata = false;
|
|
1431
1300
|
for (const serializedKeyring of serializedKeyrings) {
|
|
1432
1301
|
const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
|
|
1433
1302
|
if (result) {
|
|
1434
1303
|
const { keyring, metadata } = result;
|
|
1435
1304
|
keyrings.push({ keyring, metadata });
|
|
1436
|
-
if (result.
|
|
1437
|
-
|
|
1305
|
+
if (result.newMetadata) {
|
|
1306
|
+
newMetadata = true;
|
|
1438
1307
|
}
|
|
1439
1308
|
}
|
|
1440
1309
|
}
|
|
1441
|
-
return { keyrings,
|
|
1310
|
+
return { keyrings, newMetadata };
|
|
1442
1311
|
}, _KeyringController_unlockKeyrings =
|
|
1443
1312
|
/**
|
|
1444
1313
|
* Unlock Keyrings, decrypting the vault and deserializing all
|
|
@@ -1468,14 +1337,14 @@ async function _KeyringController_unlockKeyrings(credentials) {
|
|
|
1468
1337
|
if (!isSerializedKeyringsArray(vault)) {
|
|
1469
1338
|
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.VaultDataError);
|
|
1470
1339
|
}
|
|
1471
|
-
const { keyrings,
|
|
1340
|
+
const { keyrings, newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
|
|
1472
1341
|
const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
|
|
1473
1342
|
this.update((state) => {
|
|
1474
1343
|
state.keyrings = updatedKeyrings;
|
|
1475
1344
|
state.encryptionKey = encryptionKey;
|
|
1476
1345
|
state.encryptionSalt = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.salt;
|
|
1477
1346
|
});
|
|
1478
|
-
return { keyrings,
|
|
1347
|
+
return { keyrings, newMetadata };
|
|
1479
1348
|
});
|
|
1480
1349
|
}, _KeyringController_updateVault = function _KeyringController_updateVault() {
|
|
1481
1350
|
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
|
|
@@ -1563,8 +1432,8 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
|
1563
1432
|
* @throws If the keyring includes duplicated accounts.
|
|
1564
1433
|
*/
|
|
1565
1434
|
async function _KeyringController_newKeyring(type, data) {
|
|
1566
|
-
const
|
|
1567
|
-
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring,
|
|
1435
|
+
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1436
|
+
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, metadata: getDefaultKeyringMetadata() });
|
|
1568
1437
|
return keyring;
|
|
1569
1438
|
}, _KeyringController_createKeyring =
|
|
1570
1439
|
/**
|
|
@@ -1581,13 +1450,11 @@ async function _KeyringController_newKeyring(type, data) {
|
|
|
1581
1450
|
*
|
|
1582
1451
|
* @param type - The type of keyring to add.
|
|
1583
1452
|
* @param data - Keyring initialization options.
|
|
1584
|
-
* @param metadata - Keyring metadata if available.
|
|
1585
1453
|
* @returns The new keyring.
|
|
1586
1454
|
* @throws If the keyring includes duplicated accounts.
|
|
1587
1455
|
*/
|
|
1588
|
-
async function _KeyringController_createKeyring(type, data
|
|
1456
|
+
async function _KeyringController_createKeyring(type, data) {
|
|
1589
1457
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1590
|
-
const keyringMetadata = metadata ?? getDefaultKeyringMetadata();
|
|
1591
1458
|
const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
|
|
1592
1459
|
if (!keyringBuilder) {
|
|
1593
1460
|
throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
|
|
@@ -1611,13 +1478,7 @@ async function _KeyringController_createKeyring(type, data, metadata) {
|
|
|
1611
1478
|
await keyring.generateRandomMnemonic();
|
|
1612
1479
|
await keyring.addAccounts(1);
|
|
1613
1480
|
}
|
|
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 };
|
|
1481
|
+
return keyring;
|
|
1621
1482
|
}, _KeyringController_clearKeyrings =
|
|
1622
1483
|
/**
|
|
1623
1484
|
* Remove all managed keyrings, destroying all their
|
|
@@ -1625,8 +1486,8 @@ async function _KeyringController_createKeyring(type, data, metadata) {
|
|
|
1625
1486
|
*/
|
|
1626
1487
|
async function _KeyringController_clearKeyrings() {
|
|
1627
1488
|
__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
|
|
1489
|
+
for (const { keyring } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
|
|
1490
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
1630
1491
|
}
|
|
1631
1492
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
1632
1493
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -1642,28 +1503,23 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1642
1503
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1643
1504
|
try {
|
|
1644
1505
|
const { type, data, metadata: serializedMetadata } = serialized;
|
|
1645
|
-
|
|
1646
|
-
let hasChanged = false;
|
|
1647
|
-
// If metadata is missing, assume the data is from an installation before we had
|
|
1648
|
-
// keyring metadata.
|
|
1506
|
+
let newMetadata = false;
|
|
1649
1507
|
let metadata = serializedMetadata;
|
|
1508
|
+
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1509
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
|
|
1510
|
+
// If metadata is missing, assume the data is from an installation before
|
|
1511
|
+
// we had keyring metadata.
|
|
1650
1512
|
if (!metadata) {
|
|
1651
|
-
|
|
1513
|
+
newMetadata = true;
|
|
1652
1514
|
metadata = getDefaultKeyringMetadata();
|
|
1653
1515
|
}
|
|
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
1516
|
// The keyring is added to the keyrings array only if it's successfully restored
|
|
1660
1517
|
// and the metadata is successfully added to the controller
|
|
1661
1518
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
|
|
1662
1519
|
keyring,
|
|
1663
|
-
keyringV2,
|
|
1664
1520
|
metadata,
|
|
1665
1521
|
});
|
|
1666
|
-
return { keyring,
|
|
1522
|
+
return { keyring, metadata, newMetadata };
|
|
1667
1523
|
}
|
|
1668
1524
|
catch (error) {
|
|
1669
1525
|
console.error(error);
|
|
@@ -1679,13 +1535,9 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1679
1535
|
* clears the keyring bridge iframe from the DOM.
|
|
1680
1536
|
*
|
|
1681
1537
|
* @param keyring - The keyring to destroy.
|
|
1682
|
-
* @param keyringV2 - The keyring v2 to destroy (if any).
|
|
1683
1538
|
*/
|
|
1684
|
-
async function _KeyringController_destroyKeyring(keyring
|
|
1539
|
+
async function _KeyringController_destroyKeyring(keyring) {
|
|
1685
1540
|
await keyring.destroy?.();
|
|
1686
|
-
if (keyringV2) {
|
|
1687
|
-
await keyringV2.destroy?.();
|
|
1688
|
-
}
|
|
1689
1541
|
}, _KeyringController_assertNoDuplicateAccounts =
|
|
1690
1542
|
/**
|
|
1691
1543
|
* Assert that there are no duplicate accounts in the keyrings.
|
|
@@ -1723,7 +1575,7 @@ async function _KeyringController_persistOrRollback(callback) {
|
|
|
1723
1575
|
const callbackResult = await callback({ releaseLock });
|
|
1724
1576
|
const newState = JSON.stringify(await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSessionState).call(this));
|
|
1725
1577
|
// State is committed only if the operation is successful and need to trigger a vault update.
|
|
1726
|
-
if (oldState
|
|
1578
|
+
if (!(0, lodash_1.isEqual)(oldState, newState)) {
|
|
1727
1579
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
|
|
1728
1580
|
}
|
|
1729
1581
|
return callbackResult;
|