aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2016-01-31 16:29:06 +0800
committerkumavis <aaron@kumavis.me>2016-01-31 16:29:06 +0800
commitb239bacd360ae9a381b4ed0798112aa655d6dace (patch)
treed7397e13661daa4ea7f31644d1320caec034d4e5
parent3acb8485202e971189917f86c090d90e18a525e4 (diff)
downloadtangerine-wallet-browser-b239bacd360ae9a381b4ed0798112aa655d6dace.tar
tangerine-wallet-browser-b239bacd360ae9a381b4ed0798112aa655d6dace.tar.gz
tangerine-wallet-browser-b239bacd360ae9a381b4ed0798112aa655d6dace.tar.bz2
tangerine-wallet-browser-b239bacd360ae9a381b4ed0798112aa655d6dace.tar.lz
tangerine-wallet-browser-b239bacd360ae9a381b4ed0798112aa655d6dace.tar.xz
tangerine-wallet-browser-b239bacd360ae9a381b4ed0798112aa655d6dace.tar.zst
tangerine-wallet-browser-b239bacd360ae9a381b4ed0798112aa655d6dace.zip
rpc - sync - add support for sync eth_accounts + eth_coinbase
-rw-r--r--app/scripts/inpage.js55
1 files changed, 46 insertions, 9 deletions
diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js
index 8e60c54f9..0837de338 100644
--- a/app/scripts/inpage.js
+++ b/app/scripts/inpage.js
@@ -1,4 +1,5 @@
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')
@@ -12,21 +13,57 @@ var remoteProvider = new StreamProvider()
remoteProvider.pipe(pluginStream).pipe(remoteProvider)
// handle synchronous methods remotely
-var syncProvider = new Web3.providers.HttpProvider('https://rawtestrpc.metamask.io/')
-var unsupportedMethods = ['eth_accounts']
-remoteProvider.send = function(payload){
- var payloads = Array.isArray(payload) ? payload : [payload]
- payloads.forEach(function(payload){
- if (-1 !== unsupportedMethods.indexOf(payload.method)) {
- console.error('MetaMask - Unsupported synchronous call "'+payload.method+'".')
+// handle accounts cache
+var accountsCache = []
+setInterval(populateAccountsCache, 1000)
+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)
}
})
+}
+
+var syncProvider = new Web3.providers.HttpProvider('https://rawtestrpc.metamask.io/')
+// var unsupportedMethods = ['eth_accounts']
+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
- return syncProvider.send(payload)
+ 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,
+ }
+}
// create web3
var web3 = new Web3(remoteProvider)