From ea882d4588141db6d5e78971c17cea61a5bb3558 Mon Sep 17 00:00:00 2001 From: Jyoti Puri Date: Tue, 29 Nov 2022 22:33:13 +0530 Subject: [PATCH] DAPP action replay improvements (#16250) --- app/scripts/contentscript.js | 37 ++++++++++++++++++++++++++++++++++++ package.json | 2 +- yarn.lock | 18 +++++++++--------- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 91ec8f633..6d979136c 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -297,9 +297,14 @@ const setupPageStreams = () => { pageChannel = pageMux.createStream(PROVIDER); }; +// The field below is used to ensure that replay is done only once for each restart. +let METAMASK_EXTENSION_CONNECT_SENT = false; + const setupExtensionStreams = () => { + METAMASK_EXTENSION_CONNECT_SENT = true; extensionPort = browser.runtime.connect({ name: CONTENT_SCRIPT }); extensionStream = new PortStream(extensionPort); + extensionStream.on('data', extensionStreamMessageListener); // create and connect channel muxers // so we can handle the channels individually @@ -522,6 +527,38 @@ function logStreamDisconnectWarning(remoteLabel, error) { ); } +/** + * The function notifies inpage when the extension stream connection is ready. When the + * 'metamask_chainChanged' method is received from the extension, it implies that the + * background state is completely initialized and it is ready to process method calls. + * This is used as a notification to replay any pending messages in MV3. + * + * @param {object} msg - instance of message received + */ +function extensionStreamMessageListener(msg) { + if ( + METAMASK_EXTENSION_CONNECT_SENT && + isManifestV3 && + msg.data.method === 'metamask_chainChanged' + ) { + METAMASK_EXTENSION_CONNECT_SENT = false; + window.postMessage( + { + target: INPAGE, // the post-message-stream "target" + data: { + // this object gets passed to obj-multiplex + name: PROVIDER, // the obj-multiplex channel name + data: { + jsonrpc: '2.0', + method: 'METAMASK_EXTENSION_CONNECT_CAN_RETRY', + }, + }, + }, + window.location.origin, + ); + } +} + /** * This function must ONLY be called in pump destruction/close callbacks. * Notifies the inpage context that streams have failed, via window.postMessage. diff --git a/package.json b/package.json index 9b507c02f..618c0adc9 100644 --- a/package.json +++ b/package.json @@ -130,7 +130,7 @@ "@metamask/permission-controller": "^1.0.0", "@metamask/phishing-controller": "^1.0.0", "@metamask/post-message-stream": "^6.0.0", - "@metamask/providers": "^10.0.0", + "@metamask/providers": "^10.2.1", "@metamask/rate-limit-controller": "^1.0.0", "@metamask/rpc-methods": "^0.24.1", "@metamask/slip44": "^2.1.0", diff --git a/yarn.lock b/yarn.lock index 537f1144b..ad6a702a7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3183,10 +3183,10 @@ "@metamask/base-controller" "~1.0.0" "@metamask/controller-utils" "~1.0.0" -"@metamask/providers@^10.0.0", "@metamask/providers@^10.2.0": - version "10.2.0" - resolved "https://registry.yarnpkg.com/@metamask/providers/-/providers-10.2.0.tgz#8131de667db0c55a61a150438c2a7f17b2d53615" - integrity sha512-qO3cOZZr/YJ8LLOqhR+51GGBiRknalfV/na7hwXyqZ1R/uxeLeNdqCyg+g8l3Z8JcLoEiaKGNJOEV3FFyLw8mQ== +"@metamask/providers@^10.2.0", "@metamask/providers@^10.2.1": + version "10.2.1" + resolved "https://registry.yarnpkg.com/@metamask/providers/-/providers-10.2.1.tgz#61304940adeccc7421dcda30ffd1d834273cc77b" + integrity sha512-p2TXw2a1Nb8czntDGfeIYQnk4LLVbd5vlcb3GY//lylYlKdSqp+uUTegCvxiFblRDOT68jsY8Ib1VEEzVUOolA== dependencies: "@metamask/object-multiplex" "^1.1.0" "@metamask/safe-event-emitter" "^2.0.0" @@ -3197,7 +3197,7 @@ fast-deep-equal "^2.0.1" is-stream "^2.0.0" json-rpc-engine "^6.1.0" - json-rpc-middleware-stream "^4.2.0" + json-rpc-middleware-stream "^4.2.1" pump "^3.0.0" webextension-polyfill-ts "^0.25.0" @@ -16031,10 +16031,10 @@ json-rpc-middleware-stream@^2.1.1: readable-stream "^2.3.3" safe-event-emitter "^1.0.1" -json-rpc-middleware-stream@^4.2.0: - version "4.2.0" - resolved "https://registry.yarnpkg.com/json-rpc-middleware-stream/-/json-rpc-middleware-stream-4.2.0.tgz#a235814e031a2f85cc14b213aa7d42b75527104b" - integrity sha512-vaPaFVnhozfAVx6ImO3YuSrzl6A1OCktDi9Prw6NASn2VRYfe3pzv+2QGzFHRjViztr61oHi6KC3k8cujrfK7A== +json-rpc-middleware-stream@^4.2.0, json-rpc-middleware-stream@^4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/json-rpc-middleware-stream/-/json-rpc-middleware-stream-4.2.1.tgz#e5cb8795ebfd7503c6ceaa43daaf065687cc2f22" + integrity sha512-6QKayke/8lg0nTjOpRCq4JCgRx7bVybldmloIfY21HSDV0GUevcV9i8DJNvuKTJx4KR9EDIf6HTStAnEovGUvA== dependencies: "@metamask/safe-event-emitter" "^2.0.0" readable-stream "^2.3.3"