ETH Balnaces implemented

This commit is contained in:
Luis Fernando Molina committed 2020-11-19 13:06:12 +01:00
1 parent 9bf8c2eed6
commit 6784e6bd0b
42 files changed
+979 -669

No files matched your search

+2 -2
View File
@@ -126,9 +126,9 @@ try {
console.log("What's new? The following is being implemented here:")
console.log('')
console.log('Multi-Project Infrastructure ....... This will allow us to integrate crypto projects into Superalgos.')
console.log('Machine Learning Infrastructure .... This will enable a new kind of Learning Bot that can learn from data mined. Later that knowledge can be used at trading strategies.')
console.log('Machine Learning Infrastructure .... This will enable a new kind of Bot that can learn from data mined. Later that knowledge can be used at trading strategies.')
console.log('Ethereum Integration ............... This will allow mining data from an Ethereum network node, create indicators with it, an use it on strategies.')
console.log('')
} catch (err) {
console.log('[ERROR] Client -> Error = ' + err.stack)
+145 -113
View File
@@ -54,39 +54,39 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
}
function onBrowserRequest(request, response) {
if (CONSOLE_LOG === true && request.url.indexOf('NO-LOG') === -1) { console.log('[INFO] httpInterface -> onBrowserRequest -> request.url = ' + request.url) }
function onBrowserRequest(httpRequest, httpResponse) {
if (CONSOLE_LOG === true && httpRequest.url.indexOf('NO-LOG') === -1) { console.log('[INFO] httpInterface -> onBrowserRequest -> httpRequest.url = ' + httpRequest.url) }
function getBody(callback) { // Gets the de body from a POST request to the web server
function getBody(callback) { // Gets the de body from a POST httpRequest to the web server
try {
let body = ''
request.on('data', function (data) {
httpRequest.on('data', function (data) {
body += data
// Too much POST data
//if (body.length > 1e6) {
// request.connection.destroy()
// httpRequest.connection.destroy()
//}
})
request.on('end', function () {
httpRequest.on('end', function () {
callback(body)
})
request.on('error', function (err) {
httpRequest.on('error', function (err) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse)
})
} catch (err) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> onBrowserRequest -> getBody -> err = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse)
}
}
let requestParameters = request.url.split('/')
let requestParameters = httpRequest.url.split('/')
requestParameters = request.url.split('?') // Remove version information
requestParameters = httpRequest.url.split('?') // Remove version information
requestParameters = requestParameters[0].split('/')
switch (requestParameters[1]) {
@@ -102,17 +102,38 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
switch (params.method) {
case 'getNetworkClientStatus': {
let status = await WEB3_SERVER.getNetworkClientStatus(
let serverResponse = await WEB3_SERVER.getNetworkClientStatus(
params.host,
params.port,
params.interface
)
respondWithContent(JSON.stringify(status), response)
respondWithContent(JSON.stringify(serverResponse), httpResponse)
return
}
case 'createWalletAccount': {
let serverResponse = await WEB3_SERVER.createWalletAccount(
params.entropy
)
respondWithContent(JSON.stringify(serverResponse), httpResponse)
return
}
case 'getWalletBalances': {
let serverResponse = await WEB3_SERVER.getWalletBalances(
params.host,
params.port,
params.interface,
params.walletDefinition
)
respondWithContent(JSON.stringify(serverResponse), httpResponse)
return
}
default: {
respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), response)
respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), httpResponse)
}
}
} catch (err) {
@@ -125,7 +146,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
message: err.message,
stack: err.stack
}
respondWithContent(JSON.stringify(error), response)
respondWithContent(JSON.stringify(error), httpResponse)
}
}
break
@@ -157,7 +178,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
if (ccxtExchange.has.fetchMarkets === true) {
ccxtMarkets = await ccxtExchange.fetchMarkets()
}
respondWithContent(JSON.stringify(ccxtMarkets), response)
respondWithContent(JSON.stringify(ccxtMarkets), httpResponse)
return
}
case 'listExchanges': {
@@ -189,7 +210,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
}
}
respondWithContent(JSON.stringify(exchanges), response)
respondWithContent(JSON.stringify(exchanges), httpResponse)
return
}
}
@@ -203,7 +224,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
result: 'Fail Because',
message: err.message
}
respondWithContent(JSON.stringify(error), response)
respondWithContent(JSON.stringify(error), httpResponse)
}
}
break
@@ -220,12 +241,12 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
/* Some validations */
if (exchange === undefined) {
console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no Exchange received -> messageReceived = ' + messageReceived)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
if (market === undefined) {
console.log('[WARN] httpInterface -> Webhook -> Fetch-Messages -> Message with no market received -> messageReceived = ' + messageReceived)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
@@ -236,7 +257,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
webhookMessages = []
}
respondWithContent(JSON.stringify(webhookMessages), response)
respondWithContent(JSON.stringify(webhookMessages), httpResponse)
webhookMessages = []
webhook.set(key, webhookMessages)
@@ -254,17 +275,17 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
/* Some validations */
if (source === undefined) {
console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Source received -> messageReceived = ' + messageReceived)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
if (exchange === undefined) {
console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no Exchange received -> messageReceived = ' + messageReceived)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
if (market === undefined) {
console.log('[WARN] httpInterface -> Webhook -> New-Message -> Message with no market received -> messageReceived = ' + messageReceived)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
@@ -278,7 +299,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
webhookMessages.push([timestamp, source, messageReceived])
webhook.set(key, webhookMessages)
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse)
}
break
}
@@ -294,27 +315,27 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
rimraf.sync(process.env.STORAGE_PATH + '/Masters/Masters/AAJason.1.0')
rimraf.sync(process.env.LOG_PATH)
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse)
} catch (err) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> ResetLogsAndData -> Could not delete Logs and Data.') }
let error = {
result: 'Fail Because',
message: err.message
}
respondWithContent(JSON.stringify(error), response)
respondWithContent(JSON.stringify(error), httpResponse)
}
break
}
case 'LegacyPlotter.js':
{
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', response)
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/LegacyPlotter.js', httpResponse)
}
break
case 'PlotterPanel.js':
{
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', response)
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/PlotterPanel.js', httpResponse)
}
break
@@ -336,7 +357,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
path = unescape(path)
respondWithImage(path, response)
respondWithImage(path, httpResponse)
}
break
@@ -357,25 +378,25 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
path = unescape(path)
respondWithImage(path, response)
respondWithImage(path, httpResponse)
}
break
case 'favicon.ico': // This means the Scripts folder.
{
respondWithImage(process.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', response)
respondWithImage(process.env.PATH_TO_CLIENT + 'WebServer/Images/' + 'favicon.ico', httpResponse)
}
break
case 'WebServer': // This means the WebServer folder.
{
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], response)
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/' + requestParameters[2], httpResponse)
}
break
case 'externalScripts': // This means the WebServer folder.
{
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], response)
respondWithFile(process.env.PATH_TO_CLIENT + 'WebServer/externalScripts/' + requestParameters[2], httpResponse)
}
break
@@ -385,7 +406,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
let codeName = requestParameters[3]
let moduleName = requestParameters[4]
let filePath = process.env.PLOTTERS_PATH + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName
respondWithFile(filePath, response)
respondWithFile(filePath, httpResponse)
}
break
@@ -395,32 +416,32 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
let codeName = requestParameters[3]
let moduleName = requestParameters[4]
let filePath = process.env.PLOTTERS_PATH + '/' + dataMine + '/plotters/' + codeName + '/' + moduleName
respondWithFile(filePath, response)
respondWithFile(filePath, httpResponse)
}
break
case 'ChartLayers':
{
respondWithFile(process.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], response)
respondWithFile(process.env.PATH_TO_UI + '/' + requestParameters[1] + '/' + requestParameters[2], httpResponse)
}
break
case 'Files':
{
respondWithFile(process.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], response)
respondWithFile(process.env.PATH_TO_DATA_FILES + '/' + requestParameters[2], httpResponse)
}
break
case 'Fonts':
{
respondWithFont(process.env.PATH_TO_FONTS + '/' + requestParameters[2], response)
respondWithFont(process.env.PATH_TO_FONTS + '/' + requestParameters[2], httpResponse)
}
break
case 'ProjectNames':
{
let projects = getDirectories(process.env.PROJECTS_PATH)
respondWithContent(JSON.stringify(projects), response)
respondWithContent(JSON.stringify(projects), httpResponse)
}
break
@@ -452,7 +473,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
projectCounter++
if (projectCounter === totalProjects) {
respondWithContent(JSON.stringify(icons), response)
respondWithContent(JSON.stringify(icons), httpResponse)
}
})
}
@@ -468,7 +489,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
let folder = process.env.PROJECTS_PATH + '/' + project + '/Plugins/' + pluginType
fs.readdir(folder, (err, files) => {
respondWithContent(JSON.stringify(files), response)
respondWithContent(JSON.stringify(files), httpResponse)
})
}
break
@@ -479,7 +500,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
let pluginType = unescape(requestParameters[3])
let fileName = unescape(requestParameters[4])
let filePath = process.env.PROJECTS_PATH + '/' + project + '/Plugins/' + pluginType + '/' + fileName
respondWithFile(filePath, response)
respondWithFile(filePath, httpResponse)
}
break
@@ -496,10 +517,10 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onFileRead(err, workspace) {
if (err) {
respondWithContent(undefined, response)
respondWithContent(undefined, httpResponse)
} else {
let responseContent = 'function getWorkspace(){ return ' + workspace + '}'
respondWithContent(responseContent, response)
respondWithContent(responseContent, httpResponse)
}
}
}
@@ -523,7 +544,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onDirRead(err, fileList) {
if (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
} else {
let updatedFileList = []
@@ -542,7 +563,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
} catch (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
}
@@ -557,23 +578,23 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onDirRead(err, fileList) {
if (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
} else {
let updatedFileList = []
for (let i = 0; i < fileList.length; i++) {
let name = fileList[i]
updatedFileList.push(['', name])
}
for (let i = 0; i < fileList.length; i++) {
let name = fileList[i]
updatedFileList.push(['', name])
}
allWorkspaces = allWorkspaces.concat(updatedFileList)
respondWithContent(JSON.stringify(allWorkspaces), response)
respondWithContent(JSON.stringify(allWorkspaces), httpResponse)
return
}
}
} catch (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
}
@@ -584,7 +605,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
{
let fileName = unescape(requestParameters[2])
let filePath = process.env.MY_WORKSPACES_PATH + '/' + fileName + '.json'
respondWithFile(filePath, response)
respondWithFile(filePath, httpResponse)
}
break
@@ -611,16 +632,28 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onFileWritten(err) {
if (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error writting the Workspace file. fileName = ' + fileName) }
respondWithContent(JSON.stringify(exchanges), response)
console.log('[ERROR] SaveWorkspace -> onFileWritten -> Error writting the Workspace file. fileName = ' + fileName)
console.log('[ERROR] SaveWorkspace -> onFileWritten -> err.stack = ' + err.stack)
let error = {
result: 'Fail Because',
message: err.message,
stack: err.stack
}
respondWithContent(JSON.stringify(error), httpResponse)
} else {
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse)
}
}
} catch (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error writting the Workspace file. fileName = ' + fileName) }
respondWithContent(JSON.stringify(exchanges), response)
console.log('[ERROR] SaveWorkspace -> Error writting the Workspace file. fileName = ' + fileName)
console.log('[ERROR] SaveWorkspace -> err.stack = ' + err.stack)
let error = {
result: 'Fail Because',
message: err.message,
stack: err.stack
}
respondWithContent(JSON.stringify(error), httpResponse)
}
}
}
@@ -635,7 +668,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
case 'ProjectsSchema':
{
let path = process.env.PROJECTS_PATH + '/' + 'ProjectsSchema.json'
respondWithFile(path, response)
respondWithFile(path, httpResponse)
}
break
@@ -672,7 +705,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onDirRead(err, fileList) {
if (err) {
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
} else {
let updatedFileList = []
for (let k = 0; k < fileList.length; k++) {
@@ -683,7 +716,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
allFiles = allFiles.concat(updatedFileList)
dirCount++
if (dirCount === totalDirs) {
respondWithContent(JSON.stringify(allFiles), response)
respondWithContent(JSON.stringify(allFiles), httpResponse)
}
}
}
@@ -692,7 +725,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
console.log(err.stack)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
}
@@ -723,13 +756,13 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
let filePath = process.env.PROJECTS_PATH + path
respondWithFile(filePath, response)
respondWithFile(filePath, httpResponse)
}
break
case 'Storage':
{
respondWithFile(process.env.STORAGE_PATH + '/' + request.url.substring(9), response)
respondWithFile(process.env.STORAGE_PATH + '/' + httpRequest.url.substring(9), httpResponse)
}
break
@@ -784,7 +817,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
allLibraries = allLibraries.concat(updatedFileList)
projectsCount++
if (projectsCount === projects.length) {
respondWithContent(JSON.stringify(allLibraries), response)
respondWithContent(JSON.stringify(allLibraries), httpResponse)
}
return
}
@@ -793,7 +826,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), httpResponse)
return
}
}
@@ -827,9 +860,9 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onFileRead(err, schema) {
if (err) {
respondWithContent(undefined, response)
respondWithContent(undefined, httpResponse)
} else {
respondWithContent(schema, response)
respondWithContent(schema, httpResponse)
}
}
}
@@ -847,7 +880,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
fileContent = fileContent.replace('HTTP_INTERFACE_PORT', process.env.HTTP_INTERFACE_PORT)
fileContent = fileContent.replace('HTTP_INTERFACE_PORT', process.env.HTTP_INTERFACE_PORT)
fileContent = fileContent.replace('HTTP_INTERFACE_PORT', process.env.HTTP_INTERFACE_PORT)
respondWithContent(fileContent, response, 'text/css')
respondWithContent(fileContent, httpResponse, 'text/css')
} catch (err) {
console.log('[ERROR] httpInterface -> mainCSS -> File Not Found: ' + fileName + ' or Error = ' + err.stack)
}
@@ -869,7 +902,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
let fileContent = file.toString()
fileContent = fileContent.replace('HTTP_INTERFACE_PORT', process.env.HTTP_INTERFACE_PORT)
respondWithContent(fileContent, response)
respondWithContent(fileContent, httpResponse)
} catch (err) {
console.log('[ERROR] httpInterface -> homePage -> File Not Found: ' + fileName + ' or Error = ' + err.stack)
}
@@ -878,17 +911,16 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
console.log(err)
}
} else {
respondWithFile(process.env.PATH_TO_UI + '/' + requestParameters[1], response)
respondWithFile(process.env.PATH_TO_UI + '/' + requestParameters[1], httpResponse)
}
}
}
function respondWithFile(fileName, response) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> respondWithFile -> Entering function.') }
function respondWithFile(fileName, httpResponse) {
let fs = require('fs')
if (fileName.indexOf('undefined') > 0) {
console.log('[WRN] httpInterface -> respondWithFile -> Received request for undefined file. ')
respondWithContent(undefined, response)
console.log('[WRN] httpInterface -> respondWithFile -> Received httpRequest for undefined file. ')
respondWithContent(undefined, httpResponse)
} else {
try {
fs.readFile(fileName, onFileRead)
@@ -896,10 +928,10 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onFileRead(err, file) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> respondWithFile -> onFileRead -> Entering function.') }
if (!err) {
respondWithContent(file.toString(), response)
respondWithContent(file.toString(), httpResponse)
} else {
//console.log('File requested not found: ' + fileName)
respondWithContent(undefined, response)
respondWithContent(undefined, httpResponse)
}
}
} catch (err) {
@@ -908,33 +940,33 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
}
function respondWithContent(content, response, contentType) {
function respondWithContent(content, httpResponse, contentType) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> respondWithContent -> Entering function.') }
try {
response.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
response.setHeader('Pragma', 'no-cache') // HTTP 1.0.
response.setHeader('Expires', '0') // Proxies.
response.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains.
httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0.
httpResponse.setHeader('Expires', '0') // Proxies.
httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains.
if (content !== undefined) {
if (contentType !== undefined) {
response.writeHead(200, { 'Content-Type': contentType })
httpResponse.writeHead(200, { 'Content-Type': contentType })
} else {
response.writeHead(200, { 'Content-Type': 'text/html' })
httpResponse.writeHead(200, { 'Content-Type': 'text/html' })
}
response.write(content)
httpResponse.write(content)
} else {
response.writeHead(404, { 'Content-Type': 'text/html' })
response.write('The specified key does not exist.')
httpResponse.writeHead(404, { 'Content-Type': 'text/html' })
httpResponse.write('The specified key does not exist.')
}
response.end('\n')
httpResponse.end('\n')
} catch (err) {
returnEmptyArray(response)
returnEmptyArray(httpResponse)
}
}
function respondWithImage(fileName, response) {
function respondWithImage(fileName, httpResponse) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> respondWithImage -> Entering function.') }
let fs = require('fs')
@@ -947,13 +979,13 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
return
}
try {
response.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
response.setHeader('Pragma', 'no-cache') // HTTP 1.0.
response.setHeader('Expires', '0') // Proxies.
response.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains.
httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0.
httpResponse.setHeader('Expires', '0') // Proxies.
httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains.
response.writeHead(200, { 'Content-Type': 'image/png' })
response.end(file, 'binary')
httpResponse.writeHead(200, { 'Content-Type': 'image/png' })
httpResponse.end(file, 'binary')
} catch (err) {
console.log('[ERROR] httpInterface -> respondWithImage -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack)
}
@@ -963,7 +995,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
}
function respondWithFont(fileName, response) {
function respondWithFont(fileName, httpResponse) {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> respondWithBinary -> Entering function.') }
let fs = require('fs')
@@ -978,17 +1010,17 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack)
return
}
response.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
response.setHeader('Pragma', 'no-cache') // HTTP 1.0.
response.setHeader('Expires', '0') // Proxies.
response.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains.
httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0.
httpResponse.setHeader('Expires', '0') // Proxies.
httpResponse.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains.
if (fileName.indexOf('2') < 0) {
response.writeHead(200, { 'Content-Type': 'font/woff' })
httpResponse.writeHead(200, { 'Content-Type': 'font/woff' })
} else {
response.writeHead(200, { 'Content-Type': 'font/woff2' })
httpResponse.writeHead(200, { 'Content-Type': 'font/woff2' })
}
response.end(file, 'binary')
httpResponse.end(file, 'binary')
} catch (err) {
console.log('[ERROR] httpInterface -> respondWithBinary -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err.stack)
}
@@ -998,16 +1030,16 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
}
function returnEmptyArray(response) {
function returnEmptyArray(httpResponse) {
try {
if (CONSOLE_LOG === true) { console.log('[INFO] httpInterface -> returnEmptyArray -> Entering function.') }
response.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
response.setHeader('Pragma', 'no-cache') // HTTP 1.0.
response.setHeader('Expires', '0') // Proxies.
httpResponse.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate') // HTTP 1.1.
httpResponse.setHeader('Pragma', 'no-cache') // HTTP 1.0.
httpResponse.setHeader('Expires', '0') // Proxies.
response.writeHead(200, { 'Content-Type': 'text/html' })
response.write('[]')
response.end('\n')
httpResponse.writeHead(200, { 'Content-Type': 'text/html' })
httpResponse.write('[]')
httpResponse.end('\n')
} catch (err) {
console.log('[ERROR] httpInterface -> returnEmptyArray -> err.stack ' + err.stack)
}
+105 -25
View File
@@ -4,6 +4,8 @@ exports.newWeb3Server = function newWeb3Server() {
let thisObject = {
getNetworkClientStatus: getNetworkClientStatus,
createWalletAccount: createWalletAccount,
getWalletBalances: getWalletBalances,
initialize: initialize,
finalize: finalize,
run: run
@@ -35,30 +37,11 @@ exports.newWeb3Server = function newWeb3Server() {
if (web3 === undefined) {
/* This means that we never got connected to this client before */
switch (interface) {
case 'ws': {
web3 = new Web3('ws://' + host + ':' + port + '');
break
}
case 'http': {
web3 = new Web3('http://' + host + ':' + port + '');
break
}
default: {
return { error: 'Interface parameter must be eithre ws or http. Received ' + interface }
}
}
return await testConnection()
async function testConnection() {
let accounts = await web3.eth.getAccounts()
if (web3.currentProvider.connected === false) {
return { error: 'Connection to ' + key + ' could not be established.' }
} else {
return await returnStatus(web3)
}
let web3 = await connectToClient(key, host, port, interface)
if (web3.error !== undefined) {
return web3
}
return await returnStatus(web3)
} else {
return await returnStatus(web3)
}
@@ -72,8 +55,105 @@ exports.newWeb3Server = function newWeb3Server() {
status.result = 'Ok'
return status
}
} catch(err) {
return { error: 'Could not connect to ' + key }
} catch (err) {
return { error: 'Could not connect to ' + key + '. ' + err.message + '.' }
}
}
async function createWalletAccount(entropy) {
try {
let web3 = new Web3()
let account = web3.eth.accounts.create()
return {
address: account.address,
privateKey: account.privateKey,
result: 'Ok'
}
} catch (err) {
return { error: 'Could not create the account. ' + err.message }
}
}
async function getWalletBalances(host, port, interface, walletDefinition) {
let key = 'host (' + host + ') at port (' + port + ') via interface (' + interface + ')'
try {
let web3 = web3Map.get(key)
if (web3 === undefined) {
/* This means that we never got connected to this client before */
let web3 = await connectToClient(key, host, port, interface)
if (web3.error !== undefined) {
return web3
}
return await returnAccountBalances(web3)
} else {
return await returnAccountBalances(web3)
}
async function returnAccountBalances(web3) {
let responseData = {}
for (let i = 0; i < walletDefinition.walletAccounts.length; i++) {
let walletAccount = walletDefinition.walletAccounts[i]
if (walletAccount.ethBalance !== undefined) {
try {
walletAccount.ethBalance.value = await web3.eth.getBalance(walletAccount.config.address)
} catch (err) {
if (err.message.indexOf('Provided address') >= 0) {
walletAccount.error = 'Invalid configured address.'
} else {
walletAccount.error = err.message
}
}
}
for (let j = 0; j < walletAccount.tokenBalances.length; j++) {
let tokenBalance = walletAccount.tokenBalances[j]
tokenBalance.value = 0
}
}
responseData.result = 'Ok'
responseData.walletDefinition = walletDefinition
return responseData
}
} catch (err) {
return { error: 'Could not connect to ' + key + '. ' + err.message + '.' }
}
}
async function connectToClient(key, host, port, interface) {
let web3
switch (interface) {
case 'ws': {
web3 = new Web3('ws://' + host + ':' + port + '');
break
}
case 'http': {
web3 = new Web3('http://' + host + ':' + port + '');
break
}
default: {
return { error: 'Interface parameter must be either ws or http. Received ' + interface }
}
}
return await testConnection()
async function testConnection() {
let accounts = await web3.eth.getAccounts()
if (web3.currentProvider.connected === false) {
return { error: 'Connection to ' + key + ' could not be established.' }
} else {
web3Map.set(key, web3)
return web3
}
}
}
}