|
|
@ -77,7 +77,7 @@ module.exports = (shepherd) => { |
|
|
|
} else { |
|
|
|
const _mode = req.body.payload.mode === 'passthru' ? 'passthru' : 'default'; |
|
|
|
const _chain = req.body.payload.chain === 'KMD' ? null : req.body.payload.chain; |
|
|
|
const _params = req.body.payload.params ? ` ${req.body.payload.params}` : ''; |
|
|
|
let _params = req.body.payload.params ? ` ${req.body.payload.params}` : ''; |
|
|
|
let _cmd = req.body.payload.cmd; |
|
|
|
|
|
|
|
if (!shepherd.rpcConf[_chain]) { |
|
|
@ -85,91 +85,205 @@ module.exports = (shepherd) => { |
|
|
|
} |
|
|
|
|
|
|
|
if (_mode === 'default') { |
|
|
|
if (_cmd === 'debug' && |
|
|
|
_chain !== 'CHIPS') { |
|
|
|
if (shepherd.nativeCoindList[_chain.toLowerCase()]) { |
|
|
|
const _osHome = os.platform === 'win32' ? process.env.APPDATA : process.env.HOME; |
|
|
|
let coindDebugLogLocation; |
|
|
|
|
|
|
|
if (_chain === 'CHIPS') { |
|
|
|
coindDebugLogLocation = `${shepherd.chipsDir}/debug.log`; |
|
|
|
} else { |
|
|
|
coindDebugLogLocation = `${_osHome}/.${shepherd.nativeCoindList[_chain.toLowerCase()].bin.toLowerCase()}/debug.log`; |
|
|
|
} |
|
|
|
if (req.body.payload.rpc2cli) { |
|
|
|
let _coindCliBin = shepherd.komodocliBin; |
|
|
|
|
|
|
|
shepherd.readDebugLog(coindDebugLogLocation, 1) |
|
|
|
.then((result) => { |
|
|
|
const _obj = { |
|
|
|
msg: 'success', |
|
|
|
result: result, |
|
|
|
}; |
|
|
|
if (shepherd.nativeCoindList && |
|
|
|
_chain && |
|
|
|
shepherd.nativeCoindList[_chain.toLowerCase()]) { |
|
|
|
_coindCliBin = `${shepherd.coindRootDir}/${_chain.toLowerCase()}/${shepherd.nativeCoindList[_chain.toLowerCase()].bin.toLowerCase()}-cli`; |
|
|
|
} |
|
|
|
|
|
|
|
// shepherd.log('bitcoinrpc debug ====>');
|
|
|
|
// console.log(result);
|
|
|
|
console.log(`cmd ${_cmd}`); |
|
|
|
if (_params.indexOf('*')) { |
|
|
|
_params = _params.replace('*', '"*"'); |
|
|
|
} |
|
|
|
if (_params.indexOf(',') > -1) { |
|
|
|
_params = _params.split(','); |
|
|
|
} |
|
|
|
if (_cmd.indexOf('getaddressesbyaccount') > -1) { |
|
|
|
_cmd = 'getaddressesbyaccount ""'; |
|
|
|
} |
|
|
|
|
|
|
|
res.end(JSON.stringify(_obj)); |
|
|
|
}, (result) => { |
|
|
|
const _obj = { |
|
|
|
error: result, |
|
|
|
result: 'error', |
|
|
|
}; |
|
|
|
console.log(typeof _params); |
|
|
|
|
|
|
|
res.end(JSON.stringify(_obj)); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
res.end({ |
|
|
|
error: 'bitcoinrpc debug error', |
|
|
|
result: 'error', |
|
|
|
}); |
|
|
|
// console.log('bitcoinrpc debug error');
|
|
|
|
let _arg = (_chain ? ' -ac_name=' + _chain : '') + ' ' + _cmd + (typeof _params === 'object' ? _params.join(' ') : _params); |
|
|
|
|
|
|
|
console.log(_arg); |
|
|
|
|
|
|
|
if (shepherd.appConfig.dataDir.length) { |
|
|
|
_arg = `${_arg} -datadir=${shepherd.appConfig.dataDir + (_chain ? '/' + key : '')}`; |
|
|
|
} |
|
|
|
|
|
|
|
/*_arg = _arg.trim().split(' '); |
|
|
|
|
|
|
|
for (let i = 0; i < _arg.length; i++) { |
|
|
|
if (_arg[i].indexOf('getaddressesbyaccount') > -1) { |
|
|
|
_arg[i] = 'getaddressesbyaccount ""'; |
|
|
|
} |
|
|
|
}*/ |
|
|
|
|
|
|
|
shepherd.exec(`"${_coindCliBin}" ${_arg}`, (error, stdout, stderr) => { |
|
|
|
shepherd.log(`stdout: ${stdout}`); |
|
|
|
shepherd.log(`stderr: ${stderr}`); |
|
|
|
|
|
|
|
if (error !== null) { |
|
|
|
shepherd.log(`exec error: ${error}`); |
|
|
|
} |
|
|
|
|
|
|
|
let responseObj; |
|
|
|
|
|
|
|
/*error code: -5 |
|
|
|
error message: |
|
|
|
Invalid Komodo address*/ |
|
|
|
|
|
|
|
|
|
|
|
if (stderr) { |
|
|
|
let _res; |
|
|
|
let _error; |
|
|
|
|
|
|
|
if ((stderr.indexOf('{') > -1 && stderr.indexOf('}') > -1) || |
|
|
|
(stderr.indexOf('[') > -1 && stderr.indexOf(']') > -1)) { |
|
|
|
_res = JSON.parse(stderr); |
|
|
|
} else { |
|
|
|
_res = stderr.trim(); |
|
|
|
} |
|
|
|
|
|
|
|
if (stderr.indexOf('error code:') > -1) { |
|
|
|
_error = { |
|
|
|
code: stderr.substring(stderr.indexOf('error code:') + 11, stderr.indexOf('error message:') - stderr.indexOf('error code:')).trim(), |
|
|
|
message: stderr.substring(stderr.indexOf('error message:') + 15, stderr.length).trim(), |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
if (_error) { |
|
|
|
responseObj = { |
|
|
|
error: _error, |
|
|
|
}; |
|
|
|
} else { |
|
|
|
responseObj = { |
|
|
|
result: _res, |
|
|
|
}; |
|
|
|
} |
|
|
|
} else { |
|
|
|
let _res; |
|
|
|
let _error; |
|
|
|
|
|
|
|
if ((stdout.indexOf('{') > -1 && stdout.indexOf('}') > -1) || |
|
|
|
(stdout.indexOf('[') > -1 && stdout.indexOf(']') > -1)) { |
|
|
|
_res = JSON.parse(stdout); |
|
|
|
} else { |
|
|
|
_res = stdout.trim(); |
|
|
|
} |
|
|
|
|
|
|
|
if (stdout.indexOf('error code:') > -1) { |
|
|
|
_error = { |
|
|
|
code: stdout.substring(stdout.indexOf('error code:') + 11, stdout.indexOf('error message:') - stdout.indexOf('error code:')).trim(), |
|
|
|
message: stdout.substring(stdout.indexOf('error message:') + 15, stdout.length).trim(), |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
if (_error) { |
|
|
|
responseObj = { |
|
|
|
error: _error, |
|
|
|
}; |
|
|
|
} else { |
|
|
|
responseObj = { |
|
|
|
result: _res, |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
res.end(JSON.stringify(responseObj)); |
|
|
|
shepherd.killRogueProcess('komodo-cli'); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
if (_chain === 'CHIPS' && |
|
|
|
_cmd === 'debug') { |
|
|
|
_cmd = 'getblockchaininfo'; |
|
|
|
} |
|
|
|
if (_cmd === 'debug' && |
|
|
|
_chain !== 'CHIPS') { |
|
|
|
if (shepherd.nativeCoindList[_chain.toLowerCase()]) { |
|
|
|
const _osHome = os.platform === 'win32' ? process.env.APPDATA : process.env.HOME; |
|
|
|
let coindDebugLogLocation; |
|
|
|
|
|
|
|
if (_chain === 'CHIPS') { |
|
|
|
coindDebugLogLocation = `${shepherd.chipsDir}/debug.log`; |
|
|
|
} else { |
|
|
|
coindDebugLogLocation = `${_osHome}/.${shepherd.nativeCoindList[_chain.toLowerCase()].bin.toLowerCase()}/debug.log`; |
|
|
|
} |
|
|
|
|
|
|
|
let _body = { |
|
|
|
agent: 'bitcoinrpc', |
|
|
|
method: _cmd, |
|
|
|
}; |
|
|
|
shepherd.readDebugLog(coindDebugLogLocation, 1) |
|
|
|
.then((result) => { |
|
|
|
const _obj = { |
|
|
|
msg: 'success', |
|
|
|
result: result, |
|
|
|
}; |
|
|
|
|
|
|
|
// shepherd.log('bitcoinrpc debug ====>');
|
|
|
|
// console.log(result);
|
|
|
|
|
|
|
|
res.end(JSON.stringify(_obj)); |
|
|
|
}, (result) => { |
|
|
|
const _obj = { |
|
|
|
error: result, |
|
|
|
result: 'error', |
|
|
|
}; |
|
|
|
|
|
|
|
if (req.body.payload.params) { |
|
|
|
_body = { |
|
|
|
res.end(JSON.stringify(_obj)); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
res.end({ |
|
|
|
error: 'bitcoinrpc debug error', |
|
|
|
result: 'error', |
|
|
|
}); |
|
|
|
// console.log('bitcoinrpc debug error');
|
|
|
|
} |
|
|
|
} else { |
|
|
|
if (_chain === 'CHIPS' && |
|
|
|
_cmd === 'debug') { |
|
|
|
_cmd = 'getblockchaininfo'; |
|
|
|
} |
|
|
|
|
|
|
|
let _body = { |
|
|
|
agent: 'bitcoinrpc', |
|
|
|
method: _cmd, |
|
|
|
params: req.body.payload.params === ' ' ? [''] : req.body.payload.params, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
if (req.body.payload.chain) { |
|
|
|
const options = { |
|
|
|
url: `http://localhost:${shepherd.rpcConf[req.body.payload.chain].port}`, |
|
|
|
method: 'POST', |
|
|
|
auth: { |
|
|
|
user: shepherd.rpcConf[req.body.payload.chain].user, |
|
|
|
pass: shepherd.rpcConf[req.body.payload.chain].pass, |
|
|
|
}, |
|
|
|
body: JSON.stringify(_body), |
|
|
|
}; |
|
|
|
if (req.body.payload.params) { |
|
|
|
_body = { |
|
|
|
agent: 'bitcoinrpc', |
|
|
|
method: _cmd, |
|
|
|
params: req.body.payload.params === ' ' ? [''] : req.body.payload.params, |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
// send back body on both success and error
|
|
|
|
// this bit replicates iguana core's behaviour
|
|
|
|
shepherd.request(options, (error, response, body) => { |
|
|
|
if (response && |
|
|
|
response.statusCode && |
|
|
|
response.statusCode === 200) { |
|
|
|
res.end(body); |
|
|
|
} else { |
|
|
|
res.end(body ? body : JSON.stringify({ |
|
|
|
result: 'error', |
|
|
|
error: { |
|
|
|
code: -777, |
|
|
|
message: `unable to call method ${_cmd} at port ${shepherd.rpcConf[req.body.payload.chain].port}`, |
|
|
|
}, |
|
|
|
})); |
|
|
|
} |
|
|
|
}); |
|
|
|
if (req.body.payload.chain) { |
|
|
|
const options = { |
|
|
|
url: `http://localhost:${shepherd.rpcConf[req.body.payload.chain].port}`, |
|
|
|
method: 'POST', |
|
|
|
auth: { |
|
|
|
user: shepherd.rpcConf[req.body.payload.chain].user, |
|
|
|
pass: shepherd.rpcConf[req.body.payload.chain].pass, |
|
|
|
}, |
|
|
|
body: JSON.stringify(_body), |
|
|
|
}; |
|
|
|
|
|
|
|
// send back body on both success and error
|
|
|
|
// this bit replicates iguana core's behaviour
|
|
|
|
shepherd.request(options, (error, response, body) => { |
|
|
|
if (response && |
|
|
|
response.statusCode && |
|
|
|
response.statusCode === 200) { |
|
|
|
res.end(body); |
|
|
|
} else { |
|
|
|
res.end(body ? body : JSON.stringify({ |
|
|
|
result: 'error', |
|
|
|
error: { |
|
|
|
code: -777, |
|
|
|
message: `unable to call method ${_cmd} at port ${shepherd.rpcConf[req.body.payload.chain].port}`, |
|
|
|
}, |
|
|
|
})); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|