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

View File

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

View File

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

View File

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

View File

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

View File

@ -401,16 +401,14 @@ Wallet.prototype.addSeenToTxProposals = function() {
return ret; return ret;
}; };
Wallet.prototype.getAddresses = function(onlyMain) { Wallet.prototype.getAddresses = function(excludeChange) {
return this.publicKeyRing.getAddresses(onlyMain); return this.publicKeyRing.getAddresses(excludeChange);
}; };
Wallet.prototype.getAddressesStr = function(onlyMain) { Wallet.prototype.getAddressesStr = function(excludeChange) {
var ret = []; return this.getAddresses(excludeChange).map(function(a) {
this.publicKeyRing.getAddresses(onlyMain).forEach(function(a) { return a.toString();
ret.push(a.toString());
}); });
return ret;
}; };
Wallet.prototype.addressIsOwn = function(addrStr) { Wallet.prototype.addressIsOwn = function(addrStr) {
@ -446,13 +444,17 @@ Wallet.prototype.getBalance = function(safe, cb) {
for (var i = 0; i < utxos.length; i++) { for (var i = 0; i < utxos.length; i++) {
var u = utxos[i]; var u = utxos[i];
var amt = u.amount * COIN; var amt = u.amount * COIN;
balance = balance + amt; balance += amt;
balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + 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) { for (var a in balanceByAddr) {
balanceByAddr[a] = balanceByAddr[a] / COIN; 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() { root.logout = function() {
console.log('### DELETING WALLET'); //TODO
$rootScope.wallet = null; $rootScope.wallet = null;
delete $rootScope['wallet']; delete $rootScope['wallet'];
$rootScope.totalBalance = 0; $rootScope.totalBalance = 0;
@ -56,7 +55,6 @@ angular.module('copay.controllerUtils')
video.setOwnPeer(myPeerID, w, handlePeerVideo); video.setOwnPeer(myPeerID, w, handlePeerVideo);
$location.path('addresses'); $location.path('addresses');
$rootScope.wallet = w; $rootScope.wallet = w;
root.updateBalance();
}); });
w.on('refresh', function() { w.on('refresh', function() {
root.updateBalance(); root.updateBalance();
@ -78,11 +76,13 @@ angular.module('copay.controllerUtils')
w.getBalance(false, function(balance, balanceByAddr) { w.getBalance(false, function(balance, balanceByAddr) {
$rootScope.totalBalance = balance; $rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr; $rootScope.balanceByAddr = balanceByAddr;
console.log('New balance:', balance); $rootScope.$digest();
w.getBalance(true, function(balance) { console.log('New total balance:', balance);
$rootScope.availableBalance = balance; });
$rootScope.$digest(); w.getBalance(true, function(balance) {
}); $rootScope.availableBalance = balance;
$rootScope.$digest();
console.log('New available balance:', balance);
}); });
}; };

View File

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