Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BRG-494] Fix activeAccount errors. Up version 1.14.0 #52

Merged
merged 5 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"96": "images/96.png",
"128": "images/128.png"
},
"version": "1.13.2",
"version": "1.14.0",
"background": {
"scripts": [
"background.js"
Expand Down
33 changes: 25 additions & 8 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,6 @@ const resolveAccounts = async () => {
}
});
return accountsRequests.splice(0, accountsRequests.length);


} catch (e) {
return { error: e.message };
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -313,7 +312,7 @@ const updateActiveAccountInpage = async (network) => {
console.log(e.message);
}
});

activeAccountRequests.splice(0, activeAccountRequests.length);
return null;

} catch (e) {
Expand Down Expand Up @@ -352,6 +351,7 @@ const execGetAccountCallbacks = async () => {
* @returns {boolean}
*/
const onMessage = (request, sender, sendResponse) => {

const { hostname } = urlParse(sender.tab.url);
const { id: tabId } = sender.tab;

Expand All @@ -373,6 +373,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') {
Expand All @@ -399,7 +404,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;
Expand Down Expand Up @@ -457,7 +464,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;
Expand Down Expand Up @@ -492,7 +501,9 @@ const onMessage = (request, sender, sendResponse) => {

try {
emitter.emit('request', id, operations);
} catch (e) { return null; }
} catch (e) {
return null;
}

notificationManager.getPopup()
.then((popup) => {
Expand All @@ -516,16 +527,20 @@ const onMessage = (request, sender, sendResponse) => {
}

} else if (request.method === 'getActiveAccount') {
const tabIndex = activeAccountRequests.findIndex(({ tabId: reqTabId }) => tabId === reqTabId);
const { inPageId } = request;
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;
}

Expand Down Expand Up @@ -750,6 +765,7 @@ export const onSwitchNetwork = async (network) => {
console.warn('Switch network callback error', error);
}
});
networkSubscribers.splice(0, networkSubscribers.length);
}

};
Expand All @@ -765,6 +781,7 @@ const onSwitchActiveAccount = (res) => {
console.warn('Switch account callback error', error);
}
});
activeAccountSubscribers.splice(0, activeAccountSubscribers.length);
};

/**
Expand Down
1 change: 1 addition & 0 deletions extension/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const extensionizer = require('./extensionizer');
const { APP_ID } = require('../src/constants/GlobalConstants');

const getAccessRequest = {};

/**
Expand Down
61 changes: 53 additions & 8 deletions extension/inpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const accountChangedSubscribers = [];

let activeAccount = null;

const INPAGE_ID = IdHelper.getId();

/**
* network subscription
*
Expand Down Expand Up @@ -134,12 +136,42 @@ 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');
}

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);

};

Expand All @@ -151,6 +183,7 @@ const notifyAccountChanged = (accountId) => {
accountChangedSubscribers.forEach((cb) => {
cb(accountId);
});

};

/**
Expand Down Expand Up @@ -242,14 +275,17 @@ 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;

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);

};


Expand Down Expand Up @@ -346,27 +382,36 @@ 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);

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();

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,
}, '*');
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/constants/GlobalConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down