@@ -2298,18 +2298,48 @@ export class KeyringController extends BaseController<
2298
2298
* using the given `opts`. The keyring is built using the keyring builder
2299
2299
* registered for the given `type`.
2300
2300
*
2301
+ * The internal keyring and keyring metadata arrays are updated with the new
2302
+ * keyring as well.
2303
+ *
2301
2304
* @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 .
2303
2306
* @returns The new keyring.
2304
2307
* @throws If the keyring includes duplicated accounts.
2305
2308
*/
2306
2309
async #newKeyring( type : string , data ?: unknown ) : Promise < EthKeyring < Json > > {
2307
- this . #assertControllerMutexIsLocked ( ) ;
2310
+ const keyring = await this . #createKeyring ( type , data ) ;
2308
2311
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( ) ;
2313
2343
2314
2344
const keyringBuilder = this . #getKeyringBuilderForType( type ) ;
2315
2345
@@ -2349,11 +2379,6 @@ export class KeyringController extends BaseController<
2349
2379
this . #subscribeToQRKeyringEvents( keyring as unknown as QRKeyring ) ;
2350
2380
}
2351
2381
2352
- this . #keyrings. push ( keyring ) ;
2353
- if ( this . #keyringsMetadata. length < this . #keyrings. length ) {
2354
- this . #keyringsMetadata. push ( newKeyringMetadata ) ;
2355
- }
2356
-
2357
2382
return keyring ;
2358
2383
}
2359
2384
@@ -2383,7 +2408,15 @@ export class KeyringController extends BaseController<
2383
2408
2384
2409
try {
2385
2410
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 ;
2387
2420
} catch ( _ ) {
2388
2421
this . #unsupportedKeyrings. push ( serialized ) ;
2389
2422
return undefined ;
@@ -2617,4 +2650,13 @@ async function withLock<Result>(
2617
2650
}
2618
2651
}
2619
2652
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
+
2620
2662
export default KeyringController ;
0 commit comments