@metamask-previews/keyring-controller 25.2.0-preview-bce8c098b → 25.2.0-preview-54abb25
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 +11 -1
- package/dist/KeyringController.cjs +201 -42
- package/dist/KeyringController.cjs.map +1 -1
- package/dist/KeyringController.d.cts +103 -8
- package/dist/KeyringController.d.cts.map +1 -1
- package/dist/KeyringController.d.mts +103 -8
- package/dist/KeyringController.d.mts.map +1 -1
- package/dist/KeyringController.mjs +202 -40
- 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/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -9,10 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
|
-
-
|
|
12
|
+
- Added `KeyringV2` support ([#8390](https://github.com/MetaMask/core/pull/8390))
|
|
13
|
+
- The controller now maintains a list of `KeyringV2` instance in memory alongside previous `Keyring` instance.
|
|
14
|
+
- This new keyring interface is more generic and will become the new standard to interact with keyring (creating accounts, executing logic that involves accounts like signing, etc...).
|
|
15
|
+
- For now, most `KeyringV2` are wrappers (read adapters) around existing `Keyring` instance.
|
|
16
|
+
- Added `withKeyringV2Unsafe` method and `KeyringController:withKeyringV2Unsafe` messenger action for lock-free read-only access to `KeyringV2` adapters ([#8390](https://github.com/MetaMask/core/pull/8390))
|
|
17
|
+
- Mirrors `withKeyringUnsafe` semantics: no mutex acquired, no persistence or rollback.
|
|
18
|
+
- Caller is responsible for ensuring the operation is read-only and accesses only immutable keyring data.
|
|
19
|
+
- Added `withKeyringV2` method and `KeyringController:withKeyringV2` messenger action for atomic operations using the `KeyringV2` API ([#8390](https://github.com/MetaMask/core/pull/8390))
|
|
20
|
+
- Accepts a `KeyringSelectorV2` (alias for `KeyringSelector`) to select keyrings by `type`, `address`, `id`, or `filter`.
|
|
21
|
+
- Ships with default V2 builders for HD (`HdKeyringV2`) and Simple (`SimpleKeyringV2`) keyrings; additional builders can be registered via the `keyringV2Builders` constructor option.
|
|
13
22
|
|
|
14
23
|
### Changed
|
|
15
24
|
|
|
25
|
+
- Deprecated `withKeyring` in favor of `withKeyringV2`, which supports the `KeyringV2` API.
|
|
16
26
|
- Bump `@metamask/messenger` from `^1.0.0` to `^1.1.1` ([#8364](https://github.com/MetaMask/core/pull/8364), [#8373](https://github.com/MetaMask/core/pull/8373))
|
|
17
27
|
|
|
18
28
|
## [25.2.0]
|
|
@@ -33,17 +33,14 @@ 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
|
|
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;
|
|
36
|
+
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_selectKeyring, _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;
|
|
40
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
38
|
exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
|
|
42
39
|
const util_1 = require("@ethereumjs/util");
|
|
43
40
|
const base_controller_1 = require("@metamask/base-controller");
|
|
44
41
|
const eth_hd_keyring_1 = require("@metamask/eth-hd-keyring");
|
|
45
42
|
const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
46
|
-
const eth_simple_keyring_1 =
|
|
43
|
+
const eth_simple_keyring_1 = __importStar(require("@metamask/eth-simple-keyring"));
|
|
47
44
|
const utils_1 = require("@metamask/utils");
|
|
48
45
|
const async_mutex_1 = require("async-mutex");
|
|
49
46
|
const ethereumjs_wallet_1 = __importStar(require("ethereumjs-wallet"));
|
|
@@ -126,6 +123,17 @@ const defaultKeyringBuilders = [
|
|
|
126
123
|
keyringBuilderFactory(eth_simple_keyring_1.default),
|
|
127
124
|
keyringBuilderFactory(eth_hd_keyring_1.HdKeyring),
|
|
128
125
|
];
|
|
126
|
+
const hdKeyringV2Builder = Object.assign((keyring, metadata) => new eth_hd_keyring_1.HdKeyringV2({
|
|
127
|
+
legacyKeyring: keyring,
|
|
128
|
+
entropySource: metadata.id,
|
|
129
|
+
}), { type: KeyringTypes.hd });
|
|
130
|
+
const simpleKeyringV2Builder = Object.assign((keyring) => new eth_simple_keyring_1.SimpleKeyringV2({
|
|
131
|
+
legacyKeyring: keyring,
|
|
132
|
+
}), { type: KeyringTypes.simple });
|
|
133
|
+
const defaultKeyringV2Builders = [
|
|
134
|
+
simpleKeyringV2Builder,
|
|
135
|
+
hdKeyringV2Builder,
|
|
136
|
+
];
|
|
129
137
|
const getDefaultKeyringState = () => {
|
|
130
138
|
return {
|
|
131
139
|
isUnlocked: false,
|
|
@@ -251,7 +259,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
251
259
|
* @param options.state - Initial state to set on this controller.
|
|
252
260
|
*/
|
|
253
261
|
constructor(options) {
|
|
254
|
-
const { encryptor, keyringBuilders, messenger, state } = options;
|
|
262
|
+
const { encryptor, keyringBuilders, keyringV2Builders, messenger, state } = options;
|
|
255
263
|
super({
|
|
256
264
|
name,
|
|
257
265
|
metadata: {
|
|
@@ -296,6 +304,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
296
304
|
_KeyringController_controllerOperationMutex.set(this, new async_mutex_1.Mutex());
|
|
297
305
|
_KeyringController_vaultOperationMutex.set(this, new async_mutex_1.Mutex());
|
|
298
306
|
_KeyringController_keyringBuilders.set(this, void 0);
|
|
307
|
+
_KeyringController_keyringV2Builders.set(this, void 0);
|
|
299
308
|
_KeyringController_encryptor.set(this, void 0);
|
|
300
309
|
_KeyringController_keyrings.set(this, void 0);
|
|
301
310
|
_KeyringController_unsupportedKeyrings.set(this, void 0);
|
|
@@ -303,6 +312,9 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
303
312
|
__classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
|
|
304
313
|
? keyringBuilders.concat(defaultKeyringBuilders)
|
|
305
314
|
: defaultKeyringBuilders, "f");
|
|
315
|
+
__classPrivateFieldSet(this, _KeyringController_keyringV2Builders, keyringV2Builders
|
|
316
|
+
? keyringV2Builders.concat(defaultKeyringV2Builders)
|
|
317
|
+
: defaultKeyringV2Builders, "f");
|
|
306
318
|
__classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
|
|
307
319
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
308
320
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -547,9 +559,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
547
559
|
*/
|
|
548
560
|
getKeyringsByType(type) {
|
|
549
561
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
550
|
-
return __classPrivateFieldGet(this,
|
|
551
|
-
.filter(({ keyring }) => keyring.type === type)
|
|
552
|
-
.map(({ keyring }) => keyring);
|
|
562
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntriesByType).call(this, type).map(({ keyring }) => keyring);
|
|
553
563
|
}
|
|
554
564
|
/**
|
|
555
565
|
* Persist all serialized keyrings in the vault.
|
|
@@ -638,7 +648,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
638
648
|
if (keyringIndex === -1) {
|
|
639
649
|
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.NoKeyring);
|
|
640
650
|
}
|
|
641
|
-
const { keyring } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
651
|
+
const { keyring, keyringV2 } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
642
652
|
const isPrimaryKeyring = keyringIndex === 0;
|
|
643
653
|
const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
|
|
644
654
|
// Primary keyring should never be removed, so we need to keep at least one account in it
|
|
@@ -659,7 +669,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
659
669
|
await keyring.removeAccount(address);
|
|
660
670
|
if (shouldRemoveKeyring) {
|
|
661
671
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").splice(keyringIndex, 1);
|
|
662
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
672
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring, keyringV2);
|
|
663
673
|
}
|
|
664
674
|
});
|
|
665
675
|
this.messenger.publish(`${name}:accountRemoved`, address);
|
|
@@ -1017,23 +1027,125 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
1017
1027
|
metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
|
|
1018
1028
|
}), keyring);
|
|
1019
1029
|
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Select a keyring, wrap it in a `KeyringV2` adapter, and execute
|
|
1032
|
+
* the given operation with the wrapped keyring as a mutually
|
|
1033
|
+
* exclusive atomic operation.
|
|
1034
|
+
*
|
|
1035
|
+
* We re-wrap the keyring in a `KeyringV2` adapter on each invocation,
|
|
1036
|
+
* since V2 wrappers are ephemeral adapters created on-the-fly, and cheap to create.
|
|
1037
|
+
*
|
|
1038
|
+
* The method automatically persists changes at the end of the
|
|
1039
|
+
* function execution, or rolls back the changes if an error
|
|
1040
|
+
* is thrown.
|
|
1041
|
+
*
|
|
1042
|
+
* A `KeyringV2Builder` for the selected keyring's type must exist
|
|
1043
|
+
* (either as a default or registered via the `keyringV2Builders`
|
|
1044
|
+
* constructor option); otherwise an error is thrown.
|
|
1045
|
+
*
|
|
1046
|
+
* Selection is performed against the V1 keyrings in `#keyrings`, since
|
|
1047
|
+
* V2 wrappers are ephemeral adapters created on-the-fly.
|
|
1048
|
+
*
|
|
1049
|
+
* @param selector - Keyring selector object.
|
|
1050
|
+
* @param operation - Function to execute with the wrapped V2 keyring.
|
|
1051
|
+
* @returns Promise resolving to the result of the function execution.
|
|
1052
|
+
* @template CallbackResult - The type of the value resolved by the callback function.
|
|
1053
|
+
*/
|
|
1054
|
+
async withKeyringV2(selector, operation) {
|
|
1055
|
+
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1056
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
1057
|
+
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1058
|
+
v2: true,
|
|
1059
|
+
selector,
|
|
1060
|
+
});
|
|
1061
|
+
if (!entry) {
|
|
1062
|
+
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
|
|
1063
|
+
}
|
|
1064
|
+
if (!entry.keyringV2) {
|
|
1065
|
+
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1066
|
+
}
|
|
1067
|
+
const { metadata } = entry;
|
|
1068
|
+
const keyring = entry.keyringV2;
|
|
1069
|
+
const result = await operation({
|
|
1070
|
+
keyring,
|
|
1071
|
+
metadata,
|
|
1072
|
+
});
|
|
1073
|
+
if (Object.is(result, keyring)) {
|
|
1074
|
+
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
|
|
1075
|
+
}
|
|
1076
|
+
return result;
|
|
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 errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
|
|
1121
|
+
}
|
|
1122
|
+
if (!entry.keyringV2) {
|
|
1123
|
+
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
|
|
1124
|
+
}
|
|
1125
|
+
const { metadata } = entry;
|
|
1126
|
+
const keyring = entry.keyringV2;
|
|
1127
|
+
const result = await operation({ keyring, metadata });
|
|
1128
|
+
if (Object.is(result, keyring)) {
|
|
1129
|
+
throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
|
|
1130
|
+
}
|
|
1131
|
+
return result;
|
|
1132
|
+
}
|
|
1020
1133
|
async getAccountKeyringType(account) {
|
|
1021
1134
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1022
1135
|
const keyring = (await this.getKeyringForAccount(account));
|
|
1023
1136
|
return keyring.type;
|
|
1024
1137
|
}
|
|
1025
|
-
async accountSupports7702(account) {
|
|
1026
|
-
const keyringType = await this.getAccountKeyringType(account);
|
|
1027
|
-
return (keyringType === KeyringTypes.hd ||
|
|
1028
|
-
keyringType === KeyringTypes.simple);
|
|
1029
|
-
}
|
|
1030
1138
|
}
|
|
1031
1139
|
exports.KeyringController = KeyringController;
|
|
1032
|
-
_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) {
|
|
1140
|
+
_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) {
|
|
1141
|
+
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1142
|
+
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, account);
|
|
1143
|
+
return entry?.keyring;
|
|
1144
|
+
}, _KeyringController_getKeyringEntryForAccount = async function _KeyringController_getKeyringEntryForAccount(account) {
|
|
1033
1145
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1034
1146
|
const keyringIndex = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_findKeyringIndexForAccount).call(this, account);
|
|
1035
1147
|
if (keyringIndex > -1) {
|
|
1036
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex]
|
|
1148
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
|
|
1037
1149
|
}
|
|
1038
1150
|
return undefined;
|
|
1039
1151
|
}, _KeyringController_findKeyringIndexForAccount = async function _KeyringController_findKeyringIndexForAccount(account) {
|
|
@@ -1041,6 +1153,9 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1041
1153
|
const address = account.toLowerCase();
|
|
1042
1154
|
const accountsPerKeyring = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(({ keyring }) => keyring.getAccounts()));
|
|
1043
1155
|
return accountsPerKeyring.findIndex((accounts) => accounts.map((a) => a.toLowerCase()).includes(address));
|
|
1156
|
+
}, _KeyringController_getKeyringEntriesByType = function _KeyringController_getKeyringEntriesByType(type) {
|
|
1157
|
+
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
|
|
1158
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter(({ keyring }) => keyring.type === type);
|
|
1044
1159
|
}, _KeyringController_assertNoUnsafeDirectKeyringAccess = function _KeyringController_assertNoUnsafeDirectKeyringAccess(value, keyring) {
|
|
1045
1160
|
if (Object.is(value, keyring)) {
|
|
1046
1161
|
// Access to a keyring instance outside of controller safeguards
|
|
@@ -1067,37 +1182,66 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
|
|
|
1067
1182
|
this.messenger.registerActionHandler(`${name}:addNewAccount`, this.addNewAccount.bind(this));
|
|
1068
1183
|
this.messenger.registerActionHandler(`${name}:withKeyring`, this.withKeyring.bind(this));
|
|
1069
1184
|
this.messenger.registerActionHandler(`${name}:withKeyringUnsafe`, this.withKeyringUnsafe.bind(this));
|
|
1185
|
+
this.messenger.registerActionHandler(`${name}:withKeyringV2`, this.withKeyringV2.bind(this));
|
|
1186
|
+
this.messenger.registerActionHandler(`${name}:withKeyringV2Unsafe`, this.withKeyringV2Unsafe.bind(this));
|
|
1070
1187
|
this.messenger.registerActionHandler(`${name}:addNewKeyring`, this.addNewKeyring.bind(this));
|
|
1071
1188
|
this.messenger.registerActionHandler(`${name}:createNewVaultAndKeychain`, this.createNewVaultAndKeychain.bind(this));
|
|
1072
1189
|
this.messenger.registerActionHandler(`${name}:createNewVaultAndRestore`, this.createNewVaultAndRestore.bind(this));
|
|
1073
1190
|
this.messenger.registerActionHandler(`${name}:removeAccount`, this.removeAccount.bind(this));
|
|
1074
|
-
|
|
1075
|
-
}, _KeyringController_selectKeyring =
|
|
1191
|
+
}, _KeyringController_selectKeyringEntry =
|
|
1076
1192
|
/**
|
|
1077
|
-
* Select a keyring using a selector without acquiring the controller lock.
|
|
1193
|
+
* Select a keyring entry using a selector without acquiring the controller lock.
|
|
1078
1194
|
*
|
|
1079
|
-
* @param
|
|
1080
|
-
* @
|
|
1195
|
+
* @param options - Selection options.
|
|
1196
|
+
* @param options.v2 - Tag to indicate whether the selector is for a V2 keyring.
|
|
1197
|
+
* @param options.selector - Keyring selector object.
|
|
1198
|
+
* @returns The selected keyring entry, or `undefined` if no match is found.
|
|
1081
1199
|
* @template SelectedKeyring - The expected type of the selected keyring.
|
|
1200
|
+
* @template SelectedKeyringV2 - The expected type of the selected keyring (v2).
|
|
1082
1201
|
*/
|
|
1083
|
-
async function
|
|
1084
|
-
let
|
|
1202
|
+
async function _KeyringController_selectKeyringEntry({ v2, selector, }) {
|
|
1203
|
+
let entry;
|
|
1085
1204
|
if ('address' in selector) {
|
|
1086
|
-
|
|
1205
|
+
entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, selector.address);
|
|
1087
1206
|
}
|
|
1088
1207
|
else if ('type' in selector) {
|
|
1089
|
-
|
|
1208
|
+
entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntriesByType).call(this, selector.type)[selector.index ?? 0];
|
|
1090
1209
|
}
|
|
1091
1210
|
else if ('id' in selector) {
|
|
1092
|
-
|
|
1211
|
+
entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, selector.id);
|
|
1093
1212
|
}
|
|
1094
1213
|
else if ('filter' in selector) {
|
|
1095
|
-
|
|
1214
|
+
entry = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring, keyringV2, metadata }) => {
|
|
1215
|
+
// If v2, then we'll use the v2 selector which expects a `KeyringV2` instance.
|
|
1216
|
+
if (v2) {
|
|
1217
|
+
// However, some keyrings do not have a v2 wrapper, so we just skip them.
|
|
1218
|
+
if (!keyringV2) {
|
|
1219
|
+
return false;
|
|
1220
|
+
}
|
|
1221
|
+
return selector.filter(keyringV2, metadata);
|
|
1222
|
+
}
|
|
1223
|
+
return selector.filter(keyring, metadata);
|
|
1224
|
+
});
|
|
1096
1225
|
}
|
|
1097
|
-
return
|
|
1226
|
+
return entry;
|
|
1227
|
+
}, _KeyringController_selectKeyring =
|
|
1228
|
+
/**
|
|
1229
|
+
* Select a keyring using a selector without acquiring the controller lock.
|
|
1230
|
+
*
|
|
1231
|
+
* @param selector - Keyring selector object.
|
|
1232
|
+
* @returns The selected keyring, or `undefined` if no match is found.
|
|
1233
|
+
* @template SelectedKeyring - The expected type of the selected keyring.
|
|
1234
|
+
*/
|
|
1235
|
+
async function _KeyringController_selectKeyring(selector) {
|
|
1236
|
+
const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
|
|
1237
|
+
v2: false,
|
|
1238
|
+
selector,
|
|
1239
|
+
});
|
|
1240
|
+
return entry?.keyring;
|
|
1098
1241
|
}, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
|
|
1099
|
-
return __classPrivateFieldGet(this,
|
|
1100
|
-
|
|
1242
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, keyringId)?.keyring;
|
|
1243
|
+
}, _KeyringController_getKeyringEntryById = function _KeyringController_getKeyringEntryById(keyringId) {
|
|
1244
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId);
|
|
1101
1245
|
}, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
|
|
1102
1246
|
if (!keyringId) {
|
|
1103
1247
|
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
|
|
@@ -1111,6 +1255,8 @@ async function _KeyringController_selectKeyring(selector) {
|
|
|
1111
1255
|
return keyringWithMetadata.metadata;
|
|
1112
1256
|
}, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
|
|
1113
1257
|
return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
|
|
1258
|
+
}, _KeyringController_getKeyringV2BuilderForType = function _KeyringController_getKeyringV2BuilderForType(type) {
|
|
1259
|
+
return __classPrivateFieldGet(this, _KeyringController_keyringV2Builders, "f").find((builder) => builder.type === type);
|
|
1114
1260
|
}, _KeyringController_createNewVaultWithKeyring =
|
|
1115
1261
|
/**
|
|
1116
1262
|
* Create new vault with an initial keyring
|
|
@@ -1434,8 +1580,8 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
|
1434
1580
|
* @throws If the keyring includes duplicated accounts.
|
|
1435
1581
|
*/
|
|
1436
1582
|
async function _KeyringController_newKeyring(type, data) {
|
|
1437
|
-
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1438
|
-
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, metadata
|
|
1583
|
+
const { keyring, keyringV2, metadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1584
|
+
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, keyringV2, metadata });
|
|
1439
1585
|
return keyring;
|
|
1440
1586
|
}, _KeyringController_createKeyring =
|
|
1441
1587
|
/**
|
|
@@ -1452,11 +1598,13 @@ async function _KeyringController_newKeyring(type, data) {
|
|
|
1452
1598
|
*
|
|
1453
1599
|
* @param type - The type of keyring to add.
|
|
1454
1600
|
* @param data - Keyring initialization options.
|
|
1601
|
+
* @param metadata - Keyring metadata if available.
|
|
1455
1602
|
* @returns The new keyring.
|
|
1456
1603
|
* @throws If the keyring includes duplicated accounts.
|
|
1457
1604
|
*/
|
|
1458
|
-
async function _KeyringController_createKeyring(type, data) {
|
|
1605
|
+
async function _KeyringController_createKeyring(type, data, metadata) {
|
|
1459
1606
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1607
|
+
const keyringMetadata = metadata ?? getDefaultKeyringMetadata();
|
|
1460
1608
|
const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
|
|
1461
1609
|
if (!keyringBuilder) {
|
|
1462
1610
|
throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
|
|
@@ -1480,7 +1628,13 @@ async function _KeyringController_createKeyring(type, data) {
|
|
|
1480
1628
|
await keyring.generateRandomMnemonic();
|
|
1481
1629
|
await keyring.addAccounts(1);
|
|
1482
1630
|
}
|
|
1483
|
-
|
|
1631
|
+
// We now create the keyring V2 wrappers and store them in memory.
|
|
1632
|
+
const keyringBuilderV2 = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringV2BuilderForType).call(this, type);
|
|
1633
|
+
let keyringV2;
|
|
1634
|
+
if (keyringBuilderV2) {
|
|
1635
|
+
keyringV2 = keyringBuilderV2(keyring, keyringMetadata);
|
|
1636
|
+
}
|
|
1637
|
+
return { keyring, keyringV2, metadata: keyringMetadata };
|
|
1484
1638
|
}, _KeyringController_clearKeyrings =
|
|
1485
1639
|
/**
|
|
1486
1640
|
* Remove all managed keyrings, destroying all their
|
|
@@ -1488,8 +1642,8 @@ async function _KeyringController_createKeyring(type, data) {
|
|
|
1488
1642
|
*/
|
|
1489
1643
|
async function _KeyringController_clearKeyrings() {
|
|
1490
1644
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1491
|
-
for (const { keyring } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
|
|
1492
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
1645
|
+
for (const { keyring, keyringV2 } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
|
|
1646
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring, keyringV2);
|
|
1493
1647
|
}
|
|
1494
1648
|
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
1495
1649
|
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
@@ -1507,21 +1661,22 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1507
1661
|
const { type, data, metadata: serializedMetadata } = serialized;
|
|
1508
1662
|
let newMetadata = false;
|
|
1509
1663
|
let metadata = serializedMetadata;
|
|
1510
|
-
const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
|
|
1511
|
-
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
|
|
1512
1664
|
// If metadata is missing, assume the data is from an installation before
|
|
1513
1665
|
// we had keyring metadata.
|
|
1514
1666
|
if (!metadata) {
|
|
1515
1667
|
newMetadata = true;
|
|
1516
1668
|
metadata = getDefaultKeyringMetadata();
|
|
1517
1669
|
}
|
|
1670
|
+
const { keyring, keyringV2 } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data, metadata);
|
|
1671
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
|
|
1518
1672
|
// The keyring is added to the keyrings array only if it's successfully restored
|
|
1519
1673
|
// and the metadata is successfully added to the controller
|
|
1520
1674
|
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
|
|
1521
1675
|
keyring,
|
|
1676
|
+
keyringV2,
|
|
1522
1677
|
metadata,
|
|
1523
1678
|
});
|
|
1524
|
-
return { keyring, metadata, newMetadata };
|
|
1679
|
+
return { keyring, keyringV2, metadata, newMetadata };
|
|
1525
1680
|
}
|
|
1526
1681
|
catch (error) {
|
|
1527
1682
|
console.error(error);
|
|
@@ -1537,9 +1692,13 @@ async function _KeyringController_restoreKeyring(serialized) {
|
|
|
1537
1692
|
* clears the keyring bridge iframe from the DOM.
|
|
1538
1693
|
*
|
|
1539
1694
|
* @param keyring - The keyring to destroy.
|
|
1695
|
+
* @param keyringV2 - The keyring v2 to destroy (if any).
|
|
1540
1696
|
*/
|
|
1541
|
-
async function _KeyringController_destroyKeyring(keyring) {
|
|
1697
|
+
async function _KeyringController_destroyKeyring(keyring, keyringV2) {
|
|
1542
1698
|
await keyring.destroy?.();
|
|
1699
|
+
if (keyringV2) {
|
|
1700
|
+
await keyringV2.destroy?.();
|
|
1701
|
+
}
|
|
1543
1702
|
}, _KeyringController_assertNoDuplicateAccounts =
|
|
1544
1703
|
/**
|
|
1545
1704
|
* Assert that there are no duplicate accounts in the keyrings.
|