Skip to content

Commit

Permalink
refactor: remove remaining eth-keyring-controller use
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Sep 12, 2023
1 parent 422cc4e commit 0a51549
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 48 deletions.
4 changes: 2 additions & 2 deletions app/scripts/metamask-controller.actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ describe('MetaMaskController', function () {
]),
Promise.resolve(1).then(() => {
keyringControllerState1 = JSON.stringify(
metamaskController.keyringController.memStore.getState(),
metamaskController.coreKeyringController.state,
);
metamaskController.importAccountWithStrategy('privateKey', [
importPrivkey,
]);
}),
Promise.resolve(2).then(() => {
keyringControllerState2 = JSON.stringify(
metamaskController.keyringController.memStore.getState(),
metamaskController.coreKeyringController.state,
);
}),
]);
Expand Down
56 changes: 20 additions & 36 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ export default class MetamaskController extends EventEmitter {
permissionSpecifications: {
...getPermissionSpecifications({
getIdentities,
getAllAccounts: this.keyringController.getAccounts.bind(
this.keyringController,
getAllAccounts: this.coreKeyringController.getAccounts.bind(
this.coreKeyringController,
),
captureKeyringTypesWithMissingIdentities: (
identities = {},
Expand Down Expand Up @@ -1197,8 +1197,8 @@ export default class MetamaskController extends EventEmitter {
this.networkController.state.providerConfig.chainId,
preferencesStore: this.preferencesController.store,
txHistoryLimit: 60,
signTransaction: this.keyringController.signTransaction.bind(
this.keyringController,
signTransaction: this.coreKeyringController.signTransaction.bind(
this.coreKeyringController,
),
provider: this.provider,
blockTracker: this.blockTracker,
Expand Down Expand Up @@ -1695,7 +1695,7 @@ export default class MetamaskController extends EventEmitter {
AppMetadataController: this.appMetadataController.store,
NetworkController: this.networkController,
CachedBalancesController: this.cachedBalancesController.store,
KeyringController: this.keyringController.memStore,
KeyringController: this.coreKeyringController,
PreferencesController: this.preferencesController.store,
MetaMetricsController: this.metaMetricsController.store,
AddressBookController: this.addressBookController,
Expand Down Expand Up @@ -1841,7 +1841,7 @@ export default class MetamaskController extends EventEmitter {
KeyringType.snap,
);
if (!snapKeyring) {
snapKeyring = await this.keyringController.addNewKeyring(
snapKeyring = await this.coreKeyringController.addNewKeyring(
KeyringType.snap,
);
}
Expand Down Expand Up @@ -1944,7 +1944,7 @@ export default class MetamaskController extends EventEmitter {
///: BEGIN:ONLY_INCLUDE_IN(keyring-snaps)
getSnapKeyring: this.getSnapKeyring.bind(this),
saveSnapKeyring: async () => {
await this.keyringController.persistAllKeyrings();
await this.coreKeyringController.persistAllKeyrings();
},
///: END:ONLY_INCLUDE_IN
///: BEGIN:ONLY_INCLUDE_IN(snaps)
Expand Down Expand Up @@ -2193,7 +2193,7 @@ export default class MetamaskController extends EventEmitter {
* @returns {object} status
*/
getState() {
const { vault } = this.keyringController.store.getState();
const { vault } = this.coreKeyringController.state;
const isInitialized = Boolean(vault);

const flatState = this.memStore.getFlatState();
Expand Down Expand Up @@ -2822,7 +2822,7 @@ export default class MetamaskController extends EventEmitter {

async exportAccount(address, password) {
await this.verifyPassword(password);
return this.keyringController.exportAccount(address, password);
return this.coreKeyringController.exportAccount(password, address);
}

async getTokenStandardAndDetails(address, userAddress, tokenId) {
Expand Down Expand Up @@ -2917,25 +2917,7 @@ export default class MetamaskController extends EventEmitter {
* @returns {object} vault
*/
async createNewVaultAndKeychain(password) {
const releaseLock = await this.createVaultMutex.acquire();
try {
let vault;
const accounts = await this.keyringController.getAccounts();
if (accounts.length > 0) {
vault = await this.keyringController.fullUpdate();
} else {
vault = await this.keyringController.createNewVaultAndKeychain(
password,
);
const addresses = await this.keyringController.getAccounts();
this.preferencesController.setAddresses(addresses);
this.selectFirstIdentity();
}

return vault;
} finally {
releaseLock();
}
return this.coreKeyringController.createNewVaultAndKeychain(password);
}

/**
Expand Down Expand Up @@ -3171,7 +3153,7 @@ export default class MetamaskController extends EventEmitter {
* @param {string} password - The user's password
*/
async verifyPassword(password) {
await this.keyringController.verifyPassword(password);
await this.coreKeyringController.verifyPassword(password);
}

/**
Expand Down Expand Up @@ -3251,7 +3233,7 @@ export default class MetamaskController extends EventEmitter {
keyringName,
);
if (!keyring) {
keyring = await this.keyringController.addNewKeyring(keyringName);
keyring = await this.coreKeyringController.addNewKeyring(keyringName);
}
if (hdPath && keyring.setHdPath) {
keyring.setHdPath(hdPath);
Expand Down Expand Up @@ -3304,7 +3286,7 @@ export default class MetamaskController extends EventEmitter {

// Merge with existing accounts
// and make sure addresses are not repeated
const oldAccounts = await this.keyringController.getAccounts();
const oldAccounts = await this.coreKeyringController.getAccounts();
const accountsToTrack = [
...new Set(
oldAccounts.concat(accounts.map((a) => a.address.toLowerCase())),
Expand Down Expand Up @@ -3420,9 +3402,11 @@ export default class MetamaskController extends EventEmitter {
const keyring = await this.getKeyringForDevice(deviceName, hdPath);

keyring.setAccountToUnlock(index);
const oldAccounts = await this.keyringController.getAccounts();
const keyState = await this.keyringController.addNewAccount(keyring);
const newAccounts = await this.keyringController.getAccounts();
const oldAccounts = await this.coreKeyringController.getAccounts();
const keyState = await this.coreKeyringController.addNewAccountForKeyring(
keyring,
);
const newAccounts = await this.coreKeyringController.getAccounts();
this.preferencesController.setAddresses(newAccounts);
newAccounts.forEach((address) => {
if (!oldAccounts.includes(address)) {
Expand Down Expand Up @@ -3571,7 +3555,7 @@ export default class MetamaskController extends EventEmitter {
address,
);
// Remove account from the keyring
await this.keyringController.removeAccount(address);
await this.coreKeyringController.removeAccount(address);
const updatedKeyringAccounts = keyring ? await keyring.getAccounts() : {};
if (updatedKeyringAccounts?.length === 0) {
keyring.destroy?.();
Expand Down Expand Up @@ -4516,7 +4500,7 @@ export default class MetamaskController extends EventEmitter {
* @returns {boolean} Whether the extension is unlocked.
*/
isUnlocked() {
return this.keyringController.memStore.getState().isUnlocked;
return this.coreKeyringController.state.isUnlocked;
}

//=============================================================================
Expand Down
20 changes: 10 additions & 10 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ describe('MetaMaskController', () => {
});

jest.spyOn(
metamaskController.keyringController,
metamaskController.coreKeyringController,
'createNewVaultAndKeychain',
);
jest.spyOn(
Expand Down Expand Up @@ -363,7 +363,7 @@ describe('MetaMaskController', () => {

it('adds 1 account', async () => {
const keyringAccounts =
await metamaskController.keyringController.getAccounts();
await metamaskController.coreKeyringController.getAccounts();
expect(keyringAccounts[keyringAccounts.length - 1]).toStrictEqual(
'0xe18035bf8712672935fdb4e5e431b1a0183d2dfc',
);
Expand Down Expand Up @@ -420,7 +420,7 @@ describe('MetaMaskController', () => {
await metamaskController.createNewVaultAndKeychain(password);

expect(
metamaskController.keyringController.createNewVaultAndKeychain,
metamaskController.coreKeyringController.createNewVaultAndKeychain,
).toHaveBeenCalledTimes(1);
});
});
Expand Down Expand Up @@ -622,7 +622,7 @@ describe('MetaMaskController', () => {
});

it('should add the Trezor Hardware keyring', async () => {
jest.spyOn(metamaskController.keyringController, 'addNewKeyring');
jest.spyOn(metamaskController.coreKeyringController, 'addNewKeyring');
await metamaskController
.connectHardware(HardwareDeviceNames.trezor, 0)
.catch(() => null);
Expand All @@ -631,13 +631,13 @@ describe('MetaMaskController', () => {
KeyringType.trezor,
);
expect(
metamaskController.keyringController.addNewKeyring,
metamaskController.coreKeyringController.addNewKeyring,
).toHaveBeenCalledWith(KeyringType.trezor);
expect(keyrings).toHaveLength(1);
});

it('should add the Ledger Hardware keyring', async () => {
jest.spyOn(metamaskController.keyringController, 'addNewKeyring');
jest.spyOn(metamaskController.coreKeyringController, 'addNewKeyring');
await metamaskController
.connectHardware(HardwareDeviceNames.ledger, 0)
.catch(() => null);
Expand All @@ -646,7 +646,7 @@ describe('MetaMaskController', () => {
KeyringType.ledger,
);
expect(
metamaskController.keyringController.addNewKeyring,
metamaskController.coreKeyringController.addNewKeyring,
).toHaveBeenCalledWith(KeyringType.ledger);
expect(keyrings).toHaveLength(1);
});
Expand Down Expand Up @@ -731,11 +731,11 @@ describe('MetaMaskController', () => {
beforeEach(async () => {
jest.spyOn(window, 'open').mockReturnValue();
jest
.spyOn(metamaskController.keyringController, 'addNewAccount')
.spyOn(metamaskController.coreKeyringController, 'addNewAccount')
.mockReturnValue('0x123');

jest
.spyOn(metamaskController.keyringController, 'getAccounts')
.spyOn(metamaskController.coreKeyringController, 'getAccounts')
.mockResolvedValueOnce(['0x1'])
.mockResolvedValueOnce(['0x2'])
.mockResolvedValueOnce(['0x3']);
Expand Down Expand Up @@ -769,7 +769,7 @@ describe('MetaMaskController', () => {

it('should call keyringController.addNewAccount', async () => {
expect(
metamaskController.keyringController.addNewAccount,
metamaskController.coreKeyringController.addNewAccountForKeyring,
).toHaveBeenCalledTimes(1);
});

Expand Down

0 comments on commit 0a51549

Please sign in to comment.