1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

captureKeyringTypesWithMissingIdentities() when 'Missing identity for address' in permissions/specifications (#13521)

* captureKeyringTypesWithMissingIdentities() when 'Missing identity for address' in permissions/specifications

* Fix unit tests
This commit is contained in:
Dan J Miller 2022-02-07 15:30:37 -03:30 committed by Dan Miller
parent 007bc6fcba
commit 5bc4287f65
3 changed files with 32 additions and 0 deletions

View File

@ -78,11 +78,15 @@ export const getCaveatSpecifications = ({ getIdentities }) => {
* in the current MetaMask instance. * in the current MetaMask instance.
* @param options.getIdentities - A function that returns the * @param options.getIdentities - A function that returns the
* `PreferencesController` identity objects for all Ethereum accounts in the * `PreferencesController` identity objects for all Ethereum accounts in the
* @param options.captureKeyringTypesWithMissingIdentities - A function that
* captures extra error information about the "Missing identity for address"
* error.
* current MetaMask instance. * current MetaMask instance.
*/ */
export const getPermissionSpecifications = ({ export const getPermissionSpecifications = ({
getAllAccounts, getAllAccounts,
getIdentities, getIdentities,
captureKeyringTypesWithMissingIdentities,
}) => { }) => {
return { return {
[PermissionKeys.eth_accounts]: { [PermissionKeys.eth_accounts]: {
@ -119,8 +123,10 @@ export const getPermissionSpecifications = ({
return accounts.sort((firstAddress, secondAddress) => { return accounts.sort((firstAddress, secondAddress) => {
if (!identities[firstAddress]) { if (!identities[firstAddress]) {
captureKeyringTypesWithMissingIdentities(identities, accounts);
throw new Error(`Missing identity for address: "${firstAddress}".`); throw new Error(`Missing identity for address: "${firstAddress}".`);
} else if (!identities[secondAddress]) { } else if (!identities[secondAddress]) {
captureKeyringTypesWithMissingIdentities(identities, accounts);
throw new Error( throw new Error(
`Missing identity for address: "${secondAddress}".`, `Missing identity for address: "${secondAddress}".`,
); );

View File

@ -247,6 +247,7 @@ describe('PermissionController specifications', () => {
const { methodImplementation } = getPermissionSpecifications({ const { methodImplementation } = getPermissionSpecifications({
getIdentities, getIdentities,
getAllAccounts, getAllAccounts,
captureKeyringTypesWithMissingIdentities: jest.fn(),
})[RestrictedMethods.eth_accounts]; })[RestrictedMethods.eth_accounts];
await expect(() => methodImplementation()).rejects.toThrow( await expect(() => methodImplementation()).rejects.toThrow(
@ -272,6 +273,7 @@ describe('PermissionController specifications', () => {
const { methodImplementation } = getPermissionSpecifications({ const { methodImplementation } = getPermissionSpecifications({
getIdentities, getIdentities,
getAllAccounts, getAllAccounts,
captureKeyringTypesWithMissingIdentities: jest.fn(),
})[RestrictedMethods.eth_accounts]; })[RestrictedMethods.eth_accounts];
await expect(() => methodImplementation()).rejects.toThrow( await expect(() => methodImplementation()).rejects.toThrow(

View File

@ -503,6 +503,30 @@ export default class MetamaskController extends EventEmitter {
getAllAccounts: this.keyringController.getAccounts.bind( getAllAccounts: this.keyringController.getAccounts.bind(
this.keyringController, this.keyringController,
), ),
captureKeyringTypesWithMissingIdentities: (
identities = {},
accounts = [],
) => {
const accountsMissingIdentities = accounts.filter(
(address) => !identities[address],
);
const keyringTypesWithMissingIdentities = accountsMissingIdentities.map(
(address) =>
this.keyringController.getKeyringForAccount(address)?.type,
);
const identitiesCount = Object.keys(identities || {}).length;
const accountTrackerCount = Object.keys(
this.accountTracker.store.getState().accounts || {},
).length;
captureException(
new Error(
`Attempt to get permission specifications failed because their were ${accounts.length} accounts, but ${identitiesCount} identities, and the ${keyringTypesWithMissingIdentities} keyrings included accounts with missing identities. Meanwhile, there are ${accountTrackerCount} accounts in the account tracker.`,
),
);
},
}), }),
unrestrictedMethods, unrestrictedMethods,
}); });