1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 04:46:10 +01:00
metamask-extension/app/scripts/phishing-detect.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

import querystring from 'querystring';
import { EventEmitter } from 'events';
import dnode from 'dnode';
import PortStream from 'extension-port-stream';
import extension from 'extensionizer';
import { setupMultiplex } from './lib/stream-utils';
import { getEnvironmentType } from './lib/util';
import ExtensionPlatform from './platforms/extension';
document.addEventListener('DOMContentLoaded', start);
2020-11-03 00:41:28 +01:00
function start() {
const hash = window.location.hash.substring(1);
const suspect = querystring.parse(hash);
document.getElementById('csdbLink').href = `https://cryptoscamdb.org/search`;
global.platform = new ExtensionPlatform();
2020-11-03 00:41:28 +01:00
const extensionPort = extension.runtime.connect({
name: getEnvironmentType(),
});
const connectionStream = new PortStream(extensionPort);
const mx = setupMultiplex(connectionStream);
2020-11-03 00:41:28 +01:00
setupControllerConnection(
mx.createStream('controller'),
(err, metaMaskController) => {
if (err) {
return;
2020-11-03 00:41:28 +01:00
}
const continueLink = document.getElementById('unsafe-continue');
2020-11-03 00:41:28 +01:00
continueLink.addEventListener('click', () => {
metaMaskController.safelistPhishingDomain(suspect.hostname);
window.location.href = suspect.href;
});
2020-11-03 00:41:28 +01:00
},
);
}
2020-11-03 00:41:28 +01:00
function setupControllerConnection(connectionStream, cb) {
const eventEmitter = new EventEmitter();
// the "weak: false" option is for nodejs only (eg unit tests)
// it is a workaround for node v12 support
const metaMaskControllerDnode = dnode(
{
sendUpdate(state) {
eventEmitter.emit('update', state);
},
},
{ weak: false },
);
connectionStream.pipe(metaMaskControllerDnode).pipe(connectionStream);
2020-11-03 00:41:28 +01:00
metaMaskControllerDnode.once('remote', (backgroundConnection) =>
cb(null, backgroundConnection),
);
}