You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.3 KiB
40 lines
1.3 KiB
3 years ago
|
import { CaveatMutatorOperation } from '@metamask/snap-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 };
|
||
|
}
|