aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/inpage.js
blob: 8de02b69df4fda3bdfbc89db3e2350a02898a4e0 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const XHR = window.XMLHttpRequest
const fauxJax = require('faux-jax')
fauxJax.install()
const Web3 = require('web3')
const createPayload = require('web3-provider-engine/util/create-payload')
const StreamProvider = require('./lib/stream-provider.js')
const LocalMessageDuplexStream = require('./lib/local-message-stream.js')

const RPC_URL = 'https://rawtestrpc.metamask.io/'


//
// setup plugin communication
//

var pluginStream = new LocalMessageDuplexStream({
  name: 'inpage',
  target: 'contentscript',
})
var remoteProvider = new StreamProvider()
remoteProvider.pipe(pluginStream).pipe(remoteProvider)

pluginStream.on('error', console.error.bind(console))
remoteProvider.on('error', console.error.bind(console))


//
// handle synchronous requests
//

// handle accounts cache
var accountsCache = []
setInterval(populateAccountsCache, 4000)
function populateAccountsCache(){
  remoteProvider.sendAsync(createPayload({
    method: 'eth_accounts',
    params: [],
    note: 'from metamask inpage provider',
  }), function(err, response){
    if (err) return console.error('MetaMask - Error polling accounts')
    // update localStorage
    var accounts = response.result
    if (accounts.toString() !== accountsCache.toString()) {
      accountsCache = accounts
      localStorage['MetaMask-Accounts'] = JSON.stringify(accounts)
    }
  })
}

// handle synchronous methods via standard http provider
var syncProvider = new Web3.providers.HttpProvider(RPC_URL)
remoteProvider.send = function(payload){
  var result = null
  switch (payload.method) {
    
    case 'eth_accounts':
      // read from localStorage
      accountsCache = JSON.parse(localStorage['MetaMask-Accounts'] || '[]')
      result = accountsCache
      break

    case 'eth_coinbase':
      // read from localStorage
      accountsCache = JSON.parse(localStorage['MetaMask-Accounts'] || '[]')
      result = accountsCache[0] || '0x0000000000000000000000000000000000000000'
      break

    // fallback to normal rpc
    default:
      return syncProvider.send(payload)

  }

  // return the result
  return {
    id: payload.id,
    jsonrpc: payload.jsonrpc,
    result: result,
  }
}

//
// global web3
//

var web3 = new Web3(remoteProvider)
window.web3 = web3
web3.setProvider = function(){
  console.log('MetaMask Extension - overrode web3.setProvider')
}
console.log('MetaMask Extension - injected web3')


//
// intercept local node requests
//

fauxJax.on('request', function(req){
  // check if local node request
  if (req.requestURL.indexOf('localhost:8545') !== -1) {
    var rpcReq = JSON.parse(req.requestBody)
    if (req.async) {
      remoteProvider.sendAsync(rpcReq, function(err, result){
        // console.log('intercepted request (async):', rpcReq, result)
        handleResult(result)
      })
    } else {
      var result = remoteProvider.send(rpcReq)
      // console.log('intercepted request (sync):', rpcReq, result)
      handleResult(result)
    }
  } else {
    // console.log('request continuing normally:', req.requestURL)
    continueRequestNormally(req)
  }

  function handleResult(result){
    var serializedResult = JSON.stringify(result)
    req.respond(200, {
      'content-type': 'application/json',
    }, serializedResult)
  }
})

function continueRequestNormally(req){
  var xhr = new XHR()
  // set target url and method
  xhr.open(req.requestMethod, req.requestURL, req.async)
  // set headers
  Object.keys(req.requestHeaders || {}).forEach(function(headerKey){
    xhr.setRequestHeader(headerKey, req.requestHeaders[headerKey])
  })
  // send and call completion handler
  if (req.async) {
    xhr.onload = copyResult
    xhr.send(req.requestBody)
  } else {
    xhr.send(req.requestBody)
    copyResult()
  }

  function copyResult() {
    var headers = extractResponseHeaders(xhr.getAllResponseHeaders())
    req.respond(xhr.status, headers, xhr.response)
  }
}

function extractResponseHeaders(rawHeaders){
  var headers = {}
  var headerKeyValues = rawHeaders.split('\r\n').filter(Boolean)
  headerKeyValues.forEach(function(keyValue){
    var data = keyValue.split(': ')
    headers[data[0]] = data[1]
  })
  return headers
}