coding style: standard
This commit is contained in:
parent
33d4ff9079
commit
fe1ab3fc52
18
Readme.md
18
Readme.md
@ -1,4 +1,20 @@
|
||||
# node-bitcoin [](https://travis-ci.org/freewil/node-bitcoin)
|
||||
# node-bitcoin
|
||||
[![travis][travis-image]][travis-url]
|
||||
[![npm][npm-image]][npm-url]
|
||||
[![downloads][downloads-image]][downloads-url]
|
||||
[![js-standard-style][standard-image]][standard-url]
|
||||
|
||||
[travis-image]: https://travis-ci.org/freewil/node-bitcoin.svg?branch=master
|
||||
[travis-url]: https://travis-ci.org/freewil/node-bitcoin
|
||||
|
||||
[npm-image]: https://img.shields.io/npm/v/bitcoin.svg?style=flat
|
||||
[npm-url]: https://npmjs.org/package/bitcoin
|
||||
|
||||
[downloads-image]: https://img.shields.io/npm/dm/bitcoin.svg?style=flat
|
||||
[downloads-url]: https://npmjs.org/package/bitcoin
|
||||
|
||||
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat
|
||||
[standard-url]: http://standardjs.com
|
||||
|
||||
node-bitcoin is a simple wrapper for the Bitcoin client's JSON-RPC API.
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ module.exports = {
|
||||
getBalance: 'getbalance',
|
||||
getBestBlockHash: 'getbestblockhash', // bitcoind v0.9.0+
|
||||
getBlock: 'getblock',
|
||||
getBlockchainInfo : 'getblockchaininfo', // bitcoind v0.9.2+
|
||||
getBlockchainInfo: 'getblockchaininfo', // bitcoind v0.9.2+
|
||||
getBlockCount: 'getblockcount',
|
||||
getBlockHash: 'getblockhash',
|
||||
getBlockTemplate: 'getblocktemplate', // bitcoind v0.7.0+
|
||||
@ -82,4 +82,4 @@ module.exports = {
|
||||
walletLock: 'walletlock',
|
||||
walletPassphrase: 'walletpassphrase',
|
||||
walletPassphraseChange: 'walletpassphrasechange'
|
||||
};
|
||||
}
|
||||
|
||||
76
lib/index.js
76
lib/index.js
@ -1,62 +1,58 @@
|
||||
var commands = require('./commands'),
|
||||
rpc = require('./jsonrpc');
|
||||
var commands = require('./commands')
|
||||
var rpc = require('./jsonrpc')
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// ===----------------------------------------------------------------------===//
|
||||
// Client
|
||||
//===----------------------------------------------------------------------===//
|
||||
function Client(opts) {
|
||||
this.rpc = new rpc.Client(opts);
|
||||
// ===----------------------------------------------------------------------===//
|
||||
function Client (opts) {
|
||||
this.rpc = new rpc.Client(opts)
|
||||
}
|
||||
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// ===----------------------------------------------------------------------===//
|
||||
// cmd
|
||||
//===----------------------------------------------------------------------===//
|
||||
Client.prototype.cmd = function() {
|
||||
var args = [].slice.call(arguments);
|
||||
var cmd = args.shift();
|
||||
// ===----------------------------------------------------------------------===//
|
||||
Client.prototype.cmd = function () {
|
||||
var args = [].slice.call(arguments)
|
||||
var cmd = args.shift()
|
||||
|
||||
callRpc(cmd, args, this.rpc);
|
||||
callRpc(cmd, args, this.rpc)
|
||||
}
|
||||
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// ===----------------------------------------------------------------------===//
|
||||
// callRpc
|
||||
//===----------------------------------------------------------------------===//
|
||||
function callRpc(cmd, args, rpc) {
|
||||
var fn = args[args.length-1];
|
||||
// ===----------------------------------------------------------------------===//
|
||||
function callRpc (cmd, args, rpc) {
|
||||
var fn = args[args.length - 1]
|
||||
|
||||
// If the last argument is a callback, pop it from the args list
|
||||
if (typeof fn === 'function') {
|
||||
args.pop();
|
||||
args.pop()
|
||||
} else {
|
||||
fn = function() {};
|
||||
fn = function () {}
|
||||
}
|
||||
|
||||
rpc.call(cmd, args, function(){
|
||||
var args = [].slice.call(arguments);
|
||||
args.unshift(null);
|
||||
fn.apply(this, args);
|
||||
}, function(err){
|
||||
fn(err);
|
||||
});
|
||||
rpc.call(cmd, args, function () {
|
||||
var args = [].slice.call(arguments)
|
||||
args.unshift(null)
|
||||
fn.apply(this, args)
|
||||
}, function (err) {
|
||||
fn(err)
|
||||
})
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// ===----------------------------------------------------------------------===//
|
||||
// Initialize wrappers
|
||||
//===----------------------------------------------------------------------===//
|
||||
(function() {
|
||||
|
||||
// ===----------------------------------------------------------------------===//
|
||||
(function () {
|
||||
for (var protoFn in commands) {
|
||||
(function(protoFn) {
|
||||
Client.prototype[protoFn] = function() {
|
||||
var args = [].slice.call(arguments);
|
||||
callRpc(commands[protoFn], args, this.rpc);
|
||||
};
|
||||
})(protoFn);
|
||||
(function (protoFn) {
|
||||
Client.prototype[protoFn] = function () {
|
||||
var args = [].slice.call(arguments)
|
||||
callRpc(commands[protoFn], args, this.rpc)
|
||||
}
|
||||
})(protoFn)
|
||||
}
|
||||
|
||||
})();
|
||||
})()
|
||||
|
||||
// Export!
|
||||
module.exports.Client = Client;
|
||||
module.exports.Client = Client
|
||||
|
||||
147
lib/jsonrpc.js
147
lib/jsonrpc.js
@ -1,36 +1,36 @@
|
||||
var http = require('http'),
|
||||
https = require('https');
|
||||
var http = require('http')
|
||||
var https = require('https')
|
||||
|
||||
var Client = function(opts) {
|
||||
this.opts = opts || {};
|
||||
this.http = this.opts.ssl ? https : http;
|
||||
};
|
||||
var Client = function (opts) {
|
||||
this.opts = opts || {}
|
||||
this.http = this.opts.ssl ? https : http
|
||||
}
|
||||
|
||||
Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
var time = Date.now();
|
||||
var requestJSON;
|
||||
Client.prototype.call = function (method, params, callback, errback, path) {
|
||||
var time = Date.now()
|
||||
var requestJSON
|
||||
|
||||
if (Array.isArray(method)) {
|
||||
// multiple rpc batch call
|
||||
requestJSON = [];
|
||||
method.forEach(function(batchCall, i) {
|
||||
requestJSON = []
|
||||
method.forEach(function (batchCall, i) {
|
||||
requestJSON.push({
|
||||
id: time + '-' + i,
|
||||
method: batchCall.method,
|
||||
params: batchCall.params
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
} else {
|
||||
// single rpc call
|
||||
requestJSON = {
|
||||
id: time,
|
||||
method: method,
|
||||
params: params
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// First we encode the request into JSON
|
||||
var requestJSON = JSON.stringify(requestJSON);
|
||||
requestJSON = JSON.stringify(requestJSON)
|
||||
|
||||
// prepare request options
|
||||
var requestOptions = {
|
||||
@ -44,113 +44,112 @@ Client.prototype.call = function(method, params, callback, errback, path) {
|
||||
},
|
||||
agent: false,
|
||||
rejectUnauthorized: this.opts.ssl && this.opts.sslStrict !== false
|
||||
};
|
||||
}
|
||||
|
||||
if (this.opts.ssl && this.opts.sslCa) {
|
||||
requestOptions.ca = this.opts.sslCa;
|
||||
requestOptions.ca = this.opts.sslCa
|
||||
}
|
||||
|
||||
// use HTTP auth if user and password set
|
||||
if (this.opts.user && this.opts.pass) {
|
||||
requestOptions.auth = this.opts.user + ':' + this.opts.pass;
|
||||
requestOptions.auth = this.opts.user + ':' + this.opts.pass
|
||||
}
|
||||
|
||||
// Now we'll make a request to the server
|
||||
var cbCalled = false;
|
||||
var request = this.http.request(requestOptions);
|
||||
var cbCalled = false
|
||||
var request = this.http.request(requestOptions)
|
||||
|
||||
// start request timeout timer
|
||||
var reqTimeout = setTimeout(function() {
|
||||
if (cbCalled) return;
|
||||
cbCalled = true;
|
||||
request.abort();
|
||||
var err = new Error('ETIMEDOUT');
|
||||
err.code = 'ETIMEDOUT';
|
||||
errback(err);
|
||||
}, this.opts.timeout || 30000);
|
||||
var reqTimeout = setTimeout(function () {
|
||||
if (cbCalled) return
|
||||
cbCalled = true
|
||||
request.abort()
|
||||
var err = new Error('ETIMEDOUT')
|
||||
err.code = 'ETIMEDOUT'
|
||||
errback(err)
|
||||
}, this.opts.timeout || 30000)
|
||||
|
||||
// set additional timeout on socket in case of remote freeze after sending headers
|
||||
request.setTimeout(this.opts.timeout || 30000, function() {
|
||||
if (cbCalled) return;
|
||||
cbCalled = true;
|
||||
request.abort();
|
||||
request.setTimeout(this.opts.timeout || 30000, function () {
|
||||
if (cbCalled) return
|
||||
cbCalled = true
|
||||
request.abort()
|
||||
var err = new Error('ESOCKETTIMEDOUT')
|
||||
err.code = 'ESOCKETTIMEDOUT'
|
||||
errback(err);
|
||||
});
|
||||
errback(err)
|
||||
})
|
||||
|
||||
request.on('error', function(err) {
|
||||
if (cbCalled) return;
|
||||
cbCalled = true;
|
||||
clearTimeout(reqTimeout);
|
||||
errback(err);
|
||||
});
|
||||
request.on('error', function (err) {
|
||||
if (cbCalled) return
|
||||
cbCalled = true
|
||||
clearTimeout(reqTimeout)
|
||||
errback(err)
|
||||
})
|
||||
|
||||
request.on('response', function(response) {
|
||||
clearTimeout(reqTimeout);
|
||||
request.on('response', function (response) {
|
||||
clearTimeout(reqTimeout)
|
||||
|
||||
// We need to buffer the response chunks in a nonblocking way.
|
||||
var buffer = '';
|
||||
response.on('data', function(chunk) {
|
||||
buffer = buffer + chunk;
|
||||
});
|
||||
var buffer = ''
|
||||
response.on('data', function (chunk) {
|
||||
buffer = buffer + chunk
|
||||
})
|
||||
// When all the responses are finished, we decode the JSON and
|
||||
// depending on whether it's got a result or an error, we call
|
||||
// emitSuccess or emitError on the promise.
|
||||
response.on('end', function() {
|
||||
var err;
|
||||
response.on('end', function () {
|
||||
var err
|
||||
|
||||
if (cbCalled) return;
|
||||
cbCalled = true;
|
||||
if (cbCalled) return
|
||||
cbCalled = true
|
||||
|
||||
try {
|
||||
var decoded = JSON.parse(buffer);
|
||||
var decoded = JSON.parse(buffer)
|
||||
} catch (e) {
|
||||
if (response.statusCode !== 200) {
|
||||
err = new Error('Invalid params, response status code: ' + response.statusCode);
|
||||
err.code = -32602;
|
||||
errback(err);
|
||||
err = new Error('Invalid params, response status code: ' + response.statusCode)
|
||||
err.code = -32602
|
||||
errback(err)
|
||||
} else {
|
||||
err = new Error('Problem parsing JSON response from server');
|
||||
err.code = -32603;
|
||||
errback(err);
|
||||
err = new Error('Problem parsing JSON response from server')
|
||||
err.code = -32603
|
||||
errback(err)
|
||||
}
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
if (!Array.isArray(decoded)) {
|
||||
decoded = [decoded];
|
||||
decoded = [decoded]
|
||||
}
|
||||
|
||||
// iterate over each response, normally there will be just one
|
||||
// unless a batch rpc call response is being processed
|
||||
decoded.forEach(function(decodedResponse, i) {
|
||||
decoded.forEach(function (decodedResponse, i) {
|
||||
if (decodedResponse.hasOwnProperty('error') && decodedResponse.error != null) {
|
||||
if (errback) {
|
||||
err = new Error(decodedResponse.error.message || '');
|
||||
err = new Error(decodedResponse.error.message || '')
|
||||
if (decodedResponse.error.code) {
|
||||
err.code = decodedResponse.error.code;
|
||||
err.code = decodedResponse.error.code
|
||||
}
|
||||
errback(err);
|
||||
errback(err)
|
||||
}
|
||||
} else if (decodedResponse.hasOwnProperty('result')) {
|
||||
if (callback) {
|
||||
callback(decodedResponse.result, response.headers);
|
||||
callback(decodedResponse.result, response.headers)
|
||||
}
|
||||
} else {
|
||||
if (errback) {
|
||||
err = new Error(decodedResponse.error.message || '');
|
||||
err = new Error(decodedResponse.error.message || '')
|
||||
if (decodedResponse.error.code) {
|
||||
err.code = decodedResponse.error.code;
|
||||
err.code = decodedResponse.error.code
|
||||
}
|
||||
errback(err);
|
||||
errback(err)
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
request.end(requestJSON)
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
request.end(requestJSON);
|
||||
};
|
||||
|
||||
module.exports.Client = Client;
|
||||
module.exports.Client = Client
|
||||
|
||||
@ -14,7 +14,8 @@
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"clone": "^1.0.2",
|
||||
"mocha": "^2.3.3"
|
||||
"mocha": "^2.3.3",
|
||||
"standard": "^5.3.1"
|
||||
},
|
||||
"optionalDependencies": {},
|
||||
"repository": {
|
||||
@ -25,6 +26,7 @@
|
||||
"node": ">= 0.10.0"
|
||||
},
|
||||
"scripts": {
|
||||
"pretest": "standard --verbose",
|
||||
"test": "make test"
|
||||
},
|
||||
"bugs": {
|
||||
|
||||
500
test/index.js
500
test/index.js
@ -1,294 +1,292 @@
|
||||
var assert = require('assert'),
|
||||
clone = require('clone'),
|
||||
http = require('http'),
|
||||
bitcoin = require('../'),
|
||||
config = require('./config');
|
||||
/* global describe, it */
|
||||
|
||||
var assert = require('assert')
|
||||
var clone = require('clone')
|
||||
var http = require('http')
|
||||
var bitcoin = require('../')
|
||||
var config = require('./config')
|
||||
|
||||
var test = {
|
||||
account: 'test'
|
||||
};
|
||||
}
|
||||
|
||||
var makeClient = function makeClient() {
|
||||
return new bitcoin.Client(config);
|
||||
};
|
||||
var makeClient = function makeClient () {
|
||||
return new bitcoin.Client(config)
|
||||
}
|
||||
|
||||
var notEmpty = function notEmpty(data) {
|
||||
if (data === 0) return;
|
||||
assert.ok(data);
|
||||
};
|
||||
var notEmpty = function notEmpty (data) {
|
||||
if (data === 0) return
|
||||
assert.ok(data)
|
||||
}
|
||||
|
||||
var makeServer = function(port) {
|
||||
var server = http.createServer();
|
||||
server.listen(port);
|
||||
return server;
|
||||
};
|
||||
var makeServer = function (port) {
|
||||
var server = http.createServer()
|
||||
server.listen(port)
|
||||
return server
|
||||
}
|
||||
|
||||
describe('Client', function() {
|
||||
describe('Client', function () {
|
||||
describe('getAccountAddress()', function () {
|
||||
it('should be able to get an account address', function (done) {
|
||||
var client = makeClient()
|
||||
client.getAccountAddress(test.account, function (err, address) {
|
||||
assert.ifError(err)
|
||||
assert.ok(address)
|
||||
client.getAccount(address, function (err, account) {
|
||||
assert.ifError(err)
|
||||
assert.equal(account, test.account)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAccountAddress()', function() {
|
||||
it('should be able to get an account address', function(done) {
|
||||
var client = makeClient();
|
||||
client.getAccountAddress(test.account, function(err, address) {
|
||||
assert.ifError(err);
|
||||
assert.ok(address);
|
||||
client.getAccount(address, function(err, account) {
|
||||
assert.ifError(err);
|
||||
assert.equal(account, test.account);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('listTransactions()', function () {
|
||||
it('should be able to listTransactions with specific count', function (done) {
|
||||
var client = makeClient()
|
||||
client.listTransactions(test.account, 15, function (err, txs) {
|
||||
assert.ifError(err)
|
||||
assert.ok(txs)
|
||||
assert.ok(Array.isArray(txs))
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('listTransactions()', function() {
|
||||
it('should be able to listTransactions with specific count', function(done) {
|
||||
var client = makeClient();
|
||||
client.listTransactions(test.account, 15, function(err, txs) {
|
||||
assert.ifError(err);
|
||||
assert.ok(txs);
|
||||
assert.ok(Array.isArray(txs));
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should be able to listTransactions without specific count', function (done) {
|
||||
var client = makeClient()
|
||||
client.listTransactions(test.account, function (err, txs) {
|
||||
assert.ifError(err)
|
||||
assert.ok(txs)
|
||||
assert.ok(Array.isArray(txs))
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to listTransactions without specific count', function(done) {
|
||||
var client = makeClient();
|
||||
client.listTransactions(test.account, function(err, txs) {
|
||||
assert.ifError(err);
|
||||
assert.ok(txs);
|
||||
assert.ok(Array.isArray(txs));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('getNewAddress()', function () {
|
||||
it('should be able to get new address', function (done) {
|
||||
var client = makeClient()
|
||||
client.getNewAddress(test.account, function (err, address) {
|
||||
assert.ifError(err)
|
||||
client.getAddressesByAccount(test.account, function (err, addresses) {
|
||||
assert.ifError(err)
|
||||
assert.ok(addresses && addresses.length > 0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getNewAddress()', function() {
|
||||
it('should be able to get new address', function(done) {
|
||||
var client = makeClient();
|
||||
client.getNewAddress(test.account, function(err, address) {
|
||||
assert.ifError(err);
|
||||
client.getAddressesByAccount(test.account, function(err, addresses) {
|
||||
assert.ifError(err);
|
||||
assert.ok(addresses && addresses.length > 0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('getBalance()', function () {
|
||||
it('should return balance without any args', function (done) {
|
||||
var client = makeClient()
|
||||
client.getBalance(function (err, balance) {
|
||||
assert.ifError(err)
|
||||
assert.ok(typeof balance === 'number')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getBalance()', function() {
|
||||
it('should return balance without any args', function(done) {
|
||||
var client = makeClient();
|
||||
client.getBalance(function(err, balance) {
|
||||
assert.ifError(err);
|
||||
assert.ok(typeof balance === 'number');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('getDifficulty()', function () {
|
||||
it('should get difficulty', function (done) {
|
||||
var client = makeClient()
|
||||
client.getDifficulty(function (err, difficulty) {
|
||||
assert.ifError(err)
|
||||
assert.ok(typeof difficulty === 'number')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getDifficulty()', function() {
|
||||
it('should get difficulty', function(done) {
|
||||
var client = makeClient();
|
||||
client.getDifficulty(function(err, difficulty) {
|
||||
assert.ifError(err);
|
||||
assert.ok(typeof difficulty === 'number');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('getInfo()', function () {
|
||||
it('should get info', function (done) {
|
||||
var client = makeClient()
|
||||
client.getInfo(function (err, info, headers) {
|
||||
assert.ifError(err)
|
||||
notEmpty(info)
|
||||
assert.ok(info.errors === '')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getInfo()', function() {
|
||||
it('should get info', function(done) {
|
||||
var client = makeClient();
|
||||
client.getInfo(function(err, info, headers) {
|
||||
assert.ifError(err);
|
||||
notEmpty(info);
|
||||
assert.ok(info.errors === '');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('help()', function () {
|
||||
it('should return help', function (done) {
|
||||
var client = makeClient()
|
||||
client.help(function (err, help) {
|
||||
assert.ifError(err)
|
||||
notEmpty(help)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('help()', function() {
|
||||
it('should return help', function(done) {
|
||||
var client = makeClient();
|
||||
client.help(function(err, help) {
|
||||
assert.ifError(err);
|
||||
notEmpty(help);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('bitcoin related error should be an Error object', function (done) {
|
||||
var client = makeClient()
|
||||
client.cmd('nomethod', function (err, expectedValue) {
|
||||
assert.ok(err instanceof Error)
|
||||
assert.equal(err.message, 'Method not found')
|
||||
assert.equal(err.code, -32601)
|
||||
assert.equal(expectedValue, undefined)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('bitcoin related error should be an Error object', function(done) {
|
||||
var client = makeClient();
|
||||
client.cmd('nomethod', function(err, expectedValue) {
|
||||
assert.ok(err instanceof Error);
|
||||
assert.equal(err.message, 'Method not found');
|
||||
assert.equal(err.code, -32601);
|
||||
assert.equal(expectedValue, undefined);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('running batch of rpc calls', function(done) {
|
||||
this.timeout(5000);
|
||||
var batch = [];
|
||||
it('running batch of rpc calls', function (done) {
|
||||
this.timeout(5000)
|
||||
var batch = []
|
||||
for (var i = 0; i < 10; ++i) {
|
||||
batch.push({
|
||||
method: 'getnewaddress',
|
||||
params: [test.account]
|
||||
});
|
||||
})
|
||||
}
|
||||
var client = makeClient();
|
||||
var batchCallbackCount = 0;
|
||||
client.cmd(batch, function(err, address) {
|
||||
assert.ifError(err);
|
||||
assert.ok(++batchCallbackCount <= 10);
|
||||
assert.ok(address);
|
||||
if (batchCallbackCount === 10) done();
|
||||
});
|
||||
});
|
||||
var client = makeClient()
|
||||
var batchCallbackCount = 0
|
||||
client.cmd(batch, function (err, address) {
|
||||
assert.ifError(err)
|
||||
assert.ok(++batchCallbackCount <= 10)
|
||||
assert.ok(address)
|
||||
if (batchCallbackCount === 10) done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('invalid credentials', function() {
|
||||
var badCredentials = clone(config);
|
||||
badCredentials.user = 'baduser';
|
||||
badCredentials.pass = 'badpwd';
|
||||
var client = new bitcoin.Client(badCredentials);
|
||||
describe('invalid credentials', function () {
|
||||
var badCredentials = clone(config)
|
||||
badCredentials.user = 'baduser'
|
||||
badCredentials.pass = 'badpwd'
|
||||
var client = new bitcoin.Client(badCredentials)
|
||||
|
||||
it('should still return client object', function(done) {
|
||||
assert.ok(client instanceof bitcoin.Client);
|
||||
done();
|
||||
});
|
||||
it('should still return client object', function (done) {
|
||||
assert.ok(client instanceof bitcoin.Client)
|
||||
done()
|
||||
})
|
||||
|
||||
it('should return status 401 with html', function(done) {
|
||||
client.getDifficulty(function(err, difficulty) {
|
||||
assert.ok(err instanceof Error);
|
||||
assert.equal(err.message, 'Invalid params, response status code: 401');
|
||||
assert.equal(err.code, -32602);
|
||||
assert.equal(difficulty, undefined);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should return status 401 with html', function (done) {
|
||||
client.getDifficulty(function (err, difficulty) {
|
||||
assert.ok(err instanceof Error)
|
||||
assert.equal(err.message, 'Invalid params, response status code: 401')
|
||||
assert.equal(err.code, -32602)
|
||||
assert.equal(difficulty, undefined)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('creating client on non-listening port', function() {
|
||||
var badPort = clone(config);
|
||||
badPort.port = 9897;
|
||||
badPort.user = 'baduser';
|
||||
badPort.pass = 'badpwd';
|
||||
var client = new bitcoin.Client(badPort);
|
||||
describe('creating client on non-listening port', function () {
|
||||
var badPort = clone(config)
|
||||
badPort.port = 9897
|
||||
badPort.user = 'baduser'
|
||||
badPort.pass = 'badpwd'
|
||||
var client = new bitcoin.Client(badPort)
|
||||
|
||||
it('will return client object', function(done) {
|
||||
assert.ok(client instanceof bitcoin.Client);
|
||||
done();
|
||||
});
|
||||
it('will return client object', function (done) {
|
||||
assert.ok(client instanceof bitcoin.Client)
|
||||
done()
|
||||
})
|
||||
|
||||
it('should not call callback more than once', function(done) {
|
||||
client.listSinceBlock(function(err, result) {
|
||||
assert.ok(err instanceof Error);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should not call callback more than once', function (done) {
|
||||
client.listSinceBlock(function (err, result) {
|
||||
assert.ok(err instanceof Error)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('request timeouts', function() {
|
||||
it('should occur by default after 30000ms', function(done) {
|
||||
this.timeout(31000);
|
||||
var server = makeServer(19998);
|
||||
var request;
|
||||
var response;
|
||||
server.on('request', function(req, res) {
|
||||
request = req;
|
||||
response = res;
|
||||
});
|
||||
describe('request timeouts', function () {
|
||||
it('should occur by default after 30000ms', function (done) {
|
||||
this.timeout(31000)
|
||||
var server = makeServer(19998)
|
||||
var request // eslint-disable-line no-unused-vars
|
||||
var response
|
||||
server.on('request', function (req, res) {
|
||||
request = req
|
||||
response = res
|
||||
})
|
||||
var client = new bitcoin.Client({
|
||||
host: 'localhost',
|
||||
port: 19998,
|
||||
user: 'admin1',
|
||||
pass: '123'
|
||||
});
|
||||
var start = Date.now();
|
||||
client.getInfo(function(err, info) {
|
||||
var delta = Date.now() - start;
|
||||
assert.ok(err instanceof Error);
|
||||
assert.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT');
|
||||
assert.ok(delta >= 30000, 'delta should be >= 30000: ' + delta);
|
||||
response.end();
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
var start = Date.now()
|
||||
client.getInfo(function (err, info) {
|
||||
var delta = Date.now() - start
|
||||
assert.ok(err instanceof Error)
|
||||
assert.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT')
|
||||
assert.ok(delta >= 30000, 'delta should be >= 30000: ' + delta)
|
||||
response.end()
|
||||
server.close()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should be customizable', function(done) {
|
||||
this.timeout(4500);
|
||||
var server = makeServer(19999);
|
||||
var request;
|
||||
var response;
|
||||
server.on('request', function(req, res) {
|
||||
request = req;
|
||||
response = res;
|
||||
});
|
||||
it('should be customizable', function (done) {
|
||||
this.timeout(4500)
|
||||
var server = makeServer(19999)
|
||||
var request // eslint-disable-line no-unused-vars
|
||||
var response
|
||||
server.on('request', function (req, res) {
|
||||
request = req
|
||||
response = res
|
||||
})
|
||||
var client = new bitcoin.Client({
|
||||
host: 'localhost',
|
||||
port: 19999,
|
||||
user: 'admin1',
|
||||
pass: '123',
|
||||
timeout: 2500
|
||||
});
|
||||
var start = Date.now();
|
||||
client.getInfo(function(err, info) {
|
||||
var delta = Date.now() - start;
|
||||
assert.ok(err instanceof Error);
|
||||
assert.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT');
|
||||
assert.ok(delta >= 2500, 'delta should be >= 2500:' + delta);
|
||||
response.end();
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
var start = Date.now()
|
||||
client.getInfo(function (err, info) {
|
||||
var delta = Date.now() - start
|
||||
assert.ok(err instanceof Error)
|
||||
assert.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT')
|
||||
assert.ok(delta >= 2500, 'delta should be >= 2500:' + delta)
|
||||
response.end()
|
||||
server.close()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
});
|
||||
describe('response headers', function () {
|
||||
var assertResHeaders = function (resHeaders) {
|
||||
assert.ok(resHeaders)
|
||||
assert.ok(resHeaders.server)
|
||||
assert.ok(/bitcoin/.test(resHeaders.server))
|
||||
}
|
||||
it('should be returned for no parameter calls', function (done) {
|
||||
var client = makeClient()
|
||||
client.getInfo(function (err, info, resHeaders) {
|
||||
assert.ifError(err)
|
||||
notEmpty(info)
|
||||
assert.ok(info.errors === '')
|
||||
assertResHeaders(resHeaders)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('response headers', function() {
|
||||
var assertResHeaders = function(resHeaders) {
|
||||
assert.ok(resHeaders);
|
||||
assert.ok(resHeaders.server);
|
||||
assert.ok(/bitcoin/.test(resHeaders.server));
|
||||
};
|
||||
it('should be returned for no parameter calls', function(done) {
|
||||
var client = makeClient();
|
||||
client.getInfo(function(err, info, resHeaders) {
|
||||
assert.ifError(err);
|
||||
notEmpty(info);
|
||||
assert.ok(info.errors === '');
|
||||
assertResHeaders(resHeaders);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should be returned for 1-parameter call', function (done) {
|
||||
var client = makeClient()
|
||||
client.getNewAddress(test.account, function (err, address, resHeaders) {
|
||||
assert.ifError(err)
|
||||
assert.ok(address)
|
||||
assertResHeaders(resHeaders)
|
||||
done()
|
||||
})
|
||||
|
||||
it('should be returned for 1-parameter call', function(done) {
|
||||
var client = makeClient();
|
||||
client.getNewAddress(test.account, function(err, address, resHeaders) {
|
||||
assert.ifError(err);
|
||||
assert.ok(address);
|
||||
assertResHeaders(resHeaders);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should be returned for 2-parameter call', function(done) {
|
||||
var client = makeClient();
|
||||
client.listTransactions(test.account, 15, function(err, txs, resHeaders) {
|
||||
assert.ifError(err);
|
||||
assert.ok(txs);
|
||||
assert.ok(Array.isArray(txs));
|
||||
assertResHeaders(resHeaders);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
it('should be returned for 2-parameter call', function (done) {
|
||||
var client = makeClient()
|
||||
client.listTransactions(test.account, 15, function (err, txs, resHeaders) {
|
||||
assert.ifError(err)
|
||||
assert.ok(txs)
|
||||
assert.ok(Array.isArray(txs))
|
||||
assertResHeaders(resHeaders)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,76 +1,75 @@
|
||||
var assert = require('assert'),
|
||||
bitcoin = require('../'),
|
||||
config = require('./config'),
|
||||
commands = require('../lib/commands');
|
||||
/* global describe, it */
|
||||
|
||||
var assert = require('assert')
|
||||
var bitcoin = require('../')
|
||||
var config = require('./config')
|
||||
var commands = require('../lib/commands')
|
||||
|
||||
var getHelpCommands = function(client, cb) {
|
||||
var commandRegex = /^([a-z]+)/;
|
||||
client.cmd('help', function(err, commandList) {
|
||||
if (err) return cb(err);
|
||||
var getHelpCommands = function (client, cb) {
|
||||
var commandRegex = /^([a-z]+)/
|
||||
client.cmd('help', function (err, commandList) {
|
||||
if (err) return cb(err)
|
||||
|
||||
var helpCommands = [];
|
||||
var helpCommands = []
|
||||
|
||||
// split up the command list by newlines
|
||||
var commandListLines = commandList.split('\n');
|
||||
var result;
|
||||
var commandListLines = commandList.split('\n')
|
||||
var result
|
||||
for (var i in commandListLines) {
|
||||
result = commandRegex.exec(commandListLines[i]);
|
||||
if (!result) continue;
|
||||
helpCommands.push(result[1]);
|
||||
result = commandRegex.exec(commandListLines[i])
|
||||
if (!result) continue
|
||||
helpCommands.push(result[1])
|
||||
}
|
||||
cb(null, helpCommands);
|
||||
});
|
||||
};
|
||||
cb(null, helpCommands)
|
||||
})
|
||||
}
|
||||
|
||||
describe('Client Commands', function() {
|
||||
|
||||
it('should have all the commands listed by `help`', function(done) {
|
||||
var client = new bitcoin.Client(config);
|
||||
getHelpCommands(client, function(err, helpCommands) {
|
||||
assert.ifError(err);
|
||||
describe('Client Commands', function () {
|
||||
it('should have all the commands listed by `help`', function (done) {
|
||||
var client = new bitcoin.Client(config)
|
||||
getHelpCommands(client, function (err, helpCommands) {
|
||||
assert.ifError(err)
|
||||
|
||||
for (var i in helpCommands) {
|
||||
var found = false;
|
||||
var found = false
|
||||
for (var j in commands) {
|
||||
if (commands[j] === helpCommands[i]) {
|
||||
found = true;
|
||||
break;
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.ok(found, 'missing command found in `help`: ' + helpCommands[i]);
|
||||
assert.ok(found, 'missing command found in `help`: ' + helpCommands[i])
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not have any commands not listed by `help`', function(done) {
|
||||
var client = new bitcoin.Client(config);
|
||||
getHelpCommands(client, function(err, helpCommands) {
|
||||
assert.ifError(err);
|
||||
it('should not have any commands not listed by `help`', function (done) {
|
||||
var client = new bitcoin.Client(config)
|
||||
getHelpCommands(client, function (err, helpCommands) {
|
||||
assert.ifError(err)
|
||||
|
||||
for (var i in commands) {
|
||||
var found = false;
|
||||
var found = false
|
||||
for (var j in helpCommands) {
|
||||
if (commands[i] === helpCommands[j]) {
|
||||
found = true;
|
||||
break;
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// ignore commands not found in help because they are hidden
|
||||
// if the wallet isn't encrypted
|
||||
var ignore = ['walletlock', 'walletpassphrase', 'walletpassphrasechange'];
|
||||
var ignore = ['walletlock', 'walletpassphrase', 'walletpassphrasechange']
|
||||
if (~ignore.indexOf(commands[i])) {
|
||||
assert.ok(!found, 'command found in `help`: ' + commands[i]);
|
||||
assert.ok(!found, 'command found in `help`: ' + commands[i])
|
||||
} else {
|
||||
assert.ok(found, 'command not found in `help`: ' + commands[i]);
|
||||
assert.ok(found, 'command not found in `help`: ' + commands[i])
|
||||
}
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
103
test/ssl.js
103
test/ssl.js
@ -1,62 +1,63 @@
|
||||
var assert = require('assert'),
|
||||
fs = require('fs'),
|
||||
clone = require('clone'),
|
||||
bitcoin = require('../'),
|
||||
config = require('./config');
|
||||
/* global describe, it */
|
||||
|
||||
var getInfo = function(opts, cb) {
|
||||
var client = new bitcoin.Client(opts);
|
||||
client.getInfo(cb);
|
||||
};
|
||||
var assert = require('assert')
|
||||
var fs = require('fs')
|
||||
var clone = require('clone')
|
||||
var bitcoin = require('../')
|
||||
var config = require('./config')
|
||||
|
||||
describe('Client SSL', function() {
|
||||
it('use sslStrict by default', function(done) {
|
||||
var opts = clone(config);
|
||||
opts.ssl = true;
|
||||
getInfo(opts, function(err, info) {
|
||||
assert.ok(err instanceof Error);
|
||||
var getInfo = function (opts, cb) {
|
||||
var client = new bitcoin.Client(opts)
|
||||
client.getInfo(cb)
|
||||
}
|
||||
|
||||
describe('Client SSL', function () {
|
||||
it('use sslStrict by default', function (done) {
|
||||
var opts = clone(config)
|
||||
opts.ssl = true
|
||||
getInfo(opts, function (err, info) {
|
||||
assert.ok(err instanceof Error)
|
||||
// node v0.11 adds `code` param to this error
|
||||
// and uses a user-friendly `message`
|
||||
// continue using err.message for v0.8 and v0.10
|
||||
assert.equal(err.code || err.message, 'DEPTH_ZERO_SELF_SIGNED_CERT');
|
||||
done();
|
||||
});
|
||||
});
|
||||
assert.equal(err.code || err.message, 'DEPTH_ZERO_SELF_SIGNED_CERT')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('strictSSL should fail with self-signed certificate', function(done) {
|
||||
var opts = clone(config);
|
||||
opts.ssl = true;
|
||||
opts.sslStrict = true;
|
||||
getInfo(opts, function(err, info) {
|
||||
assert.ok(err instanceof Error);
|
||||
it('strictSSL should fail with self-signed certificate', function (done) {
|
||||
var opts = clone(config)
|
||||
opts.ssl = true
|
||||
opts.sslStrict = true
|
||||
getInfo(opts, function (err, info) {
|
||||
assert.ok(err instanceof Error)
|
||||
// node v0.11 adds `code` param to this error
|
||||
// and uses a user-friendly `message`
|
||||
// continue using err.message for v0.8 and v0.10
|
||||
assert.equal(err.code || err.message, 'DEPTH_ZERO_SELF_SIGNED_CERT');
|
||||
done();
|
||||
});
|
||||
});
|
||||
assert.equal(err.code || err.message, 'DEPTH_ZERO_SELF_SIGNED_CERT')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('self-signed certificate with sslStrict false', function(done) {
|
||||
var opts = clone(config);
|
||||
opts.ssl = true;
|
||||
opts.sslStrict = false;
|
||||
getInfo(opts, function(err, info) {
|
||||
assert.ifError(err);
|
||||
assert.ok(info);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('self-signed certificate with sslStrict false', function (done) {
|
||||
var opts = clone(config)
|
||||
opts.ssl = true
|
||||
opts.sslStrict = false
|
||||
getInfo(opts, function (err, info) {
|
||||
assert.ifError(err)
|
||||
assert.ok(info)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('self-signed certificate with sslStrict and CA specified', function(done) {
|
||||
var opts = clone(config);
|
||||
opts.ssl = true;
|
||||
opts.sslCa = fs.readFileSync(__dirname + '/testnet-box/1/regtest/server.cert');
|
||||
getInfo(opts, function(err, info) {
|
||||
assert.ifError(err);
|
||||
assert.ok(info);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
it('self-signed certificate with sslStrict and CA specified', function (done) {
|
||||
var opts = clone(config)
|
||||
opts.ssl = true
|
||||
opts.sslCa = fs.readFileSync(__dirname + '/testnet-box/1/regtest/server.cert')
|
||||
getInfo(opts, function (err, info) {
|
||||
assert.ifError(err)
|
||||
assert.ok(info)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user