balance Service

This commit is contained in:
Matias Alejo Garcia 2014-11-29 18:35:48 -03:00
commit 57299d675e
31 changed files with 585 additions and 645 deletions

View file

@ -172,11 +172,7 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback
blockchainOpts: self.blockchainOpts,
skipFields: []
};
return callback(null, importFunction(walletData, readOpts));
} catch (e) {
log.debug("ERROR: ", e.message);
if (e && e.message && e.message.indexOf('MISSOPTS') !== -1) {
return callback(new Error('WERROR: Could not read: ' + walletId + ': ' + e.message));
@ -184,6 +180,7 @@ Identity.prototype.retrieveWalletFromStorage = function(walletId, opts, callback
return callback(e);
}
}
return callback(null, importFunction(walletData, readOpts));
});
};
@ -564,6 +561,9 @@ Identity.prototype.listWallets = function() {
Identity.prototype.deleteWallet = function(walletId, cb) {
var self = this;
var w = this.getWalletById(walletId);
w.close();
delete this.wallets[walletId];
this.storage.removeItem(Wallet.getStorageKey(walletId), function(err) {
if (err) {

View file

@ -200,8 +200,6 @@ Insight.prototype.subscribe = function(addresses) {
addresses = Array.isArray(addresses) ? addresses : [addresses];
var self = this;
console.log('[Insight.js.202] subscribe STARTED'); //TODO
function handlerFor(self, address) {
return function(txid) {
// verify the address is still subscribed
@ -293,6 +291,7 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
};
Insight.prototype.getUnspent = function(addresses, cb) {
console.log('[Insight.js.296:addresses:]',addresses); //TODO
preconditions.shouldBeArray(addresses);
preconditions.shouldBeFunction(cb);

View file

@ -518,6 +518,20 @@ PublicKeyRing.prototype.getAddresses = function() {
return ret;
};
/**
* @desc
* Gets information about addresses for a copayer
*
* @param {Object} opts
* @returns {AddressInfo[]}
*/
PublicKeyRing.prototype.getReceiveAddresses = function() {
this._checkAndRebuildCache();
var ret = this.cache.receiveAddresses;
return ret;
};
/**
* @desc
@ -526,43 +540,43 @@ PublicKeyRing.prototype.getAddresses = function() {
* @param {string} path - the BIP32 path
* @return {Buffer[]} the public keys, in buffer format
*/
PublicKeyRing.prototype.getForPath = function(path) {
PublicKeyRing.prototype._getForPath = function(path) {
var p = HDPath.indexesForPath(path);
return this.getPubKeys(p.addressIndex, p.isChange, p.copayerIndex);
};
/**
* @desc
* Retrieve the public keys for all cosigners for multiple paths
* @see PublicKeyRing#getForPath
*
* @param {string[]} paths - the BIP32 paths
* @return {Array[]} the public keys, in buffer format (matrix of Buffer, Buffer[][])
*/
PublicKeyRing.prototype.getForPaths = function(paths) {
preconditions.checkArgument(!_.isUndefined(paths));
preconditions.checkArgument(_.isArray(paths));
preconditions.checkArgument(_.all(paths, _.isString));
return paths.map(this.getForPath.bind(this));
};
/**
* @desc
* Retrieve the public keys for derived addresses and the public keys for copayers
*
* @TODO: Should this exist? A user should just call getForPath(paths)
* @TODO: Should this exist? A user should just call _getForPath(paths)
*
* @param {string[]} paths - the paths to be derived
* @return {Object} with keys pubKeys and copayerIds
*/
PublicKeyRing.prototype.forPaths = function(paths) {
return {
pubKeys: paths.map(this.getForPath.bind(this)),
pubKeys: paths.map(this._getForPath.bind(this)),
copayerIds: this.copayerIds,
}
};
/**
* @desc
* Retrieve the public keys for all cosigners for multiple paths
*
* @param {string[]} paths - the BIP32 paths
* @return {Array[]} the public keys, in buffer format (matrix of Buffer, Buffer[][])
*/
PublicKeyRing.prototype._getForPaths = function(paths) {
preconditions.checkArgument(!_.isUndefined(paths));
preconditions.checkArgument(_.isArray(paths));
preconditions.checkArgument(_.all(paths, _.isString));
return paths.map(this._getForPath.bind(this));
};
/**
* @desc
* Returns a map from a pubkey of an address to the id that generated it
@ -580,7 +594,7 @@ PublicKeyRing.prototype.copayersForPubkeys = function(pubkeys, paths) {
inKeyMap[pubkeys[i]] = 1;
};
var keys = this.getForPaths(paths);
var keys = this._getForPaths(paths);
for (var i in keys) {
for (var copayerIndex in keys[i]) {
var kHex = keys[i][copayerIndex].toString('hex');

View file

@ -915,6 +915,8 @@ Wallet.prototype._setBlockchainListeners = function() {
}
}
/**
* @desc Sets up the networking with other peers.
*
@ -1983,29 +1985,25 @@ Wallet.prototype.addSeenToTxProposals = function() {
/**
* @desc Alias for {@link PublicKeyRing#getAddresses}
* @TODO: remove this method and use getAddressesInfo everywhere
* @return {Buffer[]}
*/
Wallet.prototype.getAddresses = function(opts) {
return this.publicKeyRing.getAddresses(opts);
Wallet.prototype.getAddresses = function() {
return this.publicKeyRing.getAddresses();
};
/**
* @desc Retrieves all addresses as strings.
*
* @param {Object} opts - Same options as {@link PublicKeyRing#getAddresses}
* @return {string[]}
* @desc Alias for {@link PublicKeyRing#getAddresses}
* @return {Buffer[]}
*/
Wallet.prototype.getAddressesStr = function(opts) {
return this.getAddresses(opts).map(function(a) {
return a.toString();
});
Wallet.prototype.getReceiveAddresses = function() {
return this.publicKeyRing.getReceiveAddresses();
};
Wallet.prototype.subscribeToAddresses = function() {
if (!this.publicKeyRing.isComplete()) return;
var addresses = this.publicKeyRing.getAddresses();
var addresses = this.getAddresses();
this.blockchain.subscribe(addresses);
log.debug('Subscribed to ' + addresses.length + ' addresses');
};
@ -2442,7 +2440,9 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
* @desc Closes the wallet and disconnects all services
*/
Wallet.prototype.close = function(cb) {
this.network.removeAllListeners();
this.network.cleanUp();
this.blockchain.removeAllListeners();
this.blockchain.destroy();
log.debug('## CLOSING Wallet: ' + this.id);
@ -2664,10 +2664,9 @@ Wallet.prototype.getTransactionHistory = function(opts, cb) {
};
if (addresses.length > 0) {
var addressesStr = _.pluck(addresses, 'addressStr');
var from = (opts.currentPage - 1) * opts.itemsPerPage;
var to = opts.currentPage * opts.itemsPerPage;
self.blockchain.getTransactions(addressesStr, from, to, function(err, res) {
self.blockchain.getTransactions(addresses, from, to, function(err, res) {
if (err) return cb(err);
_.each(res.items, function(tx) {