accounts.

This commit is contained in:
Christopher Jeffrey 2016-05-31 00:46:01 -07:00
parent 79d2471a33
commit 976fd46da5
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 1364 additions and 681 deletions

View File

@ -168,6 +168,7 @@ function Environment(options) {
this.hd = require('./hd');
this.keyring = require('./keyring');
this.wallet = require('./wallet');
this.account = this.wallet.Account;
this.walletdb = require('./walletdb');
this.provider = this.walletdb.Provider;
this.peer = require('./peer');

View File

@ -1129,22 +1129,35 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
*/
MTX.prototype.fill = function fill(coins, options) {
var result, i, change;
var result, i, change, changeAddress;
assert(this.inputs.length === 0, 'TX is already filled.');
assert(options, '`options` are required.');
assert(options.changeAddress, '`changeAddress` is required.');
if (!options)
options = {};
// Select necessary coins.
result = this.selectCoins(coins, options);
// We need a change address.
changeAddress = options.changeAddress;
// If change address is not available,
// send back to one of the coins' addresses.
for (i = 0; i < result.coins.length && !changeAddress; i++)
changeAddress = result.coins[i].getAddress();
// Will only happen in rare cases where
// we're redeeming all non-standard coins.
assert(changeAddress, 'No change address available.');
// Add coins to transaction.
for (i = 0; i < result.coins.length; i++)
this.addInput(result.coins[i]);
// Add a change output.
this.addOutput({
address: options.changeAddress,
address: changeAddress,
value: result.change
});

View File

@ -1726,35 +1726,31 @@ TXDB.prototype.getBalance = function getBalance(address, callback) {
/**
* @param {WalletID|WalletID[]} address - By address (can be null).
* @param {Number} now - Current time.
* @param {Number} age - Age delta (delete transactions older than `now - age`).
* @param {Function} callback
*/
TXDB.prototype.zap = function zap(address, now, age, callback, force) {
TXDB.prototype.zap = function zap(address, age, callback, force) {
var self = this;
if (typeof address !== 'string') {
force = callback;
callback = age;
age = now;
now = address;
age = address;
address = null;
}
var unlock = this._lock(zap, [address, now, age, callback], force);
var unlock = this._lock(zap, [address, age, callback], force);
if (!unlock)
return;
callback = utils.wrap(callback, unlock);
assert(utils.isNumber(now));
assert(utils.isNumber(age));
assert(now >= age);
return this.getRange(address, {
start: 0,
end: now - age
end: bcoin.now() - age
}, function(err, txs) {
if (err)
return callback(err);

View File

@ -4,6 +4,15 @@
* @global
*/
/**
* @typedef {Object} Path
* @property {String} name - Account name.
* @property {Number} account - Account index.
* @property {Number} change - Change chain.
* @property {Number} index - Address index.
* @global
*/
/**
* @typedef {Object} InvItem
* @property {Number|String} type - Inv type. See {@link constants.inv}.

View File

@ -2277,7 +2277,7 @@ utils.parallel = function parallel(stack, callback) {
for (i = 0; i < stack.length; i++) {
try {
if (stack[i].length >= 2) {
if (0 && stack[i].length >= 2) {
stack[i](error, next);
error = null;
} else {
@ -2306,6 +2306,7 @@ utils.serial = function serial(stack, callback) {
if (!cb)
return callback(err);
if (0)
if (cb.length >= 2) {
try {
return cb(err, next);

File diff suppressed because it is too large Load Diff

View File

@ -354,7 +354,7 @@ WalletDB.prototype.save = function save(wallet, callback) {
*/
WalletDB.prototype.getAccountIndex = function getAccountIndex(wid, name, callback) {
return this.db.get('a/' + wid + '/' + name, function(err, index) {
return this.db.get('i/' + wid + '/' + name, function(err, index) {
if (err && err.type !== 'NotFoundError')
return callback();
@ -370,7 +370,7 @@ WalletDB.prototype.getAccount = function getAccount(wid, id, callback) {
var aid = wid + '/' + id;
var account;
if (!id)
if (id == null)
return callback();
if (typeof id === 'string') {
@ -387,9 +387,6 @@ WalletDB.prototype.getAccount = function getAccount(wid, id, callback) {
this.db.get('a/' + aid, function(err, data) {
if (err && err.type !== 'NotFoundError')
return callback();
if (err)
return callback(err);
if (!data)
@ -398,7 +395,7 @@ WalletDB.prototype.getAccount = function getAccount(wid, id, callback) {
try {
data = bcoin.account.parseRaw(data);
data.db = self;
account = bcoin.account.fromRaw(data);
account = new bcoin.account(data);
} catch (e) {
return callback(e);
}
@ -435,16 +432,52 @@ WalletDB.prototype.remove = function remove(id, callback) {
WalletDB.prototype.saveAccount = function saveAccount(account, callback) {
var index = new Buffer(4);
this.db.put('a/' + account.wid + '/' + account.index, account.toRaw(), function(err) {
var batch = this.db.batch();
index.writeUInt32LE(account.accountIndex, 0, true);
batch.put('a/' + account.wid + '/' + account.accountIndex, account.toRaw());
batch.put('i/' + account.wid + '/' + account.name, index);
batch.write(callback);
};
WalletDB.prototype.createAccount = function createAccount(options, callback) {
var self = this;
var account;
this.hasAccount(options.wid, options.accountIndex, function(err, exists) {
if (err)
return callback(err);
index.writeUInt32LE(account.index, 0, true);
if (err)
return callback(err);
self.db.put('a/' + account.wid + '/' + account.name, index, callback);
if (exists)
return callback(new Error('account already exists.'));
options = utils.merge({}, options);
if (self.network.witness)
options.witness = options.witness !== false;
options.network = self.network;
options.db = self;
account = new bcoin.account(options);
account.open(function(err) {
if (err)
return callback(err);
return callback(null, account);
});
});
};
WalletDB.prototype.hasAccount = function hasAccount(wid, account, callback) {
if (!wid || account == null)
return callback(null, false);
this.db.has('a/' + wid + '/' + account, callback);
};
/**
* Create a new wallet, save to database, setup watcher.
* @param {Object} options - See {@link Wallet}.
@ -493,7 +526,7 @@ WalletDB.prototype.has = function has(id, callback) {
if (!id)
return callback(null, false);
this.db.hash('w/' + id, callback);
this.db.has('w/' + id, callback);
};
/**
@ -727,8 +760,8 @@ WalletDB.prototype.fillCoins = function fillCoins(tx, callback) {
* @see {@link TXDB#zap}.
*/
WalletDB.prototype.zap = function zap(id, now, age, callback) {
return this.tx.zap(id, now, age, callback);
WalletDB.prototype.zap = function zap(id, age, callback) {
return this.tx.zap(id, age, callback);
};
/**