From b10f50fd4e021cd6d3c0a41081c3548cb4fa8903 Mon Sep 17 00:00:00 2001 From: Vitaly Krasutsky Date: Tue, 19 Nov 2019 15:55:47 +0300 Subject: [PATCH 1/5] [BRG-494] Fix activeAccount --- extension/background.js | 51 ++++++++++++++++++++++++++------ extension/inpage.js | 51 ++++++++++++++++++++++++++++---- src/constants/GlobalConstants.js | 2 +- 3 files changed, 88 insertions(+), 16 deletions(-) diff --git a/extension/background.js b/extension/background.js index 25264f11..a6e30823 100644 --- a/extension/background.js +++ b/extension/background.js @@ -254,8 +254,6 @@ const resolveAccounts = async () => { } }); return accountsRequests.splice(0, accountsRequests.length); - - } catch (e) { return { error: e.message }; } @@ -292,6 +290,7 @@ const resolveActiveAccount = async (request) => { try { const account = await getActiveAccount(); request.cb({ id: request.id, res: account }); + request.cb = function cb() {}; } catch (e) { console.log(e.message); } @@ -313,7 +312,7 @@ const updateActiveAccountInpage = async (network) => { console.log(e.message); } }); - + activeAccountRequests.splice(0, activeAccountRequests.length); return null; } catch (e) { @@ -351,11 +350,21 @@ const execGetAccountCallbacks = async () => { * @param sendResponse * @returns {boolean} */ -const onMessage = (request, sender, sendResponse) => { +const onMessage = (request, sender, responseCb) => { + + const sendResponse = (data) => { + try { + responseCb(data); + } catch (e) { + console.log('Response Callback error', e); + } + }; + const { hostname } = urlParse(sender.tab.url); const { id: tabId } = sender.tab; request = JSON.parse(JSON.stringify(request)); + const { inPageId } = request; if (!request.method || !request.appId || request.appId !== APP_ID) return false; @@ -373,6 +382,11 @@ const onMessage = (request, sender, sendResponse) => { return true; } + if (request.method === 'checkAccess') { + sendResponse({ id: request.id, response: !!processedOrigins[hostname] }); + return true; + } + if (typeof processedOrigins[hostname] !== 'boolean') { if (request.method !== 'getAccess') { @@ -399,7 +413,9 @@ const onMessage = (request, sender, sendResponse) => { try { emitter.emit('addProviderRequest', request.id, hostname); - } catch (e) { return null; } + } catch (e) { + return null; + } triggerPopup(INCOMING_CONNECTION_PATH, providerNotification); return true; @@ -457,7 +473,9 @@ const onMessage = (request, sender, sendResponse) => { request.data.accountId, request.data.message, ); - } catch (e) { return null; } + } catch (e) { + return null; + } triggerPopup(SIGN_MESSAGE_PATH, signNotification); return true; @@ -492,7 +510,9 @@ const onMessage = (request, sender, sendResponse) => { try { emitter.emit('request', id, operations); - } catch (e) { return null; } + } catch (e) { + return null; + } notificationManager.getPopup() .then((popup) => { @@ -516,16 +536,19 @@ const onMessage = (request, sender, sendResponse) => { } } else if (request.method === 'getActiveAccount') { - const tabIndex = activeAccountRequests.findIndex(({ tabId: reqTabId }) => tabId === reqTabId); + const tabIndex = activeAccountRequests + .findIndex(({ inPageId: reqInPageId }) => inPageId === reqInPageId); const req = { - id: request.id, cb: sendResponse, tabId, + id: request.id, cb: sendResponse, inPageId, }; if (tabIndex === -1) { resolveActiveAccount(req); activeAccountRequests.push(req); return true; } + activeAccountRequests[tabIndex] = req; + return true; } @@ -750,6 +773,7 @@ export const onSwitchNetwork = async (network) => { console.warn('Switch network callback error', error); } }); + networkSubscribers.splice(0, networkSubscribers.length); } }; @@ -765,6 +789,7 @@ const onSwitchActiveAccount = (res) => { console.warn('Switch account callback error', error); } }); + activeAccountSubscribers.splice(0, activeAccountSubscribers.length); }; /** @@ -905,6 +930,14 @@ window.getSignMessageMap = () => signMessageRequests.reduce((map, { }, {}); extensionizer.runtime.onMessage.addListener(onMessage); +extensionizer.tabs.onRemoved.addListener((tabEvent) => { + console.log('tabEvent onRemoved', tabEvent); +}); + +extensionizer.tabs.onUpdated.addListener((tabEvent) => { + console.log('tabEvent onUpdated', tabEvent); +}); + crypto.on('unlocked', onPinUnlock); crypto.on('locked', onLock); diff --git a/extension/inpage.js b/extension/inpage.js index c96ff75f..d08c1552 100644 --- a/extension/inpage.js +++ b/extension/inpage.js @@ -19,6 +19,8 @@ const accountChangedSubscribers = []; let activeAccount = null; +const INPAGE_ID = IdHelper.getId(); + /** * network subscription * @@ -137,9 +139,39 @@ const subscribeAccountChanged = (subscriberCb) => { throw new Error('Is not a function'); } - accountChangedSubscribers.push(subscriberCb); + const id = IdHelper.getId(); + + const result = new Promise((resolve, reject) => { + + const callback = ({ data }) => { + + if (data.error) { + reject(data.error); + return; + } + + if (data.response) { + + accountChangedSubscribers.push(subscriberCb); + + resolve(activeAccount); + subscriberCb(activeAccount); + return; + } + + reject(new Error('No access')); + }; + + requestQueue.push({ id, cb: callback }); + + window.postMessage({ + method: 'checkAccess', target: 'content', appId: APP_ID, id, + }, '*'); + + }); + + return result; - subscriberCb(activeAccount); }; @@ -242,6 +274,7 @@ echojslib.echo.connect = (url, params) => { * @param event */ const onMessage = (event) => { + const { id, target, appId } = event.data; if (!id || target !== 'inpage' || !appId || appId !== APP_ID) return; @@ -249,7 +282,9 @@ const onMessage = (event) => { const requestIndex = requestQueue.findIndex(({ id: requestId }) => requestId === id); if (requestIndex === -1) return; - requestQueue.splice(requestIndex, 1)[0].cb(event); + const request = requestQueue.splice(requestIndex, 1); + request[0].cb(event); + }; @@ -346,11 +381,13 @@ const requestAccount = () => backgroundRequest('requestAccount'); * @returns {undefined} */ const loadActiveAccount = () => { - const id = IdHelper.getId(); const cb = ({ data }) => { + + const id = IdHelper.getId(); + window.postMessage({ - method: 'getActiveAccount', id, target: 'content', appId: APP_ID, + method: 'getActiveAccount', id, target: 'content', appId: APP_ID, inPageId: INPAGE_ID, }, '*'); const error = data.error || (data.res && data.res.error); @@ -364,9 +401,11 @@ const loadActiveAccount = () => { notifyAccountChanged(activeAccount); }; + const id = IdHelper.getId(); + requestQueue.push({ id, cb }); window.postMessage({ - method: 'getActiveAccount', id, target: 'content', appId: APP_ID, + method: 'getActiveAccount', id, target: 'content', appId: APP_ID, inPageId: INPAGE_ID, }, '*'); }; diff --git a/src/constants/GlobalConstants.js b/src/constants/GlobalConstants.js index c50ebe5f..2d41153e 100644 --- a/src/constants/GlobalConstants.js +++ b/src/constants/GlobalConstants.js @@ -156,7 +156,7 @@ export const ALGORITHM_IV_BYTES_LENGTH = 16; export const STORE = 'keyval'; export const PATTERN_ID_MESSAGE = '*'; -export const LENGTH_ID_MESSAGE = 10; +export const LENGTH_ID_MESSAGE = 20; export const ERC20_HASHES = { 'totalSupply()': '18160ddd', From d7df655f5f1a37b4fc4e937d729182ffc94fb713 Mon Sep 17 00:00:00 2001 From: Vitaly Krasutsky Date: Tue, 19 Nov 2019 17:58:21 +0300 Subject: [PATCH 2/5] [BRG-494] Fix active account errors --- extension/background.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/extension/background.js b/extension/background.js index a6e30823..f7a014ce 100644 --- a/extension/background.js +++ b/extension/background.js @@ -350,21 +350,12 @@ const execGetAccountCallbacks = async () => { * @param sendResponse * @returns {boolean} */ -const onMessage = (request, sender, responseCb) => { - - const sendResponse = (data) => { - try { - responseCb(data); - } catch (e) { - console.log('Response Callback error', e); - } - }; +const onMessage = (request, sender, sendResponse) => { const { hostname } = urlParse(sender.tab.url); const { id: tabId } = sender.tab; request = JSON.parse(JSON.stringify(request)); - const { inPageId } = request; if (!request.method || !request.appId || request.appId !== APP_ID) return false; @@ -536,6 +527,7 @@ const onMessage = (request, sender, responseCb) => { } } else if (request.method === 'getActiveAccount') { + const { inPageId } = request; const tabIndex = activeAccountRequests .findIndex(({ inPageId: reqInPageId }) => inPageId === reqInPageId); const req = { @@ -930,14 +922,6 @@ window.getSignMessageMap = () => signMessageRequests.reduce((map, { }, {}); extensionizer.runtime.onMessage.addListener(onMessage); -extensionizer.tabs.onRemoved.addListener((tabEvent) => { - console.log('tabEvent onRemoved', tabEvent); -}); - -extensionizer.tabs.onUpdated.addListener((tabEvent) => { - console.log('tabEvent onUpdated', tabEvent); -}); - crypto.on('unlocked', onPinUnlock); crypto.on('locked', onLock); From cae5d072b1311e93eb65365e80c609cfddbf0b72 Mon Sep 17 00:00:00 2001 From: Vitaly Krasutsky Date: Tue, 19 Nov 2019 18:04:53 +0300 Subject: [PATCH 3/5] Up version --- config/manifest.json | 2 +- extension/contentscript.js | 1 + package.json | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/config/manifest.json b/config/manifest.json index 54f2740e..c0257ff4 100644 --- a/config/manifest.json +++ b/config/manifest.json @@ -18,7 +18,7 @@ "96": "images/96.png", "128": "images/128.png" }, - "version": "1.13.2", + "version": "1.14.0", "background": { "scripts": [ "background.js" diff --git a/extension/contentscript.js b/extension/contentscript.js index 6e8378ff..baac121f 100644 --- a/extension/contentscript.js +++ b/extension/contentscript.js @@ -2,6 +2,7 @@ const extensionizer = require('./extensionizer'); const { APP_ID } = require('../src/constants/GlobalConstants'); + const getAccessRequest = {}; /** diff --git a/package.json b/package.json index 77c885f4..167b0541 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "echo-bridge", "description": "The bridge between your web site and the Echo", - "version": "1.13.2", + "version": "1.14.0", "scripts": { "start": "cross-env NODE_ENV=local webpack-dev-server --mode development --progress --colors --history-api-fallback --hot --inline --port 8081", "build": "cross-env NODE_ENV=production webpack --progress --display-error-details --colors --mode production", From 2d2b5efaf7075b439bf0d3ab5e422ca2519c07e4 Mon Sep 17 00:00:00 2001 From: Vitaly Krasutsky Date: Tue, 19 Nov 2019 18:11:34 +0300 Subject: [PATCH 4/5] Fix error message --- extension/inpage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/inpage.js b/extension/inpage.js index d08c1552..eb30bb8f 100644 --- a/extension/inpage.js +++ b/extension/inpage.js @@ -136,7 +136,7 @@ const subscribeSwitchNetwork = (subscriberCb) => { const subscribeAccountChanged = (subscriberCb) => { if (!lodash.isFunction(subscriberCb)) { - throw new Error('Is not a function'); + throw new Error('The first argument is not a function'); } const id = IdHelper.getId(); From 3e2cd989c784c65e542546856d33272fb273385c Mon Sep 17 00:00:00 2001 From: Vitaly Krasutsky Date: Tue, 19 Nov 2019 18:33:57 +0300 Subject: [PATCH 5/5] Fix double notification --- extension/inpage.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/extension/inpage.js b/extension/inpage.js index eb30bb8f..99207b23 100644 --- a/extension/inpage.js +++ b/extension/inpage.js @@ -183,6 +183,7 @@ const notifyAccountChanged = (accountId) => { accountChangedSubscribers.forEach((cb) => { cb(accountId); }); + }; /** @@ -392,13 +393,18 @@ const loadActiveAccount = () => { const error = data.error || (data.res && data.res.error); + const prevAccount = activeAccount; + if (error) { activeAccount = null; } else { activeAccount = data.res; requestQueue.push({ id, cb }); } - notifyAccountChanged(activeAccount); + if (prevAccount !== activeAccount) { + notifyAccountChanged(activeAccount); + } + }; const id = IdHelper.getId();