Skip to content

Commit

Permalink
updated selector
Browse files Browse the repository at this point in the history
  • Loading branch information
NidhiKJha committed Aug 23, 2024
1 parent 697f0c5 commit da2478a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/scripts/controllers/permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ export const getPermissionSpecifications = ({
subjectTypes: [SubjectType.Website],

factory: (permissionOptions, requestData) => {
if (requestData === undefined) {
return constructPermission({
...permissionOptions,
});
}
if (!requestData.approvedChainIds) {
throw new Error(
`${PermissionNames.permittedChains}: No approved networks specified.`,
Expand Down
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3144,6 +3144,7 @@ export default class MetamaskController extends EventEmitter {
getUseRequestQueue: this.preferencesController.getUseRequestQueue.bind(
this.preferencesController,
),
grantPermissionsIncremental: this.permissionController.grantPermissionsIncremental.bind(this.permissionController),
getProviderConfig: () => this.networkController.state.providerConfig,
setSecurityAlertsEnabled:
preferencesController.setSecurityAlertsEnabled.bind(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
updateNetworksList,
setNetworkClientIdForDomain,
setEditedNetwork,
grantPermittedChain,
} from '../../../store/actions';
import {
FEATURED_RPCS,
Expand All @@ -34,6 +35,7 @@ import {
getNetworkConfigurations,
getEditedNetwork,
getAllDomains,
getPermittedChainsByOrigin,
} from '../../../selectors';
import ToggleButton from '../../ui/toggle-button';
import {
Expand Down Expand Up @@ -101,7 +103,8 @@ export const NetworkListMenu = ({ onClose }) => {
const useRequestQueue = useSelector(getUseRequestQueue);
const networkConfigurations = useSelector(getNetworkConfigurations);
const domains = useSelector(getAllDomains);

const chains = useSelector(getPermittedChainsByOrigin);
console.log(chains, "hh")
const dispatch = useDispatch();
const history = useHistory();
const trackEvent = useContext(MetaMetricsContext);
Expand Down Expand Up @@ -171,6 +174,7 @@ export const NetworkListMenu = ({ onClose }) => {
return sortedNonTestNetworks;
};

//check if not granted chain is clicked, then show the toast and grant permission
const networksList = newOrderNetworks();
const [items, setItems] = useState([...networksList]);

Expand Down Expand Up @@ -303,6 +307,7 @@ export const NetworkListMenu = ({ onClose }) => {
onClick={() => {
dispatch(toggleNetworkMenu());
dispatch(setActiveNetwork(network.providerType || network.id));
grantPermittedChain(selectedTabOrigin, network.chainId);

// If presently on and connected to a dapp, communicate a change to
// the dapp via silent switchEthereumChain that the network has
Expand Down
37 changes: 37 additions & 0 deletions ui/selectors/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function getPermittedAccountsForSelectedTab(state, activeTab) {
*/
export function getPermittedAccountsByOrigin(state) {
const subjects = getPermissionSubjects(state);
console.log(subjects, "nidhi")
return Object.keys(subjects).reduce((acc, subjectKey) => {
const accounts = getAccountsFromSubject(subjects[subjectKey]);
if (accounts.length > 0) {
Expand All @@ -92,6 +93,18 @@ export function getPermittedAccountsByOrigin(state) {
}, {});
}

export function getPermittedChainsByOrigin(state) {
const subjects = getPermissionSubjects(state);
console.log(subjects, 'nidhi');
return Object.keys(subjects).reduce((acc, subjectKey) => {
const chains = getChainsFromSubject(subjects[subjectKey]);
if (chains.length > 0) {
acc[subjectKey] = chains;
}
return acc;
}, {});
}

export function getSubjectMetadata(state) {
return state.metamask.subjectMetadata;
}
Expand Down Expand Up @@ -256,13 +269,37 @@ function getAccountsPermissionFromSubject(subject = {}) {
return subject.permissions?.eth_accounts || {};
}

function getChainsFromSubject(subject) {
return getChainsFromPermission(getChainsPermissionFromSubject(subject));
}

function getChainsPermissionFromSubject(subject = {}) {
return subject.permissions?.['endowment:permitted-chains'] || {};
}

function getAccountsFromPermission(accountsPermission) {
const accountsCaveat = getAccountsCaveatFromPermission(accountsPermission);
return accountsCaveat && Array.isArray(accountsCaveat.value)
? accountsCaveat.value
: [];
}

function getChainsFromPermission(chainsPermission) {
const chainsCaveat = getChainsCaveatFromPermission(chainsPermission);
return chainsCaveat && Array.isArray(chainsCaveat.value)
? chainsCaveat.value
: [];
}

function getChainsCaveatFromPermission(chainsPermission = {}) {
return (
Array.isArray(chainsPermission.caveats) &&
chainsPermission.caveats.find(
(caveat) => caveat.type === CaveatTypes.restrictNetworkSwitching,
)
);
}

function getAccountsCaveatFromPermission(accountsPermission = {}) {
return (
Array.isArray(accountsPermission.caveats) &&
Expand Down
22 changes: 22 additions & 0 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ import {
MetaMaskReduxState,
TemporaryMessageDataType,
} from './store';
import { CaveatFactories, PermissionNames } from '../../app/scripts/controllers/permissions/specifications';
import { CaveatTypes } from '../../shared/constants/permissions';

type CustomGasSettings = {
gas?: string;
Expand Down Expand Up @@ -5580,6 +5582,26 @@ export async function getNextAvailableAccountName(
);
}

export async function grantPermittedChain(
selectedTabOrigin: string,
chainId?: [],
): Promise<string> {
return await submitRequestToBackground<void>('grantPermissionsIncremental', [
{
subject: { origin: selectedTabOrigin },
approvedPermissions: {
[PermissionNames.permittedChains]: {
caveats: [
CaveatFactories[CaveatTypes.restrictNetworkSwitching](
[chainId],
),
],
},
},
}
]);
}

export async function decodeTransactionData({
transactionData,
contractAddress,
Expand Down

0 comments on commit da2478a

Please sign in to comment.