aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/phishing-detect.js
blob: b30c76b728a0c501fa8687abeb132c0bb6263742 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const querystring = require('querystring')
const dnode = require('dnode')
const { EventEmitter } = require('events')
const PortStream = require('extension-port-stream')
const extension = require('extensionizer')
const {setupMultiplex} = require('./lib/stream-utils.js')
const { getEnvironmentType } = require('./lib/util')
const ExtensionPlatform = require('./platforms/extension')

document.addEventListener('DOMContentLoaded', start)

function start () {
  const windowType = getEnvironmentType(window.location.href)
  const hash = window.location.hash.substring(1)
  const suspect = querystring.parse(hash)

  document.getElementById('esdbLink').href = `https://etherscamdb.info/domain/${suspect.hostname}`

  global.platform = new ExtensionPlatform()
  global.METAMASK_UI_TYPE = windowType

  const extensionPort = extension.runtime.connect({ name: windowType })
  const connectionStream = new PortStream(extensionPort)
  const mx = setupMultiplex(connectionStream)
  setupControllerConnection(mx.createStream('controller'), (err, metaMaskController) => {
    if (err) {
      return
    }

    const continueLink = document.getElementById('unsafe-continue')
    continueLink.addEventListener('click', () => {
      metaMaskController.whitelistPhishingDomain(suspect.hostname)
      window.location.href = suspect.href
    })
  })
}

function setupControllerConnection (connectionStream, cb) {
  const eventEmitter = new EventEmitter()
  const accountManagerDnode = dnode({
    sendUpdate (state) {
      eventEmitter.emit('update', state)
    },
  })
  connectionStream.pipe(accountManagerDnode).pipe(connectionStream)
  accountManagerDnode.once('remote', (accountManager) => cb(null, accountManager))
}