mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
intercept xhrs for localhost:8545
This commit is contained in:
parent
4723a2949e
commit
a487c8a952
@ -37,7 +37,10 @@
|
|||||||
],
|
],
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"storage",
|
"storage",
|
||||||
"tabs"
|
"tabs",
|
||||||
|
"webRequest",
|
||||||
|
"webRequestBlocking",
|
||||||
|
"http://localhost:8545/"
|
||||||
],
|
],
|
||||||
"web_accessible_resources": [
|
"web_accessible_resources": [
|
||||||
"scripts/inpage.js"
|
"scripts/inpage.js"
|
||||||
|
@ -1,10 +1,18 @@
|
|||||||
|
const XHR = window.XMLHttpRequest
|
||||||
|
const fauxJax = require('faux-jax')
|
||||||
|
fauxJax.install()
|
||||||
const Web3 = require('web3')
|
const Web3 = require('web3')
|
||||||
const createPayload = require('web3-provider-engine/util/create-payload')
|
const createPayload = require('web3-provider-engine/util/create-payload')
|
||||||
const StreamProvider = require('./lib/stream-provider.js')
|
const StreamProvider = require('./lib/stream-provider.js')
|
||||||
const LocalMessageDuplexStream = require('./lib/local-message-stream.js')
|
const LocalMessageDuplexStream = require('./lib/local-message-stream.js')
|
||||||
|
|
||||||
|
const RPC_URL = 'https://rawtestrpc.metamask.io/'
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
// setup plugin communication
|
// setup plugin communication
|
||||||
|
//
|
||||||
|
|
||||||
var pluginStream = new LocalMessageDuplexStream({
|
var pluginStream = new LocalMessageDuplexStream({
|
||||||
name: 'inpage',
|
name: 'inpage',
|
||||||
target: 'contentscript',
|
target: 'contentscript',
|
||||||
@ -15,7 +23,10 @@ remoteProvider.pipe(pluginStream).pipe(remoteProvider)
|
|||||||
pluginStream.on('error', console.error.bind(console))
|
pluginStream.on('error', console.error.bind(console))
|
||||||
remoteProvider.on('error', console.error.bind(console))
|
remoteProvider.on('error', console.error.bind(console))
|
||||||
|
|
||||||
// handle synchronous methods remotely
|
|
||||||
|
//
|
||||||
|
// handle synchronous requests
|
||||||
|
//
|
||||||
|
|
||||||
// handle accounts cache
|
// handle accounts cache
|
||||||
var accountsCache = []
|
var accountsCache = []
|
||||||
@ -36,8 +47,8 @@ function populateAccountsCache(){
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var syncProvider = new Web3.providers.HttpProvider('https://rawtestrpc.metamask.io/')
|
// handle synchronous methods via standard http provider
|
||||||
// var unsupportedMethods = ['eth_accounts']
|
var syncProvider = new Web3.providers.HttpProvider(RPC_URL)
|
||||||
remoteProvider.send = function(payload){
|
remoteProvider.send = function(payload){
|
||||||
var result = null
|
var result = null
|
||||||
switch (payload.method) {
|
switch (payload.method) {
|
||||||
@ -62,16 +73,84 @@ remoteProvider.send = function(payload){
|
|||||||
|
|
||||||
// return the result
|
// return the result
|
||||||
return {
|
return {
|
||||||
id: payload.id,
|
id: payload.id,
|
||||||
jsonrpc: payload.jsonrpc,
|
jsonrpc: payload.jsonrpc,
|
||||||
result: result,
|
result: result,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create web3
|
//
|
||||||
|
// global web3
|
||||||
|
//
|
||||||
|
|
||||||
var web3 = new Web3(remoteProvider)
|
var web3 = new Web3(remoteProvider)
|
||||||
window.web3 = web3
|
window.web3 = web3
|
||||||
web3.setProvider = function(){
|
web3.setProvider = function(){
|
||||||
console.log('MetaMask Extension - overrode web3.setProvider')
|
console.log('MetaMask Extension - overrode web3.setProvider')
|
||||||
}
|
}
|
||||||
console.log('MetaMask Extension - injected web3')
|
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
|
||||||
|
}
|
||||||
|
@ -20,7 +20,8 @@
|
|||||||
"metamask-ui": "^1.0.0",
|
"metamask-ui": "^1.0.0",
|
||||||
"readable-stream": "^2.0.5",
|
"readable-stream": "^2.0.5",
|
||||||
"web3": "^0.15.1",
|
"web3": "^0.15.1",
|
||||||
"web3-provider-engine": "^5.0.1"
|
"web3-provider-engine": "^5.0.1",
|
||||||
|
"faux-jax": "git+https://github.com/kumavis/faux-jax.git#c3648de04804f3895c5b4972750cae5b51ddb103"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"grunt": "~0.4.1",
|
"grunt": "~0.4.1",
|
||||||
|
Loading…
Reference in New Issue
Block a user