2014-04-17 11:46:49 -03:00
|
|
|
'use strict';
|
2014-06-12 17:42:26 -03:00
|
|
|
var bitcore = require('bitcore');
|
2014-04-17 11:46:49 -03:00
|
|
|
|
2014-06-03 17:42:36 -03:00
|
|
|
angular.module('copayApp.services')
|
2014-06-12 17:42:26 -03:00
|
|
|
.factory('controllerUtils', function($rootScope, $sce, $location, $notification, $timeout, Socket, video) {
|
2014-04-23 21:20:44 -03:00
|
|
|
var root = {};
|
2014-04-30 19:50:13 -03:00
|
|
|
|
2014-05-16 18:33:06 -03:00
|
|
|
root.getVideoMutedStatus = function(copayer) {
|
2014-05-07 19:04:36 -03:00
|
|
|
var vi = $rootScope.videoInfo[copayer]
|
|
|
|
|
if (!vi) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return vi.muted;
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-23 21:20:44 -03:00
|
|
|
root.logout = function() {
|
|
|
|
|
$rootScope.wallet = null;
|
|
|
|
|
delete $rootScope['wallet'];
|
2014-04-28 11:56:27 -03:00
|
|
|
video.close();
|
2014-05-16 18:33:06 -03:00
|
|
|
// Clear rootScope
|
|
|
|
|
for (var i in $rootScope) {
|
2014-05-20 08:30:20 -07:00
|
|
|
if (i.charAt(0) != '$') {
|
2014-05-16 18:33:06 -03:00
|
|
|
delete $rootScope[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 21:20:44 -03:00
|
|
|
$location.path('signin');
|
|
|
|
|
};
|
2014-04-17 16:27:15 -03:00
|
|
|
|
2014-04-23 21:20:44 -03:00
|
|
|
root.onError = function(scope) {
|
|
|
|
|
if (scope) scope.loading = false;
|
|
|
|
|
root.logout();
|
|
|
|
|
}
|
2014-04-23 18:07:20 -03:00
|
|
|
|
2014-06-09 21:00:28 -03:00
|
|
|
root.onErrorDigest = function(scope, msg) {
|
2014-04-23 21:20:44 -03:00
|
|
|
root.onError(scope);
|
2014-06-09 21:00:28 -03:00
|
|
|
if (msg) $rootScope.$flashMessage = {
|
|
|
|
|
type: 'error',
|
|
|
|
|
message: msg
|
|
|
|
|
};
|
2014-04-23 21:20:44 -03:00
|
|
|
$rootScope.$digest();
|
2014-06-12 11:03:24 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.installStartupHandlers = function(wallet, $scope) {
|
|
|
|
|
wallet.on('serverError', function(msg) {
|
|
|
|
|
$rootScope.$flashMessage = {
|
|
|
|
|
message: 'There was an error connecting to the PeerJS server.'
|
|
|
|
|
+(msg||'Check you settings and Internet connection.'),
|
|
|
|
|
type: 'error',
|
|
|
|
|
};
|
|
|
|
|
root.onErrorDigest($scope);
|
|
|
|
|
$location.path('addresses');
|
|
|
|
|
});
|
|
|
|
|
wallet.on('connectionError', function() {
|
|
|
|
|
var message = "Looks like you are already connected to this wallet, please logout from it and try importing it again.";
|
|
|
|
|
$rootScope.$flashMessage = { message: message, type: 'error'};
|
|
|
|
|
root.onErrorDigest($scope);
|
|
|
|
|
});
|
|
|
|
|
wallet.on('serverError', function() {
|
|
|
|
|
$rootScope.$flashMessage = { message: 'The PeerJS server is not responding, please try again', type: 'error'};
|
|
|
|
|
root.onErrorDigest($scope);
|
|
|
|
|
});
|
|
|
|
|
wallet.on('ready', function() {
|
|
|
|
|
$scope.loading = false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root.startNetwork = function(w, $scope) {
|
|
|
|
|
|
|
|
|
|
root.installStartupHandlers(w, $scope);
|
2014-04-24 23:13:55 -03:00
|
|
|
|
2014-04-23 21:20:44 -03:00
|
|
|
var handlePeerVideo = function(err, peerID, url) {
|
|
|
|
|
if (err) {
|
2014-05-07 19:04:36 -03:00
|
|
|
delete $rootScope.videoInfo[peerID];
|
2014-04-24 18:24:03 -03:00
|
|
|
return;
|
2014-04-23 21:20:44 -03:00
|
|
|
}
|
2014-05-07 19:04:36 -03:00
|
|
|
$rootScope.videoInfo[peerID] = {
|
|
|
|
|
url: encodeURI(url),
|
|
|
|
|
muted: peerID === w.network.peerId
|
|
|
|
|
};
|
2014-04-30 19:50:13 -03:00
|
|
|
$rootScope.$digest();
|
2014-04-23 21:20:44 -03:00
|
|
|
};
|
2014-05-20 11:05:18 -03:00
|
|
|
|
|
|
|
|
$notification.enableHtml5Mode(); // for chrome: if support, enable it
|
|
|
|
|
|
2014-04-23 21:20:44 -03:00
|
|
|
w.on('badMessage', function(peerId) {
|
2014-05-20 08:30:20 -07:00
|
|
|
$rootScope.$flashMessage = {
|
2014-04-23 21:20:44 -03:00
|
|
|
type: 'error',
|
|
|
|
|
message: 'Received wrong message from peer id:' + peerId
|
|
|
|
|
};
|
|
|
|
|
});
|
2014-05-09 12:14:57 -03:00
|
|
|
w.on('ready', function(myPeerID) {
|
2014-04-23 21:20:44 -03:00
|
|
|
$rootScope.wallet = w;
|
2014-04-30 19:50:13 -03:00
|
|
|
$location.path('addresses');
|
2014-06-09 15:59:18 -03:00
|
|
|
if (!config.disableVideo)
|
|
|
|
|
video.setOwnPeer(myPeerID, w, handlePeerVideo);
|
2014-04-23 21:20:44 -03:00
|
|
|
});
|
2014-05-16 18:48:17 -03:00
|
|
|
|
2014-05-17 01:19:52 -03:00
|
|
|
w.on('publicKeyRingUpdated', function(dontDigest) {
|
2014-05-16 18:33:06 -03:00
|
|
|
root.setSocketHandlers();
|
|
|
|
|
root.updateAddressList();
|
2014-05-17 01:19:52 -03:00
|
|
|
if (!dontDigest) {
|
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
}
|
2014-04-23 21:20:44 -03:00
|
|
|
});
|
2014-05-17 01:19:52 -03:00
|
|
|
w.on('txProposalsUpdated', function(dontDigest) {
|
|
|
|
|
root.updateTxs({onlyPending:true});
|
2014-06-12 17:42:26 -03:00
|
|
|
// give sometime to the tx to propagate.
|
|
|
|
|
$timeout(function() {
|
2014-06-13 19:45:00 -03:00
|
|
|
|
|
|
|
|
console.log('[controllerUtils.js.111] UPDATE BALANCE'); //TODO
|
2014-06-12 17:42:26 -03:00
|
|
|
root.updateBalance(function(){
|
|
|
|
|
if (!dontDigest) {
|
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},3000);
|
2014-05-13 04:03:09 -03:00
|
|
|
});
|
2014-06-07 19:12:24 -03:00
|
|
|
w.on('connectionError', function(msg) {
|
2014-06-09 21:00:28 -03:00
|
|
|
root.onErrorDigest(null, msg);
|
2014-06-07 19:12:24 -03:00
|
|
|
});
|
2014-05-09 14:35:57 -03:00
|
|
|
w.on('connect', function(peerID) {
|
2014-06-09 15:59:18 -03:00
|
|
|
if (peerID && !config.disableVideo) {
|
2014-05-09 14:35:57 -03:00
|
|
|
video.callPeer(peerID, handlePeerVideo);
|
|
|
|
|
}
|
2014-05-09 11:59:38 -03:00
|
|
|
$rootScope.$digest();
|
2014-04-23 21:20:44 -03:00
|
|
|
});
|
2014-05-09 14:44:05 -03:00
|
|
|
w.on('disconnect', function(peerID) {
|
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
});
|
2014-04-23 21:20:44 -03:00
|
|
|
w.on('close', root.onErrorDigest);
|
|
|
|
|
w.netStart();
|
|
|
|
|
};
|
2014-04-24 16:35:52 -03:00
|
|
|
|
2014-05-16 18:33:06 -03:00
|
|
|
root.updateAddressList = function() {
|
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
|
$rootScope.addrInfos = w.getAddressesInfo();
|
|
|
|
|
};
|
2014-05-15 17:43:41 -03:00
|
|
|
|
2014-05-16 18:33:06 -03:00
|
|
|
root.updateBalance = function(cb) {
|
2014-04-23 21:20:44 -03:00
|
|
|
var w = $rootScope.wallet;
|
2014-06-05 12:18:54 -03:00
|
|
|
if (!w) return root.onErrorDigest();
|
|
|
|
|
|
2014-05-16 18:33:06 -03:00
|
|
|
$rootScope.balanceByAddr = {};
|
|
|
|
|
$rootScope.updatingBalance = true;
|
2014-06-13 19:45:00 -03:00
|
|
|
|
|
|
|
|
console.log('[controllerUtils.js.147] GET'); //TODO
|
2014-05-21 18:03:11 -03:00
|
|
|
w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
|
2014-06-13 19:45:00 -03:00
|
|
|
|
|
|
|
|
console.log('[controllerUtils.js.150]', err, balance); //TODO
|
2014-05-21 18:03:11 -03:00
|
|
|
if (err) {
|
|
|
|
|
console.error('Error: ' + err.message); //TODO
|
2014-06-05 12:18:54 -03:00
|
|
|
root._setCommError();
|
2014-05-21 18:03:11 -03:00
|
|
|
return null;
|
|
|
|
|
}
|
2014-06-05 12:18:54 -03:00
|
|
|
else {
|
|
|
|
|
root._clearCommError();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 21:20:44 -03:00
|
|
|
$rootScope.totalBalance = balance;
|
2014-06-12 17:42:26 -03:00
|
|
|
$rootScope.totalBalanceBTC = (balance / 1e6).toFixed(3) ;
|
2014-05-15 17:43:41 -03:00
|
|
|
$rootScope.availableBalance = safeBalance;
|
2014-06-12 17:42:26 -03:00
|
|
|
$rootScope.availableBalanceBTC = (safeBalance / 1e6).toFixed(3);
|
|
|
|
|
$rootScope.balanceByAddr = balanceByAddr;
|
2014-05-21 18:53:17 -03:00
|
|
|
root.updateAddressList();
|
2014-05-16 18:33:06 -03:00
|
|
|
$rootScope.updatingBalance = false;
|
2014-05-17 01:19:52 -03:00
|
|
|
return cb?cb():null;
|
2014-04-21 13:16:15 -03:00
|
|
|
});
|
2014-04-23 21:20:44 -03:00
|
|
|
};
|
2014-04-17 11:46:49 -03:00
|
|
|
|
2014-05-17 01:19:52 -03:00
|
|
|
root.updateTxs = function(opts) {
|
2014-05-13 04:03:09 -03:00
|
|
|
var w = $rootScope.wallet;
|
|
|
|
|
if (!w) return;
|
2014-05-17 01:19:52 -03:00
|
|
|
opts = opts || {};
|
2014-05-13 04:03:09 -03:00
|
|
|
|
2014-05-15 02:12:14 -03:00
|
|
|
var myCopayerId = w.getMyCopayerId();
|
2014-05-17 01:19:52 -03:00
|
|
|
var pendingForUs = 0;
|
2014-06-12 12:49:08 -03:00
|
|
|
var inT = w.getTxProposals().sort(function(t1, t2) { return t2.createdTs - t1.createdTs });
|
2014-05-13 04:03:09 -03:00
|
|
|
var txs = [];
|
|
|
|
|
|
2014-05-20 14:34:55 -07:00
|
|
|
inT.forEach(function(i, index){
|
|
|
|
|
if (opts.skip && (index < opts.skip[0] || index >= opts.skip[1])) {
|
|
|
|
|
return txs.push(null);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 02:12:14 -03:00
|
|
|
if (myCopayerId != i.creator && !i.finallyRejected && !i.sentTs && !i.rejectedByUs && !i.signedByUs) {
|
2014-05-17 01:19:52 -03:00
|
|
|
pendingForUs++;
|
|
|
|
|
}
|
|
|
|
|
if (!i.finallyRejected && !i.sentTs) {
|
|
|
|
|
i.isPending=1;
|
|
|
|
|
}
|
|
|
|
|
if (!opts.onlyPending || i.isPending) {
|
|
|
|
|
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,
|
2014-06-12 17:42:26 -03:00
|
|
|
value: bitcore.util.valueToBigInt(o.getValue())/bitcore.util.BIT,
|
2014-05-17 01:19:52 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// extra fields
|
|
|
|
|
i.outs = outs;
|
2014-06-12 17:42:26 -03:00
|
|
|
i.fee = i.builder.feeSat/bitcore.util.BIT;
|
2014-05-17 01:19:52 -03:00
|
|
|
i.missingSignatures = tx.countInputMissingSignatures(0);
|
|
|
|
|
txs.push(i);
|
2014-05-13 05:02:21 -03:00
|
|
|
}
|
2014-05-15 02:12:14 -03:00
|
|
|
});
|
|
|
|
|
|
2014-05-17 01:19:52 -03:00
|
|
|
$rootScope.txs = txs; //.some(function(i) {return i.isPending; } );
|
|
|
|
|
if ($rootScope.pendingTxCount < pendingForUs) {
|
|
|
|
|
$rootScope.txAlertCount = pendingForUs;
|
2014-05-13 05:02:21 -03:00
|
|
|
}
|
2014-05-17 01:19:52 -03:00
|
|
|
$rootScope.pendingTxCount = pendingForUs;
|
2014-05-13 04:03:09 -03:00
|
|
|
};
|
|
|
|
|
|
2014-06-04 18:10:47 -03:00
|
|
|
root._setCommError = function(e) {
|
2014-06-05 12:18:54 -03:00
|
|
|
if ($rootScope.insightError<0)
|
|
|
|
|
$rootScope.insightError=0;
|
|
|
|
|
$rootScope.insightError++;
|
2014-06-04 18:10:47 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root._clearCommError = function(e) {
|
2014-06-05 14:55:19 -03:00
|
|
|
if ($rootScope.insightError>0)
|
|
|
|
|
$rootScope.insightError=-1;
|
|
|
|
|
else
|
|
|
|
|
$rootScope.insightError=0;
|
2014-06-04 18:10:47 -03:00
|
|
|
};
|
|
|
|
|
|
2014-04-23 21:20:44 -03:00
|
|
|
root.setSocketHandlers = function() {
|
2014-06-04 18:10:47 -03:00
|
|
|
if (!Socket.sysEventsSet) {
|
|
|
|
|
Socket.sysOn('error', root._setCommError);
|
|
|
|
|
Socket.sysOn('reconnect_error', root._setCommError);
|
|
|
|
|
Socket.sysOn('reconnect_failed', root._setCommError);
|
|
|
|
|
Socket.sysOn('connect', root._clearCommError);
|
|
|
|
|
Socket.sysOn('reconnect', root._clearCommError);
|
|
|
|
|
Socket.sysEventsSet=true;
|
|
|
|
|
}
|
2014-04-30 14:28:33 -03:00
|
|
|
if (!$rootScope.wallet) return;
|
2014-04-18 19:08:01 -03:00
|
|
|
|
2014-05-16 18:33:06 -03:00
|
|
|
var currentAddrs= Socket.getListeners();
|
2014-04-23 21:20:44 -03:00
|
|
|
var addrs = $rootScope.wallet.getAddressesStr();
|
2014-06-04 18:10:47 -03:00
|
|
|
|
2014-05-16 18:33:06 -03:00
|
|
|
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++) {
|
|
|
|
|
Socket.emit('subscribe', newAddrs[i]);
|
2014-04-23 21:20:44 -03:00
|
|
|
}
|
2014-05-16 18:33:06 -03:00
|
|
|
newAddrs.forEach(function(addr) {
|
2014-04-23 21:20:44 -03:00
|
|
|
Socket.on(addr, function(txid) {
|
2014-05-20 14:31:00 -03:00
|
|
|
$rootScope.receivedFund = [txid, addr];
|
2014-05-16 18:33:06 -03:00
|
|
|
root.updateBalance(function(){
|
|
|
|
|
$rootScope.$digest();
|
|
|
|
|
});
|
2014-04-23 21:20:44 -03:00
|
|
|
});
|
2014-04-17 13:25:36 -03:00
|
|
|
});
|
2014-04-23 21:20:44 -03:00
|
|
|
};
|
|
|
|
|
return root;
|
|
|
|
|
});
|