diff options
author | kumavis <kumavis@users.noreply.github.com> | 2016-04-16 04:06:28 +0800 |
---|---|---|
committer | kumavis <kumavis@users.noreply.github.com> | 2016-04-16 04:06:28 +0800 |
commit | 83b8741bbb335ad1629b72180616835dbb3f5433 (patch) | |
tree | b8adfc80551c1032dc6fc43aa3a5055169446143 /app/scripts/inpage.js | |
parent | 81fc1e5f7dc958af40d8113b80c8cfb38c2016ef (diff) | |
parent | d6114292d0c05b42b1d28d75baad8af0c06509a1 (diff) | |
download | tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.gz tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.bz2 tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.lz tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.xz tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.tar.zst tangerine-wallet-browser-83b8741bbb335ad1629b72180616835dbb3f5433.zip |
Merge pull request #113 from MetaMask/i53
publicConfigStore for sync provider and selected address
Diffstat (limited to 'app/scripts/inpage.js')
-rw-r--r-- | app/scripts/inpage.js | 80 |
1 files changed, 44 insertions, 36 deletions
diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 01f35d0fe..e6684cbd4 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -1,31 +1,42 @@ -const XHR = window.XMLHttpRequest - -// bring in web3 but rename on window -const Web3 = require('web3') -delete window.Web3 -window.MetamaskWeb3 = 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 setupMultiplex = require('./lib/stream-utils.js').setupMultiplex +const RemoteStore = require('./lib/remote-store.js').RemoteStore +const Web3 = require('web3') -const RPC_URL = 'https://testrpc.metamask.io/' +// rename on window +delete window.Web3 +window.MetamaskWeb3 = Web3 + +const DEFAULT_RPC_URL = 'https://rpc.metamask.io/' // // setup plugin communication // +// setup background connection var pluginStream = new LocalMessageDuplexStream({ name: 'inpage', target: 'contentscript', }) +var mx = setupMultiplex(pluginStream) +// connect features var remoteProvider = new StreamProvider() -remoteProvider.pipe(pluginStream).pipe(remoteProvider) - -pluginStream.on('error', console.error.bind(console)) +remoteProvider.pipe(mx.createStream('provider')).pipe(remoteProvider) 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) +}) + + // // global web3 // @@ -42,42 +53,39 @@ console.log('MetaMask - injected web3') // handle synchronous requests // -// handle accounts cache -var accountsCache = JSON.parse(localStorage['MetaMask-Accounts'] || '[]') -web3.eth.defaultAccount = accountsCache[0] - -setInterval(populateAccountsCache, 4000) -function populateAccountsCache(){ - remoteProvider.sendAsync(createPayload({ - method: 'eth_accounts', - params: [], - isMetamaskInternal: true, - }), 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 - web3.eth.defaultAccount = accountsCache[0] - localStorage['MetaMask-Accounts'] = JSON.stringify(accounts) - } - }) -} +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 synchronous methods via standard http provider -var syncProvider = new Web3.providers.HttpProvider(RPC_URL) +// handle sync methods remoteProvider.send = function(payload){ var result = null switch (payload.method) { case 'eth_accounts': // read from localStorage - result = accountsCache + var selectedAddress = publicConfigStore.get('selectedAddress') + result = selectedAddress ? [selectedAddress] : [] break case 'eth_coinbase': // read from localStorage - result = accountsCache[0] || '0x0000000000000000000000000000000000000000' + var selectedAddress = publicConfigStore.get('selectedAddress') + result = selectedAddress || '0x0000000000000000000000000000000000000000' break // fallback to normal rpc |