Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Subscription support #5458

Merged
merged 6 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/scripts/controllers/network/createInfuraClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware')
const createBlockReEmitMiddleware = require('eth-json-rpc-middleware/block-reemit')
const createBlockReRefMiddleware = require('eth-json-rpc-middleware/block-ref')
const createRetryOnEmptyMiddleware = require('eth-json-rpc-middleware/retryOnEmpty')
const createBlockCacheMiddleware = require('eth-json-rpc-middleware/block-cache')
const createInflightMiddleware = require('eth-json-rpc-middleware/inflight-cache')
const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector')
Expand All @@ -11,13 +12,14 @@ module.exports = createInfuraClient

function createInfuraClient ({ network }) {
const infuraMiddleware = createInfuraMiddleware({ network })
const blockProvider = providerFromMiddleware(infuraMiddleware)
const blockTracker = new BlockTracker({ provider: blockProvider })
const infuraProvider = providerFromMiddleware(infuraMiddleware)
const blockTracker = new BlockTracker({ provider: infuraProvider })

const networkMiddleware = mergeMiddleware([
createBlockCacheMiddleware({ blockTracker }),
createInflightMiddleware(),
createBlockReEmitMiddleware({ blockTracker, provider: blockProvider }),
createBlockReRefMiddleware({ blockTracker, provider: infuraProvider }),
createRetryOnEmptyMiddleware({ blockTracker, provider: infuraProvider }),
createBlockTrackerInspectorMiddleware({ blockTracker }),
infuraMiddleware,
])
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/controllers/network/createJsonRpcClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware')
const createFetchMiddleware = require('eth-json-rpc-middleware/fetch')
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-ref')
const createBlockRefRewriteMiddleware = require('eth-json-rpc-middleware/block-ref-rewrite')
const createBlockCacheMiddleware = require('eth-json-rpc-middleware/block-cache')
const createInflightMiddleware = require('eth-json-rpc-middleware/inflight-cache')
const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector')
Expand All @@ -15,7 +15,7 @@ function createJsonRpcClient ({ rpcUrl }) {
const blockTracker = new BlockTracker({ provider: blockProvider })

const networkMiddleware = mergeMiddleware([
createBlockRefMiddleware({ blockTracker }),
createBlockRefRewriteMiddleware({ blockTracker }),
createBlockCacheMiddleware({ blockTracker }),
createInflightMiddleware(),
createBlockTrackerInspectorMiddleware({ blockTracker }),
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/controllers/network/createLocalhostClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware')
const createFetchMiddleware = require('eth-json-rpc-middleware/fetch')
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-ref')
const createBlockRefRewriteMiddleware = require('eth-json-rpc-middleware/block-ref-rewrite')
const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector')
const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware')
const BlockTracker = require('eth-block-tracker')
Expand All @@ -13,7 +13,7 @@ function createLocalhostClient () {
const blockTracker = new BlockTracker({ provider: blockProvider, pollingInterval: 1000 })

const networkMiddleware = mergeMiddleware([
createBlockRefMiddleware({ blockTracker }),
createBlockRefRewriteMiddleware({ blockTracker }),
createBlockTrackerInspectorMiddleware({ blockTracker }),
fetchMiddleware,
])
Expand Down
13 changes: 11 additions & 2 deletions app/scripts/inpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const log = require('loglevel')
const LocalMessageDuplexStream = require('post-message-stream')
const setupDappAutoReload = require('./lib/auto-reload.js')
const MetamaskInpageProvider = require('metamask-inpage-provider')

restoreContextAfterImports()

log.setDefaultLevel(process.env.METAMASK_DEBUG ? 'debug' : 'warn')
Expand Down Expand Up @@ -44,7 +45,15 @@ inpageProvider.enable = function (options = {}) {
})
}

window.ethereum = inpageProvider
// Work around for web3@1.0 deleting the bound `sendAsync` but not the unbound
// `sendAsync` method on the prototype, causing `this` reference issues with drizzle
const proxiedInpageProvider = new Proxy(inpageProvider, {
// straight up lie that we deleted the property so that it doesnt
// throw an error in strict mode
deleteProperty: () => true,
})

window.ethereum = proxiedInpageProvider

//
// setup web3
Expand All @@ -58,7 +67,7 @@ if (typeof window.web3 !== 'undefined') {
and try again.`)
}

var web3 = new Web3(inpageProvider)
var web3 = new Web3(proxiedInpageProvider)
web3.setProvider = function () {
log.debug('MetaMask - overrode web3.setProvider')
}
Expand Down
20 changes: 15 additions & 5 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const RpcEngine = require('json-rpc-engine')
const debounce = require('debounce')
const createEngineStream = require('json-rpc-middleware-stream/engineStream')
const createFilterMiddleware = require('eth-json-rpc-filters')
const createSubscriptionManager = require('eth-json-rpc-filters/subscriptionManager')
const createOriginMiddleware = require('./lib/createOriginMiddleware')
const createLoggerMiddleware = require('./lib/createLoggerMiddleware')
const createProviderMiddleware = require('./lib/createProviderMiddleware')
Expand Down Expand Up @@ -1246,24 +1247,33 @@ module.exports = class MetamaskController extends EventEmitter {
setupProviderConnection (outStream, origin) {
// setup json rpc engine stack
const engine = new RpcEngine()
const provider = this.provider
const blockTracker = this.blockTracker

// create filter polyfill middleware
const filterMiddleware = createFilterMiddleware({
provider: this.provider,
blockTracker: this.blockTracker,
})
const filterMiddleware = createFilterMiddleware({ provider, blockTracker })
// create subscription polyfill middleware
const subscriptionManager = createSubscriptionManager({ provider, blockTracker })
subscriptionManager.events.on('notification', (message) => engine.emit('notification', message))

// metadata
engine.push(createOriginMiddleware({ origin }))
engine.push(createLoggerMiddleware({ origin }))
// filter and subscription polyfills
engine.push(filterMiddleware)
engine.push(subscriptionManager.middleware)
// watch asset
engine.push(this.preferencesController.requestWatchAsset.bind(this.preferencesController))
// sign typed data middleware
engine.push(this.createTypedDataMiddleware('eth_signTypedData', 'V1').bind(this))
engine.push(this.createTypedDataMiddleware('eth_signTypedData_v1', 'V1').bind(this))
engine.push(this.createTypedDataMiddleware('eth_signTypedData_v3', 'V3', true).bind(this))
engine.push(createProviderMiddleware({ provider: this.provider }))
// forward to metamask primary provider
engine.push(createProviderMiddleware({ provider }))

// setup connection
const providerStream = createEngineStream({ engine })

pump(
outStream,
providerStream,
Expand Down
Loading