1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 11:01:41 +01:00
metamask-extension/app/scripts/inpage.js

105 lines
2.8 KiB
JavaScript
Raw Normal View History

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')
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
const RemoteStore = require('./lib/remote-store.js').RemoteStore
const Web3 = require('web3')
// rename on window
delete window.Web3
window.MetamaskWeb3 = Web3
2015-10-10 08:14:18 +02:00
const DEFAULT_RPC_URL = 'https://rpc.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
//
// setup background connection
2015-12-19 07:05:16 +01:00
var pluginStream = new LocalMessageDuplexStream({
name: 'inpage',
target: 'contentscript',
})
var mx = setupMultiplex(pluginStream)
// connect features
2015-12-19 07:05:16 +01:00
var remoteProvider = new StreamProvider()
remoteProvider.pipe(mx.createStream('provider')).pipe(remoteProvider)
2016-02-10 20:46:13 +01:00
remoteProvider.on('error', console.error.bind(console))
var initState = JSON.parse(localStorage['MetaMask-Config'] || '{}')
var publicConfigStore = new RemoteStore(initState)
var storeStream = publicConfigStore.createStream()
storeStream.pipe(mx.createStream('publicConfig')).pipe(storeStream)
publicConfigStore.subscribe(function(state){
localStorage['MetaMask-Config'] = JSON.stringify(state)
})
2016-04-15 06:11:35 +02:00
//
// global web3
//
var web3 = new Web3(remoteProvider)
window.web3 = web3
web3.setProvider = function(){
console.log('MetaMask - overrode web3.setProvider')
}
console.log('MetaMask - injected web3')
2016-02-15 06:53:54 +01:00
//
// handle synchronous requests
//
global.publicConfigStore = publicConfigStore
// set web3 defaultAcount
publicConfigStore.subscribe(function(state){
web3.eth.defaultAccount = state.selectedAddress
})
// setup sync http provider
var providerConfig = publicConfigStore.get('provider') || {}
var providerUrl = providerConfig.rpcTarget ? providerConfig.rpcTarget : DEFAULT_RPC_URL
var syncProvider = new Web3.providers.HttpProvider(providerUrl)
publicConfigStore.subscribe(function(state){
if (!state.provider) return
if (!state.provider.rpcTarget || state.provider.rpcTarget === providerUrl) return
providerUrl = state.provider.rpcTarget
syncProvider = new Web3.providers.HttpProvider(providerUrl)
})
// handle sync methods
remoteProvider.send = function(payload){
var result = null
switch (payload.method) {
2016-04-12 23:48:48 +02:00
case 'eth_accounts':
// read from localStorage
var selectedAddress = publicConfigStore.get('selectedAddress')
result = selectedAddress ? [selectedAddress] : []
break
case 'eth_coinbase':
// read from localStorage
var selectedAddress = publicConfigStore.get('selectedAddress')
result = selectedAddress || '0x0000000000000000000000000000000000000000'
break
// fallback to normal rpc
default:
return syncProvider.send(payload)
}
// return the result
return {
2016-02-15 06:53:54 +01:00
id: payload.id,
jsonrpc: payload.jsonrpc,
result: result,
}
}
2016-01-31 08:15:38 +01:00