balance. walletdb.
This commit is contained in:
parent
5b3cc7c0d3
commit
e08b4cf563
@ -72,6 +72,9 @@ KeyRing.prototype.addKey = function addKey(key) {
|
|||||||
if (utils.indexOf(this.keys, key) !== -1)
|
if (utils.indexOf(this.keys, key) !== -1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (this.keys.length === this.n)
|
||||||
|
throw new Error('Cannot add more keys.');
|
||||||
|
|
||||||
utils.binaryInsert(this.keys, key, utils.cmp);
|
utils.binaryInsert(this.keys, key, utils.cmp);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,6 +84,9 @@ KeyRing.prototype.addKey = function addKey(key) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
KeyRing.prototype.removeKey = function removeKey(key) {
|
KeyRing.prototype.removeKey = function removeKey(key) {
|
||||||
|
if (this.keys.length === this.n)
|
||||||
|
throw new Error('Cannot remove key.');
|
||||||
|
|
||||||
utils.binaryRemove(this.keys, key, utils.cmp);
|
utils.binaryRemove(this.keys, key, utils.cmp);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -628,7 +634,7 @@ KeyRing.fromRaw = function fromRaw(data) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test an object to see if it is an KeyRing.
|
* Test whether an object is a KeyRing.
|
||||||
* @param {Object} obj
|
* @param {Object} obj
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -560,7 +560,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
self.walletdb.sync(tx, map, function(err) {
|
self.walletdb.syncOutputs(tx, map, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
@ -751,7 +751,7 @@ TXDB.prototype._confirm = function _confirm(tx, map, callback, force) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
self.walletdb.sync(tx, map, function(err) {
|
self.walletdb.syncOutputs(tx, map, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
|
|||||||
@ -918,13 +918,8 @@ Wallet.prototype.scan = function scan(getByAddress, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (result === 0) {
|
if (result === 0)
|
||||||
return self.save(function(err) {
|
return callback(null, total);
|
||||||
if (err)
|
|
||||||
return callback(err);
|
|
||||||
return callback(null, total);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
total += result;
|
total += result;
|
||||||
|
|
||||||
@ -1568,7 +1563,7 @@ function Account(options) {
|
|||||||
this.options = options;
|
this.options = options;
|
||||||
this.network = bcoin.network.get(options.network);
|
this.network = bcoin.network.get(options.network);
|
||||||
this.db = options.db;
|
this.db = options.db;
|
||||||
this.lookahead = options.lookahead != null ? options.lookahead : 5;
|
this.lookahead = Account.LOOKAHEAD;
|
||||||
|
|
||||||
this.id = options.id;
|
this.id = options.id;
|
||||||
this.name = options.name;
|
this.name = options.name;
|
||||||
@ -1612,6 +1607,13 @@ function Account(options) {
|
|||||||
|
|
||||||
utils.inherits(Account, EventEmitter);
|
utils.inherits(Account, EventEmitter);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Default address lookahead.
|
||||||
|
* @const {Number}
|
||||||
|
*/
|
||||||
|
|
||||||
|
Account.LOOKAHEAD = 5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open the account, register with the database.
|
* Open the account, register with the database.
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
|
|||||||
@ -143,36 +143,18 @@ WalletDB.prototype._init = function _init() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.tx.on('updated', function(tx, map) {
|
this.tx.on('updated', function(tx, map) {
|
||||||
var balances = {};
|
|
||||||
|
|
||||||
self.emit('updated', tx, map);
|
self.emit('updated', tx, map);
|
||||||
map.all.forEach(function(path) {
|
map.all.forEach(function(path) {
|
||||||
self.fire(path.id, 'updated', tx, path.name);
|
self.fire(path.id, 'updated', tx, path.name);
|
||||||
});
|
});
|
||||||
|
self.updateBalances(tx, map, function(err, balances) {
|
||||||
utils.forEachSerial(map.output, function(path, next) {
|
|
||||||
if (self.listeners('balance').length === 0
|
|
||||||
&& !self.hasListener(path.id, 'balance')) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (balances[path.id] != null)
|
|
||||||
return next();
|
|
||||||
|
|
||||||
self.getBalance(path.id, function(err, balance) {
|
|
||||||
if (err)
|
|
||||||
return next(err);
|
|
||||||
|
|
||||||
balances[path.id] = balance;
|
|
||||||
|
|
||||||
self.fire(path.id, 'balance', balance);
|
|
||||||
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
}, function(err) {
|
|
||||||
if (err)
|
if (err)
|
||||||
return self.emit('error', err);
|
return self.emit('error', err);
|
||||||
|
|
||||||
|
Object.keys(balances).forEach(function(id) {
|
||||||
|
self.fire(id, 'balance', balances[id]);
|
||||||
|
});
|
||||||
|
|
||||||
self.emit('balances', balances, map);
|
self.emit('balances', balances, map);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -191,7 +173,36 @@ WalletDB.prototype._init = function _init() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletDB.prototype.sync = function sync(tx, map, callback) {
|
WalletDB.prototype.updateBalances = function updateBalances(tx, map, callback) {
|
||||||
|
var self = this;
|
||||||
|
var balances = {};
|
||||||
|
|
||||||
|
utils.forEachSerial(map.output, function(path, next) {
|
||||||
|
if (self.listeners('balance').length === 0
|
||||||
|
&& !self.hasListener(path.id, 'balance')) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (balances[path.id] != null)
|
||||||
|
return next();
|
||||||
|
|
||||||
|
self.getBalance(path.id, function(err, balance) {
|
||||||
|
if (err)
|
||||||
|
return next(err);
|
||||||
|
|
||||||
|
balances[path.id] = balance;
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}, function(err) {
|
||||||
|
if (err)
|
||||||
|
return callback(err);
|
||||||
|
|
||||||
|
return callback(null, balances);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
WalletDB.prototype.syncOutputs = function syncOutputs(tx, map, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
utils.forEachSerial(map.output, function(path, next) {
|
utils.forEachSerial(map.output, function(path, next) {
|
||||||
self.syncOutputDepth(path.id, tx, next);
|
self.syncOutputDepth(path.id, tx, next);
|
||||||
@ -832,12 +843,12 @@ WalletDB.prototype.removeBlockSPV = function removeBlockSPV(block, callback) {
|
|||||||
|
|
||||||
callback = utils.ensure(callback);
|
callback = utils.ensure(callback);
|
||||||
|
|
||||||
this.tx.getHeightHashes(block.height, function(err, txs) {
|
this.tx.getHeightHashes(block.height, function(err, hashes) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
utils.forEachSerial(txs, function(tx, next) {
|
utils.forEachSerial(hashes, function(hash, next) {
|
||||||
self.tx.unconfirm(tx, next);
|
self.tx.unconfirm(hash, next);
|
||||||
}, callback);
|
}, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -895,21 +906,9 @@ WalletDB.prototype.syncOutputDepth = function syncOutputDepth(id, tx, callback)
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletDB.prototype.createAddress = function createAddress(id, change, callback) {
|
WalletDB.prototype.createAddress = function createAddress(id, name, change, callback) {
|
||||||
this.fetchWallet(id, callback, function(wallet, callback) {
|
this.fetchWallet(id, callback, function(wallet, callback) {
|
||||||
wallet.createAddress(change, callback);
|
wallet.createAddress(name, change, callback);
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
WalletDB.prototype.getReceiveAddress = function getReceiveAddress(id, callback) {
|
|
||||||
this.fetchWallet(id, callback, function(wallet, callback) {
|
|
||||||
callback(null, wallet.receiveAddress);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
WalletDB.prototype.getChangeAddress = function getChangeAddress(id, callback) {
|
|
||||||
this.fetchWallet(id, callback, function(wallet, callback) {
|
|
||||||
callback(null, wallet.changeAddress);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -947,15 +946,15 @@ WalletDB.prototype.createTX = function createTX(id, options, outputs, callback)
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletDB.prototype.addKey = function addKey(id, key, callback) {
|
WalletDB.prototype.addKey = function addKey(id, name, key, callback) {
|
||||||
this.fetchWallet(id, callback, function(wallet, callback) {
|
this.fetchWallet(id, callback, function(wallet, callback) {
|
||||||
wallet.addKey(key, callback);
|
wallet.addKey(name, key, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
WalletDB.prototype.removeKey = function removeKey(id, key, callback) {
|
WalletDB.prototype.removeKey = function removeKey(id, name, key, callback) {
|
||||||
this.fetchWallet(id, callback, function(wallet, callback) {
|
this.fetchWallet(id, callback, function(wallet, callback) {
|
||||||
wallet.removeKey(key, callback);
|
wallet.removeKey(name, key, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1013,7 +1012,8 @@ function serializePaths(out) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isAlpha(key) {
|
function isAlpha(key) {
|
||||||
return /^[a-zA-Z0-9]+$/.test(key);
|
// We allow /-~ (exclusive), 0-} (inclusive)
|
||||||
|
return /^[\u0030-\u007d]+$/.test(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -28,7 +28,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/indutny/bcoin",
|
"homepage": "https://github.com/indutny/bcoin",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.8.0"
|
"node": ">= 0.10.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bn.js": "4.11.0",
|
"bn.js": "4.11.0",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user