From 20b7f34ad9704064fca737db6f868d7e957aee02 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Thu, 12 Jan 2023 23:32:18 +0530 Subject: [PATCH 1/7] chore: clean up unused permissions --- src/manifest.json | 6 +----- src/manifest.v3.json | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index ebfa64f..b8caec5 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -25,16 +25,12 @@ "permissions": [ "tabs", "activeTab", - "contextMenus", "storage", "unlimitedStorage", "clipboardRead", "clipboardWrite", - "idle", "http://*/*", - "https://*/*", - "webRequest", - "webRequestBlocking" + "https://*/*" ], "commands": { "_execute_browser_action": { diff --git a/src/manifest.v3.json b/src/manifest.v3.json index e8f8307..6cf8e17 100644 --- a/src/manifest.v3.json +++ b/src/manifest.v3.json @@ -24,15 +24,12 @@ "permissions": [ "tabs", "activeTab", - "contextMenus", "storage", "unlimitedStorage", "clipboardRead", "clipboardWrite", - "idle", "http://*/*", - "https://*/*", - "webRequest" + "https://*/*" ], "commands": { "_execute_browser_action": { From 4680dc7d8eba771d6cbb3f744eb4a6238d681f61 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Thu, 12 Jan 2023 23:32:34 +0530 Subject: [PATCH 2/7] feat: add util to return the manifest version --- src/utils/browser.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/utils/browser.ts b/src/utils/browser.ts index cd1242a..30d75fa 100644 --- a/src/utils/browser.ts +++ b/src/utils/browser.ts @@ -19,3 +19,12 @@ export const tabsQuery = ( } }) } + +/** + * Returns the manifest version + * + * @returns {2 | 3} + */ +export const manifestVersion = () => { + return chrome.runtime.getManifest().manifest_version; +} From 4c8c3b58a96361b41369ce4aa676652cf30a9617 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Thu, 12 Jan 2023 23:32:56 +0530 Subject: [PATCH 3/7] refactor: handle session persistence for manifest version 3 --- src/components/App/Layout/Sidebar.tsx | 33 ++++-- src/components/Authentication/Keyroute.tsx | 131 ++++++++++++++------- 2 files changed, 110 insertions(+), 54 deletions(-) diff --git a/src/components/App/Layout/Sidebar.tsx b/src/components/App/Layout/Sidebar.tsx index cf2a261..ec9ed0c 100644 --- a/src/components/App/Layout/Sidebar.tsx +++ b/src/components/App/Layout/Sidebar.tsx @@ -19,6 +19,7 @@ import { useAppSelector, useAppDispatch } from '../../../store/hooks' import { removeSession } from '../../../store/reducers/auth' import { View } from '../../../types' import { Logo } from '../../../Logo' +import { manifestVersion } from '../../../utils/browser' interface SidebarProps { toggleSidebar?: Function @@ -30,18 +31,28 @@ export const Sidebar = (props: SidebarProps) => { const dispatch = useAppDispatch() const endSession = () => { - try { - chrome.runtime.sendMessage( - { cmd: 'removeSession' }, - function (response) { - console.log( - `message from background: ${JSON.stringify(response)}` - ) - } - ) - } catch (error) { - console.log('Error syncing with background script:', error) + if (manifestVersion() === 2) { + try { + chrome.runtime.sendMessage( + { cmd: 'removeSession' }, + function (response) { + console.log( + `message from background: ${JSON.stringify(response)}` + ) + } + ) + } catch (error) { + console.log('Error syncing with background script:', error) + } } + else { + try { + chrome.storage.session.clear() + } catch(error) { + console.log('Error clearing session storage', error) + } + } + dispatch(removeSession()) } diff --git a/src/components/Authentication/Keyroute.tsx b/src/components/Authentication/Keyroute.tsx index 445ddd4..c9619d9 100644 --- a/src/components/Authentication/Keyroute.tsx +++ b/src/components/Authentication/Keyroute.tsx @@ -30,6 +30,7 @@ import { addSession } from '../../store/reducers/auth' import { FiAlertTriangle, FiRefreshCcw } from 'react-icons/fi' import { PopoutButton } from '../Common/PopoutButton' import { Logo } from '../../Logo' +import { manifestVersion } from '../../utils/browser' export const Keyroute = () => { const QR_REFRESH_INTERVAL = 60 //seconds @@ -83,32 +84,62 @@ export const Keyroute = () => { * Sends a 'getSession' message to the background script, and restores the user session if possible */ useEffect(() => { - try { - chrome.runtime.sendMessage( - { cmd: 'getSession' }, - function (response) { - if (response.result === 'success') { - const decodedSessionData = { - email: response.message.email, - token: response.message.token, - keyring: { - publicKey: hex2buf( - response.message.keyring.publicKey - ), - privateKey: hex2buf( - response.message.keyring.privateKey - ), - symmetricKey: hex2buf( - response.message.keyring.symmetricKey - ), - }, + const getSessionFromBackgroundScript = () => { + try { + chrome.runtime.sendMessage( + { cmd: 'getSession' }, + function (response) { + if (response.result === 'success') { + const decodedSessionData = { + email: response.message.email, + token: response.message.token, + keyring: { + publicKey: hex2buf( + response.message.keyring.publicKey + ), + privateKey: hex2buf( + response.message.keyring.privateKey + ), + symmetricKey: hex2buf( + response.message.keyring.symmetricKey + ), + }, + } + dispatch(addSession(decodedSessionData)) } - dispatch(addSession(decodedSessionData)) } + ) + } catch (error) { + console.log('Error syncing with background script:', error) + } + } + + const getSessionFromStorage = async () => { + try { + const storedData = await chrome.storage.session.get('session') + if (storedData !== undefined && storedData !== null) { + const { session } = storedData + const decodedSessionData = { + email: session.email, + token: session.token, + keyring: { + publicKey: hex2buf(session.keyring.publicKey), + privateKey: hex2buf(session.keyring.privateKey), + symmetricKey: hex2buf( + session.keyring.symmetricKey + ), + }, + } + dispatch(addSession(decodedSessionData)) } - ) - } catch (error) { - console.log('Error syncing with background script:', error) + } catch (error) { + console.log('Error getting stored session', error) + } + } + if (manifestVersion() === 2) { + getSessionFromBackgroundScript() + } else { + getSessionFromStorage() } }, [dispatch]) @@ -153,7 +184,7 @@ export const Keyroute = () => { useEffect(() => { const initSession = async (payload: KeyroutePayload) => { if (keypair) { - keyreouteLogin(payload, keypair).then((session) => { + keyreouteLogin(payload, keypair).then(async (session) => { if (session.email !== undefined) { toast({ title: 'Logged in!', @@ -182,25 +213,39 @@ export const Keyroute = () => { ), }, } - try { - chrome.runtime.sendMessage( - { - message: encodedSessionData, - cmd: 'saveSession', - }, - function (response) { - console.log( - `message from background: ${JSON.stringify( - response - )}` - ) - } - ) - } catch (error) { - console.log( - 'Error syncing with background script:', - error - ) + if (manifestVersion() === 2) { + try { + chrome.runtime.sendMessage( + { + message: encodedSessionData, + cmd: 'saveSession', + }, + function (response) { + console.log( + `message from background: ${JSON.stringify( + response + )}` + ) + } + ) + } catch (error) { + console.log( + 'Error syncing with background script:', + error + ) + } + } else { + try { + await chrome.storage.session.set({ + session: encodedSessionData, + }) + console.log('saved session to storage') + } catch (error) { + console.log( + 'Error saving session to storage', + error + ) + } } dispatch(addSession(session)) From 7951309db04676ed0ead67ecb20f940356aa95c1 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Thu, 12 Jan 2023 23:39:37 +0530 Subject: [PATCH 4/7] chore: bump version --- package.json | 2 +- src/manifest.json | 2 +- src/manifest.v3.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 6440de6..a20fd84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "keyspace-plugin", - "version": "1.0.2", + "version": "1.0.3", "private": false, "dependencies": { "@chakra-ui/icons": "^2.0.0", diff --git a/src/manifest.json b/src/manifest.json index b8caec5..9f42366 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { "name" :"Keyspace", "description": "Keyspace wallet browser add-on", - "version": "1.0.2", + "version": "1.0.3", "manifest_version": 2, "icons": { "16": "./images/favicon-16x16.png", diff --git a/src/manifest.v3.json b/src/manifest.v3.json index 6cf8e17..acc564c 100644 --- a/src/manifest.v3.json +++ b/src/manifest.v3.json @@ -1,7 +1,7 @@ { "name" :"Keyspace", "description": "Keyspace wallet browser add-on", - "version": "1.0.2", + "version": "1.0.3", "manifest_version": 3, "icons": { "16": "./images/favicon-16x16.png", From 7e097726528451cf0c02899c31bc31c4f1eac526 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Fri, 13 Jan 2023 21:04:22 +0530 Subject: [PATCH 5/7] fix: exception handle manifest version util --- src/utils/browser.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/utils/browser.ts b/src/utils/browser.ts index 30d75fa..350cb76 100644 --- a/src/utils/browser.ts +++ b/src/utils/browser.ts @@ -23,8 +23,12 @@ export const tabsQuery = ( /** * Returns the manifest version * - * @returns {2 | 3} + * @returns {2 | 3 | null} */ export const manifestVersion = () => { - return chrome.runtime.getManifest().manifest_version; + try { + return chrome.runtime.getManifest().manifest_version; + } catch(e) { + return null + } } From 642cd5361d768c8514cbe03a43ded23316689105 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Fri, 13 Jan 2023 21:04:33 +0530 Subject: [PATCH 6/7] fix explicitly check manifest version --- src/components/App/Layout/Sidebar.tsx | 2 +- src/components/Authentication/Keyroute.tsx | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/components/App/Layout/Sidebar.tsx b/src/components/App/Layout/Sidebar.tsx index ec9ed0c..d68cc80 100644 --- a/src/components/App/Layout/Sidebar.tsx +++ b/src/components/App/Layout/Sidebar.tsx @@ -45,7 +45,7 @@ export const Sidebar = (props: SidebarProps) => { console.log('Error syncing with background script:', error) } } - else { + else if(manifestVersion() === 3) { try { chrome.storage.session.clear() } catch(error) { diff --git a/src/components/Authentication/Keyroute.tsx b/src/components/Authentication/Keyroute.tsx index c9619d9..91245dd 100644 --- a/src/components/Authentication/Keyroute.tsx +++ b/src/components/Authentication/Keyroute.tsx @@ -125,9 +125,7 @@ export const Keyroute = () => { keyring: { publicKey: hex2buf(session.keyring.publicKey), privateKey: hex2buf(session.keyring.privateKey), - symmetricKey: hex2buf( - session.keyring.symmetricKey - ), + symmetricKey: hex2buf(session.keyring.symmetricKey), }, } dispatch(addSession(decodedSessionData)) @@ -138,7 +136,7 @@ export const Keyroute = () => { } if (manifestVersion() === 2) { getSessionFromBackgroundScript() - } else { + } else if (manifestVersion() === 3) { getSessionFromStorage() } }, [dispatch]) @@ -234,7 +232,7 @@ export const Keyroute = () => { error ) } - } else { + } else if(manifestVersion() === 3) { try { await chrome.storage.session.set({ session: encodedSessionData, From 2d1f05658773baa54ecd96bb48190b724e087e90 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Fri, 13 Jan 2023 21:23:33 +0530 Subject: [PATCH 7/7] chore: update build scripts, add separate builds for mv2 --- README.md | 6 ++++-- config-overrides.js | 31 +++++++++++++++++++++---------- package.json | 3 ++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9e42f3e..d6b3958 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,11 @@ The browser plugin for [Keyspace](https://keyspace.cloud) - A secure self-custod `yarn` ### Build -Build for manifest version 2 with `yarn build` +Build webapp with `yarn build` -Build for manifest version 3 with `yarn v3build` +Build for manifest version 2 with `yarn build:mv2` + +Build for manifest version 3 with `yarn build:mv3` ### Rebuild icon manifest diff --git a/config-overrides.js b/config-overrides.js index e5100fb..8b0176e 100644 --- a/config-overrides.js +++ b/config-overrides.js @@ -1,8 +1,19 @@ const webpack = require('webpack') -const CopyWebpackPlugin = require("copy-webpack-plugin"); -const manifestVersion = process.env.MANIFEST_VERSION == 3 ? 3 : 2 +const CopyWebpackPlugin = require('copy-webpack-plugin') -console.log(`Building for manifest version ${manifestVersion}`) +const manifestVersion = Number(process.env.MANIFEST_VERSION) +const copyPluginOptions = [] + +if (manifestVersion) { + console.log(`Building for manifest version ${manifestVersion}`) + if (manifestVersion === 2) { + copyPluginOptions.push('./src/manifest.json') + } else + copyPluginOptions.push({ + from: './src/manifest.v3.json', + to: 'manifest.json', + }) +} module.exports = function override(config, env) { config.resolve.fallback = { @@ -23,13 +34,6 @@ module.exports = function override(config, env) { new webpack.optimize.AggressiveSplittingPlugin({ minSize: 20000, maxSize: 50000, - }), - new CopyWebpackPlugin({ - patterns: [ - manifestVersion == 3 - ? { from: './src/manifest.v3.json', to: 'manifest.json' } - : './src/manifest.json', - ], }) ) config.module.rules.push({ @@ -39,5 +43,12 @@ module.exports = function override(config, env) { }, }) + if (copyPluginOptions.length) + config.plugins.push( + new CopyWebpackPlugin({ + patterns: copyPluginOptions, + }) + ) + return config } diff --git a/package.json b/package.json index a20fd84..dcc232f 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", - "v3build": "cross-env MANIFEST_VERSION=3 react-app-rewired build", + "build:mv2": "cross-env MANIFEST_VERSION=2 react-app-rewired build", + "build:mv3": "cross-env MANIFEST_VERSION=3 react-app-rewired build", "build-icons": "node build-icons.js", "test": "react-app-rewired test", "eject": "react-scripts eject"