1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 16:18:07 +01:00
metamask-extension/app/scripts/lib/inpage-provider.js

140 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-01-25 04:47:00 +01:00
const pipe = require('pump')
const StreamProvider = require('web3-stream-provider')
2017-01-25 04:47:00 +01:00
const LocalStorageStore = require('obs-store')
const ObjectMultiplex = require('./obj-multiplex')
const createRandomId = require('./random-id')
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-01-25 04:47:00 +01:00
var multiStream = self.multiStream = ObjectMultiplex()
pipe(
connectionStream,
multiStream,
connectionStream,
(err) => logStreamDisconnectWarning('MetaMask', err)
)
// subscribe to metamask public config (one-way)
self.publicConfigStore = new LocalStorageStore({ storageKey: 'MetaMask-Config' })
pipe(
multiStream.createStream('publicConfig'),
self.publicConfigStore,
(err) => logStreamDisconnectWarning('MetaMask PublicConfigStore', err)
)
2016-05-23 00:23:16 +02:00
// ignore phishing warning message (handled elsewhere)
multiStream.ignoreStream('phishing')
2016-05-23 00:23:16 +02:00
// connect to async provider
const asyncProvider = self.asyncProvider = new StreamProvider()
2017-01-25 04:47:00 +01:00
pipe(
asyncProvider,
2017-01-25 04:47:00 +01:00
multiStream.createStream('provider'),
asyncProvider,
2017-01-25 04:47:00 +01:00
(err) => logStreamDisconnectWarning('MetaMask RpcProvider', err)
)
// start and stop polling to unblock first block lock
2016-09-01 00:40:05 +02:00
self.idMap = {}
// handle sendAsync requests via asyncProvider
2016-11-11 19:26:12 +01:00
self.sendAsync = function (payload, cb) {
// rewrite request ids
var request = eachJsonMessage(payload, (_message) => {
const message = Object.assign({}, _message)
const newId = createRandomId()
2016-09-01 00:40:05 +02:00
self.idMap[newId] = message.id
message.id = newId
return message
})
// forward to asyncProvider
asyncProvider.sendAsync(request, function (err, res) {
2016-09-01 00:40:05 +02:00
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)
})
}
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.sendAsync = function () {
2016-05-23 00:23:16 +02:00
throw new Error('MetamaskInpageProvider - sendAsync not overwritten')
}
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
2016-11-19 01:27:26 +01:00
function eachJsonMessage (payload, transformFn) {
if (Array.isArray(payload)) {
return payload.map(transformFn)
} else {
return transformFn(payload)
}
2016-09-06 05:20:22 +02:00
}
2016-11-23 19:48:20 +01:00
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 () {}