Skip to content

Commit

Permalink
Swapped to old JS syntax to work on older node.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallewoof committed Sep 8, 2016
1 parent 12dd984 commit 16d811f
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions lib/bcrpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* RPC agent, based on bitcore's RpcClient.
*/

const http = require('http');
const https = require('https');
var http = require('http');
var https = require('https');

/**
* Source: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list
* @type {Object}
*/
const BC_RPC = {
var BC_RPC = {
addMultiSigAddress: [],
addNode: [],
backupWallet: [],
Expand Down Expand Up @@ -78,9 +78,12 @@ const BC_RPC = {
walletPassphraseChange: [],
};

const slice = (arr, start, end) => Array.prototype.slice.call(arr, start, end);
function slice(arr, start, end) {
return Array.prototype.slice.call(arr, start, end);
}

function RpcAgent(opts = {}) {
function RpcAgent(opts) {
opts = opts || {};
this.host = opts.host || '127.0.0.1';
this.port = opts.port || 8332;
this.user = opts.user || 'user';
Expand All @@ -89,21 +92,21 @@ function RpcAgent(opts = {}) {
}

function rpc(request, callback) {
const requestSerialized = JSON.stringify(request);
const auth = new Buffer(`${this.user}:${this.pass}`).toString('base64');
const options = {
var requestSerialized = JSON.stringify(request);
var auth = new Buffer(this.user + ':' + this.pass).toString('base64');
var options = {
host: this.host,
port: this.port,
path: '/',
method: 'POST',
};
let err = null;
const req = this.prot.request(options, (res) => {
let buf = '';
res.on('data', (data) => {
var err = null;
var req = this.prot.request(options, function(res) {
var buf = '';
res.on('data', function(data) {
buf += data;
});
res.on('end', () => {
res.on('end', function() {
if (res.statusCode === 401) {
return callback(new Error('bitcoin JSON-RPC connection rejected: 401 unauthorized'));
}
Expand All @@ -113,7 +116,7 @@ function rpc(request, callback) {
if (err) {
return callback(err);
}
let bufDeserialized;
var bufDeserialized;
try {
bufDeserialized = JSON.parse(buf);
} catch (e) {
Expand All @@ -122,26 +125,28 @@ function rpc(request, callback) {
return callback(bufDeserialized.error, bufDeserialized);
});
});
req.on('error', (e) => {
err = new Error(`Could not connect to bitcoin via RPC at \
host: ${this.host} port: ${this.port} Error: ${e.message}`);
req.on('error', function(e) {
err = new Error('Could not connect to bitcoin via RPC at host: ' + this.host + ' port: ' + this.port + ' Error: ' + e.message);
callback(err);
});

req.setHeader('Content-Length', requestSerialized.length);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Authorization', `Basic ${auth}`);
req.setHeader('Authorization', 'Basic ' + auth);
req.write(requestSerialized);
req.end();
}

for (const cmd of Object.keys(BC_RPC)) {
RpcAgent.prototype[cmd] = function rpccmd(...args) {
rpc.call(this, {
method: cmd.toLowerCase(),
params: slice(args, 0, args.length - 1),
}, args[args.length - 1]);
};
for (var cmd in BC_RPC) {
RpcAgent.prototype[cmd] = (function(cmd) {
return function rpccmd() {
var args = arguments;
rpc.call(this, {
method: cmd.toLowerCase(),
params: slice(args, 0, args.length - 1),
}, args[args.length - 1]);
};
})(cmd);
}

module.exports = RpcAgent;

0 comments on commit 16d811f

Please sign in to comment.