aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/metamask-controller.js
diff options
context:
space:
mode:
authorLazaridis <info@lazaridis.com>2018-03-17 00:37:56 +0800
committerLazaridis <info@lazaridis.com>2018-03-17 00:37:56 +0800
commite1d6398b0fed39e579c0383e34236b30d3154295 (patch)
tree44e1772fe2f5b924e613203aa9ad2aff8d928268 /app/scripts/metamask-controller.js
parentbb6af25e20f723942a2b5770a8763bff3b6d6032 (diff)
downloadtangerine-wallet-browser-e1d6398b0fed39e579c0383e34236b30d3154295.tar
tangerine-wallet-browser-e1d6398b0fed39e579c0383e34236b30d3154295.tar.gz
tangerine-wallet-browser-e1d6398b0fed39e579c0383e34236b30d3154295.tar.bz2
tangerine-wallet-browser-e1d6398b0fed39e579c0383e34236b30d3154295.tar.lz
tangerine-wallet-browser-e1d6398b0fed39e579c0383e34236b30d3154295.tar.xz
tangerine-wallet-browser-e1d6398b0fed39e579c0383e34236b30d3154295.tar.zst
tangerine-wallet-browser-e1d6398b0fed39e579c0383e34236b30d3154295.zip
moves setup-related code towads end of file, re #3568
Diffstat (limited to 'app/scripts/metamask-controller.js')
-rw-r--r--app/scripts/metamask-controller.js239
1 files changed, 124 insertions, 115 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 953e22fc7..18d71874a 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -290,6 +290,9 @@ module.exports = class MetamaskController extends EventEmitter {
return publicConfigStore
}
+//=============================================================================
+// EXPOSED TO THE UI SUBSYSTEM
+//=============================================================================
/**
* The metamask-state of the various controllers, made available to the UI
@@ -415,122 +418,7 @@ module.exports = class MetamaskController extends EventEmitter {
}
}
- setupUntrustedCommunication (connectionStream, originDomain) {
- // Check if new connection is blacklisted
- if (this.blacklistController.checkForPhishing(originDomain)) {
- log.debug('MetaMask - sending phishing warning for', originDomain)
- this.sendPhishingWarning(connectionStream, originDomain)
- return
- }
-
- // setup multiplexing
- const mux = setupMultiplex(connectionStream)
- // connect features
- this.setupProviderConnection(mux.createStream('provider'), originDomain)
- this.setupPublicConfig(mux.createStream('publicConfig'))
- }
-
- setupTrustedCommunication (connectionStream, originDomain) {
- // setup multiplexing
- const mux = setupMultiplex(connectionStream)
- // connect features
- this.setupControllerConnection(mux.createStream('controller'))
- this.setupProviderConnection(mux.createStream('provider'), originDomain)
- }
-
- sendPhishingWarning (connectionStream, hostname) {
- const mux = setupMultiplex(connectionStream)
- const phishingStream = mux.createStream('phishing')
- phishingStream.write({ hostname })
- }
-
- setupControllerConnection (outStream) {
- const api = this.getApi()
- const dnode = Dnode(api)
- pump(
- outStream,
- dnode,
- outStream,
- (err) => {
- if (err) log.error(err)
- }
- )
- dnode.on('remote', (remote) => {
- // push updates to popup
- const sendUpdate = remote.sendUpdate.bind(remote)
- this.on('update', sendUpdate)
- })
- }
-
- setupProviderConnection (outStream, origin) {
- // setup json rpc engine stack
- const engine = new RpcEngine()
- // create filter polyfill middleware
- const filterMiddleware = createFilterMiddleware({
- provider: this.provider,
- blockTracker: this.provider._blockTracker,
- })
-
- engine.push(createOriginMiddleware({ origin }))
- engine.push(createLoggerMiddleware({ origin }))
- engine.push(filterMiddleware)
- engine.push(createProviderMiddleware({ provider: this.provider }))
-
- // setup connection
- const providerStream = createEngineStream({ engine })
- pump(
- outStream,
- providerStream,
- outStream,
- (err) => {
- // cleanup filter polyfill middleware
- filterMiddleware.destroy()
- if (err) log.error(err)
- }
- )
- }
-
- setupPublicConfig (outStream) {
- pump(
- asStream(this.publicConfigStore),
- outStream,
- (err) => {
- if (err) log.error(err)
- }
- )
- }
-
- privateSendUpdate () {
- this.emit('update', this.getState())
- }
-
- getGasPrice () {
- const { recentBlocksController } = this
- const { recentBlocks } = recentBlocksController.store.getState()
-
- // Return 1 gwei if no blocks have been observed:
- if (recentBlocks.length === 0) {
- return '0x' + GWEI_BN.toString(16)
- }
-
- const lowestPrices = recentBlocks.map((block) => {
- if (!block.gasPrices || block.gasPrices.length < 1) {
- return GWEI_BN
- }
- return block.gasPrices
- .map(hexPrefix => hexPrefix.substr(2))
- .map(hex => new BN(hex, 16))
- .sort((a, b) => {
- return a.gt(b) ? 1 : -1
- })[0]
- })
- .map(number => number.div(GWEI_BN).toNumber())
-
- const percentileNum = percentile(50, lowestPrices)
- const percentileNumBn = new BN(percentileNum)
- return '0x' + percentileNumBn.mul(GWEI_BN).toString(16)
- }
//=============================================================================
// VAULT / KEYRING RELATED METHODS
@@ -972,6 +860,127 @@ module.exports = class MetamaskController extends EventEmitter {
}
//=============================================================================
+// SETUP
+//=============================================================================
+
+ setupUntrustedCommunication (connectionStream, originDomain) {
+ // Check if new connection is blacklisted
+ if (this.blacklistController.checkForPhishing(originDomain)) {
+ log.debug('MetaMask - sending phishing warning for', originDomain)
+ this.sendPhishingWarning(connectionStream, originDomain)
+ return
+ }
+
+ // setup multiplexing
+ const mux = setupMultiplex(connectionStream)
+ // connect features
+ this.setupProviderConnection(mux.createStream('provider'), originDomain)
+ this.setupPublicConfig(mux.createStream('publicConfig'))
+ }
+
+ setupTrustedCommunication (connectionStream, originDomain) {
+ // setup multiplexing
+ const mux = setupMultiplex(connectionStream)
+ // connect features
+ this.setupControllerConnection(mux.createStream('controller'))
+ this.setupProviderConnection(mux.createStream('provider'), originDomain)
+ }
+
+ sendPhishingWarning (connectionStream, hostname) {
+ const mux = setupMultiplex(connectionStream)
+ const phishingStream = mux.createStream('phishing')
+ phishingStream.write({ hostname })
+ }
+
+ setupControllerConnection (outStream) {
+ const api = this.getApi()
+ const dnode = Dnode(api)
+ pump(
+ outStream,
+ dnode,
+ outStream,
+ (err) => {
+ if (err) log.error(err)
+ }
+ )
+ dnode.on('remote', (remote) => {
+ // push updates to popup
+ const sendUpdate = remote.sendUpdate.bind(remote)
+ this.on('update', sendUpdate)
+ })
+ }
+
+ setupProviderConnection (outStream, origin) {
+ // setup json rpc engine stack
+ const engine = new RpcEngine()
+
+ // create filter polyfill middleware
+ const filterMiddleware = createFilterMiddleware({
+ provider: this.provider,
+ blockTracker: this.provider._blockTracker,
+ })
+
+ engine.push(createOriginMiddleware({ origin }))
+ engine.push(createLoggerMiddleware({ origin }))
+ engine.push(filterMiddleware)
+ engine.push(createProviderMiddleware({ provider: this.provider }))
+
+ // setup connection
+ const providerStream = createEngineStream({ engine })
+ pump(
+ outStream,
+ providerStream,
+ outStream,
+ (err) => {
+ // cleanup filter polyfill middleware
+ filterMiddleware.destroy()
+ if (err) log.error(err)
+ }
+ )
+ }
+
+ setupPublicConfig (outStream) {
+ pump(
+ asStream(this.publicConfigStore),
+ outStream,
+ (err) => {
+ if (err) log.error(err)
+ }
+ )
+ }
+
+ privateSendUpdate () {
+ this.emit('update', this.getState())
+ }
+
+ getGasPrice () {
+ const { recentBlocksController } = this
+ const { recentBlocks } = recentBlocksController.store.getState()
+
+ // Return 1 gwei if no blocks have been observed:
+ if (recentBlocks.length === 0) {
+ return '0x' + GWEI_BN.toString(16)
+ }
+
+ const lowestPrices = recentBlocks.map((block) => {
+ if (!block.gasPrices || block.gasPrices.length < 1) {
+ return GWEI_BN
+ }
+ return block.gasPrices
+ .map(hexPrefix => hexPrefix.substr(2))
+ .map(hex => new BN(hex, 16))
+ .sort((a, b) => {
+ return a.gt(b) ? 1 : -1
+ })[0]
+ })
+ .map(number => number.div(GWEI_BN).toNumber())
+
+ const percentileNum = percentile(50, lowestPrices)
+ const percentileNumBn = new BN(percentileNum)
+ return '0x' + percentileNumBn.mul(GWEI_BN).toString(16)
+ }
+
+//=============================================================================
// CONFIG
//=============================================================================