diff --git a/example/index.js b/example/index.js index 40f4326f..4f4ea9d1 100755 --- a/example/index.js +++ b/example/index.js @@ -7,13 +7,21 @@ process.title = 'bitcoind.js'; var util = require('util'); +var fs = require('fs'); var argv = require('optimist').argv; +var rimraf = require('rimraf'); /** * bitcoind */ -var bitcoind = require('../')(); +if (fs.existsSync(process.env.HOME + '/.libbitcoind-example')) { + rimraf.sync(process.env.HOME + '/.libbitcoind-example'); +} + +var bitcoind = require('../')({ + directory: '~/.libbitcoind-example' +}); var genesisBlock = '0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; var genesisTx = '0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'; diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 17b74d9f..37027650 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -8,6 +8,8 @@ var net = require('net'); var EventEmitter = require('events').EventEmitter; var bitcoindjs = require('../build/Release/bitcoindjs.node'); var util = require('util'); +var fs = require('fs'); +var mkdirp = require('mkdirp'); /** * Bitcoin @@ -28,9 +30,37 @@ function Bitcoin(options) { EventEmitter.call(this); - this.options = options; + this.options = options || {}; this.wallet = Wallet; + if (typeof this.options === 'string') { + this.options = { datadir: this.options }; + } + + if (this.options.directory) { + this.options.datadir = this.options.directory; + delete this.options.directory; + } + + this.options.datadir = this.options.datadir.replace(/^~/, process.env.HOME); + + this.config = this.datadir + '/bitcoin.conf'; + + if (!fs.existsSync(this.options.datadir)) { + mkdirp.sync(this.options.datadir); + } + + if (!fs.existsSync(this.config)) { + var password = Math.random().toString(36).slice(2) + + Math.random().toString(36).slice(2) + + Math.random().toString(36).slice(2); + fs.writeFileSync(this.config, '' + + 'rpcuser=bitcoinrpc\n' + + 'rpcpassword=' + password + '\n' + + '\n' + ); + } + Bitcoin.global = this; Object.keys(exports).forEach(function(key) { @@ -65,8 +95,13 @@ Bitcoin.prototype.start = function(options, callback) { if (!callback) { callback = options; + options = null; + } + + if (!options) { options = {}; } + if (!callback) { callback = utils.NOOP; } @@ -82,6 +117,12 @@ Bitcoin.prototype.start = function(options, callback) { var exitCaught = none; var errorCaught = none; + Object.keys(this.options).forEach(function(key) { + if (options[key] == null) { + options[key] = self.options[key]; + } + }); + this.log_pipe = bitcoindjs.start(options, function(err, status) { self._started = true; diff --git a/package.json b/package.json index cf470719..5b006146 100644 --- a/package.json +++ b/package.json @@ -18,10 +18,11 @@ ], "dependencies": { "nan": "1.3.0", - "bn.js": "^0.10.0" + "mkdirp": "0.5.0" }, "devDependencies": { "mocha": "~1.16.2", - "optimist": "0.6.0" + "optimist": "0.6.0", + "rimraf": "2.2.8" } }