optimizing address page

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

View File

@ -249,15 +249,23 @@
<div ng-show='$root.wallet.publicKeyRing.isComplete()'>
<div class="row">
<div class="large-9 columns" ng-if="addrs[0]">
<h3>Addresses (available to spend)</h3>
<div class="large-8 columns">
<a class="panel db" ng-repeat="addr in addrs" ng-click="selectAddr(addr)">{{addr}}
<span ng-if="!isMain[addr]">(change)</span>
<span class="right">({{balanceByAddr[addr] || 0}} <i class="fi-bitcoin"></i>)</span></a>
<a class="panel db" ng-repeat="addr in addrs" ng-click="selectAddr(addr)">
<span>{{addr}}</span>
<span ng-if="!isMain[addr]">(change)</span>
<span ng-if="typeof(balanceByAddr[addr]) !== 'undefined'" class="right">
{{balanceByAddr[addr]}} <i class="fi-bitcoin"></i>
</span>
<span ng-if="typeof(balanceByAddr[addr]) === 'undefined'" class="right">
Loading...
</span>
</a>
</div>
<div class="large-4 columns line-dashed-v text-center">
<qrcode size="160" data="{{selectedAddr}}"></qrcode>
<p class="m10t" ng-repeat="addr in addrs" ng-if="selectedAddr==addr"> <strong> {{balanceByAddr[addr]}} <i class="fi-bitcoin"></i> </strong> </p>
<p class="m10t" ng-repeat="addr in addrs" ng-if="selectedAddr==addr">
<strong> {{balanceByAddr[addr]}} <i class="fi-bitcoin"></i> </strong>
</p>
</div>
</div>
<div class="columns text-center" ng-class="{'large-3' : addrs[0]}">

View File

@ -1,5 +1,8 @@
'use strict';
var copay = require('copay');
var copayApp = window.copayApp = angular.module('copay',[
'ngRoute',
'mm.foundation',

View File

@ -3,21 +3,22 @@
angular.module('copay.addresses').controller('AddressesController',
function($scope, $rootScope, controllerUtils) {
$scope.title = 'Home';
$scope.oneAtATime = true;
$scope.addrBalance = {};
var w = $rootScope.wallet;
var _updateBalance = function () {
var _updateBalance = function() {
$scope.addrs = w.getAddressesStr(true);
controllerUtils.setSocketHandlers();
w.getBalance(true, function (balance, balanceByAddr, isMain) {
if (balanceByAddr && Object.keys(balanceByAddr).length) {
w.getBalance(true, function(balance, balanceByAddr, isMain) {
if (balanceByAddr && Object.keys(balanceByAddr).length) {
$rootScope.balanceByAddr = balanceByAddr;
$scope.isMain = isMain;
$scope.addrs = Object.keys(balanceByAddr);
$scope.addrs = Object.keys(balanceByAddr);
$scope.selectedAddr = $scope.addrs[0];
$scope.loading = false;
$rootScope.$digest();
alert('digest');
}
});
};
@ -26,6 +27,7 @@ angular.module('copay.addresses').controller('AddressesController',
$scope.loading = true;
w.generateAddress();
_updateBalance();
alert('new address');
};
$scope.selectAddr = function(addr) {

View File

@ -1,5 +1,5 @@
'use strict';
var copay = require('copay');
angular.element(document).ready(function() {
// Init the app
angular.bootstrap(document, ['copay']);

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);
});
};

View File

@ -18,7 +18,6 @@ angular.module('copay.controllerUtils')
};
root.logout = function() {
console.log('### DELETING WALLET'); //TODO
$rootScope.wallet = null;
delete $rootScope['wallet'];
$rootScope.totalBalance = 0;
@ -56,7 +55,6 @@ angular.module('copay.controllerUtils')
video.setOwnPeer(myPeerID, w, handlePeerVideo);
$location.path('addresses');
$rootScope.wallet = w;
root.updateBalance();
});
w.on('refresh', function() {
root.updateBalance();
@ -78,11 +76,13 @@ angular.module('copay.controllerUtils')
w.getBalance(false, function(balance, balanceByAddr) {
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
console.log('New balance:', balance);
w.getBalance(true, function(balance) {
$rootScope.availableBalance = balance;
$rootScope.$digest();
});
$rootScope.$digest();
console.log('New total balance:', balance);
});
w.getBalance(true, function(balance) {
$rootScope.availableBalance = balance;
$rootScope.$digest();
console.log('New available balance:', balance);
});
};

View File

@ -1,8 +1,4 @@
'use strict';
var wf;
angular.module('copay.walletFactory').factory('walletFactory', function($rootScope) {
wf = wf || new copay.WalletFactory(config);
return wf;
});
angular.module('copay.walletFactory').value('walletFactory', new copay.WalletFactory(config));