diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 00000000..9d37e492 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,42 @@ +{ + "bitwise": false, + "browser": true, + "camelcase": false, + "curly": true, + "devel": false, + "eqeqeq": true, + "esnext": true, + "freeze": true, + "immed": true, + "indent": 2, + "latedef": true, + "newcap": false, + "noarg": true, + "node": true, + "noempty": true, + "nonew": true, + "quotmark": "single", + "regexp": true, + "smarttabs": false, + "strict": true, + "trailing": true, + "undef": true, + "unused": true, + "maxparams": 4, + "maxstatements": 15, + "maxcomplexity": 10, + "maxdepth": 3, + "maxlen": 120, + "multistr": true, + "predef": [ + "after", + "afterEach", + "before", + "beforeEach", + "describe", + "exports", + "it", + "module", + "require" + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 1b275d8e..8dbc5d61 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,20 @@ npm install var BitcoinNode = require('bitcoind.js'); var configuration = { - directory: '~/.bitcoin', + datadir: '~/.bitcoin', testnet: true }; var node = new BitcoinNode(configuration); +node.on('ready', function() { + console.log('Bitcoin Node Ready'); +}); + +node.on('error', function(err) { + console.error(err); +}); + node.chain.on('addblock', function(block) { console.log('New Best Tip:', block.hash); }); diff --git a/benchmarks/index.js b/benchmarks/index.js index dbe0e980..574d512c 100644 --- a/benchmarks/index.js +++ b/benchmarks/index.js @@ -27,7 +27,7 @@ var fixtureData = { }; var bitcoind = require('../').daemon({ - directory: '~/.bitcoin', + datadir: process.env.BITCOINDJS_DIR || '~/.bitcoin', testnet: true }); diff --git a/example/node.js b/example/node.js index bafc2e37..7d50950d 100644 --- a/example/node.js +++ b/example/node.js @@ -6,32 +6,16 @@ var chainlib = require('chainlib'); var log = chainlib.log; //log.debug = function() {}; -var privkey = 'tprv8ZgxMBicQKsPdj1QowoT9z1tY5Et38qaMjCHZVoPdPFb6narfmYkqTygEVHfUmY78k3HcaEpkyNCAQDANaXtwNe1HLFvcA7nqYj1B7wTSTo'; - var configuration = { - db: { - xprivkey: privkey, - path: './bitcoind.db' - }, - p2p: { - addrs: [ - { - ip: { - v4: '127.0.0.1' - }, - port: 8333 - } - ], - dnsSeed: false - }, - testnet: false + datadir: process.env.BITCOINDJS_DIR || '~/.bitcoin', + testnet: true }; var node = new BitcoinNode(configuration); var startHeight; var count = 100; -var times = Array(count); +var times = new Array(count); node.on('ready', function() { times[node.chain.tip.__height % count] = Date.now(); @@ -52,4 +36,4 @@ node.chain.on('addblock', function(block) { } times[node.chain.tip.__height % count] = Date.now(); -}); \ No newline at end of file +}); diff --git a/lib/node.js b/lib/node.js index 833f4c4b..6c94173b 100644 --- a/lib/node.js +++ b/lib/node.js @@ -6,12 +6,14 @@ var Block = require('./block'); var DB = require('./db'); var chainlib = require('chainlib'); var P2P = chainlib.P2P; +var fs = require('fs'); var BaseNode = chainlib.Node; var util = require('util'); var log = chainlib.log; var bitcore = require('bitcore'); var Networks = bitcore.Networks; var _ = bitcore.deps._; +var $ = bitcore.util.preconditions; var genesis = require('./genesis.json'); var daemon = require('./daemon'); @@ -24,6 +26,7 @@ util.inherits(Node, BaseNode); Node.prototype._loadConfiguration = function(config) { var self = this; + this._loadBitcoinConf(config); this._loadBitcoind(config); Node.super_.prototype._loadConfiguration.call(self, config); }; @@ -47,13 +50,25 @@ Node.prototype.setSyncStrategy = function(strategy) { }; +Node.prototype._loadBitcoinConf = function(config) { + var datadir = config.datadir.replace(/^~/, process.env.HOME); + this.bitcoinConfiguration = {}; + var file = fs.readFileSync(datadir + '/bitcoin.conf'); + var unparsed = file.toString().split('\n'); + for(var i = 0; i < unparsed.length; i++) { + var line = unparsed[i]; + if (!line.match(/^\#/) && line.match(/\=/)) { + var option = line.split('='); + this.bitcoinConfiguration[option[0]] = option[1]; + } + } +}; + Node.prototype._loadBitcoind = function(config) { var bitcoindConfig = {}; - if (config.testnet) { - bitcoindConfig.directory = '~/.bitcoin/testnet3'; - } else { - bitcoindConfig.directory = '~/.bitcoin'; - } + $.checkArgument(config.datadir, 'Please specify "datadir" in configuration options'); + bitcoindConfig.datadir = config.datadir; + bitcoindConfig.testnet = config.testnet; // start the bitcoind daemon this.bitcoind = daemon(bitcoindConfig); @@ -104,6 +119,7 @@ Node.prototype._loadNetwork = function(config) { } else { this.network = Networks.get('livenet'); } + $.checkState(this.network, 'Unrecognized network'); }; Node.prototype._loadDB = function(config) { @@ -116,6 +132,14 @@ Node.prototype._loadDB = function(config) { config.db = {}; } + // Store the additional indexes in a new directory + // based on the network configuration and the datadir + var datadir = config.datadir.replace(/^~/, process.env.HOME); + if (this.network === Networks.testnet) { + config.db.path = datadir + '/testnet3/bitcoindjs.db'; + } else if (this.network === Networks.livenet) { + config.db.path = datadir + '/bitcoindjs.db'; + } config.db.network = this.network; this.db = new DB(config.db); @@ -127,6 +151,23 @@ Node.prototype._loadP2P = function(config) { } config.p2p.noListen = true; config.p2p.network = this.network; + + // We only want to directly connect via p2p to the trusted bitcoind daemon + var port = 8333; + if (this.bitcoinConfiguration.port) { + port = this.bitcoinConfiguration.port; + } else if (this.network === Networks.testnet) { + port = 18333; + } + config.p2p.addrs = [ + { + ip: { + v4: '127.0.0.1' + }, + port: port + } + ]; + config.p2p.dnsSeed = false; config.p2p.Transaction = this.db.Transaction; config.p2p.Block = this.Block; config.p2p.disableSync = true; // Disable p2p syncing and instead use bitcoind sync diff --git a/package.json b/package.json index b7cdd308..74b63809 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "scripts": { "preinstall": "./bin/build-libbitcoind", "install": "./bin/build-bindings", - "start": "export LD_LIBRARY_PATH=`./platform/os.sh osdir` && node example", + "start": "node example", "debug_install": "./bin/build-libbitcoind debug && ./bin/build-bindings debug", "test": "NODE_ENV=test mocha --recursive", "coverage": "istanbul cover _mocha -- --recursive"