openethereum/js/src/shellMiddleware.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-08-09 13:03:26 +02:00
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import * as mobx from 'mobx';
2017-08-09 13:03:26 +02:00
import flatten from 'lodash.flatten';
import DappsStore from '@parity/shared/lib/mobx/dappsStore';
2017-08-09 13:03:26 +02:00
import RequestStore from './DappRequests/store';
import methodGroups from './DappRequests/methodGroups';
2017-08-09 13:03:26 +02:00
export default function execute (appId, method, params, callback) {
const dappsStore = DappsStore.get();
2017-08-09 13:03:26 +02:00
const requestStore = RequestStore.get();
switch (method) {
case 'shell_getApps': {
2017-08-09 13:03:26 +02:00
const [displayAll] = params;
callback(
null,
displayAll
? dappsStore.allApps.slice().map(mobx.toJS)
: dappsStore.visibleApps.slice().map(mobx.toJS)
2017-08-09 13:03:26 +02:00
);
return true;
}
2017-08-09 13:03:26 +02:00
case 'shell_getFilteredMethods': {
callback(
null,
flatten(Object.keys(methodGroups).map(key => methodGroups[key].methods))
);
return true;
}
case 'shell_getMethodGroups': {
callback(
null,
methodGroups
);
2017-08-09 13:03:26 +02:00
return true;
}
2017-08-09 13:03:26 +02:00
case 'shell_getMethodPermissions': {
callback(null, mobx.toJS(requestStore.permissions));
2017-08-09 13:03:26 +02:00
return true;
}
2017-08-09 13:03:26 +02:00
case 'shell_loadApp': {
const [loadId, loadParams] = params;
2017-08-09 13:03:26 +02:00
const loadUrl = `/${loadId}/${loadParams || ''}`;
window.location.hash = loadUrl;
callback(null, true);
return true;
}
2017-08-09 13:03:26 +02:00
case 'shell_requestNewToken': {
2017-08-09 13:03:26 +02:00
callback(null, requestStore.createToken(appId));
return true;
}
2017-08-09 13:03:26 +02:00
case 'shell_setAppPinned': {
const [appId, pinned] = params;
2017-08-09 13:03:26 +02:00
callback(
null,
pinned
? dappsStore.pinApp(appId)
: dappsStore.unpinApp(appId)
);
return true;
}
case 'shell_setAppVisibility': {
const [appId, visibility] = params;
callback(
null,
visibility
? dappsStore.showApp(appId)
: dappsStore.hideApp(appId)
2017-08-09 13:03:26 +02:00
);
return true;
}
2017-08-09 13:03:26 +02:00
case 'shell_setMethodPermissions': {
2017-08-09 13:03:26 +02:00
const [permissions] = params;
callback(null, requestStore.setPermissions(permissions));
return true;
}
2017-08-09 13:03:26 +02:00
default:
return false;
}
}