aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/inpage-provider.js
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-08-25 06:44:40 +0800
committerkumavis <aaron@kumavis.me>2017-08-25 06:44:40 +0800
commitcc56d0d2f61da576acc72b1e7f63df4015469267 (patch)
tree5ddce6b8b9a32a0a83009b576904b778991e53bc /app/scripts/lib/inpage-provider.js
parent34b327caf07cd395229806cedecdeb770c034562 (diff)
downloadtangerine-wallet-browser-cc56d0d2f61da576acc72b1e7f63df4015469267.tar
tangerine-wallet-browser-cc56d0d2f61da576acc72b1e7f63df4015469267.tar.gz
tangerine-wallet-browser-cc56d0d2f61da576acc72b1e7f63df4015469267.tar.bz2
tangerine-wallet-browser-cc56d0d2f61da576acc72b1e7f63df4015469267.tar.lz
tangerine-wallet-browser-cc56d0d2f61da576acc72b1e7f63df4015469267.tar.xz
tangerine-wallet-browser-cc56d0d2f61da576acc72b1e7f63df4015469267.tar.zst
tangerine-wallet-browser-cc56d0d2f61da576acc72b1e7f63df4015469267.zip
inpage - use json-rpc-engine for inpage-provider
Diffstat (limited to 'app/scripts/lib/inpage-provider.js')
-rw-r--r--app/scripts/lib/inpage-provider.js46
1 files changed, 10 insertions, 36 deletions
diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js
index fd032a673..de6e8b811 100644
--- a/app/scripts/lib/inpage-provider.js
+++ b/app/scripts/lib/inpage-provider.js
@@ -1,8 +1,8 @@
const pipe = require('pump')
-const StreamProvider = require('web3-stream-provider')
+const RpcEngine = require('json-rpc-engine')
+const createStreamMiddleware = require('json-rpc-middleware-stream')
const LocalStorageStore = require('obs-store')
const ObjectMultiplex = require('./obj-multiplex')
-const createRandomId = require('./random-id')
module.exports = MetamaskInpageProvider
@@ -30,38 +30,20 @@ function MetamaskInpageProvider (connectionStream) {
multiStream.ignoreStream('phishing')
// connect to async provider
- const asyncProvider = self.asyncProvider = new StreamProvider()
+ const streamMiddleware = createStreamMiddleware()
pipe(
- asyncProvider,
+ streamMiddleware.stream,
multiStream.createStream('provider'),
- asyncProvider,
+ streamMiddleware.stream,
(err) => logStreamDisconnectWarning('MetaMask RpcProvider', err)
)
// start and stop polling to unblock first block lock
- self.idMap = {}
- // handle sendAsync requests via asyncProvider
- self.sendAsync = function (payload, cb) {
- // rewrite request ids
- var request = eachJsonMessage(payload, (message) => {
- var newId = createRandomId()
- self.idMap[newId] = message.id
- message.id = newId
- return message
- })
- // forward to asyncProvider
- asyncProvider.sendAsync(request, function (err, res) {
- if (err) return cb(err)
- // transform messages to original ids
- eachJsonMessage(res, (message) => {
- var oldId = self.idMap[message.id]
- delete self.idMap[message.id]
- message.id = oldId
- return message
- })
- cb(null, res)
- })
- }
+ // handle sendAsync requests via dapp-side rpc engine
+ const engine = new RpcEngine()
+ engine.push(streamMiddleware)
+
+ self.sendAsync = engine.handle.bind(engine)
}
MetamaskInpageProvider.prototype.send = function (payload) {
@@ -121,14 +103,6 @@ MetamaskInpageProvider.prototype.isMetaMask = true
// util
-function eachJsonMessage (payload, transformFn) {
- if (Array.isArray(payload)) {
- return payload.map(transformFn)
- } else {
- return transformFn(payload)
- }
-}
-
function logStreamDisconnectWarning (remoteLabel, err) {
let warningMsg = `MetamaskInpageProvider - lost connection to ${remoteLabel}`
if (err) warningMsg += '\n' + err.stack