@metamask-previews/keyring-controller 25.2.0-preview-548bdd1d9 → 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 -17
- 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 +62 -215
- package/dist/KeyringController.cjs.map +1 -1
- package/dist/KeyringController.d.cts +1 -90
- package/dist/KeyringController.d.cts.map +1 -1
- package/dist/KeyringController.d.mts +1 -90
- package/dist/KeyringController.d.mts.map +1 -1
- package/dist/KeyringController.mjs +63 -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 +19 -19
|
@@ -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;
|
|
@@ -19,17 +19,15 @@ function $importDefault(module) {
|
|
|
19
19
|
import { isValidPrivate, getBinarySize } from "@ethereumjs/util";
|
|
20
20
|
import { BaseController } from "@metamask/base-controller";
|
|
21
21
|
import { HdKeyring } from "@metamask/eth-hd-keyring";
|
|
22
|
-
import { HdKeyring as HdKeyringV2 } from "@metamask/eth-hd-keyring/v2";
|
|
23
22
|
import { normalize as ethNormalize } from "@metamask/eth-sig-util";
|
|
24
23
|
import SimpleKeyring from "@metamask/eth-simple-keyring";
|
|
25
|
-
import { SimpleKeyring as SimpleKeyringV2 } from "@metamask/eth-simple-keyring/v2";
|
|
26
24
|
import { add0x, assertIsStrictHexString, bytesToHex, hasProperty, hexToBytes, isObject, isStrictHexString, isValidHexAddress, isValidJson, remove0x } from "@metamask/utils";
|
|
27
25
|
import { Mutex } from "async-mutex";
|
|
28
26
|
import $Wallet from "ethereumjs-wallet";
|
|
29
27
|
const { thirdparty: importers } = $Wallet;
|
|
30
28
|
const Wallet = $importDefault($Wallet);
|
|
31
29
|
import $lodash from "lodash";
|
|
32
|
-
const { cloneDeep } = $lodash;
|
|
30
|
+
const { cloneDeep, isEqual } = $lodash;
|
|
33
31
|
// When generating a ULID within the same millisecond, monotonicFactory provides some guarantees regarding sort order.
|
|
34
32
|
import { ulid } from "ulid";
|
|
35
33
|
import { KeyringControllerErrorMessage } from "./constants.mjs";
|
|
@@ -53,8 +51,6 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
53
51
|
'addNewAccount',
|
|
54
52
|
'withKeyring',
|
|
55
53
|
'withKeyringUnsafe',
|
|
56
|
-
'withKeyringV2',
|
|
57
|
-
'withKeyringV2Unsafe',
|
|
58
54
|
'addNewKeyring',
|
|
59
55
|
'createNewVaultAndKeychain',
|
|
60
56
|
'createNewVaultAndRestore',
|
|
@@ -131,17 +127,6 @@ const defaultKeyringBuilders = [
|
|
|
131
127
|
keyringBuilderFactory(SimpleKeyring),
|
|
132
128
|
keyringBuilderFactory(HdKeyring),
|
|
133
129
|
];
|
|
134
|
-
const hdKeyringV2Builder = Object.assign((keyring, metadata) => new HdKeyringV2({
|
|
135
|
-
legacyKeyring: keyring,
|
|
136
|
-
entropySource: metadata.id,
|
|
137
|
-
}), { type: KeyringTypes.hd });
|
|
138
|
-
const simpleKeyringV2Builder = Object.assign((keyring) => new SimpleKeyringV2({
|
|
139
|
-
legacyKeyring: keyring,
|
|
140
|
-
}), { type: KeyringTypes.simple });
|
|
141
|
-
const defaultKeyringV2Builders = [
|
|
142
|
-
simpleKeyringV2Builder,
|
|
143
|
-
hdKeyringV2Builder,
|
|
144
|
-
];
|
|
145
130
|
export const getDefaultKeyringState = () => {
|
|
146
131
|
return {
|
|
147
132
|
isUnlocked: false,
|
|
@@ -266,7 +251,7 @@ export class KeyringController extends BaseController {
|
|
|
266
251
|
* @param options.state - Initial state to set on this controller.
|
|
267
252
|
*/
|
|
268
253
|
constructor(options) {
|
|
269
|
-
const { encryptor, keyringBuilders,
|
|
254
|
+
const { encryptor, keyringBuilders, messenger, state } = options;
|
|
270
255
|
super({
|
|
271
256
|
name,
|
|
272
257
|
metadata: {
|
|
@@ -311,7 +296,6 @@ export class KeyringController extends BaseController {
|
|
|
311
296
|
_KeyringController_controllerOperationMutex.set(this, new Mutex());
|
|
312
297
|
_KeyringController_vaultOperationMutex.set(this, new Mutex());
|
|
313
298
|
_KeyringController_keyringBuilders.set(this, void 0);
|
|
314
|
-
_KeyringController_keyringV2Builders.set(this, void 0);
|
|
315
299
|
_KeyringController_encryptor.set(this, void 0);
|
|
316
300
|
_KeyringController_keyrings.set(this, void 0);
|
|
317
301
|
_KeyringController_unsupportedKeyrings.set(this, void 0);
|
|
@@ -319,9 +303,6 @@ export class KeyringController extends BaseController {
|
|
|
319
303
|
__classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
|
|
320
304
|
? keyringBuilders.concat(defaultKeyringBuilders)
|
|
321
305
|
: defaultKeyringBuilders, "f");
|
|
322
|
-
__classPrivateFieldSet(this, _KeyringController_keyringV2Builders, keyringV2Builders
|
|
323
|
-
? keyringV2Builders.concat(defaultKeyringV2Builders)
|
|
324
|
-
: defaultKeyringV2Builders, "f");
|
|
325
306
|
__classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
|
|
326
307
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
327
308
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -566,7 +547,9 @@ export class KeyringController extends BaseController {
|
|
|
566
547
|
*/
|
|
567
548
|
getKeyringsByType(type) {
|
|
568
549
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
569
|
-
return __classPrivateFieldGet(this,
|
|
550
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")
|
|
551
|
+
.filter(({ keyring }) => keyring.type === type)
|
|
552
|
+
.map(({ keyring }) => keyring);
|
|
570
553
|
}
|
|
571
554
|
/**
|
|
572
555
|
* Persist all serialized keyrings in the vault.
|
|
@@ -655,7 +638,7 @@ export class KeyringController extends BaseController {
|
|
|
655
638
|
if (keyringIndex === -1) {
|
|
656
639
|
throw new KeyringControllerError(KeyringControllerErrorMessage.NoKeyring);
|
|
657
640
|
}
|
|
658
|
-
const { keyring
|
|
641
|
+
const { keyring } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
659
642
|
const isPrimaryKeyring = keyringIndex === 0;
|
|
660
643
|
const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
|
|
661
644
|
// Primary keyring should never be removed, so we need to keep at least one account in it
|
|
@@ -676,7 +659,7 @@ export class KeyringController extends BaseController {
|
|
|
676
659
|
await keyring.removeAccount(address);
|
|
677
660
|
if (shouldRemoveKeyring) {
|
|
678
661
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").splice(keyringIndex, 1);
|
|
679
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring
|
|
662
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
680
663
|
}
|
|
681
664
|
});
|
|
682
665
|
this.messenger.publish(`${name}:accountRemoved`, address);
|
|
@@ -889,7 +872,7 @@ export class KeyringController extends BaseController {
|
|
|
889
872
|
* @returns Promise resolving when the operation completes.
|
|
890
873
|
*/
|
|
891
874
|
async submitEncryptionKey(encryptionKey, encryptionSalt) {
|
|
892
|
-
const {
|
|
875
|
+
const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
893
876
|
const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, {
|
|
894
877
|
encryptionKey,
|
|
895
878
|
encryptionSalt,
|
|
@@ -901,7 +884,7 @@ export class KeyringController extends BaseController {
|
|
|
901
884
|
// if new metadata has been generated during login, we
|
|
902
885
|
// can attempt to upgrade the vault.
|
|
903
886
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
904
|
-
if (
|
|
887
|
+
if (newMetadata) {
|
|
905
888
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
|
|
906
889
|
}
|
|
907
890
|
});
|
|
@@ -932,17 +915,17 @@ export class KeyringController extends BaseController {
|
|
|
932
915
|
* @returns Promise resolving when the operation completes.
|
|
933
916
|
*/
|
|
934
917
|
async submitPassword(password) {
|
|
935
|
-
const {
|
|
918
|
+
const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
936
919
|
const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, { password });
|
|
937
920
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
|
|
938
921
|
return result;
|
|
939
922
|
});
|
|
940
923
|
try {
|
|
941
924
|
// If there are stronger encryption params available, or
|
|
942
|
-
// if
|
|
925
|
+
// if new metadata has been generated during login, we
|
|
943
926
|
// can attempt to upgrade the vault.
|
|
944
927
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
|
|
945
|
-
if (
|
|
928
|
+
if (newMetadata || __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_isNewEncryptionAvailable).call(this)) {
|
|
946
929
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveAndSetEncryptionKey).call(this, password, {
|
|
947
930
|
// If the vault is being upgraded, we want to ignore the metadata
|
|
948
931
|
// that is already in the vault, so we can effectively
|
|
@@ -974,20 +957,17 @@ export class KeyringController extends BaseController {
|
|
|
974
957
|
}) {
|
|
975
958
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
976
959
|
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
977
|
-
let
|
|
978
|
-
|
|
979
|
-
selector,
|
|
980
|
-
});
|
|
981
|
-
if (!entry && 'type' in selector && options.createIfMissing) {
|
|
982
|
-
const newKeyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
|
|
983
|
-
entry = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring }) => keyring === newKeyring);
|
|
960
|
+
let keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyring).call(this, selector);
|
|
961
|
+
if (!keyring && 'type' in selector && options.createIfMissing) {
|
|
962
|
+
keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
|
|
984
963
|
}
|
|
985
|
-
if (!
|
|
964
|
+
if (!keyring) {
|
|
986
965
|
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
|
|
987
966
|
}
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
967
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
968
|
+
keyring,
|
|
969
|
+
metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
|
|
970
|
+
}), keyring);
|
|
991
971
|
});
|
|
992
972
|
}
|
|
993
973
|
/**
|
|
@@ -1026,109 +1006,16 @@ export class KeyringController extends BaseController {
|
|
|
1026
1006
|
*/
|
|
1027
1007
|
async withKeyringUnsafe(selector, operation) {
|
|
1028
1008
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1029
|
-
const
|
|
1030
|
-
if (!
|
|
1031
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
|
|
1032
|
-
}
|
|
1033
|
-
const { metadata } = entry;
|
|
1034
|
-
const keyring = entry.keyring;
|
|
1035
|
-
// Even if this method is "unsafe", we still want to prevent returning
|
|
1036
|
-
// the keyring directly.
|
|
1037
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({ keyring, metadata }), keyring);
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* Select a keyring using its `KeyringV2` adapter, and execute
|
|
1041
|
-
* the given operation with the wrapped keyring as a mutually
|
|
1042
|
-
* exclusive atomic operation.
|
|
1043
|
-
*
|
|
1044
|
-
* The cached `KeyringV2` adapter is retrieved from the keyring
|
|
1045
|
-
* entry.
|
|
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
|
-
* The method automatically persists changes at the end of the
|
|
1052
|
-
* function execution, or rolls back the changes if an error
|
|
1053
|
-
* is thrown.
|
|
1054
|
-
*
|
|
1055
|
-
* @param selector - Keyring selector object.
|
|
1056
|
-
* @param operation - Function to execute with the wrapped V2 keyring.
|
|
1057
|
-
* @returns Promise resolving to the result of the function execution.
|
|
1058
|
-
* @template CallbackResult - The type of the value resolved by the callback function.
|
|
1059
|
-
*/
|
|
1060
|
-
async withKeyringV2(selector, operation) {
|
|
1061
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1062
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
1063
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1064
|
-
v2: true,
|
|
1065
|
-
selector,
|
|
1066
|
-
});
|
|
1067
|
-
if (!entry) {
|
|
1068
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
|
|
1069
|
-
}
|
|
1070
|
-
if (!entry.keyringV2) {
|
|
1071
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1072
|
-
}
|
|
1073
|
-
const { metadata } = entry;
|
|
1074
|
-
const keyring = entry.keyringV2;
|
|
1075
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
1076
|
-
keyring,
|
|
1077
|
-
metadata,
|
|
1078
|
-
}), keyring);
|
|
1079
|
-
});
|
|
1080
|
-
}
|
|
1081
|
-
/**
|
|
1082
|
-
* Select a keyring, wrap it in a `KeyringV2` adapter, and execute
|
|
1083
|
-
* the given read-only operation **without** acquiring the controller's
|
|
1084
|
-
* mutual exclusion lock.
|
|
1085
|
-
*
|
|
1086
|
-
* ## When to use this method
|
|
1087
|
-
*
|
|
1088
|
-
* This method is an escape hatch for read-only access to keyring data that
|
|
1089
|
-
* is immutable once the keyring is initialized. A typical safe use case is
|
|
1090
|
-
* reading immutable fields from a `KeyringV2` adapter: data that is set
|
|
1091
|
-
* during initialization and never mutated afterwards.
|
|
1092
|
-
*
|
|
1093
|
-
* ## Why it is "unsafe"
|
|
1094
|
-
*
|
|
1095
|
-
* The "unsafe" designation mirrors the semantics of `unsafe { }` blocks in
|
|
1096
|
-
* Rust: the method itself does not enforce thread-safety guarantees. By
|
|
1097
|
-
* calling this method the **caller** explicitly takes responsibility for
|
|
1098
|
-
* ensuring that:
|
|
1099
|
-
*
|
|
1100
|
-
* - The operation is **read-only** — no state is mutated.
|
|
1101
|
-
* - The data being read is **immutable** after the keyring is initialized,
|
|
1102
|
-
* so concurrent locked operations cannot alter it while this callback
|
|
1103
|
-
* runs.
|
|
1104
|
-
*
|
|
1105
|
-
* Do **not** use this method to:
|
|
1106
|
-
* - Mutate keyring state (add accounts, sign, etc.) — use `withKeyringV2`.
|
|
1107
|
-
* - Read mutable fields that could change during concurrent operations.
|
|
1108
|
-
*
|
|
1109
|
-
* @param selector - Keyring selector object.
|
|
1110
|
-
* @param operation - Read-only function to execute with the wrapped V2 keyring.
|
|
1111
|
-
* @returns Promise resolving to the result of the function execution.
|
|
1112
|
-
* @template SelectedKeyring - The type of the selected V2 keyring.
|
|
1113
|
-
* @template CallbackResult - The type of the value resolved by the callback function.
|
|
1114
|
-
*/
|
|
1115
|
-
async withKeyringV2Unsafe(selector, operation) {
|
|
1116
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1117
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1118
|
-
v2: true,
|
|
1119
|
-
selector,
|
|
1120
|
-
});
|
|
1121
|
-
if (!entry) {
|
|
1009
|
+
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyring).call(this, selector);
|
|
1010
|
+
if (!keyring) {
|
|
1122
1011
|
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
|
|
1123
1012
|
}
|
|
1124
|
-
if (!entry.keyringV2) {
|
|
1125
|
-
throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1126
|
-
}
|
|
1127
|
-
const { metadata } = entry;
|
|
1128
|
-
const keyring = entry.keyringV2;
|
|
1129
1013
|
// Even if this method is "unsafe", we still want to prevent returning
|
|
1130
1014
|
// the keyring directly.
|
|
1131
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
1015
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
|
|
1016
|
+
keyring,
|
|
1017
|
+
metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
|
|
1018
|
+
}), keyring);
|
|
1132
1019
|
}
|
|
1133
1020
|
async getAccountKeyringType(account) {
|
|
1134
1021
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
@@ -1136,15 +1023,11 @@ export class KeyringController extends BaseController {
|
|
|
1136
1023
|
return keyring.type;
|
|
1137
1024
|
}
|
|
1138
1025
|
}
|
|
1139
|
-
_KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(),
|
|
1140
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1141
|
-
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, account);
|
|
1142
|
-
return entry?.keyring;
|
|
1143
|
-
}, _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) {
|
|
1144
1027
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1145
1028
|
const keyringIndex = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_findKeyringIndexForAccount).call(this, account);
|
|
1146
1029
|
if (keyringIndex > -1) {
|
|
1147
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
1030
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex].keyring;
|
|
1148
1031
|
}
|
|
1149
1032
|
return undefined;
|
|
1150
1033
|
}, _KeyringController_findKeyringIndexForAccount = async function _KeyringController_findKeyringIndexForAccount(account) {
|
|
@@ -1152,9 +1035,6 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1152
1035
|
const address = account.toLowerCase();
|
|
1153
1036
|
const accountsPerKeyring = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(({ keyring }) => keyring.getAccounts()));
|
|
1154
1037
|
return accountsPerKeyring.findIndex((accounts) => accounts.map((a) => a.toLowerCase()).includes(address));
|
|
1155
|
-
}, _KeyringController_getKeyringEntriesByType = function _KeyringController_getKeyringEntriesByType(type) {
|
|
1156
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1157
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter(({ keyring }) => keyring.type === type);
|
|
1158
1038
|
}, _KeyringController_assertNoUnsafeDirectKeyringAccess = function _KeyringController_assertNoUnsafeDirectKeyringAccess(value, keyring) {
|
|
1159
1039
|
if (Object.is(value, keyring)) {
|
|
1160
1040
|
// Access to a keyring instance outside of controller safeguards
|
|
@@ -1166,46 +1046,32 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1166
1046
|
return value;
|
|
1167
1047
|
}, _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
|
|
1168
1048
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
1169
|
-
},
|
|
1049
|
+
}, _KeyringController_selectKeyring =
|
|
1170
1050
|
/**
|
|
1171
|
-
* Select a keyring
|
|
1051
|
+
* Select a keyring using a selector without acquiring the controller lock.
|
|
1172
1052
|
*
|
|
1173
|
-
* @param
|
|
1174
|
-
* @
|
|
1175
|
-
* @param options.selector - Keyring selector object.
|
|
1176
|
-
* @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.
|
|
1177
1055
|
* @template SelectedKeyring - The expected type of the selected keyring.
|
|
1178
|
-
* @template SelectedKeyringV2 - The expected type of the selected keyring (v2).
|
|
1179
1056
|
*/
|
|
1180
|
-
async function
|
|
1181
|
-
let
|
|
1057
|
+
async function _KeyringController_selectKeyring(selector) {
|
|
1058
|
+
let keyring;
|
|
1182
1059
|
if ('address' in selector) {
|
|
1183
|
-
|
|
1060
|
+
keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringForAccount).call(this, selector.address));
|
|
1184
1061
|
}
|
|
1185
1062
|
else if ('type' in selector) {
|
|
1186
|
-
|
|
1063
|
+
keyring = this.getKeyringsByType(selector.type)[selector.index ?? 0];
|
|
1187
1064
|
}
|
|
1188
1065
|
else if ('id' in selector) {
|
|
1189
|
-
|
|
1066
|
+
keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
|
|
1190
1067
|
}
|
|
1191
1068
|
else if ('filter' in selector) {
|
|
1192
|
-
|
|
1193
|
-
// If v2, then we'll use the v2 selector which expects a `KeyringV2` instance.
|
|
1194
|
-
if (v2) {
|
|
1195
|
-
// However, some keyrings do not have a v2 wrapper, so we just skip them.
|
|
1196
|
-
if (!keyringV2) {
|
|
1197
|
-
return false;
|
|
1198
|
-
}
|
|
1199
|
-
return selector.filter(keyringV2, metadata);
|
|
1200
|
-
}
|
|
1201
|
-
return selector.filter(keyring, metadata);
|
|
1202
|
-
});
|
|
1069
|
+
keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring: filteredKeyring, metadata }) => selector.filter(filteredKeyring, metadata))?.keyring;
|
|
1203
1070
|
}
|
|
1204
|
-
return
|
|
1071
|
+
return keyring;
|
|
1205
1072
|
}, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
|
|
1206
|
-
return __classPrivateFieldGet(this,
|
|
1207
|
-
|
|
1208
|
-
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;
|
|
1209
1075
|
}, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
|
|
1210
1076
|
if (!keyringId) {
|
|
1211
1077
|
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
|
|
@@ -1219,8 +1085,6 @@ async function _KeyringController_selectKeyringEntry({ v2, selector, }) {
|
|
|
1219
1085
|
return keyringWithMetadata.metadata;
|
|
1220
1086
|
}, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
|
|
1221
1087
|
return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
|
|
1222
|
-
}, _KeyringController_getKeyringV2BuilderForType = function _KeyringController_getKeyringV2BuilderForType(type) {
|
|
1223
|
-
return __classPrivateFieldGet(this, _KeyringController_keyringV2Builders, "f").find((builder) => builder.type === type);
|
|
1224
1088
|
}, _KeyringController_createNewVaultWithKeyring =
|
|
1225
1089
|
/**
|
|
1226
1090
|
* Create new vault with an initial keyring
|
|
@@ -1408,18 +1272,18 @@ async function _KeyringController_getSessionState() {
|
|
|
1408
1272
|
async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings) {
|
|
1409
1273
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
|
|
1410
1274
|
const keyrings = [];
|
|
1411
|
-
let
|
|
1275
|
+
let newMetadata = false;
|
|
1412
1276
|
for (const serializedKeyring of serializedKeyrings) {
|
|
1413
1277
|
const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
|
|
1414
1278
|
if (result) {
|
|
1415
1279
|
const { keyring, metadata } = result;
|
|
1416
1280
|
keyrings.push({ keyring, metadata });
|
|
1417
|
-
if (result.
|
|
1418
|
-
|
|
1281
|
+
if (result.newMetadata) {
|
|
1282
|
+
newMetadata = true;
|
|
1419
1283
|
}
|
|
1420
1284
|
}
|
|
1421
1285
|
}
|
|
1422
|
-
return { keyrings,
|
|
1286
|
+
return { keyrings, newMetadata };
|
|
1423
1287
|
}, _KeyringController_unlockKeyrings =
|
|
1424
1288
|
/**
|
|
1425
1289
|
* Unlock Keyrings, decrypting the vault and deserializing all
|
|
@@ -1449,14 +1313,14 @@ async function _KeyringController_unlockKeyrings(credentials) {
|
|
|
1449
1313
|
if (!isSerializedKeyringsArray(vault)) {
|
|
1450
1314
|
throw new KeyringControllerError(KeyringControllerErrorMessage.VaultDataError);
|
|
1451
1315
|
}
|
|
1452
|
-
const { keyrings,
|
|
1316
|
+
const { keyrings, newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
|
|
1453
1317
|
const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
|
|
1454
1318
|
this.update((state) => {
|
|
1455
1319
|
state.keyrings = updatedKeyrings;
|
|
1456
1320
|
state.encryptionKey = encryptionKey;
|
|
1457
1321
|
state.encryptionSalt = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.salt;
|
|
1458
1322
|
});
|
|
1459
|
-
return { keyrings,
|
|
1323
|
+
return { keyrings, newMetadata };
|
|
1460
1324
|
});
|
|
1461
1325
|
}, _KeyringController_updateVault = function _KeyringController_updateVault() {
|
|
1462
1326
|
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
|
|
@@ -1544,8 +1408,8 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
|
1544
1408
|
* @throws If the keyring includes duplicated accounts.
|
|
1545
1409
|
*/
|
|
1546
1410
|
async function _KeyringController_newKeyring(type, data) {
|
|
1547
|
-
const
|
|
1548
|
-
__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() });
|
|
1549
1413
|
return keyring;
|
|
1550
1414
|
}, _KeyringController_createKeyring =
|
|
1551
1415
|
/**
|
|
@@ -1562,13 +1426,11 @@ async function _KeyringController_newKeyring(type, data) {
|
|
|
1562
1426
|
*
|
|
1563
1427
|
* @param type - The type of keyring to add.
|
|
1564
1428
|
* @param data - Keyring initialization options.
|
|
1565
|
-
* @param metadata - Keyring metadata if available.
|
|
1566
1429
|
* @returns The new keyring.
|
|
1567
1430
|
* @throws If the keyring includes duplicated accounts.
|
|
1568
1431
|
*/
|
|
1569
|
-
async function _KeyringController_createKeyring(type, data
|
|
1432
|
+
async function _KeyringController_createKeyring(type, data) {
|
|
1570
1433
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1571
|
-
const keyringMetadata = metadata ?? getDefaultKeyringMetadata();
|
|
1572
1434
|
const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
|
|
1573
1435
|
if (!keyringBuilder) {
|
|
1574
1436
|
throw new KeyringControllerError(`${KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
|
|
@@ -1592,13 +1454,7 @@ async function _KeyringController_createKeyring(type, data, metadata) {
|
|
|
1592
1454
|
await keyring.generateRandomMnemonic();
|
|
1593
1455
|
await keyring.addAccounts(1);
|
|
1594
1456
|
}
|
|
1595
|
-
|
|
1596
|
-
const keyringBuilderV2 = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringV2BuilderForType).call(this, type);
|
|
1597
|
-
let keyringV2;
|
|
1598
|
-
if (keyringBuilderV2) {
|
|
1599
|
-
keyringV2 = keyringBuilderV2(keyring, keyringMetadata);
|
|
1600
|
-
}
|
|
1601
|
-
return { keyring, keyringV2, metadata: keyringMetadata };
|
|
1457
|
+
return keyring;
|
|
1602
1458
|
}, _KeyringController_clearKeyrings =
|
|
1603
1459
|
/**
|
|
1604
1460
|
* Remove all managed keyrings, destroying all their
|
|
@@ -1606,8 +1462,8 @@ async function _KeyringController_createKeyring(type, data, metadata) {
|
|
|
1606
1462
|
*/
|
|
1607
1463
|
async function _KeyringController_clearKeyrings() {
|
|
1608
1464
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1609
|
-
for (const { keyring
|
|
1610
|
-
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);
|
|
1611
1467
|
}
|
|
1612
1468
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
1613
1469
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -1623,28 +1479,23 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1623
1479
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1624
1480
|
try {
|
|
1625
1481
|
const { type, data, metadata: serializedMetadata } = serialized;
|
|
1626
|
-
|
|
1627
|
-
let hasChanged = false;
|
|
1628
|
-
// If metadata is missing, assume the data is from an installation before we had
|
|
1629
|
-
// keyring metadata.
|
|
1482
|
+
let newMetadata = false;
|
|
1630
1483
|
let metadata = serializedMetadata;
|
|
1484
|
+
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1485
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
|
|
1486
|
+
// If metadata is missing, assume the data is from an installation before
|
|
1487
|
+
// we had keyring metadata.
|
|
1631
1488
|
if (!metadata) {
|
|
1632
|
-
|
|
1489
|
+
newMetadata = true;
|
|
1633
1490
|
metadata = getDefaultKeyringMetadata();
|
|
1634
1491
|
}
|
|
1635
|
-
const oldState = JSON.stringify(data);
|
|
1636
|
-
const { keyring, keyringV2 } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data, metadata);
|
|
1637
|
-
const newState = JSON.stringify(await keyring.serialize());
|
|
1638
|
-
hasChanged || (hasChanged = oldState !== newState);
|
|
1639
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
|
|
1640
1492
|
// The keyring is added to the keyrings array only if it's successfully restored
|
|
1641
1493
|
// and the metadata is successfully added to the controller
|
|
1642
1494
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
|
|
1643
1495
|
keyring,
|
|
1644
|
-
keyringV2,
|
|
1645
1496
|
metadata,
|
|
1646
1497
|
});
|
|
1647
|
-
return { keyring,
|
|
1498
|
+
return { keyring, metadata, newMetadata };
|
|
1648
1499
|
}
|
|
1649
1500
|
catch (error) {
|
|
1650
1501
|
console.error(error);
|
|
@@ -1660,13 +1511,9 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1660
1511
|
* clears the keyring bridge iframe from the DOM.
|
|
1661
1512
|
*
|
|
1662
1513
|
* @param keyring - The keyring to destroy.
|
|
1663
|
-
* @param keyringV2 - The keyring v2 to destroy (if any).
|
|
1664
1514
|
*/
|
|
1665
|
-
async function _KeyringController_destroyKeyring(keyring
|
|
1515
|
+
async function _KeyringController_destroyKeyring(keyring) {
|
|
1666
1516
|
await keyring.destroy?.();
|
|
1667
|
-
if (keyringV2) {
|
|
1668
|
-
await keyringV2.destroy?.();
|
|
1669
|
-
}
|
|
1670
1517
|
}, _KeyringController_assertNoDuplicateAccounts =
|
|
1671
1518
|
/**
|
|
1672
1519
|
* Assert that there are no duplicate accounts in the keyrings.
|
|
@@ -1704,7 +1551,7 @@ async function _KeyringController_persistOrRollback(callback) {
|
|
|
1704
1551
|
const callbackResult = await callback({ releaseLock });
|
|
1705
1552
|
const newState = JSON.stringify(await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSessionState).call(this));
|
|
1706
1553
|
// State is committed only if the operation is successful and need to trigger a vault update.
|
|
1707
|
-
if (oldState
|
|
1554
|
+
if (!isEqual(oldState, newState)) {
|
|
1708
1555
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
|
|
1709
1556
|
}
|
|
1710
1557
|
return callbackResult;
|