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