Skip to content

fix: fix granted permissions when adding an existing network via wallet_addEthereumChain #29837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b7bf2ac
remove approvalFlowId. replace isAddFlow with autoApprove
jiexi Jan 21, 2025
65a837b
WIP wallet_addEthereumChain specs
jiexi Jan 21, 2025
15248fb
replace withPermissionControllerConnectedToTestDappWithChain with wit…
jiexi Jan 21, 2025
0d44554
add wallet_addEthereumChain spec
jiexi Jan 21, 2025
493e644
lint
jiexi Jan 21, 2025
9097ee2
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 21, 2025
d21a6bc
remove withPermissionControllerConnectedToTestDappWithTwoChains
jiexi Jan 22, 2025
fcc79fc
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 22, 2025
11f009f
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 22, 2025
2e5ea92
Update test/e2e/json-rpc/wallet_addEthereumChain.spec.ts
jiexi Jan 22, 2025
0d3c7f9
Update test/e2e/json-rpc/wallet_addEthereumChain.spec.ts
jiexi Jan 22, 2025
9d67497
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 22, 2025
1cffddb
Merge remote-tracking branch 'origin/jl/mmp-3814/fix-grant-permission…
jiexi Jan 22, 2025
d37a303
Merge remote-tracking branch 'origin/jl/mmp-3814/fix-grant-permission…
jiexi Jan 22, 2025
d9b397b
assert the approval is for a switch
jiexi Jan 22, 2025
6b5c973
jsdoc
jiexi Jan 22, 2025
06f8e6a
always call switchChain to ensure correct permittedChain permissions …
jiexi Jan 22, 2025
e5f13fb
update hooks.autoApprove jsdoc
jiexi Jan 22, 2025
81ea328
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 22, 2025
7921445
lint
jiexi Jan 23, 2025
3fb039a
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 23, 2025
b1b2f8e
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
adonesky1 Jan 23, 2025
a686e2b
add test case (#29885)
adonesky1 Jan 23, 2025
8b5cedd
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 28, 2025
21ee113
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
adonesky1 Jan 28, 2025
b5905f7
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
jiexi Jan 29, 2025
d924efc
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
adonesky1 Jan 29, 2025
4579d6a
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
adonesky1 Jan 29, 2025
060a6cd
Merge branch 'main' into jl/mmp-3814/fix-grant-permissions-add-ethere…
adonesky1 Jan 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const addEthereumChain = {
getNetworkConfigurationByChainId: true,
setActiveNetwork: true,
requestUserApproval: true,
startApprovalFlow: true,
endApprovalFlow: true,
getCurrentChainIdForDomain: true,
getCaveat: true,
requestPermittedChainsPermissionForOrigin: true,
Expand All @@ -40,8 +38,6 @@ async function addEthereumChainHandler(
getNetworkConfigurationByChainId,
setActiveNetwork,
requestUserApproval,
startApprovalFlow,
endApprovalFlow,
getCurrentChainIdForDomain,
getCaveat,
requestPermittedChainsPermissionForOrigin,
Expand Down Expand Up @@ -79,7 +75,6 @@ async function addEthereumChainHandler(
);
}

let approvalFlowId;
let updatedNetwork = existingNetwork;

let rpcIndex = existingNetwork?.rpcEndpoints.findIndex(({ url }) =>
Expand All @@ -93,14 +88,14 @@ async function addEthereumChainHandler(
: undefined;

// If there's something to add or update
if (

const shouldAddOrUpdateNetwork =
!existingNetwork ||
rpcIndex !== existingNetwork.defaultRpcEndpointIndex ||
(firstValidBlockExplorerUrl &&
blockExplorerIndex !== existingNetwork.defaultBlockExplorerUrlIndex)
) {
({ id: approvalFlowId } = await startApprovalFlow());
blockExplorerIndex !== existingNetwork.defaultBlockExplorerUrlIndex);

if (shouldAddOrUpdateNetwork) {
try {
await requestUserApproval({
origin,
Expand Down Expand Up @@ -183,7 +178,6 @@ async function addEthereumChainHandler(
});
}
} catch (error) {
endApprovalFlow({ id: approvalFlowId });
return end(error);
}
}
Expand All @@ -193,16 +187,13 @@ async function addEthereumChainHandler(
const { networkClientId } =
updatedNetwork.rpcEndpoints[updatedNetwork.defaultRpcEndpointIndex];

return switchChain(res, end, chainId, networkClientId, approvalFlowId, {
isAddFlow: true,
return switchChain(res, end, chainId, networkClientId, {
autoApprove: shouldAddOrUpdateNetwork,
setActiveNetwork,
getCaveat,
endApprovalFlow,
requestPermittedChainsPermissionForOrigin,
requestPermittedChainsPermissionIncrementalForOrigin,
});
} else if (approvalFlowId) {
endApprovalFlow({ id: approvalFlowId });
}

res.result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ const createMockedHandler = () => {
setActiveNetwork: jest.fn(),
requestUserApproval: jest.fn().mockResolvedValue(123),
getCaveat: jest.fn(),
startApprovalFlow: () => ({ id: 'approvalFlowId' }),
endApprovalFlow: jest.fn(),
addNetwork: jest.fn().mockResolvedValue({
defaultRpcEndpointIndex: 0,
rpcEndpoints: [{ networkClientId: 123 }],
Expand Down Expand Up @@ -185,10 +183,8 @@ describe('addEthereumChainHandler', () => {
end,
NON_INFURA_CHAIN_ID,
123,
'approvalFlowId',
{
isAddFlow: true,
endApprovalFlow: mocks.endApprovalFlow,
autoApprove: true,
getCaveat: mocks.getCaveat,
setActiveNetwork: mocks.setActiveNetwork,
requestPermittedChainsPermissionForOrigin:
Expand Down Expand Up @@ -254,10 +250,8 @@ describe('addEthereumChainHandler', () => {
end,
'0x1',
123,
'approvalFlowId',
{
isAddFlow: true,
endApprovalFlow: mocks.endApprovalFlow,
autoApprove: true,
getCaveat: mocks.getCaveat,
setActiveNetwork: mocks.setActiveNetwork,
requestPermittedChainsPermissionForOrigin:
Expand All @@ -270,7 +264,7 @@ describe('addEthereumChainHandler', () => {
});

describe('if the proposed networkConfiguration does not have a different rpcUrl from the one already in state', () => {
it('should only switch to the existing networkConfiguration if one already exists for the given chain id', async () => {
it('should only switch to the existing networkConfiguration if one already exists for the given chain id without auto approving the chain permission', async () => {
const { mocks, end, handler } = createMockedHandler();
mocks.getCurrentChainIdForDomain.mockReturnValue(CHAIN_IDS.MAINNET);
mocks.getNetworkConfigurationByChainId.mockReturnValue(
Expand Down Expand Up @@ -303,10 +297,8 @@ describe('addEthereumChainHandler', () => {
end,
'0xa',
createMockOptimismConfiguration().rpcEndpoints[0].networkClientId,
undefined,
{
isAddFlow: true,
endApprovalFlow: mocks.endApprovalFlow,
autoApprove: false,
getCaveat: mocks.getCaveat,
setActiveNetwork: mocks.setActiveNetwork,
requestPermittedChainsPermissionForOrigin:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { errorCodes, rpcErrors } from '@metamask/rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';
import {
Caip25CaveatType,
Caip25EndowmentPermissionName,
Expand Down Expand Up @@ -164,26 +164,22 @@ export function validateAddEthereumChainParams(params) {
* @param end - The JSON RPC request's end callback.
* @param {string} chainId - The chainId being switched to.
* @param {string} networkClientId - The network client being switched to.
* @param {string} [approvalFlowId] - The optional approval flow ID to handle.
* @param {object} hooks - The hooks object.
* @param {boolean} hooks.isAddFlow - The boolean determining if this call originates from wallet_addEthereumChain.
* @param {Function} hooks.setActiveNetwork - The callback to change the current network for the origin.
* @param {Function} hooks.endApprovalFlow - The optional callback to end the approval flow when approvalFlowId is provided.
* @param {Function} hooks.getCaveat - The callback to get the CAIP-25 caveat for the origin.
* @param {Function} hooks.requestPermittedChainsPermissionForOrigin - The callback to request a new permittedChains-equivalent CAIP-25 permission.
* @param {Function} hooks.requestPermittedChainsPermissionIncrementalForOrigin - The callback to add a new chain to the permittedChains-equivalent CAIP-25 permission.
* @returns a null response on success or an error if user rejects an approval when isAddFlow is false or on unexpected errors.
* @param hooks.autoApprove
* @returns a null response on success or an error if user rejects an approval when autoApprove is false or on unexpected errors.
*/
export async function switchChain(
response,
end,
chainId,
networkClientId,
approvalFlowId,
{
isAddFlow,
autoApprove,
setActiveNetwork,
endApprovalFlow,
getCaveat,
requestPermittedChainsPermissionForOrigin,
requestPermittedChainsPermissionIncrementalForOrigin,
Expand All @@ -201,36 +197,20 @@ export async function switchChain(
if (!ethChainIds.includes(chainId)) {
await requestPermittedChainsPermissionIncrementalForOrigin({
chainId,
autoApprove: isAddFlow,
autoApprove,
});
}
} else {
await requestPermittedChainsPermissionForOrigin({
chainId,
autoApprove: isAddFlow,
autoApprove,
});
}

await setActiveNetwork(networkClientId);
response.result = null;
} catch (error) {
// We don't want to return an error if user rejects the request
// and this is a chained switch request after wallet_addEthereumChain.
// approvalFlowId is only defined when this call is of a
// wallet_addEthereumChain request so we can use it to determine
// if we should return an error
if (
error.code === errorCodes.provider.userRejectedRequest &&
approvalFlowId
) {
response.result = null;
return end();
}
return end(error);
} finally {
if (approvalFlowId) {
endApprovalFlow({ id: approvalFlowId });
}
}
return end();
}
Loading
Loading