@ -4,6 +4,7 @@ import LocalMessageDuplexStream from 'post-message-stream'
import ObjectMultiplex from 'obj-multiplex'
import extension from 'extensionizer'
import PortStream from 'extension-port-stream'
import { obj as createThoughStream } from 'through2'
// These require calls need to use require to be statically recognized by browserify
const fs = require ( 'fs' )
@ -20,6 +21,12 @@ const CONTENT_SCRIPT = 'metamask-contentscript'
const INPAGE = 'metamask-inpage'
const PROVIDER = 'metamask-provider'
// TODO:LegacyProvider: Delete
const LEGACY _CONTENT _SCRIPT = 'contentscript'
const LEGACY _INPAGE = 'inpage'
const LEGACY _PROVIDER = 'provider'
const LEGACY _PUBLIC _CONFIG = 'publicConfig'
if ( shouldInjectProvider ( ) ) {
injectScript ( inpageBundle )
setupStreams ( )
@ -63,6 +70,7 @@ async function setupStreams() {
pageMux . setMaxListeners ( 25 )
const extensionMux = new ObjectMultiplex ( )
extensionMux . setMaxListeners ( 25 )
extensionMux . ignoreStream ( LEGACY _PUBLIC _CONFIG ) // TODO:LegacyProvider: Delete
pump ( pageMux , pageStream , pageMux , ( err ) =>
logStreamDisconnectWarning ( 'MetaMask Inpage Multiplex' , err ) ,
@ -78,6 +86,44 @@ async function setupStreams() {
// connect "phishing" channel to warning system
const phishingStream = extensionMux . createStream ( 'phishing' )
phishingStream . once ( 'data' , redirectToPhishingWarning )
// TODO:LegacyProvider: Delete
// handle legacy provider
const legacyPageStream = new LocalMessageDuplexStream ( {
name : LEGACY _CONTENT _SCRIPT ,
target : LEGACY _INPAGE ,
} )
const legacyPageMux = new ObjectMultiplex ( )
legacyPageMux . setMaxListeners ( 25 )
const legacyExtensionMux = new ObjectMultiplex ( )
legacyExtensionMux . setMaxListeners ( 25 )
pump ( legacyPageMux , legacyPageStream , legacyPageMux , ( err ) =>
logStreamDisconnectWarning ( 'MetaMask Legacy Inpage Multiplex' , err ) ,
)
pump (
legacyExtensionMux ,
extensionStream ,
getNotificationTransformStream ( ) ,
legacyExtensionMux ,
( err ) => {
logStreamDisconnectWarning ( 'MetaMask Background Legacy Multiplex' , err )
notifyInpageOfStreamFailure ( )
} ,
)
forwardNamedTrafficBetweenMuxes (
LEGACY _PROVIDER ,
PROVIDER ,
legacyPageMux ,
legacyExtensionMux ,
)
forwardTrafficBetweenMuxes (
LEGACY _PUBLIC _CONFIG ,
legacyPageMux ,
legacyExtensionMux ,
)
}
function forwardTrafficBetweenMuxes ( channelName , muxA , muxB ) {
@ -91,6 +137,37 @@ function forwardTrafficBetweenMuxes(channelName, muxA, muxB) {
)
}
// TODO:LegacyProvider: Delete
function forwardNamedTrafficBetweenMuxes (
channelAName ,
channelBName ,
muxA ,
muxB ,
) {
const channelA = muxA . createStream ( channelAName )
const channelB = muxB . createStream ( channelBName )
pump ( channelA , channelB , channelA , ( error ) =>
console . debug (
` MetaMask: Muxed traffic between channels " ${ channelAName } " and " ${ channelBName } " failed. ` ,
error ,
) ,
)
}
// TODO:LegacyProvider: Delete
function getNotificationTransformStream ( ) {
return createThoughStream ( ( chunk , _ , cb ) => {
if ( chunk ? . name === PROVIDER ) {
if ( chunk . data ? . method === 'metamask_accountsChanged' ) {
chunk . data . method = 'wallet_accountsChanged'
chunk . data . result = chunk . data . params
delete chunk . data . params
}
}
cb ( null , chunk )
} )
}
/ * *
* Error handler for page to extension stream disconnections
*