wallet: refactor and comments.
This commit is contained in:
parent
4f46bb3324
commit
10ee1dce6e
@ -74,7 +74,7 @@ function Wallet(db, options) {
|
||||
this.accountDepth = 0;
|
||||
this.token = constants.ZERO_HASH;
|
||||
this.tokenDepth = 0;
|
||||
this.tx = new TXDB(this);
|
||||
this.txdb = new TXDB(this);
|
||||
|
||||
this.account = null;
|
||||
|
||||
@ -184,7 +184,7 @@ Wallet.prototype.init = co(function* init(options) {
|
||||
|
||||
this.logger.info('Wallet initialized (%s).', this.id);
|
||||
|
||||
yield this.tx.open();
|
||||
yield this.txdb.open();
|
||||
});
|
||||
|
||||
/**
|
||||
@ -206,7 +206,7 @@ Wallet.prototype.open = co(function* open() {
|
||||
|
||||
this.logger.info('Wallet opened (%s).', this.id);
|
||||
|
||||
yield this.tx.open();
|
||||
yield this.txdb.open();
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1155,7 +1155,7 @@ Wallet.prototype._fund = co(function* fund(tx, options) {
|
||||
}
|
||||
|
||||
// Don't use any locked coins.
|
||||
coins = this.tx.filterLocked(coins);
|
||||
coins = this.txdb.filterLocked(coins);
|
||||
|
||||
tx.fund(coins, {
|
||||
selection: options.selection,
|
||||
@ -1609,7 +1609,7 @@ Wallet.prototype.sign = co(function* sign(tx, options) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.fillCoins = function fillCoins(tx) {
|
||||
return this.tx.fillCoins(tx);
|
||||
return this.txdb.fillCoins(tx);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1619,7 +1619,7 @@ Wallet.prototype.fillCoins = function fillCoins(tx) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.fillHistory = function fillHistory(tx) {
|
||||
return this.tx.fillHistory(tx);
|
||||
return this.txdb.fillHistory(tx);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1629,7 +1629,7 @@ Wallet.prototype.fillHistory = function fillHistory(tx) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.toDetails = function toDetails(tx) {
|
||||
return this.tx.toDetails(tx);
|
||||
return this.txdb.toDetails(tx);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1639,7 +1639,7 @@ Wallet.prototype.toDetails = function toDetails(tx) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.getDetails = function getDetails(tx) {
|
||||
return this.tx.getDetails(tx);
|
||||
return this.txdb.getDetails(tx);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1650,7 +1650,7 @@ Wallet.prototype.getDetails = function getDetails(tx) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.getCoin = function getCoin(hash, index) {
|
||||
return this.tx.getCoin(hash, index);
|
||||
return this.txdb.getCoin(hash, index);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1660,7 +1660,7 @@ Wallet.prototype.getCoin = function getCoin(hash, index) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.getTX = function getTX(hash) {
|
||||
return this.tx.getTX(hash);
|
||||
return this.txdb.getTX(hash);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -1681,7 +1681,7 @@ Wallet.prototype.addTX = function addTX(tx) {
|
||||
|
||||
Wallet.prototype.getHistory = co(function* getHistory(account) {
|
||||
account = yield this._getIndex(account);
|
||||
return this.tx.getHistory(account);
|
||||
return this.txdb.getHistory(account);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1692,7 +1692,7 @@ Wallet.prototype.getHistory = co(function* getHistory(account) {
|
||||
|
||||
Wallet.prototype.getCoins = co(function* getCoins(account) {
|
||||
account = yield this._getIndex(account);
|
||||
return yield this.tx.getCoins(account);
|
||||
return yield this.txdb.getCoins(account);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1703,7 +1703,7 @@ Wallet.prototype.getCoins = co(function* getCoins(account) {
|
||||
|
||||
Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(account) {
|
||||
account = yield this._getIndex(account);
|
||||
return yield this.tx.getUnconfirmed(account);
|
||||
return yield this.txdb.getUnconfirmed(account);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1714,7 +1714,7 @@ Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(account) {
|
||||
|
||||
Wallet.prototype.getBalance = co(function* getBalance(account) {
|
||||
account = yield this._getIndex(account);
|
||||
return yield this.tx.getBalance(account);
|
||||
return yield this.txdb.getBalance(account);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1732,7 +1732,7 @@ Wallet.prototype.getRange = co(function* getRange(account, options) {
|
||||
account = null;
|
||||
}
|
||||
account = yield this._getIndex(account);
|
||||
return yield this.tx.getRange(account, options);
|
||||
return yield this.txdb.getRange(account, options);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1744,7 +1744,7 @@ Wallet.prototype.getRange = co(function* getRange(account, options) {
|
||||
|
||||
Wallet.prototype.getLast = co(function* getLast(account, limit) {
|
||||
account = yield this._getIndex(account);
|
||||
return yield this.tx.getLast(account, limit);
|
||||
return yield this.txdb.getLast(account, limit);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1756,7 +1756,7 @@ Wallet.prototype.getLast = co(function* getLast(account, limit) {
|
||||
|
||||
Wallet.prototype.zap = co(function* zap(account, age) {
|
||||
account = yield this._getIndex(account);
|
||||
return yield this.tx.zap(account, age);
|
||||
return yield this.txdb.zap(account, age);
|
||||
});
|
||||
|
||||
/**
|
||||
@ -1766,7 +1766,7 @@ Wallet.prototype.zap = co(function* zap(account, age) {
|
||||
*/
|
||||
|
||||
Wallet.prototype.abandon = function abandon(hash) {
|
||||
return this.tx.abandon(hash);
|
||||
return this.txdb.abandon(hash);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -559,7 +559,6 @@ WalletDB.prototype._rename = co(function* _rename(wallet, id) {
|
||||
|
||||
paths = this.pathCache.values();
|
||||
|
||||
// TODO: Optimize this bullshit.
|
||||
for (i = 0; i < paths.length; i++) {
|
||||
path = paths[i];
|
||||
|
||||
@ -603,7 +602,6 @@ WalletDB.prototype.renameAccount = co(function* renameAccount(account, name) {
|
||||
|
||||
paths = this.pathCache.values();
|
||||
|
||||
// TODO: Optimize this bullshit.
|
||||
for (i = 0; i < paths.length; i++) {
|
||||
path = paths[i];
|
||||
|
||||
@ -1623,7 +1621,7 @@ WalletDB.prototype._removeBlock = co(function* removeBlock(entry) {
|
||||
if (!wallet)
|
||||
continue;
|
||||
|
||||
yield wallet.tx.unconfirm(hash);
|
||||
yield wallet.txdb.unconfirm(hash);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1683,7 +1681,7 @@ WalletDB.prototype._addTX = co(function* addTX(tx, force) {
|
||||
|
||||
info.id = wallet.id;
|
||||
|
||||
yield wallet.tx.add(tx, info);
|
||||
yield wallet.txdb.add(tx, info);
|
||||
yield wallet.handleTX(info);
|
||||
}
|
||||
|
||||
@ -1693,6 +1691,10 @@ WalletDB.prototype._addTX = co(function* addTX(tx, force) {
|
||||
/**
|
||||
* Path Info
|
||||
* @constructor
|
||||
* @param {WalletDB} db
|
||||
* @param {WalletID} wid
|
||||
* @param {TX} tx
|
||||
* @param {Object} table
|
||||
*/
|
||||
|
||||
function PathInfo(db, wid, tx, table) {
|
||||
@ -1732,6 +1734,14 @@ function PathInfo(db, wid, tx, table) {
|
||||
this.fromTX(tx, table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a transaction to multiple wallets.
|
||||
* @param {WalletDB} db
|
||||
* @param {TX} tx
|
||||
* @param {Object} table
|
||||
* @returns {PathInfo[]}
|
||||
*/
|
||||
|
||||
PathInfo.map = function map(db, tx, table) {
|
||||
var hashes = Object.keys(table);
|
||||
var wallets = [];
|
||||
@ -1762,6 +1772,14 @@ PathInfo.map = function map(db, tx, table) {
|
||||
return info;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate path info from a transaction.
|
||||
* @private
|
||||
* @param {TX} tx
|
||||
* @param {Object} table
|
||||
* @returns {PathInfo}
|
||||
*/
|
||||
|
||||
PathInfo.prototype.fromTX = function fromTX(tx, table) {
|
||||
var uniq = {};
|
||||
var i, j, hashes, hash, paths, path;
|
||||
@ -1809,6 +1827,15 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) {
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate path info from a transaction.
|
||||
* @param {WalletDB} db
|
||||
* @param {WalletID} wid
|
||||
* @param {TX} tx
|
||||
* @param {Object} table
|
||||
* @returns {PathInfo}
|
||||
*/
|
||||
|
||||
PathInfo.fromTX = function fromTX(db, wid, tx, table) {
|
||||
return new PathInfo(db, wid).fromTX(tx, table);
|
||||
};
|
||||
@ -1828,9 +1855,9 @@ PathInfo.prototype.hasPath = function hasPath(hash) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Get paths for a given address hash.
|
||||
* Get path for a given address hash.
|
||||
* @param {Hash} hash
|
||||
* @returns {Path[]|null}
|
||||
* @returns {Path}
|
||||
*/
|
||||
|
||||
PathInfo.prototype.getPath = function getPath(hash) {
|
||||
@ -1840,6 +1867,11 @@ PathInfo.prototype.getPath = function getPath(hash) {
|
||||
return this.pathMap[hash];
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert path info to transaction details.
|
||||
* @returns {Details}
|
||||
*/
|
||||
|
||||
PathInfo.prototype.toDetails = function toDetails() {
|
||||
var details = this._details;
|
||||
|
||||
@ -1851,6 +1883,11 @@ PathInfo.prototype.toDetails = function toDetails() {
|
||||
return details;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert path info to JSON details (caches json).
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
PathInfo.prototype.toJSON = function toJSON() {
|
||||
var json = this._json;
|
||||
|
||||
@ -1863,8 +1900,9 @@ PathInfo.prototype.toJSON = function toJSON() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Details
|
||||
* Transaction Details
|
||||
* @constructor
|
||||
* @param {PathInfo} info
|
||||
*/
|
||||
|
||||
function Details(info) {
|
||||
@ -1890,11 +1928,26 @@ function Details(info) {
|
||||
this.init(info.table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize transactions details
|
||||
* by pushing on mapped members.
|
||||
* @private
|
||||
* @param {Object} table
|
||||
*/
|
||||
|
||||
Details.prototype.init = function init(table) {
|
||||
this._insert(this.tx.inputs, this.inputs, table);
|
||||
this._insert(this.tx.outputs, this.outputs, table);
|
||||
};
|
||||
|
||||
/**
|
||||
* Insert members in the input or output vector.
|
||||
* @private
|
||||
* @param {Input[]|Output[]} vector
|
||||
* @param {Array} target
|
||||
* @param {Object} table
|
||||
*/
|
||||
|
||||
Details.prototype._insert = function _insert(vector, target, table) {
|
||||
var i, j, io, address, hash, paths, path, member;
|
||||
|
||||
@ -1929,6 +1982,11 @@ Details.prototype._insert = function _insert(vector, target, table) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert details to a more json-friendly object.
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
Details.prototype.toJSON = function toJSON() {
|
||||
var self = this;
|
||||
return {
|
||||
@ -1953,8 +2011,11 @@ Details.prototype.toJSON = function toJSON() {
|
||||
};
|
||||
|
||||
/**
|
||||
* DetailsMember
|
||||
* Transaction Details Member
|
||||
* @constructor
|
||||
* @property {Number} value
|
||||
* @property {Address} address
|
||||
* @property {Path} path
|
||||
*/
|
||||
|
||||
function DetailsMember() {
|
||||
@ -1966,6 +2027,12 @@ function DetailsMember() {
|
||||
this.path = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the member to a more json-friendly object.
|
||||
* @param {Network} network
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
DetailsMember.prototype.toJSON = function toJSON(network) {
|
||||
return {
|
||||
value: utils.btc(this.value),
|
||||
@ -1981,6 +2048,8 @@ DetailsMember.prototype.toJSON = function toJSON(network) {
|
||||
/**
|
||||
* Wallet Block
|
||||
* @constructor
|
||||
* @param {Hash} hash
|
||||
* @param {Number} height
|
||||
*/
|
||||
|
||||
function WalletBlock(hash, height) {
|
||||
@ -1993,6 +2062,12 @@ function WalletBlock(hash, height) {
|
||||
this.hashes = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from chain entry.
|
||||
* @private
|
||||
* @param {ChainEntry} entry
|
||||
*/
|
||||
|
||||
WalletBlock.prototype.fromEntry = function fromEntry(entry) {
|
||||
this.hash = entry.hash;
|
||||
this.height = entry.height;
|
||||
@ -2000,6 +2075,12 @@ WalletBlock.prototype.fromEntry = function fromEntry(entry) {
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from json object.
|
||||
* @private
|
||||
* @param {Object} json
|
||||
*/
|
||||
|
||||
WalletBlock.prototype.fromJSON = function fromJSON(json) {
|
||||
this.hash = utils.revHex(json.hash);
|
||||
this.height = json.height;
|
||||
@ -2008,6 +2089,13 @@ WalletBlock.prototype.fromJSON = function fromJSON(json) {
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from serialized data.
|
||||
* @private
|
||||
* @param {Hash} hash
|
||||
* @param {Buffer} data
|
||||
*/
|
||||
|
||||
WalletBlock.prototype.fromRaw = function fromRaw(hash, data) {
|
||||
var p = new BufferReader(data);
|
||||
this.hash = hash;
|
||||
@ -2017,6 +2105,12 @@ WalletBlock.prototype.fromRaw = function fromRaw(hash, data) {
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from serialized tip data.
|
||||
* @private
|
||||
* @param {Buffer} data
|
||||
*/
|
||||
|
||||
WalletBlock.prototype.fromTip = function fromTip(data) {
|
||||
var p = new BufferReader(data);
|
||||
this.hash = p.readHash('hex');
|
||||
@ -2024,22 +2118,52 @@ WalletBlock.prototype.fromTip = function fromTip(data) {
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from chain entry.
|
||||
* @param {ChainEntry} entry
|
||||
* @returns {WalletBlock}
|
||||
*/
|
||||
|
||||
WalletBlock.fromEntry = function fromEntry(entry) {
|
||||
return new WalletBlock().fromEntry(entry);
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from json object.
|
||||
* @param {Object} json
|
||||
* @returns {WalletBlock}
|
||||
*/
|
||||
|
||||
WalletBlock.fromJSON = function fromJSON(json) {
|
||||
return new WalletBlock().fromJSON(json);
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from serialized data.
|
||||
* @param {Hash} hash
|
||||
* @param {Buffer} data
|
||||
* @returns {WalletBlock}
|
||||
*/
|
||||
|
||||
WalletBlock.fromRaw = function fromRaw(hash, data) {
|
||||
return new WalletBlock().fromRaw(hash, data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiate wallet block from serialized tip data.
|
||||
* @private
|
||||
* @param {Buffer} data
|
||||
*/
|
||||
|
||||
WalletBlock.fromTip = function fromTip(data) {
|
||||
return new WalletBlock().fromTip(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize the wallet block as a tip (hash and height).
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
|
||||
WalletBlock.prototype.toTip = function toTip() {
|
||||
var p = new BufferWriter();
|
||||
p.writeHash(this.hash);
|
||||
@ -2047,6 +2171,12 @@ WalletBlock.prototype.toTip = function toTip() {
|
||||
return p.render();
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize the wallet block as a block.
|
||||
* Contains matching transaction hashes.
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
|
||||
WalletBlock.prototype.toRaw = function toRaw() {
|
||||
var p = new BufferWriter();
|
||||
var i;
|
||||
@ -2059,6 +2189,11 @@ WalletBlock.prototype.toRaw = function toRaw() {
|
||||
return p.render();
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert the block to a more json-friendly object.
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
WalletBlock.prototype.toJSON = function toJSON() {
|
||||
return {
|
||||
hash: utils.revHex(this.hash),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user