Skip to content
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

Bump controllers v30.0.2 #14906

Merged
merged 15 commits into from
Jul 18, 2022
5 changes: 4 additions & 1 deletion app/scripts/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,15 @@ function blockedDomainCheck() {

/**
* Redirects the current page to a phishing information page
*
* @param data
*/
function redirectToPhishingWarning() {
function redirectToPhishingWarning(data = {}) {
console.debug('MetaMask: Routing to Phishing Warning page.');
const baseUrl = process.env.PHISHING_WARNING_PAGE_URL;
window.location.href = `${baseUrl}#${querystring.stringify({
hostname: window.location.hostname,
href: window.location.href,
newIssueUrl: data.newIssueUrl,
})}`;
}
28 changes: 19 additions & 9 deletions app/scripts/controllers/detect-tokens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ import {
TokenListController,
TokensController,
} from '@metamask/controllers';
import { MAINNET, ROPSTEN } from '../../../shared/constants/network';
import {
MAINNET,
MAINNET_NETWORK_ID,
ROPSTEN,
} from '../../../shared/constants/network';
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
import DetectTokensController from './detect-tokens';
import NetworkController from './network';
import PreferencesController from './preferences';

const tokenIconsApiBaseUrl =
'https://static.metaswap.codefi.network/api/v1/tokenIcons';

describe('DetectTokensController', function () {
let tokenListController;
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -127,7 +134,6 @@ describe('DetectTokensController', function () {
});
tokenListController = new TokenListController({
chainId: '1',
useStaticTokenList: false,
onNetworkStateChange: sinon.spy(),
onPreferencesStateChange: sinon.spy(),
messenger: tokenListMessenger,
Expand Down Expand Up @@ -179,7 +185,6 @@ describe('DetectTokensController', function () {
});
tokenListController = new TokenListController({
chainId: '3',
useStaticTokenList: false,
onNetworkStateChange: sinon.spy(),
onPreferencesStateChange: sinon.spy(),
messenger: tokenListMessengerRopsten,
Expand Down Expand Up @@ -241,15 +246,16 @@ describe('DetectTokensController', function () {
),
);

await tokensController.removeAndIgnoreToken(tokenAddressToSkip);
await tokensController.ignoreTokens([tokenAddressToSkip]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all instances of TokensControllers.removeAndIgnoreToken() have been replaced with use of TokensControllers.ignoreTokens()

await controller.detectNewTokens();

assert.deepEqual(tokensController.state.tokens, [
{
address: toChecksumHexAddress(existingTokenAddress),
decimals: existingToken.decimals,
symbol: existingToken.symbol,
image: undefined,
aggregators: [],
image: `${tokenIconsApiBaseUrl}/${MAINNET_NETWORK_ID}/${existingTokenAddress}.png`,
isERC721: false,
},
]);
Expand Down Expand Up @@ -303,14 +309,16 @@ describe('DetectTokensController', function () {
decimals: existingToken.decimals,
symbol: existingToken.symbol,
isERC721: false,
image: undefined,
aggregators: [],
image: `${tokenIconsApiBaseUrl}/${MAINNET_NETWORK_ID}/${existingTokenAddress}.png`,
},
{
address: toChecksumHexAddress(tokenAddressToAdd),
decimals: tokenToAdd.decimals,
symbol: tokenToAdd.symbol,
image: undefined,
isERC721: false,
aggregators: [],
image: `${tokenIconsApiBaseUrl}/${MAINNET_NETWORK_ID}/${tokenAddressToAdd}.png`,
},
]);
});
Expand Down Expand Up @@ -363,15 +371,17 @@ describe('DetectTokensController', function () {
address: toChecksumHexAddress(existingTokenAddress),
decimals: existingToken.decimals,
symbol: existingToken.symbol,
image: undefined,
image: `${tokenIconsApiBaseUrl}/${MAINNET_NETWORK_ID}/${existingTokenAddress}.png`,
isERC721: false,
aggregators: [],
},
{
address: toChecksumHexAddress(tokenAddressToAdd),
decimals: tokenToAdd.decimals,
symbol: tokenToAdd.symbol,
image: undefined,
image: `${tokenIconsApiBaseUrl}/${MAINNET_NETWORK_ID}/${tokenAddressToAdd}.png`,
isERC721: false,
aggregators: [],
},
]);
});
Expand Down
172 changes: 78 additions & 94 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ import {
///: END:ONLY_INCLUDE_IN

import {
ASSET_TYPES,
TRANSACTION_STATUSES,
TRANSACTION_TYPES,
} from '../../shared/constants/transaction';
import { PHISHING_NEW_ISSUE_URLS } from '../../shared/constants/phishing';
import {
GAS_API_BASE_URL,
GAS_DEV_API_BASE_URL,
Expand Down Expand Up @@ -85,7 +87,7 @@ import {
POLLING_TOKEN_ENVIRONMENT_TYPES,
SUBJECT_TYPES,
} from '../../shared/constants/app';
import { EVENT } from '../../shared/constants/metametrics';
import { EVENT, EVENT_NAMES } from '../../shared/constants/metametrics';

import { hexToDecimal } from '../../ui/helpers/utils/conversions.util';
import { getTokenValueParam } from '../../ui/helpers/utils/token-util';
Expand Down Expand Up @@ -245,35 +247,28 @@ export default class MetamaskController extends EventEmitter {
config: { provider: this.provider },
state: initState.TokensController,
});
process.env.TOKEN_DETECTION_V2
? (this.assetsContractController = new AssetsContractController({
onPreferencesStateChange: (listener) =>
this.preferencesController.store.subscribe(listener),
onNetworkStateChange: (cb) =>
this.networkController.store.subscribe((networkState) => {
const modifiedNetworkState = {
...networkState,
provider: {
...networkState.provider,
chainId: hexToDecimal(networkState.provider.chainId),
},
};
return cb(modifiedNetworkState);
}),
config: {
provider: this.provider,
},
state: initState.AssetsContractController,
}))
: (this.assetsContractController = new AssetsContractController(
{
onPreferencesStateChange: (listener) =>
this.preferencesController.store.subscribe(listener),
},
{
provider: this.provider,
},
));

this.assetsContractController = new AssetsContractController(
{
onPreferencesStateChange: (listener) =>
this.preferencesController.store.subscribe(listener),
onNetworkStateChange: (cb) =>
this.networkController.store.subscribe((networkState) => {
const modifiedNetworkState = {
...networkState,
provider: {
...networkState.provider,
chainId: hexToDecimal(networkState.provider.chainId),
},
};
return cb(modifiedNetworkState);
}),
},
{
provider: this.provider,
},
initState.AssetsContractController,
);

this.collectiblesController = new CollectiblesController(
{
Expand Down Expand Up @@ -301,6 +296,21 @@ export default class MetamaskController extends EventEmitter {
getERC1155TokenURI: this.assetsContractController.getERC1155TokenURI.bind(
this.assetsContractController,
),
onCollectibleAdded: ({ address, symbol, tokenId, standard, source }) =>
this.metaMetricsController.trackEvent({
event: EVENT_NAMES.NFT_ADDED,
category: EVENT.CATEGORIES.WALLET,
properties: {
token_contract_address: address,
token_symbol: symbol,
asset_type: ASSET_TYPES.COLLECTIBLE,
token_standard: standard,
source,
},
sensitiveProperties: {
tokenId,
},
}),
},
{},
initState.CollectiblesController,
Expand Down Expand Up @@ -417,50 +427,23 @@ export default class MetamaskController extends EventEmitter {
const tokenListMessenger = this.controllerMessenger.getRestricted({
name: 'TokenListController',
});
process.env.TOKEN_DETECTION_V2
? (this.tokenListController = new TokenListController({
chainId: hexToDecimal(this.networkController.getCurrentChainId()),
onNetworkStateChange: (cb) =>
this.networkController.store.subscribe((networkState) => {
const modifiedNetworkState = {
...networkState,
provider: {
...networkState.provider,
chainId: hexToDecimal(networkState.provider.chainId),
},
};
return cb(modifiedNetworkState);
}),
messenger: tokenListMessenger,
state: initState.TokenListController,
}))
: (this.tokenListController = new TokenListController({
chainId: hexToDecimal(this.networkController.getCurrentChainId()),
useStaticTokenList: !this.preferencesController.store.getState()
.useTokenDetection,
onNetworkStateChange: (cb) =>
this.networkController.store.subscribe((networkState) => {
const modifiedNetworkState = {
...networkState,
provider: {
...networkState.provider,
chainId: hexToDecimal(networkState.provider.chainId),
},
};
return cb(modifiedNetworkState);
}),
onPreferencesStateChange: (cb) =>
this.preferencesController.store.subscribe((preferencesState) => {
const modifiedPreferencesState = {
...preferencesState,
useStaticTokenList: !this.preferencesController.store.getState()
.useTokenDetection,
};
return cb(modifiedPreferencesState);
}),
messenger: tokenListMessenger,
state: initState.TokenListController,
}));
Comment on lines -439 to -463
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useStaticTokenList and onPreferencesStateChange removed from TokenListController options object here


this.tokenListController = new TokenListController({
chainId: hexToDecimal(this.networkController.getCurrentChainId()),
onNetworkStateChange: (cb) =>
this.networkController.store.subscribe((networkState) => {
const modifiedNetworkState = {
...networkState,
provider: {
...networkState.provider,
chainId: hexToDecimal(networkState.provider.chainId),
},
};
return cb(modifiedNetworkState);
}),
messenger: tokenListMessenger,
state: initState.TokenListController,
});

this.phishingController = new PhishingController();

Expand Down Expand Up @@ -1586,7 +1569,6 @@ export default class MetamaskController extends EventEmitter {
tokensController,
),
updateTokenType: tokensController.updateTokenType.bind(tokensController),
removeToken: tokensController.removeAndIgnoreToken.bind(tokensController),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeAndIgnoreToken method removed from TokensController here

setAccountLabel: preferencesController.setAccountLabel.bind(
preferencesController,
),
Expand Down Expand Up @@ -1960,20 +1942,14 @@ export default class MetamaskController extends EventEmitter {
: null,

/** Token Detection V2 */
addDetectedTokens: process.env.TOKEN_DETECTION_V2
? tokensController.addDetectedTokens.bind(tokensController)
: null,
importTokens: process.env.TOKEN_DETECTION_V2
? tokensController.importTokens.bind(tokensController)
: null,
ignoreTokens: process.env.TOKEN_DETECTION_V2
? tokensController.ignoreTokens.bind(tokensController)
: null,
getBalancesInSingleCall: process.env.TOKEN_DETECTION_V2
? assetsContractController.getBalancesInSingleCall.bind(
assetsContractController,
)
: null,
addDetectedTokens: tokensController.addDetectedTokens.bind(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addDetectedTokens method added to TokensController here

tokensController,
),
addImportedTokens: tokensController.addTokens.bind(tokensController),
ignoreTokens: tokensController.ignoreTokens.bind(tokensController),
getBalancesInSingleCall: assetsContractController.getBalancesInSingleCall.bind(
assetsContractController,
),
};
}

Expand Down Expand Up @@ -3342,9 +3318,13 @@ export default class MetamaskController extends EventEmitter {
if (sender.url) {
const { hostname } = new URL(sender.url);
// Check if new connection is blocked if phishing detection is on
if (usePhishDetect && this.phishingController.test(hostname)) {
log.debug('MetaMask - sending phishing warning for', hostname);
this.sendPhishingWarning(connectionStream, hostname);
const phishingTestResponse = this.phishingController.test(hostname);
if (usePhishDetect && phishingTestResponse?.result) {
this.sendPhishingWarning(
connectionStream,
hostname,
phishingTestResponse,
);
return;
}
}
Expand Down Expand Up @@ -3422,11 +3402,15 @@ export default class MetamaskController extends EventEmitter {
* @param {*} connectionStream - The duplex stream to the per-page script,
* for sending the reload attempt to.
* @param {string} hostname - The hostname that triggered the suspicion.
* @param {object} phishingTestResponse - Result of calling `phishingController.test`,
* which is the result of calling eth-phishing-detects detector.check method https://github.com/MetaMask/eth-phishing-detect/blob/master/src/detector.js#L55-L112
*/
sendPhishingWarning(connectionStream, hostname) {
sendPhishingWarning(connectionStream, hostname, phishingTestResponse) {
const newIssueUrl = PHISHING_NEW_ISSUE_URLS[phishingTestResponse?.name];

const mux = setupMultiplex(connectionStream);
const phishingStream = mux.createStream('phishing');
phishingStream.write({ hostname });
phishingStream.write({ hostname, newIssueUrl });
}

/**
Expand Down
Loading