diff --git a/packages/embark-api-client/package.json b/packages/embark-api-client/package.json new file mode 100644 index 000000000..172ef2472 --- /dev/null +++ b/packages/embark-api-client/package.json @@ -0,0 +1,64 @@ +{ + "name": "embark-api-client", + "version": "4.0.0", + "author": "Iuri Matias ", + "contributors": [], + "description": "Embark api module", + "homepage": "https://github.com/embark-framework/embark/tree/master/packages/embark-api-client#readme", + "bugs": "https://github.com/embark-framework/embark/issues", + "keywords": [ + "blockchain", + "dapps", + "ethereum", + "ipfs", + "serverless", + "solc", + "solidity" + ], + "license": "MIT", + "repository": { + "directory": "packages/embark-api-client", + "type": "git", + "url": "https://github.com/embark-framework/embark.git" + }, + "main": "./dist/index.js", + "files": [ + "dist" + ], + "scripts": { + "build": "cross-env BABEL_ENV=node babel src --extensions \".js,.ts\" --out-dir dist --root-mode upward --source-maps", + "ci": "npm run qa", + "clean": "npm run reset", + "lint": "npm-run-all lint:*", + "lint:js": "eslint src/", + "lint:ts": "tslint -c tslint.json \"src/**/*.ts\"", + "package": "npm pack", + "// qa": "npm-run-all lint typecheck build package", + "qa": "npm-run-all lint build package", + "reset": "npx rimraf dist embark-*.tgz package", + "start": "npm run watch", + "typecheck": "tsc", + "watch": "run-p watch:*", + "watch:build": "npm run build -- --verbose --watch", + "// watch:typecheck": "npm run typecheck -- --preserveWatchOutput --watch" + }, + "dependencies": { + "@babel/runtime-corejs2": "7.3.1", + "axios": "0.18.0", + "keccakjs": "0.2.1" + }, + "devDependencies": { + "@babel/cli": "7.2.3", + "@babel/core": "7.2.2", + "cross-env": "5.2.0", + "npm-run-all": "4.1.5", + "rimraf": "2.6.3", + "tslint": "5.11.0", + "typescript": "3.3.1" + }, + "engines": { + "node": ">=8.12.0", + "npm": ">=6.4.1", + "yarn": ">=1.12.3" + } +} diff --git a/packages/embark-api-client/src/index.js b/packages/embark-api-client/src/index.js new file mode 100644 index 000000000..289eda262 --- /dev/null +++ b/packages/embark-api-client/src/index.js @@ -0,0 +1,281 @@ +import axios from "axios"; +import keccak from "keccakjs"; + +function hash(cnonce, token, type, path) { + let hash = new keccak(); + hash.update(cnonce.toString()); + hash.update(token); + hash.update(type.toUpperCase()); + hash.update(`/embark-api${path}`); + return hash.digest('hex'); +} + +function request(type, path, params = {}) { + const cnonce = Date.now() + Math.random(); + + // Extract credentials out of the params and delete so the token doesn't get sent + // as cleartext. + let credentials = params.credentials; + delete params.credentials; + + let requestHash = hash(cnonce, credentials.token, type, path, params); + + const endpoint = `http://${credentials.host}/embark-api${path}`; + const req = { + method: type, + url: endpoint, + headers: { + 'X-Embark-Request-Hash': requestHash, + 'X-Embark-Cnonce': cnonce + }, + ...(type === 'post' ? { data: params } : {}), + ...(['get', 'delete'].includes(type) ? { params: params.params } : {}) + }; + + return axios(req) + .then((response) => { + return (response.data && response.data.error) ? {error: response.data.error} : {response, error: null}; + }).catch((error) => { + return {response: null, error: error.message || 'Something bad happened'}; + }); +} + +function get() { + console.dir("== get"); + console.dir(...arguments); + return request('get', ...arguments); +} + +function post() { + console.dir("== post"); + console.dir(...arguments); + return request('post', ...arguments); +} + +function destroy() { + console.dir("== destroy"); + console.dir(...arguments); + return request('delete', ...arguments); +} + +function websocket(credentials, path) { + const cnonce = Date.now() + Math.random(); + const requestHash = hash(cnonce, credentials.token, 'ws', '/', {}); + + return new WebSocket(`ws://${credentials.host}/embark-api${path}`, [`${cnonce}|${requestHash}`]); +} + +class EmbarkAPI { + + postCommand() { + return post('/command', ...arguments); + } + + postCommandSuggestions() { + return post('/suggestions', ...arguments); + } + + fetchAccounts() { + return get('/blockchain/accounts', ...arguments); + } + + fetchAccount(payload) { + return get(`/blockchain/accounts/${payload.address}`, ...arguments); + } + + fetchBlocks(payload) { + return get('/blockchain/blocks', {params: payload, credentials: payload.credentials}); + } + + fetchBlock(payload) { + return get(`/blockchain/blocks/${payload.blockNumber}`, ...arguments); + } + + fetchTransactions(payload) { + return get('/blockchain/transactions', {params: payload, credentials: payload.credentials}); + } + + fetchTransaction(payload) { + return get(`/blockchain/transactions/${payload.hash}`, ...arguments); + } + + fetchProcesses() { + return get('/processes', ...arguments); + } + + fetchServices() { + return get('/services', ...arguments); + } + + fetchProcessLogs(payload) { + return get(`/process-logs/${payload.processName}`, {params: payload, credentials: payload.credentials}); + } + + fetchContractLogs() { + return get(`/contracts/logs`, ...arguments); + } + + fetchContractEvents() { + return get(`/blockchain/contracts/events`, ...arguments); + } + + fetchContracts() { + return get('/contracts', ...arguments); + } + + fetchContract(payload) { + return get(`/contract/${payload.contractName}`, ...arguments); + } + + postContractFunction(payload) { + return post(`/contract/${payload.contractName}/function`, ...arguments); + } + + postContractDeploy(payload) { + return post(`/contract/${payload.contractName}/deploy`, ...arguments); + } + + postContractCompile() { + return post('/contract/compile', ...arguments); + } + + fetchVersions() { + return get('/versions', ...arguments); + } + + fetchPlugins() { + return get('/plugins', ...arguments); + } + + sendMessage(payload) { + return post(`/communication/sendMessage`, Object.assign({}, payload.body, {credentials: payload.credentials})); + } + + fetchContractProfile(payload) { + return get(`/profiler/${payload.contractName}`, ...arguments); + } + + fetchEnsRecord(payload) { + const _payload = {params: payload, credentials: payload.credentials}; + if (payload.name) { + return get('/ens/resolve', _payload); + } + + return get('/ens/lookup', _payload); + } + + postEnsRecord() { + return post('/ens/register', ...arguments); + } + + getEthGasAPI() { + return get('/blockchain/gas/oracle', ...arguments); + } + + fetchFiles() { + return get('/files', ...arguments); + } + + fetchFile(payload) { + return get('/file', {params: payload, credentials: payload.credentials}); + } + + postFile() { + return post('/files', ...arguments); + } + + postFolder() { + return post('/folders', ...arguments); + } + + deleteFile(payload) { + return destroy('/file', {params: payload, credentials: payload.credentials}); + } + + authenticate(payload) { + return post('/authenticate', {...payload, credentials: payload}); + } + + signMessage() { + return post('/messages/sign', ...arguments); + } + + verifyMessage() { + return post('/messages/verify', ...arguments); + } + + startDebug(payload) { + return post('/debugger/start', {params: payload, credentials: payload.credentials}); + } + + debugJumpBack(payload) { + return post('/debugger/jumpBack', {params: payload, credentials: payload.credentials}); + } + + debugJumpForward(payload) { + return post('/debugger/jumpForward', {params: payload, credentials: payload.credentials}); + } + + debugStepOverForward(payload) { + return post('/debugger/stepOverForward', {params: payload, credentials: payload.credentials}); + } + + debugStepOverBackward(payload) { + return post('/debugger/stepOverBackward', {params: payload, credentials: payload.credentials}); + } + + debugStepIntoForward(payload) { + return post('/debugger/stepIntoForward', {params: payload, credentials: payload.credentials}); + } + + debugStepIntoBackward(payload) { + return post('/debugger/stepIntoBackward', {params: payload, credentials: payload.credentials}); + } + + toggleBreakpoint(payload) { + return post('/debugger/breakpoint', {params: payload, credentials: payload.credentials}); + } + + regularTxs(payload) { + return get('/regular-txs', {params: payload, credentials: payload.credentials}); + } + + listenToDebugger(credentials) { + return websocket(credentials, '/debugger'); + } + + listenToChannel(credentials, channel) { + return websocket(credentials, `/communication/listenTo/${channel}`); + } + + webSocketProcess(credentials, processName) { + return websocket(credentials, `/process-logs/${processName}`); + } + + webSocketServices(credentials) { + return websocket(credentials, `/services`); + } + + webSocketContractLogs(credentials) { + return websocket(credentials, `/contracts/logs`); + } + + webSocketContracts(credentials) { + return websocket(credentials, `/contracts`); + } + + webSocketContractEvents(credentials) { + return websocket(credentials, `/blockchain/contracts/event`); + } + + webSocketBlockHeader(credentials) { + return websocket(credentials, `/blockchain/blockHeader`); + } + + websocketGasOracle(credentials) { + return websocket(credentials, `/blockchain/gas/oracle`); + } + +} + +export default EmbarkAPI; diff --git a/packages/embark-api-client/tsconfig.json b/packages/embark-api-client/tsconfig.json new file mode 100644 index 000000000..52d43eaaa --- /dev/null +++ b/packages/embark-api-client/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*"] +} diff --git a/packages/embark-api-client/tslint.json b/packages/embark-api-client/tslint.json new file mode 100644 index 000000000..0946f2096 --- /dev/null +++ b/packages/embark-api-client/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tslint.json" +} diff --git a/packages/embark-ui/package.json b/packages/embark-ui/package.json index b5c9a4095..35e5f6b36 100644 --- a/packages/embark-ui/package.json +++ b/packages/embark-ui/package.json @@ -51,7 +51,6 @@ "@svgr/webpack": "2.4.1", "ansi-to-html": "0.6.8", "autoscroll-react": "3.2.0", - "axios": "0.18.0", "babel-core": "7.0.0-bridge.0", "babel-eslint": "9.0.0", "babel-jest": "23.6.0", @@ -66,6 +65,7 @@ "css-loader": "1.0.0", "dotenv": "6.0.0", "dotenv-expand": "4.2.0", + "embark-api-client": "^4.0.0", "eslint": "5.6.0", "eslint-config-react-app": "3.0.5", "eslint-loader": "2.1.1", @@ -84,7 +84,6 @@ "jest": "23.6.0", "jest-pnp-resolver": "1.0.1", "jest-resolve": "23.6.0", - "keccakjs": "0.2.1", "mini-css-extract-plugin": "0.4.3", "monaco-editor": "0.14.3", "monaco-editor-webpack-plugin": "1.6.0", diff --git a/packages/embark-ui/src/services/api.js b/packages/embark-ui/src/services/api.js index fe9bac122..bd2d17d17 100644 --- a/packages/embark-ui/src/services/api.js +++ b/packages/embark-ui/src/services/api.js @@ -1,269 +1,208 @@ -import axios from "axios"; -import keccak from "keccakjs"; +import EmbarkAPI from 'embark-api-client'; -function hash(cnonce, token, type, path, params = {}) { - let hash = new keccak(); - hash.update(cnonce.toString()); - hash.update(token); - hash.update(type.toUpperCase()); - hash.update(`/embark-api${path}`); - return hash.digest('hex'); -} - -function request(type, path, params = {}) { - const cnonce = Date.now() + Math.random(); - - // Extract credentials out of the params and delete so the token doesn't get sent - // as cleartext. - let credentials = params.credentials; - delete params.credentials; - - let requestHash = hash(cnonce, credentials.token, type, path, params); - - const endpoint = `http://${credentials.host}/embark-api${path}`; - const req = { - method: type, - url: endpoint, - headers: { - 'X-Embark-Request-Hash': requestHash, - 'X-Embark-Cnonce': cnonce - }, - ...(type === 'post' ? { data: params } : {}), - ...(['get', 'delete'].includes(type) ? { params: params.params } : {}) - } - - return axios(req) - .then((response) => { - return (response.data && response.data.error) ? {error: response.data.error} : {response, error: null}; - }).catch((error) => { - return {response: null, error: error.message || 'Something bad happened'}; - }); -} - -function get() { - return request('get', ...arguments); -} - -function post() { - return request('post', ...arguments); -} - -function destroy() { - return request('delete', ...arguments); -} - -function websocket(credentials, path) { - const cnonce = Date.now() + Math.random(); - const requestHash = hash(cnonce, credentials.token, 'ws', '/', {}); - - return new WebSocket(`ws://${credentials.host}/embark-api${path}`, [`${cnonce}|${requestHash}`]); -} +const embarkAPI = new EmbarkAPI() export function postCommand() { - return post('/command', ...arguments); + return embarkAPI.postCommand(...arguments) } export function postCommandSuggestions() { - return post('/suggestions', ...arguments); + return embarkAPI.postCommandSuggestions(...arguments); } export function fetchAccounts() { - return get('/blockchain/accounts', ...arguments); + return embarkAPI.fetchAccounts(...arguments); } export function fetchAccount(payload) { - return get(`/blockchain/accounts/${payload.address}`, ...arguments); + return embarkAPI.fetchAccount(payload, ...arguments); } export function fetchBlocks(payload) { - return get('/blockchain/blocks', {params: payload, credentials: payload.credentials}); + return embarkAPI.fetchBlocks(payload); } export function fetchBlock(payload) { - return get(`/blockchain/blocks/${payload.blockNumber}`, ...arguments); + return embarkAPI.fetchBlock(payload, ...arguments); } export function fetchTransactions(payload) { - return get('/blockchain/transactions', {params: payload, credentials: payload.credentials}); + return embarkAPI.fetchTransactions(payload); } export function fetchTransaction(payload) { - return get(`/blockchain/transactions/${payload.hash}`, ...arguments); + return embarkAPI.fetchTransaction(payload, ...arguments); } export function fetchProcesses() { - return get('/processes', ...arguments); + return embarkAPI.fetchProcesses(...arguments); } export function fetchServices() { - return get('/services', ...arguments); + return embarkAPI.fetchServices(...arguments); } export function fetchProcessLogs(payload) { - return get(`/process-logs/${payload.processName}`, {params: payload, credentials: payload.credentials}); + return embarkAPI.fetchProcessLogs(payload); } export function fetchContractLogs() { - return get(`/contracts/logs`, ...arguments); + return embarkAPI.fetchContractLogs(...arguments); } export function fetchContractEvents() { - return get(`/blockchain/contracts/events`, ...arguments); + return embarkAPI.fetchContractEvents(...arguments); } export function fetchContracts() { - return get('/contracts', ...arguments); + return embarkAPI.fetchContracts(...arguments); } export function fetchContract(payload) { - return get(`/contract/${payload.contractName}`, ...arguments); + return embarkAPI.fetchContract(payload, ...arguments); } export function postContractFunction(payload) { - return post(`/contract/${payload.contractName}/function`, ...arguments); + return embarkAPI.postContractFunction(payload, ...arguments); } export function postContractDeploy(payload) { - return post(`/contract/${payload.contractName}/deploy`, ...arguments); + return embarkAPI.postContractDeploy(payload, ...arguments); } export function postContractCompile() { - return post('/contract/compile', ...arguments); + return embarkAPI.postContractCompile(...arguments); } export function fetchVersions() { - return get('/versions', ...arguments); + return embarkAPI.fetchVersions(...arguments); } export function fetchPlugins() { - return get('/plugins', ...arguments); + return embarkAPI.fetchPlugins(...arguments); } export function sendMessage(payload) { - return post(`/communication/sendMessage`, Object.assign({}, payload.body, {credentials: payload.credentials})); + return embarkAPI.sendMessage(payload); } export function fetchContractProfile(payload) { - return get(`/profiler/${payload.contractName}`, ...arguments); + return embarkAPI.fetchContractProfile(payload, ...arguments); } export function fetchEnsRecord(payload) { - const _payload = {params: payload, credentials: payload.credentials}; - if (payload.name) { - return get('/ens/resolve', _payload); - } - - return get('/ens/lookup', _payload); + return embarkAPI.fetchEnsRecord(payload); } export function postEnsRecord() { - return post('/ens/register', ...arguments); + return embarkAPI.postEnsRecord(...arguments); } export function getEthGasAPI() { - return get('/blockchain/gas/oracle', ...arguments); + return embarkAPI.getEthGasAPI(...arguments); } export function fetchFiles() { - return get('/files', ...arguments); + return embarkAPI.fetchFiles(...arguments); } export function fetchFile(payload) { - return get('/file', {params: payload, credentials: payload.credentials}); + return embarkAPI.fetchFile(payload); } export function postFile() { - return post('/files', ...arguments); + return embarkAPI.postFile(...arguments); } export function postFolder() { - return post('/folders', ...arguments); + return embarkAPI.postFolder(...arguments); } export function deleteFile(payload) { - return destroy('/file', {params: payload, credentials: payload.credentials}); + return embarkAPI.deleteFile(payload); } export function authenticate(payload) { - return post('/authenticate', {...payload, credentials: payload}); + return embarkAPI.authenticate(payload); } export function signMessage(payload) { - return post('/messages/sign', ...arguments); + return embarkAPI.signMessage(payload, ...arguments); } export function verifyMessage(payload) { - return post('/messages/verify', ...arguments); + return embarkAPI.verifyMessage(payload, ...arguments); } export function startDebug(payload) { - return post('/debugger/start', {params: payload, credentials: payload.credentials}); + return embarkAPI.startDebug(payload); } export function debugJumpBack(payload) { - return post('/debugger/jumpBack', {params: payload, credentials: payload.credentials}); + return embarkAPI.debugJumpBack(payload); } export function debugJumpForward(payload) { - return post('/debugger/jumpForward', {params: payload, credentials: payload.credentials}); + return embarkAPI.debugJumpForward(payload); } export function debugStepOverForward(payload) { - return post('/debugger/stepOverForward', {params: payload, credentials: payload.credentials}); + return embarkAPI.debugStepOverForward(payload); } export function debugStepOverBackward(payload) { - return post('/debugger/stepOverBackward', {params: payload, credentials: payload.credentials}); + return embarkAPI.debugStepOverBackward(payload); } export function debugStepIntoForward(payload) { - return post('/debugger/stepIntoForward', {params: payload, credentials: payload.credentials}); + return embarkAPI.debugStepIntoForward(payload); } export function debugStepIntoBackward(payload) { - return post('/debugger/stepIntoBackward', {params: payload, credentials: payload.credentials}); + return embarkAPI.debugStepIntoBackward(payload); } export function toggleBreakpoint(payload) { - return post('/debugger/breakpoint', {params: payload, credentials: payload.credentials}); + return embarkAPI.toggleBreakpoint(payload); } export function regularTxs(payload) { - return get('/regular-txs', {params: payload, credentials: payload.credentials}); + return embarkAPI.regularTxs(payload); } export function listenToDebugger(credentials) { - return websocket(credentials, '/debugger'); + return embarkAPI.listenToDebugger(credentials, '/debugger'); } export function listenToChannel(credentials, channel) { - return websocket(credentials, `/communication/listenTo/${channel}`); + return embarkAPI.listenToChannel(credentials, `/communication/listenTo/${channel}`); } export function webSocketProcess(credentials, processName) { - return websocket(credentials, `/process-logs/${processName}`); + return embarkAPI.webSocketProcess(credentials, `/process-logs/${processName}`); } export function webSocketServices(credentials) { - return websocket(credentials, `/services`); + return embarkAPI.webSocketServices(credentials, `/services`); } export function webSocketContractLogs(credentials) { - return websocket(credentials, `/contracts/logs`); + return embarkAPI.webSocketContractLogs(credentials, `/contracts/logs`); } export function webSocketContracts(credentials) { - return websocket(credentials, `/contracts`); + return embarkAPI.webSocketContracts(credentials, `/contracts`); } export function webSocketContractEvents(credentials) { - return websocket(credentials, `/blockchain/contracts/event`); + return embarkAPI.webSocketContractEvents(credentials, `/blockchain/contracts/event`); } export function webSocketBlockHeader(credentials) { - return websocket(credentials, `/blockchain/blockHeader`); + return embarkAPI.webSocketBlockHeader(credentials, `/blockchain/blockHeader`); } export function websocketGasOracle(credentials) { - return websocket(credentials, `/blockchain/gas/oracle`); + return embarkAPI.websocketGasOracle(credentials, `/blockchain/gas/oracle`); } +