|
| 1 | +// Based of: https://github.com/MetaMask/mobile-provider/blob/main/src/inpage/index.js |
| 2 | + |
| 3 | +import { initializeProvider } from '@metamask/inpage-provider'; |
| 4 | +import ObjectMultiplex from '@metamask/object-multiplex'; |
| 5 | +import pump from 'pump'; |
| 6 | + |
| 7 | +import MobilePortStream from './MetaMask/MobilePortStream'; |
| 8 | +import ReactNativePostMessageStream from './MetaMask/ReactNativePostMessageStream'; |
| 9 | + |
| 10 | +const INPAGE = 'metamask-inpage'; |
| 11 | +const CONTENT_SCRIPT = 'metamask-contentscript'; |
| 12 | +const PROVIDER = 'metamask-provider'; |
| 13 | + |
| 14 | +export const injectMobile = () => { |
| 15 | + // Setup stream for content script communication |
| 16 | + const metamaskStream = new ReactNativePostMessageStream({ |
| 17 | + name: INPAGE, |
| 18 | + target: CONTENT_SCRIPT |
| 19 | + }); |
| 20 | + |
| 21 | + // Initialize provider object (window.ethereum) |
| 22 | + initializeProvider({ |
| 23 | + connectionStream: metamaskStream, |
| 24 | + shouldSendMetadata: false |
| 25 | + }); |
| 26 | + |
| 27 | + setupProviderStreams(); |
| 28 | +}; |
| 29 | + |
| 30 | +// Functions |
| 31 | + |
| 32 | +/** |
| 33 | + * Setup function called from content script after the DOM is ready. |
| 34 | + */ |
| 35 | +function setupProviderStreams() { |
| 36 | + // the transport-specific streams for communication between inpage and background |
| 37 | + const pageStream = new ReactNativePostMessageStream({ |
| 38 | + name: CONTENT_SCRIPT, |
| 39 | + target: INPAGE |
| 40 | + }); |
| 41 | + |
| 42 | + const appStream = new MobilePortStream({ |
| 43 | + name: CONTENT_SCRIPT |
| 44 | + }); |
| 45 | + |
| 46 | + // create and connect channel muxes |
| 47 | + // so we can handle the channels individually |
| 48 | + const pageMux = new ObjectMultiplex(); |
| 49 | + pageMux.setMaxListeners(25); |
| 50 | + const appMux = new ObjectMultiplex(); |
| 51 | + appMux.setMaxListeners(25); |
| 52 | + |
| 53 | + pump(pageMux, pageStream, pageMux, (err) => |
| 54 | + logStreamDisconnectWarning('MetaMask Inpage Multiplex', err) |
| 55 | + ); |
| 56 | + pump(appMux, appStream, appMux, (err) => { |
| 57 | + logStreamDisconnectWarning('MetaMask Background Multiplex', err); |
| 58 | + notifyProviderOfStreamFailure(); |
| 59 | + }); |
| 60 | + |
| 61 | + // forward communication across inpage-background for these channels only |
| 62 | + forwardTrafficBetweenMuxes(PROVIDER, pageMux, appMux); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Set up two-way communication between muxes for a single, named channel. |
| 67 | + * |
| 68 | + * @param {string} channelName - The name of the channel. |
| 69 | + * @param {ObjectMultiplex} muxA - The first mux. |
| 70 | + * @param {ObjectMultiplex} muxB - The second mux. |
| 71 | + */ |
| 72 | +function forwardTrafficBetweenMuxes(channelName, muxA, muxB) { |
| 73 | + const channelA = muxA.createStream(channelName); |
| 74 | + const channelB = muxB.createStream(channelName); |
| 75 | + pump(channelA, channelB, channelA, (err) => |
| 76 | + logStreamDisconnectWarning(`MetaMask muxed traffic for channel "${channelName}" failed.`, err) |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Error handler for page to extension stream disconnections |
| 82 | + * |
| 83 | + * @param {string} remoteLabel - Remote stream name |
| 84 | + * @param {Error} err - Stream connection error |
| 85 | + */ |
| 86 | +function logStreamDisconnectWarning(remoteLabel, err) { |
| 87 | + let warningMsg = `MetamaskContentscript - lost connection to ${remoteLabel}`; |
| 88 | + if (err) { |
| 89 | + warningMsg += `\n${err.stack}`; |
| 90 | + } |
| 91 | + console.warn(warningMsg); |
| 92 | + console.error(err); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * This function must ONLY be called in pump destruction/close callbacks. |
| 97 | + * Notifies the inpage context that streams have failed, via window.postMessage. |
| 98 | + * Relies on @metamask/object-multiplex and post-message-stream implementation details. |
| 99 | + */ |
| 100 | +function notifyProviderOfStreamFailure() { |
| 101 | + window.postMessage( |
| 102 | + { |
| 103 | + target: INPAGE, // the post-message-stream "target" |
| 104 | + data: { |
| 105 | + // this object gets passed to object-multiplex |
| 106 | + name: PROVIDER, // the object-multiplex channel name |
| 107 | + data: { |
| 108 | + jsonrpc: '2.0', |
| 109 | + method: 'METAMASK_STREAM_FAILURE' |
| 110 | + } |
| 111 | + } |
| 112 | + }, |
| 113 | + window.location.origin |
| 114 | + ); |
| 115 | +} |
0 commit comments