2016-02-15 06:53:54 +01:00
|
|
|
const XHR = window.XMLHttpRequest
|
2016-02-15 20:03:50 +01:00
|
|
|
// const fauxJax = require('faux-jax')
|
|
|
|
// fauxJax.install()
|
2016-03-06 00:03:43 +01:00
|
|
|
|
|
|
|
// bring in web3 but rename on window
|
2015-12-19 07:05:16 +01:00
|
|
|
const Web3 = require('web3')
|
2016-03-06 00:03:43 +01:00
|
|
|
delete window.Web3
|
|
|
|
window.MetamaskWeb3 = Web3
|
|
|
|
|
2016-01-31 09:29:06 +01:00
|
|
|
const createPayload = require('web3-provider-engine/util/create-payload')
|
2015-12-19 07:05:16 +01:00
|
|
|
const StreamProvider = require('./lib/stream-provider.js')
|
|
|
|
const LocalMessageDuplexStream = require('./lib/local-message-stream.js')
|
2015-10-10 08:14:18 +02:00
|
|
|
|
2016-02-15 06:53:54 +01:00
|
|
|
const RPC_URL = 'https://rawtestrpc.metamask.io/'
|
2015-10-10 08:14:18 +02:00
|
|
|
|
2016-02-15 06:53:54 +01:00
|
|
|
|
|
|
|
//
|
2015-12-19 07:05:16 +01:00
|
|
|
// setup plugin communication
|
2016-02-15 06:53:54 +01:00
|
|
|
//
|
|
|
|
|
2015-12-19 07:05:16 +01:00
|
|
|
var pluginStream = new LocalMessageDuplexStream({
|
|
|
|
name: 'inpage',
|
|
|
|
target: 'contentscript',
|
|
|
|
})
|
|
|
|
var remoteProvider = new StreamProvider()
|
|
|
|
remoteProvider.pipe(pluginStream).pipe(remoteProvider)
|
2015-10-10 08:14:18 +02:00
|
|
|
|
2016-02-10 20:46:13 +01:00
|
|
|
pluginStream.on('error', console.error.bind(console))
|
|
|
|
remoteProvider.on('error', console.error.bind(console))
|
|
|
|
|
2016-02-15 06:53:54 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// handle synchronous requests
|
|
|
|
//
|
2016-01-31 08:35:52 +01:00
|
|
|
|
2016-01-31 09:29:06 +01:00
|
|
|
// handle accounts cache
|
|
|
|
var accountsCache = []
|
2016-02-09 02:13:30 +01:00
|
|
|
setInterval(populateAccountsCache, 4000)
|
2016-01-31 09:29:06 +01:00
|
|
|
function populateAccountsCache(){
|
|
|
|
remoteProvider.sendAsync(createPayload({
|
|
|
|
method: 'eth_accounts',
|
|
|
|
params: [],
|
2016-02-15 23:43:39 +01:00
|
|
|
isMetamaskInternal: true,
|
2016-01-31 09:29:06 +01:00
|
|
|
}), 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)
|
2016-01-31 08:35:52 +01:00
|
|
|
}
|
|
|
|
})
|
2016-01-31 09:29:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-15 06:53:54 +01:00
|
|
|
// handle synchronous methods via standard http provider
|
|
|
|
var syncProvider = new Web3.providers.HttpProvider(RPC_URL)
|
2016-01-31 09:29:06 +01:00
|
|
|
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
|
2016-01-31 08:35:52 +01:00
|
|
|
|
2016-01-31 09:29:06 +01:00
|
|
|
case 'eth_coinbase':
|
|
|
|
// read from localStorage
|
|
|
|
accountsCache = JSON.parse(localStorage['MetaMask-Accounts'] || '[]')
|
|
|
|
result = accountsCache[0] || '0x0000000000000000000000000000000000000000'
|
|
|
|
break
|
2016-01-31 08:35:52 +01:00
|
|
|
|
2016-01-31 09:29:06 +01:00
|
|
|
// fallback to normal rpc
|
|
|
|
default:
|
|
|
|
return syncProvider.send(payload)
|
|
|
|
|
|
|
|
}
|
2016-01-31 08:35:52 +01:00
|
|
|
|
2016-01-31 09:29:06 +01:00
|
|
|
// return the result
|
|
|
|
return {
|
2016-02-15 06:53:54 +01:00
|
|
|
id: payload.id,
|
|
|
|
jsonrpc: payload.jsonrpc,
|
|
|
|
result: result,
|
|
|
|
}
|
2016-01-31 09:29:06 +01:00
|
|
|
}
|
2016-01-31 08:15:38 +01:00
|
|
|
|
2016-02-15 06:53:54 +01:00
|
|
|
//
|
|
|
|
// global web3
|
|
|
|
//
|
|
|
|
|
2015-12-19 07:05:16 +01:00
|
|
|
var web3 = new Web3(remoteProvider)
|
2015-08-02 01:33:31 +02:00
|
|
|
window.web3 = web3
|
2016-01-31 08:15:38 +01:00
|
|
|
web3.setProvider = function(){
|
2016-02-15 20:02:41 +01:00
|
|
|
console.log('MetaMask - overrode web3.setProvider')
|
2016-01-31 08:15:38 +01:00
|
|
|
}
|
2016-02-15 20:02:41 +01:00
|
|
|
console.log('MetaMask - injected web3')
|
2016-02-15 06:53:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// intercept local node requests
|
|
|
|
//
|
|
|
|
|
2016-02-15 20:02:41 +01:00
|
|
|
|
2016-02-15 20:03:50 +01:00
|
|
|
// console.log('MetaMask - intercepting localhost:8545 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
|
|
|
|
// }
|