Skip to content

Commit

Permalink
refactor: use option bag for constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed May 29, 2023
1 parent 3b1afec commit 281d6ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
19 changes: 11 additions & 8 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import {
KeyringObject,
KeyringControllerEvents,
KeyringControllerMessenger,
KeyringControllerConfig,
KeyringControllerState,
KeyringTypes,
KeyringControllerOptions,
} from './KeyringController';

jest.mock('uuid', () => {
Expand Down Expand Up @@ -248,11 +248,12 @@ describe('KeyringController', () => {
await withController(
{ cacheEncryptionKey },
async ({ controller, initialState, preferences, encryptor }) => {
const cleanKeyringController = new KeyringController(
preferences,
buildKeyringControllerMessenger(),
{ cacheEncryptionKey, encryptor },
);
const cleanKeyringController = new KeyringController({
...preferences,
messenger: buildKeyringControllerMessenger(),
cacheEncryptionKey,
encryptor,
});
const initialSeedWord = await controller.exportSeedPhrase(
password,
);
Expand Down Expand Up @@ -1636,7 +1637,7 @@ type WithControllerCallback<ReturnValue> = ({
messenger: KeyringControllerMessenger;
}) => Promise<ReturnValue> | ReturnValue;

type WithControllerOptions = Partial<KeyringControllerConfig>;
type WithControllerOptions = Partial<KeyringControllerOptions>;

type WithControllerArgs<ReturnValue> =
| [WithControllerCallback<ReturnValue>]
Expand Down Expand Up @@ -1693,9 +1694,11 @@ async function withController<ReturnValue>(
setSelectedAddress: sinon.stub(),
};
const messenger = buildKeyringControllerMessenger();
const controller = new KeyringController(preferences, messenger, {
const controller = new KeyringController({
encryptor,
cacheEncryptionKey: false,
messenger,
...preferences,
...rest,
});
const initialState = await controller.createNewVaultAndKeychain(password);
Expand Down
70 changes: 35 additions & 35 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ export type KeyringControllerMessenger = RestrictedControllerMessenger<
string
>;

/**
* @type KeyringControllerConfig
*
* Keyring controller configuration
* @property encryptor - Keyring encryptor
*/
export type KeyringControllerConfig = {
export type KeyringControllerOptions = {
removeIdentity: PreferencesController['removeIdentity'];
syncIdentities: PreferencesController['syncIdentities'];
updateIdentities: PreferencesController['updateIdentities'];
setSelectedAddress: PreferencesController['setSelectedAddress'];
setAccountLabel?: PreferencesController['setAccountLabel'];
encryptor?: any;
keyringBuilders?: any[];
cacheEncryptionKey?: boolean;
messenger: KeyringControllerMessenger;
state?: Partial<KeyringControllerState>;
};

/**
Expand Down Expand Up @@ -189,34 +190,30 @@ export class KeyringController extends BaseControllerV2<
/**
* Creates a KeyringController instance.
*
* @param options - The controller options.
* @param options.removeIdentity - Remove the identity with the given address.
* @param options.syncIdentities - Sync identities with the given list of addresses.
* @param options.updateIdentities - Generate an identity for each address given that doesn't already have an identity.
* @param options.setSelectedAddress - Set the selected address.
* @param options.setAccountLabel - Set a new name for account.
* @param messenger - A restricted controller messenger.
* @param config - Initial options used to configure this controller.
* @param state - Initial state to set on this controller.
* @param opts - Initial options used to configure this controller
* @param opts.removeIdentity - Remove the identity with the given address.
* @param opts.syncIdentities - Sync identities with the given list of addresses.
* @param opts.updateIdentities - Generate an identity for each address given that doesn't already have an identity.
* @param opts.setSelectedAddress - Set the selected address.
* @param opts.setAccountLabel - Set a new name for account.
* @param opts.encryptor - An optional object for defining encryption schemes.
* @param opts.keyringBuilders - Set a new name for account.
* @param opts.cacheEncryptionKey - Whether to cache or not encryption key.
* @param opts.messenger - A restricted controller messenger.
* @param opts.state - Initial state to set on this controller.
*/
constructor(
{
removeIdentity,
syncIdentities,
updateIdentities,
setSelectedAddress,
setAccountLabel,
}: {
removeIdentity: PreferencesController['removeIdentity'];
syncIdentities: PreferencesController['syncIdentities'];
updateIdentities: PreferencesController['updateIdentities'];
setSelectedAddress: PreferencesController['setSelectedAddress'];
setAccountLabel?: PreferencesController['setAccountLabel'];
},
messenger: KeyringControllerMessenger,
config?: KeyringControllerConfig,
state?: Partial<KeyringControllerState>,
) {
constructor({
removeIdentity,
syncIdentities,
updateIdentities,
setSelectedAddress,
setAccountLabel,
encryptor,
keyringBuilders,
cacheEncryptionKey,
messenger,
state,
}: KeyringControllerOptions) {
super({
name,
metadata: {
Expand All @@ -233,7 +230,10 @@ export class KeyringController extends BaseControllerV2<
});

this.#keyring = new EthKeyringController(
Object.assign({ initState: state }, config),
Object.assign(
{ initState: state },
{ encryptor, keyringBuilders, cacheEncryptionKey },
),
);
this.#keyring.memStore.subscribe(this.#fullUpdate.bind(this));
this.#keyring.store.subscribe(this.#fullUpdate.bind(this));
Expand Down

0 comments on commit 281d6ff

Please sign in to comment.