Skip to content

Commit f671b62

Browse files
committed
Merge branch 'improved-keyring-metadata-error-handling' into multi-srp-mvp
2 parents cb58d7d + ae1a759 commit f671b62

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

packages/keyring-controller/src/KeyringController.ts

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,18 +2298,48 @@ export class KeyringController extends BaseController<
22982298
* using the given `opts`. The keyring is built using the keyring builder
22992299
* registered for the given `type`.
23002300
*
2301+
* The internal keyring and keyring metadata arrays are updated with the new
2302+
* keyring as well.
2303+
*
23012304
* @param type - The type of keyring to add.
2302-
* @param data - The data to restore a previously serialized keyring.
2305+
* @param data - Keyring initialization options.
23032306
* @returns The new keyring.
23042307
* @throws If the keyring includes duplicated accounts.
23052308
*/
23062309
async #newKeyring(type: string, data?: unknown): Promise<EthKeyring<Json>> {
2307-
this.#assertControllerMutexIsLocked();
2310+
const keyring = await this.#createKeyring(type, data);
23082311

2309-
const newKeyringMetadata: KeyringMetadata = {
2310-
id: ulid(),
2311-
name: '',
2312-
};
2312+
this.#keyrings.push(keyring);
2313+
this.#keyringsMetadata.push(getDefaultKeyringMetadata());
2314+
if (this.#keyrings.length !== this.#keyringsMetadata.length) {
2315+
throw new Error('Keyring metadata missing');
2316+
}
2317+
2318+
return keyring;
2319+
}
2320+
2321+
/**
2322+
* Instantiate, initialize and return a keyring of the given `type` using the
2323+
* given `opts`. The keyring is built using the keyring builder registered
2324+
* for the given `type`.
2325+
*
2326+
* The keyring might be new, or it might be restored from the vault. This
2327+
* function should only be called from `#newKeyring` or `#restoreKeyring`,
2328+
* for the "new" and "restore" cases respectively.
2329+
*
2330+
* The internal keyring and keyring metadata arrays are *not* updated, the
2331+
* caller is expected to update them.
2332+
*
2333+
* @param type - The type of keyring to add.
2334+
* @param data - Keyring initialization options.
2335+
* @returns The new keyring.
2336+
* @throws If the keyring includes duplicated accounts.
2337+
*/
2338+
async #createKeyring(
2339+
type: string,
2340+
data?: unknown,
2341+
): Promise<EthKeyring<Json>> {
2342+
this.#assertControllerMutexIsLocked();
23132343

23142344
const keyringBuilder = this.#getKeyringBuilderForType(type);
23152345

@@ -2349,11 +2379,6 @@ export class KeyringController extends BaseController<
23492379
this.#subscribeToQRKeyringEvents(keyring as unknown as QRKeyring);
23502380
}
23512381

2352-
this.#keyrings.push(keyring);
2353-
if (this.#keyringsMetadata.length < this.#keyrings.length) {
2354-
this.#keyringsMetadata.push(newKeyringMetadata);
2355-
}
2356-
23572382
return keyring;
23582383
}
23592384

@@ -2383,7 +2408,15 @@ export class KeyringController extends BaseController<
23832408

23842409
try {
23852410
const { type, data } = serialized;
2386-
return await this.#newKeyring(type, data);
2411+
const keyring = await this.#createKeyring(type, data);
2412+
this.#keyrings.push(keyring);
2413+
// If metadata is missing, assume the data is from an installation before
2414+
// we had keyring metadata.
2415+
if (this.#keyringsMetadata.length < this.#keyrings.length) {
2416+
console.log(`Adding missing metadata for '${type}' keyring`);
2417+
this.#keyringsMetadata.push(getDefaultKeyringMetadata());
2418+
}
2419+
return keyring;
23872420
} catch (_) {
23882421
this.#unsupportedKeyrings.push(serialized);
23892422
return undefined;
@@ -2617,4 +2650,13 @@ async function withLock<Result>(
26172650
}
26182651
}
26192652

2653+
/**
2654+
* Generate a new keyring metadata object.
2655+
*
2656+
* @returns Keyring metadata.
2657+
*/
2658+
function getDefaultKeyringMetadata(): KeyringMetadata {
2659+
return { id: ulid(), name: '' };
2660+
}
2661+
26202662
export default KeyringController;

0 commit comments

Comments
 (0)