@metamask-previews/keyring-controller 13.0.0-preview.d78968a2 → 13.0.0-preview.eb2135e
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/dist/KeyringController.js +21 -1294
- package/dist/KeyringController.js.map +1 -1
- package/dist/KeyringController.mjs +22 -0
- package/dist/KeyringController.mjs.map +1 -0
- package/dist/chunk-6HZWCYLD.mjs +1290 -0
- package/dist/chunk-6HZWCYLD.mjs.map +1 -0
- package/dist/chunk-BVSGYW4D.js +1290 -0
- package/dist/chunk-BVSGYW4D.js.map +1 -0
- package/dist/chunk-CHLPTPMZ.js +63 -0
- package/dist/chunk-CHLPTPMZ.js.map +1 -0
- package/dist/chunk-NAAWD7HX.mjs +63 -0
- package/dist/chunk-NAAWD7HX.mjs.map +1 -0
- package/dist/constants.js +6 -33
- package/dist/constants.js.map +1 -1
- package/dist/constants.mjs +7 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/index.js +19 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -0
- package/dist/index.mjs.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/{KeyringController.d.ts → types/KeyringController.d.ts} +1 -1
- package/dist/types/KeyringController.d.ts.map +1 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/{index.d.ts.map → types/index.d.ts.map} +1 -1
- package/package.json +11 -1
- package/dist/KeyringController.d.ts.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- /package/dist/{constants.d.ts → types/constants.d.ts} +0 -0
- /package/dist/{index.d.ts → types/index.d.ts} +0 -0
|
@@ -1,1295 +1,22 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
35
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
36
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
37
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
38
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
39
|
-
};
|
|
40
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
41
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
42
|
-
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");
|
|
43
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
44
|
-
};
|
|
45
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
46
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
47
|
-
};
|
|
48
|
-
var _KeyringController_instances, _KeyringController_keyringBuilders, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_updateKeyringsInState, _KeyringController_unlockKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _KeyringController_setUnlocked, _KeyringController_getMemState;
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
|
|
51
|
-
const util_1 = require("@ethereumjs/util");
|
|
52
|
-
const base_controller_1 = require("@metamask/base-controller");
|
|
53
|
-
const encryptorUtils = __importStar(require("@metamask/browser-passworder"));
|
|
54
|
-
const eth_hd_keyring_1 = __importDefault(require("@metamask/eth-hd-keyring"));
|
|
55
|
-
const eth_sig_util_1 = require("@metamask/eth-sig-util");
|
|
56
|
-
const eth_simple_keyring_1 = __importDefault(require("@metamask/eth-simple-keyring"));
|
|
57
|
-
const utils_1 = require("@metamask/utils");
|
|
58
|
-
const async_mutex_1 = require("async-mutex");
|
|
59
|
-
const ethereumjs_wallet_1 = __importStar(require("ethereumjs-wallet"));
|
|
60
|
-
const constants_1 = require("./constants");
|
|
61
|
-
const name = 'KeyringController';
|
|
62
|
-
/**
|
|
63
|
-
* Available keyring types
|
|
64
|
-
*/
|
|
65
|
-
var KeyringTypes;
|
|
66
|
-
(function (KeyringTypes) {
|
|
67
|
-
KeyringTypes["simple"] = "Simple Key Pair";
|
|
68
|
-
KeyringTypes["hd"] = "HD Key Tree";
|
|
69
|
-
KeyringTypes["qr"] = "QR Hardware Wallet Device";
|
|
70
|
-
KeyringTypes["trezor"] = "Trezor Hardware";
|
|
71
|
-
KeyringTypes["ledger"] = "Ledger Hardware";
|
|
72
|
-
KeyringTypes["lattice"] = "Lattice Hardware";
|
|
73
|
-
KeyringTypes["snap"] = "Snap Keyring";
|
|
74
|
-
})(KeyringTypes = exports.KeyringTypes || (exports.KeyringTypes = {}));
|
|
75
|
-
/**
|
|
76
|
-
* Custody keyring types are a special case, as they are not a single type
|
|
77
|
-
* but they all start with the prefix "Custody".
|
|
78
|
-
* @param keyringType - The type of the keyring.
|
|
79
|
-
* @returns Whether the keyring type is a custody keyring.
|
|
80
|
-
*/
|
|
81
|
-
const isCustodyKeyring = (keyringType) => {
|
|
82
|
-
return keyringType.startsWith('Custody');
|
|
83
|
-
};
|
|
84
|
-
exports.isCustodyKeyring = isCustodyKeyring;
|
|
85
|
-
/**
|
|
86
|
-
* A strategy for importing an account
|
|
87
|
-
*/
|
|
88
|
-
var AccountImportStrategy;
|
|
89
|
-
(function (AccountImportStrategy) {
|
|
90
|
-
AccountImportStrategy["privateKey"] = "privateKey";
|
|
91
|
-
AccountImportStrategy["json"] = "json";
|
|
92
|
-
})(AccountImportStrategy = exports.AccountImportStrategy || (exports.AccountImportStrategy = {}));
|
|
93
|
-
/**
|
|
94
|
-
* The `signTypedMessage` version
|
|
95
|
-
*
|
|
96
|
-
* @see https://docs.metamask.io/guide/signing-data.html
|
|
97
|
-
*/
|
|
98
|
-
var SignTypedDataVersion;
|
|
99
|
-
(function (SignTypedDataVersion) {
|
|
100
|
-
SignTypedDataVersion["V1"] = "V1";
|
|
101
|
-
SignTypedDataVersion["V3"] = "V3";
|
|
102
|
-
SignTypedDataVersion["V4"] = "V4";
|
|
103
|
-
})(SignTypedDataVersion = exports.SignTypedDataVersion || (exports.SignTypedDataVersion = {}));
|
|
104
|
-
/**
|
|
105
|
-
* Get builder function for `Keyring`
|
|
106
|
-
*
|
|
107
|
-
* Returns a builder function for `Keyring` with a `type` property.
|
|
108
|
-
*
|
|
109
|
-
* @param KeyringConstructor - The Keyring class for the builder.
|
|
110
|
-
* @returns A builder function for the given Keyring.
|
|
111
|
-
*/
|
|
112
|
-
function keyringBuilderFactory(KeyringConstructor) {
|
|
113
|
-
const builder = () => new KeyringConstructor();
|
|
114
|
-
builder.type = KeyringConstructor.type;
|
|
115
|
-
return builder;
|
|
116
|
-
}
|
|
117
|
-
exports.keyringBuilderFactory = keyringBuilderFactory;
|
|
118
|
-
const defaultKeyringBuilders = [
|
|
119
|
-
keyringBuilderFactory(eth_simple_keyring_1.default),
|
|
120
|
-
keyringBuilderFactory(eth_hd_keyring_1.default),
|
|
121
|
-
];
|
|
122
|
-
const getDefaultKeyringState = () => {
|
|
123
|
-
return {
|
|
124
|
-
isUnlocked: false,
|
|
125
|
-
keyrings: [],
|
|
126
|
-
};
|
|
127
|
-
};
|
|
128
|
-
exports.getDefaultKeyringState = getDefaultKeyringState;
|
|
129
|
-
/**
|
|
130
|
-
* Assert that the given keyring has an exportable
|
|
131
|
-
* mnemonic.
|
|
132
|
-
*
|
|
133
|
-
* @param keyring - The keyring to check
|
|
134
|
-
* @throws When the keyring does not have a mnemonic
|
|
135
|
-
*/
|
|
136
|
-
function assertHasUint8ArrayMnemonic(keyring) {
|
|
137
|
-
if (!((0, utils_1.hasProperty)(keyring, 'mnemonic') && keyring.mnemonic instanceof Uint8Array)) {
|
|
138
|
-
throw new Error("Can't get mnemonic bytes from keyring");
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Assert that the provided encryptor supports
|
|
143
|
-
* encryption and encryption key export.
|
|
144
|
-
*
|
|
145
|
-
* @param encryptor - The encryptor to check.
|
|
146
|
-
* @throws If the encryptor does not support key encryption.
|
|
147
|
-
*/
|
|
148
|
-
function assertIsExportableKeyEncryptor(encryptor) {
|
|
149
|
-
if (!('importKey' in encryptor &&
|
|
150
|
-
typeof encryptor.importKey === 'function' &&
|
|
151
|
-
'decryptWithKey' in encryptor &&
|
|
152
|
-
typeof encryptor.decryptWithKey === 'function' &&
|
|
153
|
-
'encryptWithKey' in encryptor &&
|
|
154
|
-
typeof encryptor.encryptWithKey === 'function')) {
|
|
155
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedEncryptionKeyExport);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Checks if the provided value is a serialized keyrings array.
|
|
160
|
-
*
|
|
161
|
-
* @param array - The value to check.
|
|
162
|
-
* @returns True if the value is a serialized keyrings array.
|
|
163
|
-
*/
|
|
164
|
-
function isSerializedKeyringsArray(array) {
|
|
165
|
-
return (typeof array === 'object' &&
|
|
166
|
-
Array.isArray(array) &&
|
|
167
|
-
array.every((value) => value.type && (0, utils_1.isValidJson)(value.data)));
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Display For Keyring
|
|
171
|
-
*
|
|
172
|
-
* Is used for adding the current keyrings to the state object.
|
|
173
|
-
*
|
|
174
|
-
* @param keyring - The keyring to display.
|
|
175
|
-
* @returns A keyring display object, with type and accounts properties.
|
|
176
|
-
*/
|
|
177
|
-
function displayForKeyring(keyring) {
|
|
178
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
-
const accounts = yield keyring.getAccounts();
|
|
180
|
-
return {
|
|
181
|
-
type: keyring.type,
|
|
182
|
-
// Cast to `Hex[]` here is safe here because `accounts` has no nullish
|
|
183
|
-
// values, and `normalize` returns `Hex` unless given a nullish value
|
|
184
|
-
accounts: accounts.map(eth_sig_util_1.normalize),
|
|
185
|
-
};
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Controller responsible for establishing and managing user identity.
|
|
190
|
-
*
|
|
191
|
-
* This class is a wrapper around the `eth-keyring-controller` package. The
|
|
192
|
-
* `eth-keyring-controller` manages the "vault", which is an encrypted store of private keys, and
|
|
193
|
-
* it manages the wallet "lock" state. This wrapper class has convenience methods for interacting
|
|
194
|
-
* with the internal keyring controller and handling certain complex operations that involve the
|
|
195
|
-
* keyrings.
|
|
196
|
-
*/
|
|
197
|
-
class KeyringController extends base_controller_1.BaseController {
|
|
198
|
-
/**
|
|
199
|
-
* Creates a KeyringController instance.
|
|
200
|
-
*
|
|
201
|
-
* @param options - Initial options used to configure this controller
|
|
202
|
-
* @param options.encryptor - An optional object for defining encryption schemes.
|
|
203
|
-
* @param options.keyringBuilders - Set a new name for account.
|
|
204
|
-
* @param options.cacheEncryptionKey - Whether to cache or not encryption key.
|
|
205
|
-
* @param options.messenger - A restricted controller messenger.
|
|
206
|
-
* @param options.state - Initial state to set on this controller.
|
|
207
|
-
*/
|
|
208
|
-
constructor(options) {
|
|
209
|
-
const { encryptor = encryptorUtils, keyringBuilders, messenger, state, } = options;
|
|
210
|
-
super({
|
|
211
|
-
name,
|
|
212
|
-
metadata: {
|
|
213
|
-
vault: { persist: true, anonymous: false },
|
|
214
|
-
isUnlocked: { persist: false, anonymous: true },
|
|
215
|
-
keyrings: { persist: false, anonymous: false },
|
|
216
|
-
encryptionKey: { persist: false, anonymous: false },
|
|
217
|
-
encryptionSalt: { persist: false, anonymous: false },
|
|
218
|
-
},
|
|
219
|
-
messenger,
|
|
220
|
-
state: Object.assign(Object.assign({}, (0, exports.getDefaultKeyringState)()), state),
|
|
221
|
-
});
|
|
222
|
-
_KeyringController_instances.add(this);
|
|
223
|
-
this.mutex = new async_mutex_1.Mutex();
|
|
224
|
-
_KeyringController_keyringBuilders.set(this, void 0);
|
|
225
|
-
_KeyringController_keyrings.set(this, void 0);
|
|
226
|
-
_KeyringController_unsupportedKeyrings.set(this, void 0);
|
|
227
|
-
_KeyringController_password.set(this, void 0);
|
|
228
|
-
_KeyringController_encryptor.set(this, void 0);
|
|
229
|
-
_KeyringController_cacheEncryptionKey.set(this, void 0);
|
|
230
|
-
_KeyringController_qrKeyringStateListener.set(this, void 0);
|
|
231
|
-
__classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
|
|
232
|
-
? defaultKeyringBuilders.concat(keyringBuilders)
|
|
233
|
-
: defaultKeyringBuilders, "f");
|
|
234
|
-
__classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
|
|
235
|
-
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
236
|
-
__classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
|
|
237
|
-
// This option allows the controller to cache an exported key
|
|
238
|
-
// for use in decrypting and encrypting data without password
|
|
239
|
-
__classPrivateFieldSet(this, _KeyringController_cacheEncryptionKey, Boolean(options.cacheEncryptionKey), "f");
|
|
240
|
-
if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
|
|
241
|
-
assertIsExportableKeyEncryptor(encryptor);
|
|
242
|
-
}
|
|
243
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_registerMessageHandlers).call(this);
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Adds a new account to the default (first) HD seed phrase keyring.
|
|
247
|
-
*
|
|
248
|
-
* @param accountCount - Number of accounts before adding a new one, used to
|
|
249
|
-
* make the method idempotent.
|
|
250
|
-
* @returns Promise resolving to keyring current state and added account
|
|
251
|
-
* address.
|
|
252
|
-
*/
|
|
253
|
-
addNewAccount(accountCount) {
|
|
254
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
255
|
-
const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
|
|
256
|
-
if (!primaryKeyring) {
|
|
257
|
-
throw new Error('No HD keyring found');
|
|
258
|
-
}
|
|
259
|
-
const oldAccounts = yield this.getAccounts();
|
|
260
|
-
if (accountCount && oldAccounts.length !== accountCount) {
|
|
261
|
-
if (accountCount > oldAccounts.length) {
|
|
262
|
-
throw new Error('Account out of sequence');
|
|
263
|
-
}
|
|
264
|
-
// we return the account already existing at index `accountCount`
|
|
265
|
-
const primaryKeyringAccounts = yield primaryKeyring.getAccounts();
|
|
266
|
-
return {
|
|
267
|
-
keyringState: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this),
|
|
268
|
-
addedAccountAddress: primaryKeyringAccounts[accountCount],
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
const addedAccountAddress = yield this.addNewAccountForKeyring(primaryKeyring);
|
|
272
|
-
yield this.verifySeedPhrase();
|
|
273
|
-
return {
|
|
274
|
-
keyringState: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this),
|
|
275
|
-
addedAccountAddress,
|
|
276
|
-
};
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Adds a new account to the specified keyring.
|
|
281
|
-
*
|
|
282
|
-
* @param keyring - Keyring to add the account to.
|
|
283
|
-
* @param accountCount - Number of accounts before adding a new one, used to make the method idempotent.
|
|
284
|
-
* @returns Promise resolving to keyring current state and added account
|
|
285
|
-
*/
|
|
286
|
-
addNewAccountForKeyring(keyring, accountCount) {
|
|
287
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
288
|
-
const oldAccounts = yield this.getAccounts();
|
|
289
|
-
if (accountCount && oldAccounts.length !== accountCount) {
|
|
290
|
-
if (accountCount > oldAccounts.length) {
|
|
291
|
-
throw new Error('Account out of sequence');
|
|
292
|
-
}
|
|
293
|
-
const existingAccount = oldAccounts[accountCount];
|
|
294
|
-
(0, utils_1.assertIsStrictHexString)(existingAccount);
|
|
295
|
-
return existingAccount;
|
|
296
|
-
}
|
|
297
|
-
yield keyring.addAccounts(1);
|
|
298
|
-
yield this.persistAllKeyrings();
|
|
299
|
-
const addedAccountAddress = (yield this.getAccounts()).find((selectedAddress) => !oldAccounts.includes(selectedAddress));
|
|
300
|
-
(0, utils_1.assertIsStrictHexString)(addedAccountAddress);
|
|
301
|
-
return addedAccountAddress;
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* Adds a new account to the default (first) HD seed phrase keyring without updating identities in preferences.
|
|
306
|
-
*
|
|
307
|
-
* @returns Promise resolving to current state when the account is added.
|
|
308
|
-
*/
|
|
309
|
-
addNewAccountWithoutUpdate() {
|
|
310
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
311
|
-
const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
|
|
312
|
-
if (!primaryKeyring) {
|
|
313
|
-
throw new Error('No HD keyring found');
|
|
314
|
-
}
|
|
315
|
-
yield primaryKeyring.addAccounts(1);
|
|
316
|
-
yield this.persistAllKeyrings();
|
|
317
|
-
yield this.verifySeedPhrase();
|
|
318
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Effectively the same as creating a new keychain then populating it
|
|
323
|
-
* using the given seed phrase.
|
|
324
|
-
*
|
|
325
|
-
* @param password - Password to unlock keychain.
|
|
326
|
-
* @param seed - A BIP39-compliant seed phrase as Uint8Array,
|
|
327
|
-
* either as a string or an array of UTF-8 bytes that represent the string.
|
|
328
|
-
* @returns Promise resolving to the restored keychain object.
|
|
329
|
-
*/
|
|
330
|
-
createNewVaultAndRestore(password, seed) {
|
|
331
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
332
|
-
const releaseLock = yield this.mutex.acquire();
|
|
333
|
-
if (!password || !password.length) {
|
|
334
|
-
throw new Error('Invalid password');
|
|
335
|
-
}
|
|
336
|
-
try {
|
|
337
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createNewVaultWithKeyring).call(this, password, {
|
|
338
|
-
type: KeyringTypes.hd,
|
|
339
|
-
opts: {
|
|
340
|
-
mnemonic: seed,
|
|
341
|
-
numberOfAccounts: 1,
|
|
342
|
-
},
|
|
343
|
-
});
|
|
344
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
345
|
-
}
|
|
346
|
-
finally {
|
|
347
|
-
releaseLock();
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Create a new primary keychain and wipe any previous keychains.
|
|
353
|
-
*
|
|
354
|
-
* @param password - Password to unlock the new vault.
|
|
355
|
-
* @returns Newly-created keychain object.
|
|
356
|
-
*/
|
|
357
|
-
createNewVaultAndKeychain(password) {
|
|
358
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
-
const releaseLock = yield this.mutex.acquire();
|
|
360
|
-
try {
|
|
361
|
-
const accounts = yield this.getAccounts();
|
|
362
|
-
if (!accounts.length) {
|
|
363
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createNewVaultWithKeyring).call(this, password, {
|
|
364
|
-
type: KeyringTypes.hd,
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
368
|
-
}
|
|
369
|
-
finally {
|
|
370
|
-
releaseLock();
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
/**
|
|
375
|
-
* Adds a new keyring of the given `type`.
|
|
376
|
-
*
|
|
377
|
-
* @param type - Keyring type name.
|
|
378
|
-
* @param opts - Keyring options.
|
|
379
|
-
* @throws If a builder for the given `type` does not exist.
|
|
380
|
-
* @returns Promise resolving to the added keyring.
|
|
381
|
-
*/
|
|
382
|
-
addNewKeyring(type, opts) {
|
|
383
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
384
|
-
if (type === KeyringTypes.qr) {
|
|
385
|
-
return this.getOrAddQRKeyring();
|
|
386
|
-
}
|
|
387
|
-
const keyring = yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts);
|
|
388
|
-
if (type === KeyringTypes.hd && (!(0, utils_1.isObject)(opts) || !opts.mnemonic)) {
|
|
389
|
-
if (!keyring.generateRandomMnemonic) {
|
|
390
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedGenerateRandomMnemonic);
|
|
391
|
-
}
|
|
392
|
-
keyring.generateRandomMnemonic();
|
|
393
|
-
yield keyring.addAccounts(1);
|
|
394
|
-
}
|
|
395
|
-
const accounts = yield keyring.getAccounts();
|
|
396
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_checkForDuplicate).call(this, type, accounts);
|
|
397
|
-
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
|
|
398
|
-
yield this.persistAllKeyrings();
|
|
399
|
-
return keyring;
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
|
-
* Method to verify a given password validity. Throws an
|
|
404
|
-
* error if the password is invalid.
|
|
405
|
-
*
|
|
406
|
-
* @param password - Password of the keyring.
|
|
407
|
-
*/
|
|
408
|
-
verifyPassword(password) {
|
|
409
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
410
|
-
if (!this.state.vault) {
|
|
411
|
-
throw new Error(constants_1.KeyringControllerError.VaultError);
|
|
412
|
-
}
|
|
413
|
-
yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decrypt(password, this.state.vault);
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* Returns the status of the vault.
|
|
418
|
-
*
|
|
419
|
-
* @returns Boolean returning true if the vault is unlocked.
|
|
420
|
-
*/
|
|
421
|
-
isUnlocked() {
|
|
422
|
-
return this.state.isUnlocked;
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Gets the seed phrase of the HD keyring.
|
|
426
|
-
*
|
|
427
|
-
* @param password - Password of the keyring.
|
|
428
|
-
* @returns Promise resolving to the seed phrase.
|
|
429
|
-
*/
|
|
430
|
-
exportSeedPhrase(password) {
|
|
431
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
432
|
-
yield this.verifyPassword(password);
|
|
433
|
-
assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
|
|
434
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Gets the private key from the keyring controlling an address.
|
|
439
|
-
*
|
|
440
|
-
* @param password - Password of the keyring.
|
|
441
|
-
* @param address - Address to export.
|
|
442
|
-
* @returns Promise resolving to the private key for an address.
|
|
443
|
-
*/
|
|
444
|
-
exportAccount(password, address) {
|
|
445
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
446
|
-
yield this.verifyPassword(password);
|
|
447
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
448
|
-
if (!keyring.exportAccount) {
|
|
449
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedExportAccount);
|
|
450
|
-
}
|
|
451
|
-
return yield keyring.exportAccount((0, eth_sig_util_1.normalize)(address));
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Returns the public addresses of all accounts for the current keyring.
|
|
456
|
-
*
|
|
457
|
-
* @returns A promise resolving to an array of addresses.
|
|
458
|
-
*/
|
|
459
|
-
getAccounts() {
|
|
460
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
461
|
-
const keyrings = __classPrivateFieldGet(this, _KeyringController_keyrings, "f");
|
|
462
|
-
const keyringArrays = yield Promise.all(keyrings.map((keyring) => __awaiter(this, void 0, void 0, function* () { return keyring.getAccounts(); })));
|
|
463
|
-
const addresses = keyringArrays.reduce((res, arr) => {
|
|
464
|
-
return res.concat(arr);
|
|
465
|
-
}, []);
|
|
466
|
-
// Cast to `Hex[]` here is safe here because `addresses` has no nullish
|
|
467
|
-
// values, and `normalize` returns `Hex` unless given a nullish value
|
|
468
|
-
return addresses.map(eth_sig_util_1.normalize);
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* Get encryption public key.
|
|
473
|
-
*
|
|
474
|
-
* @param account - An account address.
|
|
475
|
-
* @param opts - Additional encryption options.
|
|
476
|
-
* @throws If the `account` does not exist or does not support the `getEncryptionPublicKey` method
|
|
477
|
-
* @returns Promise resolving to encyption public key of the `account` if one exists.
|
|
478
|
-
*/
|
|
479
|
-
getEncryptionPublicKey(account, opts) {
|
|
480
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
481
|
-
const normalizedAddress = (0, eth_sig_util_1.normalize)(account);
|
|
482
|
-
const keyring = (yield this.getKeyringForAccount(account));
|
|
483
|
-
if (!keyring.getEncryptionPublicKey) {
|
|
484
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedGetEncryptionPublicKey);
|
|
485
|
-
}
|
|
486
|
-
return yield keyring.getEncryptionPublicKey(normalizedAddress, opts);
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Attempts to decrypt the provided message parameters.
|
|
491
|
-
*
|
|
492
|
-
* @param messageParams - The decryption message parameters.
|
|
493
|
-
* @param messageParams.from - The address of the account you want to use to decrypt the message.
|
|
494
|
-
* @param messageParams.data - The encrypted data that you want to decrypt.
|
|
495
|
-
* @returns The raw decryption result.
|
|
496
|
-
*/
|
|
497
|
-
decryptMessage(messageParams) {
|
|
498
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
499
|
-
const address = (0, eth_sig_util_1.normalize)(messageParams.from);
|
|
500
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
501
|
-
if (!keyring.decryptMessage) {
|
|
502
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedDecryptMessage);
|
|
503
|
-
}
|
|
504
|
-
return keyring.decryptMessage(address, messageParams.data);
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* Returns the currently initialized keyring that manages
|
|
509
|
-
* the specified `address` if one exists.
|
|
510
|
-
*
|
|
511
|
-
* @deprecated Use of this method is discouraged as actions executed directly on
|
|
512
|
-
* keyrings are not being reflected in the KeyringController state and not
|
|
513
|
-
* persisted in the vault.
|
|
514
|
-
* @param account - An account address.
|
|
515
|
-
* @returns Promise resolving to keyring of the `account` if one exists.
|
|
516
|
-
*/
|
|
517
|
-
getKeyringForAccount(account) {
|
|
518
|
-
var _a;
|
|
519
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
520
|
-
// Cast to `Hex` here is safe here because `address` is not nullish.
|
|
521
|
-
// `normalizeToHex` returns `Hex` unless given a nullish value.
|
|
522
|
-
const hexed = (0, eth_sig_util_1.normalize)(account);
|
|
523
|
-
const candidates = yield Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map((keyring) => __awaiter(this, void 0, void 0, function* () {
|
|
524
|
-
return Promise.all([keyring, keyring.getAccounts()]);
|
|
525
|
-
})));
|
|
526
|
-
const winners = candidates.filter((candidate) => {
|
|
527
|
-
const accounts = candidate[1].map(eth_sig_util_1.normalize);
|
|
528
|
-
return accounts.includes(hexed);
|
|
529
|
-
});
|
|
530
|
-
if (winners.length && ((_a = winners[0]) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
531
|
-
return winners[0][0];
|
|
532
|
-
}
|
|
533
|
-
// Adding more info to the error
|
|
534
|
-
let errorInfo = '';
|
|
535
|
-
if (!(0, utils_1.isValidHexAddress)(hexed)) {
|
|
536
|
-
errorInfo = 'The address passed in is invalid/empty';
|
|
537
|
-
}
|
|
538
|
-
else if (!candidates.length) {
|
|
539
|
-
errorInfo = 'There are no keyrings';
|
|
540
|
-
}
|
|
541
|
-
else if (!winners.length) {
|
|
542
|
-
errorInfo = 'There are keyrings, but none match the address';
|
|
543
|
-
}
|
|
544
|
-
throw new Error(`${constants_1.KeyringControllerError.NoKeyring}. Error info: ${errorInfo}`);
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
/**
|
|
548
|
-
* Returns all keyrings of the given type.
|
|
549
|
-
*
|
|
550
|
-
* @deprecated Use of this method is discouraged as actions executed directly on
|
|
551
|
-
* keyrings are not being reflected in the KeyringController state and not
|
|
552
|
-
* persisted in the vault.
|
|
553
|
-
* @param type - Keyring type name.
|
|
554
|
-
* @returns An array of keyrings of the given type.
|
|
555
|
-
*/
|
|
556
|
-
getKeyringsByType(type) {
|
|
557
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter((keyring) => keyring.type === type);
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* Persist all serialized keyrings in the vault.
|
|
561
|
-
*
|
|
562
|
-
* @returns Promise resolving with `true` value when the
|
|
563
|
-
* operation completes.
|
|
564
|
-
*/
|
|
565
|
-
persistAllKeyrings() {
|
|
566
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
567
|
-
const { encryptionKey, encryptionSalt } = this.state;
|
|
568
|
-
if (!__classPrivateFieldGet(this, _KeyringController_password, "f") && !encryptionKey) {
|
|
569
|
-
throw new Error(constants_1.KeyringControllerError.MissingCredentials);
|
|
570
|
-
}
|
|
571
|
-
const serializedKeyrings = yield Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map((keyring) => __awaiter(this, void 0, void 0, function* () {
|
|
572
|
-
const [type, data] = yield Promise.all([
|
|
573
|
-
keyring.type,
|
|
574
|
-
keyring.serialize(),
|
|
575
|
-
]);
|
|
576
|
-
return { type, data };
|
|
577
|
-
})));
|
|
578
|
-
serializedKeyrings.push(...__classPrivateFieldGet(this, _KeyringController_unsupportedKeyrings, "f"));
|
|
579
|
-
let vault;
|
|
580
|
-
let newEncryptionKey;
|
|
581
|
-
if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
|
|
582
|
-
assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
|
|
583
|
-
if (encryptionKey) {
|
|
584
|
-
const key = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
|
|
585
|
-
const vaultJSON = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithKey(key, serializedKeyrings);
|
|
586
|
-
vaultJSON.salt = encryptionSalt;
|
|
587
|
-
vault = JSON.stringify(vaultJSON);
|
|
588
|
-
}
|
|
589
|
-
else if (__classPrivateFieldGet(this, _KeyringController_password, "f")) {
|
|
590
|
-
const { vault: newVault, exportedKeyString } = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithDetail(__classPrivateFieldGet(this, _KeyringController_password, "f"), serializedKeyrings);
|
|
591
|
-
vault = newVault;
|
|
592
|
-
newEncryptionKey = exportedKeyString;
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
else {
|
|
596
|
-
if (typeof __classPrivateFieldGet(this, _KeyringController_password, "f") !== 'string') {
|
|
597
|
-
throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
|
|
598
|
-
}
|
|
599
|
-
vault = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encrypt(__classPrivateFieldGet(this, _KeyringController_password, "f"), serializedKeyrings);
|
|
600
|
-
}
|
|
601
|
-
if (!vault) {
|
|
602
|
-
throw new Error(constants_1.KeyringControllerError.MissingVaultData);
|
|
603
|
-
}
|
|
604
|
-
this.update((state) => {
|
|
605
|
-
state.vault = vault;
|
|
606
|
-
});
|
|
607
|
-
// The keyring updates need to be announced before updating the encryptionKey
|
|
608
|
-
// so that the updated keyring gets propagated to the extension first.
|
|
609
|
-
// Not calling {@link updateKeyringsInState} results in the wrong account being selected
|
|
610
|
-
// in the extension.
|
|
611
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateKeyringsInState).call(this);
|
|
612
|
-
if (newEncryptionKey) {
|
|
613
|
-
this.update((state) => {
|
|
614
|
-
state.encryptionKey = newEncryptionKey;
|
|
615
|
-
state.encryptionSalt = JSON.parse(vault).salt;
|
|
616
|
-
});
|
|
617
|
-
}
|
|
618
|
-
return true;
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Imports an account with the specified import strategy.
|
|
623
|
-
*
|
|
624
|
-
* @param strategy - Import strategy name.
|
|
625
|
-
* @param args - Array of arguments to pass to the underlying stategy.
|
|
626
|
-
* @throws Will throw when passed an unrecognized strategy.
|
|
627
|
-
* @returns Promise resolving to keyring current state and imported account
|
|
628
|
-
* address.
|
|
629
|
-
*/
|
|
630
|
-
importAccountWithStrategy(strategy,
|
|
631
|
-
// TODO: Replace `any` with type
|
|
632
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
633
|
-
args) {
|
|
634
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
635
|
-
let privateKey;
|
|
636
|
-
switch (strategy) {
|
|
637
|
-
case 'privateKey':
|
|
638
|
-
const [importedKey] = args;
|
|
639
|
-
if (!importedKey) {
|
|
640
|
-
throw new Error('Cannot import an empty key.');
|
|
641
|
-
}
|
|
642
|
-
const prefixed = (0, utils_1.add0x)(importedKey);
|
|
643
|
-
let bufferedPrivateKey;
|
|
644
|
-
try {
|
|
645
|
-
bufferedPrivateKey = (0, util_1.toBuffer)(prefixed);
|
|
646
|
-
}
|
|
647
|
-
catch (_a) {
|
|
648
|
-
throw new Error('Cannot import invalid private key.');
|
|
649
|
-
}
|
|
650
|
-
if (!(0, util_1.isValidPrivate)(bufferedPrivateKey) ||
|
|
651
|
-
// ensures that the key is 64 bytes long
|
|
652
|
-
(0, util_1.getBinarySize)(prefixed) !== 64 + '0x'.length) {
|
|
653
|
-
throw new Error('Cannot import invalid private key.');
|
|
654
|
-
}
|
|
655
|
-
privateKey = (0, utils_1.remove0x)(prefixed);
|
|
656
|
-
break;
|
|
657
|
-
case 'json':
|
|
658
|
-
let wallet;
|
|
659
|
-
const [input, password] = args;
|
|
660
|
-
try {
|
|
661
|
-
wallet = ethereumjs_wallet_1.thirdparty.fromEtherWallet(input, password);
|
|
662
|
-
}
|
|
663
|
-
catch (e) {
|
|
664
|
-
wallet = wallet || (yield ethereumjs_wallet_1.default.fromV3(input, password, true));
|
|
665
|
-
}
|
|
666
|
-
privateKey = (0, utils_1.bytesToHex)(wallet.getPrivateKey());
|
|
667
|
-
break;
|
|
668
|
-
default:
|
|
669
|
-
throw new Error(`Unexpected import strategy: '${strategy}'`);
|
|
670
|
-
}
|
|
671
|
-
const newKeyring = (yield this.addNewKeyring(KeyringTypes.simple, [
|
|
672
|
-
privateKey,
|
|
673
|
-
]));
|
|
674
|
-
const accounts = yield newKeyring.getAccounts();
|
|
675
|
-
return {
|
|
676
|
-
keyringState: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this),
|
|
677
|
-
importedAccountAddress: accounts[0],
|
|
678
|
-
};
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
/**
|
|
682
|
-
* Removes an account from keyring state.
|
|
683
|
-
*
|
|
684
|
-
* @param address - Address of the account to remove.
|
|
685
|
-
* @fires KeyringController:accountRemoved
|
|
686
|
-
* @returns Promise resolving current state when this account removal completes.
|
|
687
|
-
*/
|
|
688
|
-
removeAccount(address) {
|
|
689
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
690
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
691
|
-
// Not all the keyrings support this, so we have to check
|
|
692
|
-
if (!keyring.removeAccount) {
|
|
693
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedRemoveAccount);
|
|
694
|
-
}
|
|
695
|
-
// The `removeAccount` method of snaps keyring is async. We have to update
|
|
696
|
-
// the interface of the other keyrings to be async as well.
|
|
697
|
-
// eslint-disable-next-line @typescript-eslint/await-thenable
|
|
698
|
-
yield keyring.removeAccount(address);
|
|
699
|
-
const accounts = yield keyring.getAccounts();
|
|
700
|
-
// Check if this was the last/only account
|
|
701
|
-
if (accounts.length === 0) {
|
|
702
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_removeEmptyKeyrings).call(this);
|
|
703
|
-
}
|
|
704
|
-
yield this.persistAllKeyrings();
|
|
705
|
-
this.messagingSystem.publish(`${name}:accountRemoved`, address);
|
|
706
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
707
|
-
});
|
|
708
|
-
}
|
|
709
|
-
/**
|
|
710
|
-
* Deallocates all secrets and locks the wallet.
|
|
711
|
-
*
|
|
712
|
-
* @returns Promise resolving to current state.
|
|
713
|
-
*/
|
|
714
|
-
setLocked() {
|
|
715
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
716
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unsubscribeFromQRKeyringsEvents).call(this);
|
|
717
|
-
__classPrivateFieldSet(this, _KeyringController_password, undefined, "f");
|
|
718
|
-
this.update((state) => {
|
|
719
|
-
state.isUnlocked = false;
|
|
720
|
-
state.keyrings = [];
|
|
721
|
-
});
|
|
722
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
|
|
723
|
-
this.messagingSystem.publish(`${name}:lock`);
|
|
724
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
725
|
-
});
|
|
726
|
-
}
|
|
727
|
-
/**
|
|
728
|
-
* Signs message by calling down into a specific keyring.
|
|
729
|
-
*
|
|
730
|
-
* @param messageParams - PersonalMessageParams object to sign.
|
|
731
|
-
* @returns Promise resolving to a signed message string.
|
|
732
|
-
*/
|
|
733
|
-
signMessage(messageParams) {
|
|
734
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
735
|
-
if (!messageParams.data) {
|
|
736
|
-
throw new Error("Can't sign an empty message");
|
|
737
|
-
}
|
|
738
|
-
const address = (0, eth_sig_util_1.normalize)(messageParams.from);
|
|
739
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
740
|
-
if (!keyring.signMessage) {
|
|
741
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedSignMessage);
|
|
742
|
-
}
|
|
743
|
-
return yield keyring.signMessage(address, messageParams.data);
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
/**
|
|
747
|
-
* Signs personal message by calling down into a specific keyring.
|
|
748
|
-
*
|
|
749
|
-
* @param messageParams - PersonalMessageParams object to sign.
|
|
750
|
-
* @returns Promise resolving to a signed message string.
|
|
751
|
-
*/
|
|
752
|
-
signPersonalMessage(messageParams) {
|
|
753
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
754
|
-
const address = (0, eth_sig_util_1.normalize)(messageParams.from);
|
|
755
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
756
|
-
if (!keyring.signPersonalMessage) {
|
|
757
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedSignPersonalMessage);
|
|
758
|
-
}
|
|
759
|
-
const normalizedData = (0, eth_sig_util_1.normalize)(messageParams.data);
|
|
760
|
-
return yield keyring.signPersonalMessage(address, normalizedData);
|
|
761
|
-
});
|
|
762
|
-
}
|
|
763
|
-
/**
|
|
764
|
-
* Signs typed message by calling down into a specific keyring.
|
|
765
|
-
*
|
|
766
|
-
* @param messageParams - TypedMessageParams object to sign.
|
|
767
|
-
* @param version - Compatibility version EIP712.
|
|
768
|
-
* @throws Will throw when passed an unrecognized version.
|
|
769
|
-
* @returns Promise resolving to a signed message string or an error if any.
|
|
770
|
-
*/
|
|
771
|
-
signTypedMessage(messageParams, version) {
|
|
772
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
773
|
-
try {
|
|
774
|
-
if (![
|
|
775
|
-
SignTypedDataVersion.V1,
|
|
776
|
-
SignTypedDataVersion.V3,
|
|
777
|
-
SignTypedDataVersion.V4,
|
|
778
|
-
].includes(version)) {
|
|
779
|
-
throw new Error(`Unexpected signTypedMessage version: '${version}'`);
|
|
780
|
-
}
|
|
781
|
-
// Cast to `Hex` here is safe here because `messageParams.from` is not nullish.
|
|
782
|
-
// `normalize` returns `Hex` unless given a nullish value.
|
|
783
|
-
const address = (0, eth_sig_util_1.normalize)(messageParams.from);
|
|
784
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
785
|
-
if (!keyring.signTypedData) {
|
|
786
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedSignTypedMessage);
|
|
787
|
-
}
|
|
788
|
-
return yield keyring.signTypedData(address, version !== SignTypedDataVersion.V1 &&
|
|
789
|
-
typeof messageParams.data === 'string'
|
|
790
|
-
? JSON.parse(messageParams.data)
|
|
791
|
-
: messageParams.data, { version });
|
|
792
|
-
}
|
|
793
|
-
catch (error) {
|
|
794
|
-
throw new Error(`Keyring Controller signTypedMessage: ${error}`);
|
|
795
|
-
}
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
/**
|
|
799
|
-
* Signs a transaction by calling down into a specific keyring.
|
|
800
|
-
*
|
|
801
|
-
* @param transaction - Transaction object to sign. Must be a `ethereumjs-tx` transaction instance.
|
|
802
|
-
* @param from - Address to sign from, should be in keychain.
|
|
803
|
-
* @param opts - An optional options object.
|
|
804
|
-
* @returns Promise resolving to a signed transaction string.
|
|
805
|
-
*/
|
|
806
|
-
signTransaction(transaction, from, opts) {
|
|
807
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
808
|
-
const address = (0, eth_sig_util_1.normalize)(from);
|
|
809
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
810
|
-
if (!keyring.signTransaction) {
|
|
811
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedSignTransaction);
|
|
812
|
-
}
|
|
813
|
-
return yield keyring.signTransaction(address, transaction, opts);
|
|
814
|
-
});
|
|
815
|
-
}
|
|
816
|
-
/**
|
|
817
|
-
* Convert a base transaction to a base UserOperation.
|
|
818
|
-
*
|
|
819
|
-
* @param from - Address of the sender.
|
|
820
|
-
* @param transactions - Base transactions to include in the UserOperation.
|
|
821
|
-
* @returns A pseudo-UserOperation that can be used to construct a real.
|
|
822
|
-
*/
|
|
823
|
-
prepareUserOperation(from, transactions) {
|
|
824
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
825
|
-
const address = (0, eth_sig_util_1.normalize)(from);
|
|
826
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
827
|
-
if (!keyring.prepareUserOperation) {
|
|
828
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedPrepareUserOperation);
|
|
829
|
-
}
|
|
830
|
-
return yield keyring.prepareUserOperation(address, transactions);
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
/**
|
|
834
|
-
* Patches properties of a UserOperation. Currently, only the
|
|
835
|
-
* `paymasterAndData` can be patched.
|
|
836
|
-
*
|
|
837
|
-
* @param from - Address of the sender.
|
|
838
|
-
* @param userOp - UserOperation to patch.
|
|
839
|
-
* @returns A patch to apply to the UserOperation.
|
|
840
|
-
*/
|
|
841
|
-
patchUserOperation(from, userOp) {
|
|
842
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
843
|
-
const address = (0, eth_sig_util_1.normalize)(from);
|
|
844
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
845
|
-
if (!keyring.patchUserOperation) {
|
|
846
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedPatchUserOperation);
|
|
847
|
-
}
|
|
848
|
-
return yield keyring.patchUserOperation(address, userOp);
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
|
-
/**
|
|
852
|
-
* Signs an UserOperation.
|
|
853
|
-
*
|
|
854
|
-
* @param from - Address of the sender.
|
|
855
|
-
* @param userOp - UserOperation to sign.
|
|
856
|
-
* @returns The signature of the UserOperation.
|
|
857
|
-
*/
|
|
858
|
-
signUserOperation(from, userOp) {
|
|
859
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
860
|
-
const address = (0, eth_sig_util_1.normalize)(from);
|
|
861
|
-
const keyring = (yield this.getKeyringForAccount(address));
|
|
862
|
-
if (!keyring.signUserOperation) {
|
|
863
|
-
throw new Error(constants_1.KeyringControllerError.UnsupportedSignUserOperation);
|
|
864
|
-
}
|
|
865
|
-
return yield keyring.signUserOperation(address, userOp);
|
|
866
|
-
});
|
|
867
|
-
}
|
|
868
|
-
/**
|
|
869
|
-
* Attempts to decrypt the current vault and load its keyrings,
|
|
870
|
-
* using the given encryption key and salt.
|
|
871
|
-
*
|
|
872
|
-
* @param encryptionKey - Key to unlock the keychain.
|
|
873
|
-
* @param encryptionSalt - Salt to unlock the keychain.
|
|
874
|
-
* @returns Promise resolving to the current state.
|
|
875
|
-
*/
|
|
876
|
-
submitEncryptionKey(encryptionKey, encryptionSalt) {
|
|
877
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
878
|
-
__classPrivateFieldSet(this, _KeyringController_keyrings, yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, undefined, encryptionKey, encryptionSalt), "f");
|
|
879
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
|
|
880
|
-
const qrKeyring = this.getQRKeyring();
|
|
881
|
-
if (qrKeyring) {
|
|
882
|
-
// if there is a QR keyring, we need to subscribe
|
|
883
|
-
// to its events after unlocking the vault
|
|
884
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, qrKeyring);
|
|
885
|
-
}
|
|
886
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
887
|
-
});
|
|
888
|
-
}
|
|
889
|
-
/**
|
|
890
|
-
* Attempts to decrypt the current vault and load its keyrings,
|
|
891
|
-
* using the given password.
|
|
892
|
-
*
|
|
893
|
-
* @param password - Password to unlock the keychain.
|
|
894
|
-
* @returns Promise resolving to the current state.
|
|
895
|
-
*/
|
|
896
|
-
submitPassword(password) {
|
|
897
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
898
|
-
__classPrivateFieldSet(this, _KeyringController_keyrings, yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, password), "f");
|
|
899
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
|
|
900
|
-
const qrKeyring = this.getQRKeyring();
|
|
901
|
-
if (qrKeyring) {
|
|
902
|
-
// if there is a QR keyring, we need to subscribe
|
|
903
|
-
// to its events after unlocking the vault
|
|
904
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, qrKeyring);
|
|
905
|
-
}
|
|
906
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
907
|
-
});
|
|
908
|
-
}
|
|
909
|
-
/**
|
|
910
|
-
* Verifies the that the seed phrase restores the current keychain's accounts.
|
|
911
|
-
*
|
|
912
|
-
* @returns Promise resolving to the seed phrase as Uint8Array.
|
|
913
|
-
*/
|
|
914
|
-
verifySeedPhrase() {
|
|
915
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
916
|
-
const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0];
|
|
917
|
-
if (!primaryKeyring) {
|
|
918
|
-
throw new Error('No HD keyring found.');
|
|
919
|
-
}
|
|
920
|
-
assertHasUint8ArrayMnemonic(primaryKeyring);
|
|
921
|
-
const seedWords = primaryKeyring.mnemonic;
|
|
922
|
-
const accounts = yield primaryKeyring.getAccounts();
|
|
923
|
-
/* istanbul ignore if */
|
|
924
|
-
if (accounts.length === 0) {
|
|
925
|
-
throw new Error('Cannot verify an empty keyring.');
|
|
926
|
-
}
|
|
927
|
-
// The HD Keyring Builder is a default keyring builder
|
|
928
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
929
|
-
const hdKeyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, KeyringTypes.hd);
|
|
930
|
-
const hdKeyring = hdKeyringBuilder();
|
|
931
|
-
// @ts-expect-error @metamask/eth-hd-keyring correctly handles
|
|
932
|
-
// Uint8Array seed phrases in the `deserialize` method.
|
|
933
|
-
yield hdKeyring.deserialize({
|
|
934
|
-
mnemonic: seedWords,
|
|
935
|
-
numberOfAccounts: accounts.length,
|
|
936
|
-
});
|
|
937
|
-
const testAccounts = yield hdKeyring.getAccounts();
|
|
938
|
-
/* istanbul ignore if */
|
|
939
|
-
if (testAccounts.length !== accounts.length) {
|
|
940
|
-
throw new Error('Seed phrase imported incorrect number of accounts.');
|
|
941
|
-
}
|
|
942
|
-
testAccounts.forEach((account, i) => {
|
|
943
|
-
/* istanbul ignore if */
|
|
944
|
-
if (account.toLowerCase() !== accounts[i].toLowerCase()) {
|
|
945
|
-
throw new Error('Seed phrase imported different accounts.');
|
|
946
|
-
}
|
|
947
|
-
});
|
|
948
|
-
return seedWords;
|
|
949
|
-
});
|
|
950
|
-
}
|
|
951
|
-
// QR Hardware related methods
|
|
952
|
-
/**
|
|
953
|
-
* Get QR Hardware keyring.
|
|
954
|
-
*
|
|
955
|
-
* @returns The QR Keyring if defined, otherwise undefined
|
|
956
|
-
*/
|
|
957
|
-
getQRKeyring() {
|
|
958
|
-
// QRKeyring is not yet compatible with Keyring type from @metamask/utils
|
|
959
|
-
return this.getKeyringsByType(KeyringTypes.qr)[0];
|
|
960
|
-
}
|
|
961
|
-
/**
|
|
962
|
-
* Get QR hardware keyring. If it doesn't exist, add it.
|
|
963
|
-
*
|
|
964
|
-
* @returns The added keyring
|
|
965
|
-
*/
|
|
966
|
-
getOrAddQRKeyring() {
|
|
967
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
968
|
-
return this.getQRKeyring() || (yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
|
|
969
|
-
});
|
|
970
|
-
}
|
|
971
|
-
// TODO: Replace `any` with type
|
|
972
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
973
|
-
restoreQRKeyring(serialized) {
|
|
974
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
975
|
-
(yield this.getOrAddQRKeyring()).deserialize(serialized);
|
|
976
|
-
yield this.persistAllKeyrings();
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
resetQRKeyringState() {
|
|
980
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
981
|
-
(yield this.getOrAddQRKeyring()).resetStore();
|
|
982
|
-
});
|
|
983
|
-
}
|
|
984
|
-
getQRKeyringState() {
|
|
985
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
986
|
-
return (yield this.getOrAddQRKeyring()).getMemStore();
|
|
987
|
-
});
|
|
988
|
-
}
|
|
989
|
-
submitQRCryptoHDKey(cryptoHDKey) {
|
|
990
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
991
|
-
(yield this.getOrAddQRKeyring()).submitCryptoHDKey(cryptoHDKey);
|
|
992
|
-
});
|
|
993
|
-
}
|
|
994
|
-
submitQRCryptoAccount(cryptoAccount) {
|
|
995
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
996
|
-
(yield this.getOrAddQRKeyring()).submitCryptoAccount(cryptoAccount);
|
|
997
|
-
});
|
|
998
|
-
}
|
|
999
|
-
submitQRSignature(requestId, ethSignature) {
|
|
1000
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1001
|
-
(yield this.getOrAddQRKeyring()).submitSignature(requestId, ethSignature);
|
|
1002
|
-
});
|
|
1003
|
-
}
|
|
1004
|
-
cancelQRSignRequest() {
|
|
1005
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1006
|
-
(yield this.getOrAddQRKeyring()).cancelSignRequest();
|
|
1007
|
-
});
|
|
1008
|
-
}
|
|
1009
|
-
/**
|
|
1010
|
-
* Cancels qr keyring sync.
|
|
1011
|
-
*/
|
|
1012
|
-
cancelQRSynchronization() {
|
|
1013
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1014
|
-
// eslint-disable-next-line n/no-sync
|
|
1015
|
-
(yield this.getOrAddQRKeyring()).cancelSync();
|
|
1016
|
-
});
|
|
1017
|
-
}
|
|
1018
|
-
connectQRHardware(page) {
|
|
1019
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1020
|
-
try {
|
|
1021
|
-
const keyring = yield this.getOrAddQRKeyring();
|
|
1022
|
-
let accounts;
|
|
1023
|
-
switch (page) {
|
|
1024
|
-
case -1:
|
|
1025
|
-
accounts = yield keyring.getPreviousPage();
|
|
1026
|
-
break;
|
|
1027
|
-
case 1:
|
|
1028
|
-
accounts = yield keyring.getNextPage();
|
|
1029
|
-
break;
|
|
1030
|
-
default:
|
|
1031
|
-
accounts = yield keyring.getFirstPage();
|
|
1032
|
-
}
|
|
1033
|
-
// TODO: Replace `any` with type
|
|
1034
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1035
|
-
return accounts.map((account) => {
|
|
1036
|
-
return Object.assign(Object.assign({}, account), { balance: '0x0' });
|
|
1037
|
-
});
|
|
1038
|
-
}
|
|
1039
|
-
catch (e) {
|
|
1040
|
-
// TODO: Add test case for when keyring throws
|
|
1041
|
-
/* istanbul ignore next */
|
|
1042
|
-
throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
|
|
1043
|
-
}
|
|
1044
|
-
});
|
|
1045
|
-
}
|
|
1046
|
-
unlockQRHardwareWalletAccount(index) {
|
|
1047
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1048
|
-
const keyring = yield this.getOrAddQRKeyring();
|
|
1049
|
-
keyring.setAccountToUnlock(index);
|
|
1050
|
-
// QRKeyring is not yet compatible with Keyring from
|
|
1051
|
-
// @metamask/utils, but we can use the `addNewAccount` method
|
|
1052
|
-
// as it internally calls `addAccounts` from on the keyring instance,
|
|
1053
|
-
// which is supported by QRKeyring API.
|
|
1054
|
-
yield this.addNewAccountForKeyring(keyring);
|
|
1055
|
-
yield this.persistAllKeyrings();
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
getAccountKeyringType(account) {
|
|
1059
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1060
|
-
const keyring = (yield this.getKeyringForAccount(account));
|
|
1061
|
-
return keyring.type;
|
|
1062
|
-
});
|
|
1063
|
-
}
|
|
1064
|
-
forgetQRDevice() {
|
|
1065
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1066
|
-
const keyring = yield this.getOrAddQRKeyring();
|
|
1067
|
-
const allAccounts = (yield this.getAccounts());
|
|
1068
|
-
keyring.forgetDevice();
|
|
1069
|
-
const remainingAccounts = (yield this.getAccounts());
|
|
1070
|
-
const removedAccounts = allAccounts.filter((address) => !remainingAccounts.includes(address));
|
|
1071
|
-
yield this.persistAllKeyrings();
|
|
1072
|
-
return { removedAccounts, remainingAccounts };
|
|
1073
|
-
});
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
exports.KeyringController = KeyringController;
|
|
1077
|
-
_KeyringController_keyringBuilders = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
|
|
1078
|
-
this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
|
|
1079
|
-
this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
|
|
1080
|
-
this.messagingSystem.registerActionHandler(`${name}:signTypedMessage`, this.signTypedMessage.bind(this));
|
|
1081
|
-
this.messagingSystem.registerActionHandler(`${name}:decryptMessage`, this.decryptMessage.bind(this));
|
|
1082
|
-
this.messagingSystem.registerActionHandler(`${name}:getEncryptionPublicKey`, this.getEncryptionPublicKey.bind(this));
|
|
1083
|
-
this.messagingSystem.registerActionHandler(`${name}:getAccounts`, this.getAccounts.bind(this));
|
|
1084
|
-
this.messagingSystem.registerActionHandler(`${name}:getKeyringsByType`, this.getKeyringsByType.bind(this));
|
|
1085
|
-
this.messagingSystem.registerActionHandler(`${name}:getKeyringForAccount`, this.getKeyringForAccount.bind(this));
|
|
1086
|
-
this.messagingSystem.registerActionHandler(`${name}:persistAllKeyrings`, this.persistAllKeyrings.bind(this));
|
|
1087
|
-
this.messagingSystem.registerActionHandler(`${name}:prepareUserOperation`, this.prepareUserOperation.bind(this));
|
|
1088
|
-
this.messagingSystem.registerActionHandler(`${name}:patchUserOperation`, this.patchUserOperation.bind(this));
|
|
1089
|
-
this.messagingSystem.registerActionHandler(`${name}:signUserOperation`, this.signUserOperation.bind(this));
|
|
1090
|
-
}, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
|
|
1091
|
-
return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
|
|
1092
|
-
}, _KeyringController_addQRKeyring = function _KeyringController_addQRKeyring() {
|
|
1093
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1094
|
-
// QRKeyring is not yet compatible with Keyring type from @metamask/utils
|
|
1095
|
-
const qrKeyring = (yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr, {
|
|
1096
|
-
accounts: [],
|
|
1097
|
-
}));
|
|
1098
|
-
const accounts = yield qrKeyring.getAccounts();
|
|
1099
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_checkForDuplicate).call(this, KeyringTypes.qr, accounts);
|
|
1100
|
-
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(qrKeyring);
|
|
1101
|
-
yield this.persistAllKeyrings();
|
|
1102
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, qrKeyring);
|
|
1103
|
-
return qrKeyring;
|
|
1104
|
-
});
|
|
1105
|
-
}, _KeyringController_subscribeToQRKeyringEvents = function _KeyringController_subscribeToQRKeyringEvents(qrKeyring) {
|
|
1106
|
-
__classPrivateFieldSet(this, _KeyringController_qrKeyringStateListener, (state) => {
|
|
1107
|
-
this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
|
|
1108
|
-
}, "f");
|
|
1109
|
-
qrKeyring.getMemStore().subscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
|
|
1110
|
-
}, _KeyringController_unsubscribeFromQRKeyringsEvents = function _KeyringController_unsubscribeFromQRKeyringsEvents() {
|
|
1111
|
-
const qrKeyrings = this.getKeyringsByType(KeyringTypes.qr);
|
|
1112
|
-
qrKeyrings.forEach((qrKeyring) => {
|
|
1113
|
-
if (__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f")) {
|
|
1114
|
-
qrKeyring.getMemStore().unsubscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
|
|
1115
|
-
}
|
|
1116
|
-
});
|
|
1117
|
-
}, _KeyringController_createNewVaultWithKeyring = function _KeyringController_createNewVaultWithKeyring(password, keyring) {
|
|
1118
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1119
|
-
if (typeof password !== 'string') {
|
|
1120
|
-
throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
|
|
1121
|
-
}
|
|
1122
|
-
__classPrivateFieldSet(this, _KeyringController_password, password, "f");
|
|
1123
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
|
|
1124
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
|
|
1125
|
-
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
|
|
1126
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getMemState).call(this);
|
|
1127
|
-
});
|
|
1128
|
-
}, _KeyringController_updateKeyringsInState = function _KeyringController_updateKeyringsInState() {
|
|
1129
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1130
|
-
const keyrings = yield Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(displayForKeyring));
|
|
1131
|
-
this.update((state) => {
|
|
1132
|
-
state.keyrings = keyrings;
|
|
1133
|
-
});
|
|
1134
|
-
});
|
|
1135
|
-
}, _KeyringController_unlockKeyrings = function _KeyringController_unlockKeyrings(password, encryptionKey, encryptionSalt) {
|
|
1136
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1137
|
-
const encryptedVault = this.state.vault;
|
|
1138
|
-
if (!encryptedVault) {
|
|
1139
|
-
throw new Error(constants_1.KeyringControllerError.VaultError);
|
|
1140
|
-
}
|
|
1141
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
|
|
1142
|
-
let vault;
|
|
1143
|
-
if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
|
|
1144
|
-
assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
|
|
1145
|
-
if (password) {
|
|
1146
|
-
const result = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithDetail(password, encryptedVault);
|
|
1147
|
-
vault = result.vault;
|
|
1148
|
-
__classPrivateFieldSet(this, _KeyringController_password, password, "f");
|
|
1149
|
-
this.update((state) => {
|
|
1150
|
-
state.encryptionKey = result.exportedKeyString;
|
|
1151
|
-
state.encryptionSalt = result.salt;
|
|
1152
|
-
});
|
|
1153
|
-
}
|
|
1154
|
-
else {
|
|
1155
|
-
const parsedEncryptedVault = JSON.parse(encryptedVault);
|
|
1156
|
-
if (encryptionSalt !== parsedEncryptedVault.salt) {
|
|
1157
|
-
throw new Error(constants_1.KeyringControllerError.ExpiredCredentials);
|
|
1158
|
-
}
|
|
1159
|
-
if (typeof encryptionKey !== 'string') {
|
|
1160
|
-
throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
|
|
1161
|
-
}
|
|
1162
|
-
const key = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
|
|
1163
|
-
vault = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
|
|
1164
|
-
// This call is required on the first call because encryptionKey
|
|
1165
|
-
// is not yet inside the memStore
|
|
1166
|
-
this.update((state) => {
|
|
1167
|
-
state.encryptionKey = encryptionKey;
|
|
1168
|
-
// we can safely assume that encryptionSalt is defined here
|
|
1169
|
-
// because we compare it with the salt from the vault
|
|
1170
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1171
|
-
state.encryptionSalt = encryptionSalt;
|
|
1172
|
-
});
|
|
1173
|
-
}
|
|
1174
|
-
}
|
|
1175
|
-
else {
|
|
1176
|
-
if (typeof password !== 'string') {
|
|
1177
|
-
throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
|
|
1178
|
-
}
|
|
1179
|
-
vault = yield __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decrypt(password, encryptedVault);
|
|
1180
|
-
__classPrivateFieldSet(this, _KeyringController_password, password, "f");
|
|
1181
|
-
}
|
|
1182
|
-
if (!isSerializedKeyringsArray(vault)) {
|
|
1183
|
-
throw new Error(constants_1.KeyringControllerError.VaultDataError);
|
|
1184
|
-
}
|
|
1185
|
-
yield Promise.all(vault.map(__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).bind(this)));
|
|
1186
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateKeyringsInState).call(this);
|
|
1187
|
-
if (__classPrivateFieldGet(this, _KeyringController_password, "f") &&
|
|
1188
|
-
(!__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f") || !encryptionKey) &&
|
|
1189
|
-
__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated &&
|
|
1190
|
-
!__classPrivateFieldGet(this, _KeyringController_encryptor, "f").isVaultUpdated(encryptedVault)) {
|
|
1191
|
-
// Re-encrypt the vault with safer method if one is available
|
|
1192
|
-
yield this.persistAllKeyrings();
|
|
1193
|
-
}
|
|
1194
|
-
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f");
|
|
1195
|
-
});
|
|
1196
|
-
}, _KeyringController_createKeyringWithFirstAccount = function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
1197
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1198
|
-
const keyring = (yield this.addNewKeyring(type, opts));
|
|
1199
|
-
const [firstAccount] = yield keyring.getAccounts();
|
|
1200
|
-
if (!firstAccount) {
|
|
1201
|
-
throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
|
|
1202
|
-
}
|
|
1203
|
-
});
|
|
1204
|
-
}, _KeyringController_newKeyring = function _KeyringController_newKeyring(type, data) {
|
|
1205
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1206
|
-
const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
|
|
1207
|
-
if (!keyringBuilder) {
|
|
1208
|
-
throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
|
|
1209
|
-
}
|
|
1210
|
-
const keyring = keyringBuilder();
|
|
1211
|
-
// @ts-expect-error Enforce data type after updating clients
|
|
1212
|
-
yield keyring.deserialize(data);
|
|
1213
|
-
if (keyring.init) {
|
|
1214
|
-
yield keyring.init();
|
|
1215
|
-
}
|
|
1216
|
-
return keyring;
|
|
1217
|
-
});
|
|
1218
|
-
}, _KeyringController_clearKeyrings = function _KeyringController_clearKeyrings() {
|
|
1219
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1220
|
-
for (const keyring of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
|
|
1221
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
1222
|
-
}
|
|
1223
|
-
__classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
|
|
1224
|
-
this.update((state) => {
|
|
1225
|
-
state.keyrings = [];
|
|
1226
|
-
});
|
|
1227
|
-
});
|
|
1228
|
-
}, _KeyringController_restoreKeyring = function _KeyringController_restoreKeyring(serialized) {
|
|
1229
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1230
|
-
try {
|
|
1231
|
-
const { type, data } = serialized;
|
|
1232
|
-
const keyring = yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, data);
|
|
1233
|
-
// getAccounts also validates the accounts for some keyrings
|
|
1234
|
-
yield keyring.getAccounts();
|
|
1235
|
-
__classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
|
|
1236
|
-
return keyring;
|
|
1237
|
-
}
|
|
1238
|
-
catch (_) {
|
|
1239
|
-
__classPrivateFieldGet(this, _KeyringController_unsupportedKeyrings, "f").push(serialized);
|
|
1240
|
-
return undefined;
|
|
1241
|
-
}
|
|
1242
|
-
});
|
|
1243
|
-
}, _KeyringController_destroyKeyring = function _KeyringController_destroyKeyring(keyring) {
|
|
1244
|
-
var _a;
|
|
1245
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1246
|
-
yield ((_a = keyring.destroy) === null || _a === void 0 ? void 0 : _a.call(keyring));
|
|
1247
|
-
});
|
|
1248
|
-
}, _KeyringController_removeEmptyKeyrings = function _KeyringController_removeEmptyKeyrings() {
|
|
1249
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1250
|
-
const validKeyrings = [];
|
|
1251
|
-
// Since getAccounts returns a Promise
|
|
1252
|
-
// We need to wait to hear back form each keyring
|
|
1253
|
-
// in order to decide which ones are now valid (accounts.length > 0)
|
|
1254
|
-
yield Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map((keyring) => __awaiter(this, void 0, void 0, function* () {
|
|
1255
|
-
const accounts = yield keyring.getAccounts();
|
|
1256
|
-
if (accounts.length > 0) {
|
|
1257
|
-
validKeyrings.push(keyring);
|
|
1258
|
-
}
|
|
1259
|
-
else {
|
|
1260
|
-
yield __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
|
|
1261
|
-
}
|
|
1262
|
-
})));
|
|
1263
|
-
__classPrivateFieldSet(this, _KeyringController_keyrings, validKeyrings, "f");
|
|
1264
|
-
});
|
|
1265
|
-
}, _KeyringController_checkForDuplicate = function _KeyringController_checkForDuplicate(type, newAccountArray) {
|
|
1266
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1267
|
-
const accounts = yield this.getAccounts();
|
|
1268
|
-
switch (type) {
|
|
1269
|
-
case KeyringTypes.simple: {
|
|
1270
|
-
const isIncluded = Boolean(accounts.find((key) => newAccountArray[0] &&
|
|
1271
|
-
(key === newAccountArray[0] ||
|
|
1272
|
-
key === (0, utils_1.remove0x)(newAccountArray[0]))));
|
|
1273
|
-
if (isIncluded) {
|
|
1274
|
-
throw new Error(constants_1.KeyringControllerError.DuplicatedAccount);
|
|
1275
|
-
}
|
|
1276
|
-
return newAccountArray;
|
|
1277
|
-
}
|
|
1278
|
-
default: {
|
|
1279
|
-
return newAccountArray;
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
});
|
|
1283
|
-
}, _KeyringController_setUnlocked = function _KeyringController_setUnlocked() {
|
|
1284
|
-
this.update((state) => {
|
|
1285
|
-
state.isUnlocked = true;
|
|
1286
|
-
});
|
|
1287
|
-
this.messagingSystem.publish(`${name}:unlock`);
|
|
1288
|
-
}, _KeyringController_getMemState = function _KeyringController_getMemState() {
|
|
1289
|
-
return {
|
|
1290
|
-
isUnlocked: this.state.isUnlocked,
|
|
1291
|
-
keyrings: this.state.keyrings,
|
|
1292
|
-
};
|
|
1293
|
-
};
|
|
1294
|
-
exports.default = KeyringController;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkBVSGYW4Djs = require('./chunk-BVSGYW4D.js');
|
|
11
|
+
require('./chunk-CHLPTPMZ.js');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
exports.AccountImportStrategy = _chunkBVSGYW4Djs.AccountImportStrategy; exports.KeyringController = _chunkBVSGYW4Djs.KeyringController; exports.KeyringTypes = _chunkBVSGYW4Djs.KeyringTypes; exports.SignTypedDataVersion = _chunkBVSGYW4Djs.SignTypedDataVersion; exports.default = _chunkBVSGYW4Djs.KeyringController_default; exports.getDefaultKeyringState = _chunkBVSGYW4Djs.getDefaultKeyringState; exports.isCustodyKeyring = _chunkBVSGYW4Djs.isCustodyKeyring; exports.keyringBuilderFactory = _chunkBVSGYW4Djs.keyringBuilderFactory;
|
|
1295
22
|
//# sourceMappingURL=KeyringController.js.map
|