Skip to content

Commit

Permalink
Merge branch 'develop' into hm/migrate-snap-permissions-to-caveats
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Mar 8, 2023
2 parents b15b9ba + 6222cf0 commit b1ff6c6
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 59 deletions.
9 changes: 9 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
isPrefixedFormattedHexString,
isSafeChainId,
} from '../../../../../shared/modules/network.utils';
import { jsonRpcRequest } from '../../../../../shared/modules/rpc.utils';

const addEthereumChain = {
methodNames: [MESSAGE_TYPE.ADD_ETHEREUM_CHAIN],
Expand Down Expand Up @@ -155,6 +154,7 @@ async function addEthereumChainHandler(
if (currentChainId === _chainId && currentRpcUrl === firstValidRPCUrl) {
return end();
}

// If this network is already added with but is not the currently selected network
// Ask the user to switch the network
try {
Expand Down Expand Up @@ -182,28 +182,6 @@ async function addEthereumChainHandler(
return end();
}

let endpointChainId;

try {
endpointChainId = await jsonRpcRequest(firstValidRPCUrl, 'eth_chainId');
} catch (err) {
return end(
ethErrors.rpc.internal({
message: `Request for method 'eth_chainId on ${firstValidRPCUrl} failed`,
data: { networkErr: err },
}),
);
}

if (_chainId !== endpointChainId) {
return end(
ethErrors.rpc.invalidParams({
message: `Chain ID returned by RPC URL ${firstValidRPCUrl} does not match ${_chainId}`,
data: { chainId: endpointChainId },
}),
);
}

if (typeof chainName !== 'string' || !chainName) {
return end(
ethErrors.rpc.invalidParams({
Expand Down Expand Up @@ -266,20 +244,18 @@ async function addEthereumChainHandler(
}

try {
await addCustomRpc(
await requestUserApproval({
origin,
type: MESSAGE_TYPE.ADD_ETHEREUM_CHAIN,
requestData: {
chainId: _chainId,
blockExplorerUrl: firstValidBlockExplorerUrl,
chainName: _chainName,
rpcUrl: firstValidRPCUrl,
ticker,
},
}),
);

const customRpc = await requestUserApproval({
origin,
type: MESSAGE_TYPE.ADD_ETHEREUM_CHAIN,
requestData: {
chainId: _chainId,
blockExplorerUrl: firstValidBlockExplorerUrl,
chainName: _chainName,
rpcUrl: firstValidRPCUrl,
ticker,
},
});
await addCustomRpc(customRpc);
sendMetrics({
event: 'Custom Network Added',
category: EVENT.CATEGORIES.NETWORK,
Expand Down
8 changes: 4 additions & 4 deletions coverage-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// subset of files to check against these targets.
module.exports = {
global: {
lines: 64,
branches: 52.5,
statements: 63.1,
functions: 56.1,
lines: 64.5,
branches: 53,
statements: 63,
functions: 56.5,
},
transforms: {
branches: 100,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
"@metamask/desktop": "^0.3.0",
"@metamask/eth-json-rpc-infura": "^7.0.0",
"@metamask/eth-json-rpc-middleware": "^10.0.0",
"@metamask/eth-keyring-controller": "^10.0.0",
"@metamask/eth-keyring-controller": "^10.0.1",
"@metamask/eth-ledger-bridge-keyring": "^0.13.0",
"@metamask/eth-token-tracker": "^4.0.0",
"@metamask/etherscan-link": "^2.2.0",
Expand Down
27 changes: 27 additions & 0 deletions test/e2e/mock-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ async function setupMocking(server, testSpecificMock) {
return {};
},
});
await server
.forPost(
'https://arbitrum-mainnet.infura.io/v3/00000000000000000000000000000000',
)
.thenCallback(() => {
return {
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '1675864782845',
result: '0xa4b1',
},
};
});

await server.forPost('https://api.segment.io/v1/batch').thenCallback(() => {
return {
Expand Down Expand Up @@ -372,6 +386,19 @@ async function setupMocking(server, testSpecificMock) {
json: emptyHotlist,
};
});

await server
.forPost('https://customnetwork.com/api/customRPC')
.thenCallback(() => {
return {
statusCode: 200,
json: {
jsonrpc: '2.0',
id: '1675864782845',
result: '0x122',
},
};
});
}

module.exports = { setupMocking };
208 changes: 207 additions & 1 deletion test/e2e/tests/add-custom-network.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,213 @@ describe('Custom network', function () {
},
],
};

it('should show warning when adding chainId 0x1(ethereum) and be followed by an wrong chainId error', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);

await driver.openNewPage('http://127.0.0.1:8080/');
await driver.executeScript(`
var params = [{
chainId: "0x1",
chainName: "Fake Ethereum Network",
nativeCurrency: {
name: "",
symbol: "ETH",
decimals: 18
},
rpcUrls: ["https://customnetwork.com/api/customRPC"],
blockExplorerUrls: [ "http://localhost:8080/api/customRPC" ]
}]
window.ethereum.request({
method: 'wallet_addEthereumChain',
params
})
`);
const windowHandles = await driver.waitUntilXWindowHandles(3);

await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);

await driver.clickElement({
tag: 'button',
text: 'Approve',
});

const warningTxt =
'You are adding a new RPC provider for Ethereum Mainnet';

await driver.findElement({
tag: 'h4',
text: warningTxt,
});

await driver.clickElement({
tag: 'button',
text: 'Approve',
});

const errMsg =
'Chain ID returned by the custom network does not match the submitted chain ID.';
await driver.findElement({
tag: 'span',
text: errMsg,
});

const approveBtn = await driver.findElement({
tag: 'button',
text: 'Approve',
});

assert.equal(await approveBtn.isEnabled(), false);
await driver.clickElement({
tag: 'button',
text: 'Cancel',
});
},
);
});
it("don't add bad rpc custom network", async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);

await driver.openNewPage('http://127.0.0.1:8080/');
await driver.executeScript(`
var params = [{
chainId: "0x123",
chainName: "Antani",
nativeCurrency: {
name: "",
symbol: "ANTANI",
decimals: 18
},
rpcUrls: ["https://customnetwork.com/api/customRPC"],
blockExplorerUrls: [ "http://localhost:8080/api/customRPC" ]
}]
window.ethereum.request({
method: 'wallet_addEthereumChain',
params
})
`);
const windowHandles = await driver.waitUntilXWindowHandles(3);

await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
await driver.clickElement({
tag: 'button',
text: 'Approve',
});

const errMsg =
'Chain ID returned by the custom network does not match the submitted chain ID.';
await driver.findElement({
tag: 'span',
text: errMsg,
});

const approveBtn = await driver.findElement({
tag: 'button',
text: 'Approve',
});

assert.equal(await approveBtn.isEnabled(), false);
await driver.clickElement({
tag: 'button',
text: 'Cancel',
});
},
);
});

