1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/lib/inpage-provider.js

119 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-09-08 06:19:24 +02:00
const pump = require('pump')
const RpcEngine = require('json-rpc-engine')
2017-09-07 19:08:07 +02:00
const createIdRemapMiddleware = require('json-rpc-engine/src/idRemapMiddleware')
const createStreamMiddleware = require('json-rpc-middleware-stream')
2017-01-25 04:47:00 +01:00
const LocalStorageStore = require('obs-store')
const asStream = require('obs-store/lib/asStream')
2017-09-08 07:47:08 +02:00
const ObjectMultiplex = require('obj-multiplex')
2016-05-23 00:23:16 +02:00
module.exports = MetamaskInpageProvider
2016-06-21 22:18:32 +02:00
function MetamaskInpageProvider (connectionStream) {
2016-05-23 00:23:16 +02:00
const self = this
2016-06-21 22:18:32 +02:00
// setup connectionStream multiplexing
2017-09-08 07:47:08 +02:00
const mux = self.mux = new ObjectMultiplex()
2017-09-08 06:19:24 +02:00
pump(
2017-01-25 04:47:00 +01:00
connectionStream,
2017-09-08 07:47:08 +02:00
mux,
2017-01-25 04:47:00 +01:00
connectionStream,
(err) => logStreamDisconnectWarning('MetaMask', err)
)
// subscribe to metamask public config (one-way)
self.publicConfigStore = new LocalStorageStore({ storageKey: 'MetaMask-Config' })
2017-09-08 06:19:24 +02:00
pump(
2017-09-08 07:47:08 +02:00
mux.createStream('publicConfig'),
asStream(self.publicConfigStore),
2017-01-25 04:47:00 +01:00
(err) => logStreamDisconnectWarning('MetaMask PublicConfigStore', err)
)
2016-05-23 00:23:16 +02:00
// ignore phishing warning message (handled elsewhere)
2017-09-08 07:47:08 +02:00
mux.ignoreStream('phishing')
2016-05-23 00:23:16 +02:00
// connect to async provider
const streamMiddleware = createStreamMiddleware()
2017-09-08 06:19:24 +02:00
pump(
streamMiddleware.stream,
2017-09-08 07:47:08 +02:00
mux.createStream('provider'),
streamMiddleware.stream,
2017-01-25 04:47:00 +01:00
(err) => logStreamDisconnectWarning('MetaMask RpcProvider', err)
)
// handle sendAsync requests via dapp-side rpc engine
const rpcEngine = new RpcEngine()
rpcEngine.push(createIdRemapMiddleware())
rpcEngine.push(streamMiddleware)
self.rpcEngine = rpcEngine
}
// handle sendAsync requests via asyncProvider
// also remap ids inbound and outbound
MetamaskInpageProvider.prototype.sendAsync = function (payload, cb) {
const self = this
self.rpcEngine.handle(payload, cb)
2016-05-23 00:23:16 +02:00
}
2016-06-21 22:18:32 +02:00
MetamaskInpageProvider.prototype.send = function (payload) {
2016-05-23 00:23:16 +02:00
const self = this
2016-10-05 20:10:39 +02:00
let selectedAddress
2016-08-11 22:31:00 +02:00
let result = null
2016-05-23 00:23:16 +02:00
switch (payload.method) {
case 'eth_accounts':
// read from localStorage
selectedAddress = self.publicConfigStore.getState().selectedAddress
result = selectedAddress ? [selectedAddress] : []
2016-05-23 00:23:16 +02:00
break
case 'eth_coinbase':
// read from localStorage
selectedAddress = self.publicConfigStore.getState().selectedAddress
result = selectedAddress || null
2016-05-23 00:23:16 +02:00
break
2016-11-23 19:48:20 +01:00
case 'eth_uninstallFilter':
self.sendAsync(payload, noop)
result = true
break
case 'net_version':
2017-04-27 06:05:45 +02:00
const networkVersion = self.publicConfigStore.getState().networkVersion
2017-08-28 19:41:01 +02:00
result = networkVersion || null
break
// throw not-supported Error
2016-05-23 00:23:16 +02:00
default:
2016-10-05 20:10:39 +02:00
var link = 'https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#dizzy-all-async---think-of-metamask-as-a-light-client'
var message = `The MetaMask Web3 object does not support synchronous methods like ${payload.method} without a callback parameter. See ${link} for details.`
throw new Error(message)
2016-05-23 00:23:16 +02:00
}
// return the result
return {
id: payload.id,
jsonrpc: payload.jsonrpc,
result: result,
}
}
2016-06-21 22:18:32 +02:00
MetamaskInpageProvider.prototype.isConnected = function () {
2016-05-23 00:23:16 +02:00
return true
}
MetamaskInpageProvider.prototype.isMetaMask = true
2016-05-23 00:23:16 +02:00
// util
2017-04-27 06:05:45 +02:00
function logStreamDisconnectWarning (remoteLabel, err) {
2017-01-25 04:47:00 +01:00
let warningMsg = `MetamaskInpageProvider - lost connection to ${remoteLabel}`
if (err) warningMsg += '\n' + err.stack
console.warn(warningMsg)
}
2016-11-23 19:48:20 +01:00
function noop () {}