bitcoin-request
Version:
Do Bitcoin request on Bitcoin node
62 lines (42 loc) • 1.18 kB
JavaScript
;
const execa = require( 'execa' );
const isLivenetMode = (process.env.BITCOIN_REQUEST_MODE === 'livenet');
module.exports = Object.freeze(({
command,
livenet = isLivenetMode
}) => {
if( typeof command === 'string' ) {
command = [ command ];
}
const envObject = {};
const formattedCommand = livenet ? command : (
[ '-testnet' ].concat( command )
);
return execa(
'bitcoin-cli',
formattedCommand,
{
env: envObject,
}
).then( response => {
if(
!response ||
response.failed ||
response.timedOut ||
response.isCanceled ||
response.killed ||
(Number(response.exitCode) !== 0)
) {
const errorMessage = (
'error in bitcoin-request, '+
'using the following args: ' +
JSON.stringify( formattedCommand ) +
', error: ' +
JSON.stringify( response )
);
console.log( errorMessage );
throw response;
}
return response.stdout;
});
});