mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-12 04:37:13 +01:00
40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
import { CaveatMutatorOperation } from '@metamask/controllers';
|
|
import { CaveatTypes } from '../../../../shared/constants/permissions';
|
|
|
|
/**
|
|
* Factories that construct caveat mutator functions that are passed to
|
|
* PermissionController.updatePermissionsByCaveat.
|
|
*/
|
|
export const CaveatMutatorFactories = {
|
|
[CaveatTypes.restrictReturnedAccounts]: {
|
|
removeAccount,
|
|
},
|
|
};
|
|
|
|
/**
|
|
* Removes the target account from the value arrays of all
|
|
* `restrictReturnedAccounts` caveats. No-ops if the target account is not in
|
|
* the array, and revokes the parent permission if it's the only account in
|
|
* the array.
|
|
*
|
|
* @param {string} targetAccount - The address of the account to remove from
|
|
* all accounts permissions.
|
|
* @param {string[]} existingAccounts - The account address array from the
|
|
* account permissions.
|
|
*/
|
|
function removeAccount(targetAccount, existingAccounts) {
|
|
const newAccounts = existingAccounts.filter(
|
|
(address) => address !== targetAccount,
|
|
);
|
|
|
|
if (newAccounts.length === existingAccounts.length) {
|
|
return { operation: CaveatMutatorOperation.noop };
|
|
} else if (newAccounts.length > 0) {
|
|
return {
|
|
operation: CaveatMutatorOperation.updateValue,
|
|
value: newAccounts,
|
|
};
|
|
}
|
|
return { operation: CaveatMutatorOperation.revokePermission };
|
|
}
|