ensure prefix.
This commit is contained in:
parent
26969d823a
commit
6a644be03f
27
lib/bcoin.js
27
lib/bcoin.js
@ -11,9 +11,26 @@ bcoin.isBrowser =
|
|||||||
(typeof process !== 'undefined' && process.browser)
|
(typeof process !== 'undefined' && process.browser)
|
||||||
|| typeof window !== 'undefined';
|
|| typeof window !== 'undefined';
|
||||||
|
|
||||||
|
bcoin.prefix = process.env.BCOIN_PREFIX || process.env.HOME + '/.bcoin';
|
||||||
bcoin.debug = +process.env.BCOIN_DEBUG === 1;
|
bcoin.debug = +process.env.BCOIN_DEBUG === 1;
|
||||||
bcoin.debugFile = +process.env.BCOIN_DEBUGFILE !== 0;
|
bcoin.debugFile = +process.env.BCOIN_DEBUGFILE !== 0;
|
||||||
|
|
||||||
|
bcoin.ensurePrefix = function ensurePrefix() {
|
||||||
|
if (!bcoin.fs)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (bcoin._ensured)
|
||||||
|
return;
|
||||||
|
|
||||||
|
bcoin._ensured = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
bcoin.fs.statSync(bcoin.prefix);
|
||||||
|
} catch (e) {
|
||||||
|
bcoin.fs.mkdirSync(bcoin.prefix, 0750);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
bcoin.bn = require('bn.js');
|
bcoin.bn = require('bn.js');
|
||||||
bcoin.elliptic = require('elliptic');
|
bcoin.elliptic = require('elliptic');
|
||||||
|
|
||||||
@ -32,16 +49,6 @@ if (!bcoin.isBrowser) {
|
|||||||
bcoin.hash = require('hash.js');
|
bcoin.hash = require('hash.js');
|
||||||
}
|
}
|
||||||
|
|
||||||
bcoin.dir = process.env.HOME + '/.bcoin';
|
|
||||||
|
|
||||||
if (bcoin.fs) {
|
|
||||||
try {
|
|
||||||
bcoin.fs.statSync(bcoin.dir, 0o750);
|
|
||||||
} catch (e) {
|
|
||||||
bcoin.fs.mkdirSync(bcoin.dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bcoin.ecdsa = bcoin.elliptic.ec('secp256k1');
|
bcoin.ecdsa = bcoin.elliptic.ec('secp256k1');
|
||||||
assert(!bcoin.ecdsa.signature);
|
assert(!bcoin.ecdsa.signature);
|
||||||
bcoin.ecdsa.signature = require('elliptic/lib/elliptic/ec/signature');
|
bcoin.ecdsa.signature = require('elliptic/lib/elliptic/ec/signature');
|
||||||
|
|||||||
@ -32,8 +32,10 @@ function BlockDB(options) {
|
|||||||
|
|
||||||
this.file = options.indexFile;
|
this.file = options.indexFile;
|
||||||
|
|
||||||
|
bcoin.ensurePrefix();
|
||||||
|
|
||||||
if (!this.file)
|
if (!this.file)
|
||||||
this.file = bcoin.dir + '/index-' + network.type + '.db';
|
this.file = bcoin.prefix + '/index-' + network.type + '.db';
|
||||||
|
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
|
||||||
@ -1078,8 +1080,10 @@ function BlockData(options) {
|
|||||||
this.options = options;
|
this.options = options;
|
||||||
this.file = options.blockFile;
|
this.file = options.blockFile;
|
||||||
|
|
||||||
|
bcoin.ensurePrefix();
|
||||||
|
|
||||||
if (!this.file)
|
if (!this.file)
|
||||||
this.file = bcoin.dir + '/block-' + network.type + '.db';
|
this.file = bcoin.prefix + '/block-' + network.type + '.db';
|
||||||
|
|
||||||
this.bufferPool = { used: {} };
|
this.bufferPool = { used: {} };
|
||||||
this.size = 0;
|
this.size = 0;
|
||||||
|
|||||||
@ -34,7 +34,7 @@ function ChainDB(chain, options) {
|
|||||||
this.file = options.file;
|
this.file = options.file;
|
||||||
|
|
||||||
if (!this.file)
|
if (!this.file)
|
||||||
this.file = bcoin.dir + '/chain-' + network.type + '.db';
|
this.file = bcoin.prefix + '/chain-' + network.type + '.db';
|
||||||
|
|
||||||
this.heightLookup = {};
|
this.heightLookup = {};
|
||||||
this.queue = {};
|
this.queue = {};
|
||||||
@ -81,6 +81,8 @@ ChainDB.prototype._init = function _init() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bcoin.ensurePrefix();
|
||||||
|
|
||||||
if (+process.env.BCOIN_FRESH === 1) {
|
if (+process.env.BCOIN_FRESH === 1) {
|
||||||
try {
|
try {
|
||||||
fs.unlinkSync(this.file);
|
fs.unlinkSync(this.file);
|
||||||
|
|||||||
@ -766,8 +766,9 @@ utils.debug = function debug() {
|
|||||||
|
|
||||||
if (bcoin.debugFile && bcoin.fs) {
|
if (bcoin.debugFile && bcoin.fs) {
|
||||||
if (!bcoin._debug) {
|
if (!bcoin._debug) {
|
||||||
|
bcoin.ensurePrefix();
|
||||||
bcoin._debug = bcoin.fs.createWriteStream(
|
bcoin._debug = bcoin.fs.createWriteStream(
|
||||||
bcoin.dir + '/debug.log', { flags: 'a' });
|
bcoin.prefix + '/debug.log', { flags: 'a' });
|
||||||
}
|
}
|
||||||
msg = utils.format(args, false);
|
msg = utils.format(args, false);
|
||||||
bcoin._debug.write(process.pid + ': ' + msg);
|
bcoin._debug.write(process.pid + ': ' + msg);
|
||||||
|
|||||||
@ -37,11 +37,13 @@ function WalletDB(options) {
|
|||||||
this.dir = options.dir;
|
this.dir = options.dir;
|
||||||
this.type = options.type;
|
this.type = options.type;
|
||||||
|
|
||||||
|
bcoin.ensurePrefix();
|
||||||
|
|
||||||
if (!this.file)
|
if (!this.file)
|
||||||
this.file = bcoin.dir + '/wallet-' + network.type + '.db';
|
this.file = bcoin.prefix + '/wallet-' + network.type + '.db';
|
||||||
|
|
||||||
if (!this.dir)
|
if (!this.dir)
|
||||||
this.dir = bcoin.dir + '/wallet-' + network.type;
|
this.dir = bcoin.prefix + '/wallet-' + network.type;
|
||||||
|
|
||||||
if (!this.type)
|
if (!this.type)
|
||||||
this.type = 'leveldb';
|
this.type = 'leveldb';
|
||||||
@ -66,9 +68,9 @@ WalletDB.prototype._init = function _init() {
|
|||||||
if (this.type === 'file') {
|
if (this.type === 'file') {
|
||||||
if (bcoin.fs) {
|
if (bcoin.fs) {
|
||||||
try {
|
try {
|
||||||
bcoin.fs.statSync(this.dir, 0o750);
|
bcoin.fs.statSync(this.dir);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
bcoin.fs.mkdirSync(this.dir);
|
bcoin.fs.mkdirSync(this.dir, 0750);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (+process.env.BCOIN_FRESH === 1) {
|
if (+process.env.BCOIN_FRESH === 1) {
|
||||||
@ -160,7 +162,7 @@ WalletDB.prototype.saveFile = function saveFile(id, json, callback) {
|
|||||||
|
|
||||||
options = {
|
options = {
|
||||||
encoding: 'utf8',
|
encoding: 'utf8',
|
||||||
mode: 0o600
|
mode: 0600
|
||||||
};
|
};
|
||||||
|
|
||||||
fs.writeFile(file, json, options, function(err) {
|
fs.writeFile(file, json, options, function(err) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user