it("don't add unreachable custom network", async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);

await driver.openNewPage('http://127.0.0.1:8080/');
await driver.executeScript(`
var params = [{
chainId: "0x123",
chainName: "Antani",
nativeCurrency: {
name: "",
symbol: "ANTANI",
decimals: 18
},
rpcUrls: ["https://doesntexist.abc/customRPC"],
blockExplorerUrls: [ "http://localhost:8080/api/customRPC" ]
}]
window.ethereum.request({
method: 'wallet_addEthereumChain',
params
})
`);
const windowHandles = await driver.waitUntilXWindowHandles(3);

await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
await driver.clickElement({
tag: 'button',
text: 'Approve',
});

await driver.findElement({
tag: 'span',
text: 'Error while connecting to the custom network.',
});

const approveBtn = await driver.findElement({
tag: 'button',
text: 'Approve',
});

assert.equal(await approveBtn.isEnabled(), false);
await driver.clickElement({
tag: 'button',
text: 'Cancel',
});
},
);
});

it('add custom network and switch the network', async function () {
await withFixtures(
{
Expand Down Expand Up @@ -85,7 +292,6 @@ describe('Custom network', function () {

await driver.clickElement({ tag: 'button', text: 'Close' });
await driver.clickElement({ tag: 'button', text: 'Approve' });

await driver.clickElement({
tag: 'h6',
text: 'Switch to Arbitrum One',
Expand Down
Loading

0 comments on commit b1ff6c6

Please sign in to comment.