optimizing address page

This commit is contained in:
Manuel Araoz 2014-04-30 12:25:33 -03:00
commit 4acb32c3d8
8 changed files with 52 additions and 44 deletions

View file

@ -143,7 +143,6 @@ PublicKeyRing.prototype.addCopayer = function (newEpk) {
return newEpk;
};
PublicKeyRing.prototype.getPubKeys = function (index, isChange) {
this._checkKeys();
@ -159,6 +158,7 @@ PublicKeyRing.prototype.getPubKeys = function (index, isChange) {
this.publicKeysCache[path] = pubKeys.map(function(pk){return pk.toString('hex');});
} else {
pubKeys = pubKeys.map(function(s){return new Buffer(s,'hex')});
//console.log('public keys cache HIT');
}
return pubKeys;
@ -183,21 +183,12 @@ PublicKeyRing.prototype.getRedeemScript = function (index, isChange) {
// TODO this could be cached
PublicKeyRing.prototype.getAddress = function (index, isChange) {
this._checkIndexRange(index, isChange);
var script = this.getRedeemScript(index,isChange);
return Address.fromScript(script, this.network.name);
};
// TODO this could be cached
PublicKeyRing.prototype._addScriptMap = function (map, index, isChange) {
this._checkIndexRange(index, isChange);
var script = this.getRedeemScript(index,isChange);
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
};
// TODO this could be cached
PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) {
this._checkIndexRange(index, isChange);
var addr = this.getAddress(index,isChange);
return Script.createP2SH(addr.payload()).getBuffer().toString('hex');
};
@ -218,14 +209,14 @@ PublicKeyRing.prototype.generateAddress = function(isChange) {
};
PublicKeyRing.prototype.getAddresses = function(onlyMain) {
PublicKeyRing.prototype.getAddresses = function(excludeChange) {
var ret = [];
for (var i=0; i<this.addressIndex; i++) {
ret.unshift(this.getAddress(i,false));
}
if (!onlyMain) {
if (!excludeChange) {
for (var i=0; i<this.changeAddressIndex; i++) {
ret.unshift(this.getAddress(i,true));
}
@ -233,6 +224,12 @@ PublicKeyRing.prototype.getAddresses = function(onlyMain) {
return ret;
};
// TODO this could be cached
PublicKeyRing.prototype._addScriptMap = function (map, index, isChange) {
var script = this.getRedeemScript(index,isChange);
map[Address.fromScript(script, this.network.name).toString()] = script.getBuffer().toString('hex');
};
PublicKeyRing.prototype.getRedeemScriptMap = function () {
var ret = {};

View file

@ -401,16 +401,14 @@ Wallet.prototype.addSeenToTxProposals = function() {
return ret;
};
Wallet.prototype.getAddresses = function(onlyMain) {
return this.publicKeyRing.getAddresses(onlyMain);
Wallet.prototype.getAddresses = function(excludeChange) {
return this.publicKeyRing.getAddresses(excludeChange);
};
Wallet.prototype.getAddressesStr = function(onlyMain) {
var ret = [];
this.publicKeyRing.getAddresses(onlyMain).forEach(function(a) {
ret.push(a.toString());
Wallet.prototype.getAddressesStr = function(excludeChange) {
return this.getAddresses(excludeChange).map(function(a) {
return a.toString();
});
return ret;
};
Wallet.prototype.addressIsOwn = function(addrStr) {
@ -446,13 +444,17 @@ Wallet.prototype.getBalance = function(safe, cb) {
for (var i = 0; i < utxos.length; i++) {
var u = utxos[i];
var amt = u.amount * COIN;
balance = balance + amt;
balance += amt;
balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + amt;
}
// we multiply and divide by COIN to avoid rounding errors when adding
for (var a in balanceByAddr) {
balanceByAddr[a] = balanceByAddr[a] / COIN;
}
return cb(balance / COIN, balanceByAddr, isMain);
balance = balance / COIN;
return cb(balance, balanceByAddr, isMain);
});
};