copay/js/services/controllerUtils.js

183 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-04-17 07:46:49 -07:00
'use strict';
2014-04-23 17:20:44 -07:00
angular.module('copay.controllerUtils')
.factory('controllerUtils', function($rootScope, $sce, $location, Socket, video) {
var root = {};
2014-04-30 15:50:13 -07:00
root.getVideoMutedStatus = function(copayer) {
2014-05-07 15:04:36 -07:00
var vi = $rootScope.videoInfo[copayer]
if (!vi) {
return;
}
return vi.muted;
};
2014-04-23 17:20:44 -07:00
root.logout = function() {
$rootScope.wallet = null;
delete $rootScope['wallet'];
2014-04-28 07:56:27 -07:00
video.close();
// Clear rootScope
for (var i in $rootScope) {
if (i.charAt(0) != '$') {
delete $rootScope[i];
}
}
2014-04-23 17:20:44 -07:00
$location.path('signin');
};
2014-04-23 17:20:44 -07:00
root.onError = function(scope) {
if (scope) scope.loading = false;
root.logout();
}
2014-04-23 14:07:20 -07:00
2014-04-23 17:20:44 -07:00
root.onErrorDigest = function(scope) {
root.onError(scope);
$rootScope.$digest();
}
2014-04-24 19:13:55 -07:00
root.startNetwork = function(w) {
2014-04-23 17:20:44 -07:00
var handlePeerVideo = function(err, peerID, url) {
if (err) {
2014-05-07 15:04:36 -07:00
delete $rootScope.videoInfo[peerID];
2014-04-24 14:24:03 -07:00
return;
2014-04-23 17:20:44 -07:00
}
2014-05-07 15:04:36 -07:00
$rootScope.videoInfo[peerID] = {
url: encodeURI(url),
muted: peerID === w.network.peerId
};
2014-04-30 15:50:13 -07:00
$rootScope.$digest();
2014-04-23 17:20:44 -07:00
};
w.on('badMessage', function(peerId) {
$rootScope.flashMessage = {
type: 'error',
message: 'Received wrong message from peer id:' + peerId
};
});
w.on('ready', function(myPeerID) {
2014-04-23 17:20:44 -07:00
$rootScope.wallet = w;
2014-04-30 15:50:13 -07:00
$location.path('addresses');
2014-05-16 14:48:17 -07:00
video.setOwnPeer(myPeerID, w, handlePeerVideo);
2014-04-23 17:20:44 -07:00
});
2014-05-16 14:48:17 -07:00
w.on('publicKeyRingUpdated', function() {
root.setSocketHandlers();
root.updateAddressList();
2014-04-30 15:50:13 -07:00
$rootScope.$digest();
2014-04-23 17:20:44 -07:00
});
w.on('txProposalsUpdated', function() {
root.updateTxs();
root.updateBalance(function(){
$rootScope.$digest();
});
});
2014-04-23 17:20:44 -07:00
w.on('openError', root.onErrorDigest);
2014-05-09 10:35:57 -07:00
w.on('connect', function(peerID) {
if (peerID) {
video.callPeer(peerID, handlePeerVideo);
}
$rootScope.$digest();
2014-04-23 17:20:44 -07:00
});
2014-05-09 10:44:05 -07:00
w.on('disconnect', function(peerID) {
$rootScope.$digest();
});
2014-04-23 17:20:44 -07:00
w.on('close', root.onErrorDigest);
w.netStart();
};
root.updateAddressList = function() {
var w = $rootScope.wallet;
$rootScope.addrInfos = w.getAddressesInfo();
};
2014-05-15 13:43:41 -07:00
root.updateBalance = function(cb) {
2014-05-15 13:43:41 -07:00
console.log('Updating balance...');
root.updateAddressList();
2014-04-23 17:20:44 -07:00
var w = $rootScope.wallet;
2014-04-30 15:50:13 -07:00
if ($rootScope.addrInfos.length === 0) return;
$rootScope.balanceByAddr = {};
$rootScope.updatingBalance = true;
2014-05-15 13:43:41 -07:00
w.getBalance(function(balance, balanceByAddr, safeBalance) {
2014-04-23 17:20:44 -07:00
$rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr;
2014-04-30 15:50:13 -07:00
$rootScope.selectedAddr = $rootScope.addrInfos[0].address.toString();
2014-05-15 13:43:41 -07:00
$rootScope.availableBalance = safeBalance;
$rootScope.updatingBalance = false;
2014-05-15 13:43:41 -07:00
console.log('Done updating balance.'); //TODO
2014-04-30 15:50:13 -07:00
if (cb) cb();
2014-04-21 09:16:15 -07:00
});
2014-04-23 17:20:44 -07:00
};
2014-04-17 07:46:49 -07:00
root.updateTxs = function() {
var bitcore = require('bitcore');
var w = $rootScope.wallet;
if (!w) return;
var myCopayerId = w.getMyCopayerId();
var pending = 0;
var inT = w.getTxProposals();
var txs = [];
inT.forEach(function(i){
var tx = i.builder.build();
var outs = [];
tx.outs.forEach(function(o) {
var addr = bitcore.Address.fromScriptPubKey(o.getScript(), config.networkName)[0].toString();
if (!w.addressIsOwn(addr, {excludeMain:true})) {
outs.push({
address: addr,
value: bitcore.util.valueToBigInt(o.getValue())/bitcore.util.COIN,
});
}
});
// extra fields
i.outs = outs;
i.fee = i.builder.feeSat/bitcore.util.COIN;
i.missingSignatures = tx.countInputMissingSignatures(0);
txs.push(i);
if (myCopayerId != i.creator && !i.finallyRejected && !i.sentTs && !i.rejectedByUs && !i.signedByUs) {
2014-05-13 07:27:31 -07:00
pending++;
}
});
$rootScope.txs = txs;
if ($rootScope.pendingTxCount < pending) {
2014-05-15 06:41:42 -07:00
$rootScope.txAlertCount = pending;
}
2014-05-13 07:27:31 -07:00
$rootScope.pendingTxCount = pending;
w.removeListener('txProposalsUpdated',root.updateTxs)
w.once('txProposalsUpdated',root.updateTxs);
};
2014-04-23 17:20:44 -07:00
root.setSocketHandlers = function() {
2014-04-30 10:28:33 -07:00
if (!$rootScope.wallet) return;
2014-04-18 15:08:01 -07:00
var currentAddrs= Socket.getListeners();
2014-04-23 17:20:44 -07:00
var addrs = $rootScope.wallet.getAddressesStr();
var newAddrs=[];
for(var i in addrs){
var a=addrs[i];
if (!currentAddrs[a])
newAddrs.push(a);
}
for (var i = 0; i < newAddrs.length; i++) {
console.log('### SUBSCRIBE TO', newAddrs[i]);
Socket.emit('subscribe', newAddrs[i]);
2014-04-23 17:20:44 -07:00
}
newAddrs.forEach(function(addr) {
2014-04-23 17:20:44 -07:00
Socket.on(addr, function(txid) {
console.log('Received!', txid);
root.updateBalance(function(){
$rootScope.$digest();
});
2014-04-23 17:20:44 -07:00
});
});
2014-04-23 17:20:44 -07:00
};
return root;
});