Skip to content

Commit

Permalink
use origin and title from Port when connecting to a page. remove use …
Browse files Browse the repository at this point in the history
…of Tab its permissions in manifest.
  • Loading branch information
ffox77 committed May 23, 2021
1 parent 62fcae4 commit 71602f0
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 83 deletions.
4 changes: 1 addition & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Stargazer Wallet",
"version": "1.3.0",
"version": "1.3.1",

"icons": {
"16": "assets/icons/favicon-16.png",
Expand All @@ -14,8 +14,6 @@
"short_name": "stargazer-wallet-ext",

"permissions": [
"tabs",
"clipboardWrite",
"https://www.stargazer.network/",
"https://*.coingecko.com/",
"https://*.constellationnetwork.io/",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stargazer-wallet-ext",
"version": "1.3.0",
"version": "1.3.1",
"description": "Extension wallet for $DAG tokens.",
"private": true,
"repository": "https://github.com/StardustCollective/stargazer-wallet-ext.git",
Expand Down
16 changes: 6 additions & 10 deletions source/containers/auth/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import TxsPanel from './TxsPanel';
import styles from './Home.scss';
import { formatNumber } from '../helpers';
import { IDAppState } from 'state/dapp/types';
import { browser } from 'webextension-polyfill-ts';

const Home = () => {
const controller = useController();
Expand All @@ -40,15 +39,12 @@ const Home = () => {
};

useEffect(() => {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (!tabs.length) return;
const { url } = tabs[0];
//console.log('tab: ' + id, url, favIconUrl);
const origin = url && new URL(url as string).origin;
if (tabs.length && origin && dapp[origin]) {
setConnected(true);
}
});
const { origin } = controller.dapp.getCurrent();
//console.log('tab: ' + origin, logo, title);
if (origin && dapp[origin]) {
setConnected(true);
}

}, []);

return (
Expand Down
7 changes: 3 additions & 4 deletions source/containers/auth/Start/Start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const Starter = () => {
});
const dapp: IDAppState = useSelector((state: RootState) => state.dapp);
const current = controller.dapp.getCurrent();
const origin =
current && current.uri && new URL(current.uri as string).origin;
const origin = current && current.origin;
const location = useLocation();
const [isInvalid, setInvalid] = useState(false);
const errorClass = clsx(styles.error, {
Expand All @@ -32,11 +31,11 @@ const Starter = () => {

const onSubmit = (data: any) => {
controller.wallet.unLock(data.password).then(async (res) => {
console.log(dapp, origin, dapp[origin], res, location.pathname);
//console.log(dapp, origin, dapp[origin], res, location.pathname);
if (res && location.pathname.includes('confirm.html') && dapp[origin]) {
const background = await browser.runtime.getBackgroundPage();
background.dispatchEvent(
new CustomEvent('loginWallet', { detail: window.location.hash })
new CustomEvent('connectWallet', { detail: window.location.hash })
);
window.close();
}
Expand Down
10 changes: 6 additions & 4 deletions source/containers/confirm/WalletConnect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ import { useController } from 'hooks/index';
import { browser } from 'webextension-polyfill-ts';

const WalletConnect = () => {
// @ts-ignore

const controller = useController();
const current = controller.dapp.getCurrent();
const origin = current.uri && new URL(current.uri as string).origin;
const origin = current && current.origin;

const handleClose = () => {
window.close();
};

const handleSubmit = async () => {
controller.dapp.connectDApp(origin, current);
controller.dapp.fromUserConnectDApp(origin, current);
const background = await browser.runtime.getBackgroundPage();

background.dispatchEvent(
new CustomEvent('loginWallet', { detail: window.location.hash })
new CustomEvent('connectWallet', { detail: window.location.hash })
);

window.close();
};

Expand Down
34 changes: 16 additions & 18 deletions source/scripts/Background/controllers/DAppController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { listNewDapp } from 'state/dapp';
import { IDAppInfo, IDAppState } from 'state/dapp/types';
import store from 'state/store';
import { browser } from 'webextension-polyfill-ts';


export interface IDAppController {
getCurrent: () => IDAppInfo;
connectDApp: (origin: string, dapp: IDAppInfo) => void;
fetchInfo: (origin: string, upToDate?: boolean) => IDAppInfo | undefined;
fromUserConnectDApp: (origin: string, dapp: IDAppInfo) => void;
fromPageConnectDApp: (origin: string, title: string) => boolean;
setSigRequest: (req: ISigRequest) => void;
getSigRequest: () => ISigRequest;
}
Expand All @@ -18,22 +18,23 @@ interface ISigRequest {
}

const DAppController = (): IDAppController => {
let current: IDAppInfo;
let current: IDAppInfo = { origin: '', logo: '', title: '' };
let request: ISigRequest;

const fetchInfo = (origin: string, upToDate = false) => {
const fromPageConnectDApp = (origin: string, title: string) => {
const dapp: IDAppState = store.getState().dapp;
if (upToDate) {
browser.tabs.query({ active: true }).then((tabs) => {
current = {
uri: tabs[0].url as string,
logo: `chrome://favicon/size/64@1x/${tabs[0].url}`,
title: tabs[0].title as string,
};
});

current = {
origin,
logo: `chrome://favicon/size/64@1x/${origin}`,
title
}

return dapp[origin];
return !!dapp[origin];
}

const fromUserConnectDApp = (origin: string, dapp: IDAppInfo) => {
store.dispatch(listNewDapp({ id: origin, dapp }));
};

const getCurrent = () => {
Expand All @@ -48,11 +49,8 @@ const DAppController = (): IDAppController => {
return request;
};

const connectDApp = (origin: string, dapp: IDAppInfo) => {
store.dispatch(listNewDapp({ id: origin, dapp }));
};

return { getCurrent, fetchInfo, connectDApp, setSigRequest, getSigRequest };
return { getCurrent, fromPageConnectDApp, fromUserConnectDApp, setSigRequest, getSigRequest };
};

export default DAppController;
74 changes: 32 additions & 42 deletions source/scripts/Background/controllers/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { browser, Runtime } from 'webextension-polyfill-ts';
import { IMasterController } from '.';
import { v4 as uuid } from 'uuid';
import store from 'state/store';
import watch from 'redux-watch';
import IWalletState from 'state/wallet/types';

type Message = {
Expand All @@ -15,20 +14,15 @@ export const messagesHandler = (
port: Runtime.Port,
masterController: IMasterController
) => {
// const externalConnectionApprovalMap: { [origin: string]: true } = {};

// const fromApp = (url: string = '') => {
// return url.startsWith(`${window.location.origin}/app.html`);
// };
// const fromConfirm = (url: string = '') => {
// return url.startsWith(`${window.location.origin}/confirm.html`);
// };

let pendingWindow = false;

const listener = async (message: Message, connection: Runtime.Port) => {
try {
const response = await listenerHandler(message, connection);

//console.log('listenerHandler.RESPONSE', response);

if (response) {
const { id, result } = response;
//console.log('messagesHandler.RESPONSE');
Expand All @@ -48,39 +42,28 @@ export const messagesHandler = (
) => {
if (browser.runtime.lastError) return Promise.reject('Runtime Last Error');

const sendError = (error: string) =>
Promise.reject(new CustomEvent(message.id, { detail: error }));
// const isFromAuthorizedDapp = masterController.fromAuthorizedDapp(
// sender.origin
// );
// const dappInfo = isFromAuthorizedDapp
// ? masterController.dapps.getDappInfoByURL(sender.origin)
// : undefined;
// const isFromApp = fromApp(port.sender?.url);
// const isFromConfirm = fromConfirm(port.sender?.url);
const walletIsLocked = masterController.wallet.isLocked();
const sendError = (error: string) => {
return Promise.reject(new CustomEvent(message.id, { detail: error }));
}

// console.log(
// 'messagesHandler.onMessage: ',
// isFromApp,
// isFromConfirm,
// walletIsLocked
// );
// console.log(JSON.stringify(message, null, 2));
const walletIsLocked = masterController.wallet.isLocked();

const url = connection.sender?.url;
const url = connection.sender?.url as string;
const title = connection.sender?.tab?.title as string;
const origin = url && new URL(url as string).origin;

const allowed = origin && masterController.dapp.fetchInfo(origin, true);
const allowed = masterController.dapp.fromPageConnectDApp(origin, title);

//console.log('messagesHandler.onMessage: ' + origin, allowed);
//console.log('messagesHandler.onMessage: ' + message.type, walletIsLocked, origin, allowed, url, title, pendingWindow);

if (message.type === 'STARGAZER_EVENT_REG') {
if (message.data && message.data.method) {

}
} else if (message.type === 'ENABLE_REQUEST') {

if (walletIsLocked) {

const { seedKeystoreId }: IWalletState = store.getState().wallet;
if (!seedKeystoreId) {
return sendError('Need to set up Wallet');
Expand All @@ -94,7 +77,7 @@ export const messagesHandler = (
pendingWindow = true;

window.addEventListener(
'loginWallet',
'connectWallet',
(ev: any) => {
if (ev.detail.substring(1) === windowId) {
port.postMessage({
Expand All @@ -116,6 +99,7 @@ export const messagesHandler = (
pendingWindow = false;
}
});

return Promise.resolve(null);
}

Expand All @@ -124,32 +108,38 @@ export const messagesHandler = (
return Promise.resolve(null);
}

const popup = await masterController.createPopup(uuid());
const windowId = uuid();
const popup = await masterController.createPopup(windowId);
pendingWindow = true;
const w = watch(store.getState, 'dapp');
store.subscribe(
w((newState) => {
pendingWindow = false;
port.postMessage({
id: message.id,
data: { result: !!newState[origin] },
});
})

window.addEventListener(
'connectWallet',
(ev: any) => {
console.log('Connect window addEventListener', ev.detail);
if (ev.detail.substring(1) === windowId) {
port.postMessage({ id: message.id, data: { result: true } });
pendingWindow = false;
}
},
{ once: true, passive: true }
);

browser.windows.onRemoved.addListener((id) => {
if (popup && id === popup.id) {
port.postMessage({ id: message.id, data: { result: false } });
pendingWindow = false;
//console.log('Connect window is closed');
console.log('Connect window is closed');
}
});

return Promise.resolve(null);
}

return Promise.resolve({ id: message.id, result: origin && allowed });

} else if (message.type === 'CAL_REQUEST') {
const { method, args } = message.data;
//console.log('CAL_REQUEST.method', method, args);
let result: any = undefined;
if (method === 'wallet.isConnected') {
result = { connected: !!allowed && !walletIsLocked };
Expand Down
2 changes: 1 addition & 1 deletion source/state/dapp/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface IDAppInfo {
uri: string;
origin: string;
logo: string;
title: string;
}
Expand Down

0 comments on commit 71602f0

Please sign in to comment.