refactor isChange logic

This commit is contained in:
Manuel Araoz 2014-04-30 19:50:13 -03:00
commit fd9e6cb48a
8 changed files with 89 additions and 66 deletions

View file

@ -210,17 +210,29 @@ PublicKeyRing.prototype.generateAddress = function(isChange) {
};
PublicKeyRing.prototype.getAddresses = function(excludeChange) {
return this.getAddressesInfo(excludeChange).map(function(info) {
return info.address;
});
};
PublicKeyRing.prototype.getAddressesInfo = function(excludeChange) {
var ret = [];
for (var i=0; i<this.addressIndex; i++) {
ret.unshift(this.getAddress(i,false));
}
if (!excludeChange) {
for (var i=0; i<this.changeAddressIndex; i++) {
ret.unshift(this.getAddress(i,true));
ret.unshift({
address: this.getAddress(i,true),
isChange: true
});
}
}
for (var i=0; i<this.addressIndex; i++) {
ret.unshift({
address: this.getAddress(i,false),
isChange: false
});
}
return ret;
};

View file

@ -401,6 +401,7 @@ Wallet.prototype.addSeenToTxProposals = function() {
return ret;
};
// TODO: remove this method and use getAddressesInfo everywhere
Wallet.prototype.getAddresses = function(excludeChange) {
return this.publicKeyRing.getAddresses(excludeChange);
};
@ -411,6 +412,10 @@ Wallet.prototype.getAddressesStr = function(excludeChange) {
});
};
Wallet.prototype.getAddressesInfo = function(excludeChange) {
return this.publicKeyRing.getAddressesInfo(excludeChange);
};
Wallet.prototype.addressIsOwn = function(addrStr) {
var addrList = this.getAddressesStr();
var l = addrList.length;
@ -430,15 +435,6 @@ Wallet.prototype.getBalance = function(safe, cb) {
var balanceByAddr = {};
var isMain = {};
var COIN = bitcore.util.COIN;
var addresses = this.getAddressesStr(true);
if (!addresses.length) return cb(0, []);
// Prefill balanceByAddr with main address
addresses.forEach(function(a) {
balanceByAddr[a] = 0;
isMain[a] = 1;
});
var f = safe ? this.getSafeUnspent.bind(this) : this.getUnspent.bind(this);
f(function(utxos) {
for (var i = 0; i < utxos.length; i++) {
@ -453,7 +449,6 @@ Wallet.prototype.getBalance = function(safe, cb) {
balanceByAddr[a] = balanceByAddr[a] / COIN;
}
balance = balance / COIN;
return cb(balance, balanceByAddr, isMain);
});
};