'use strict'; angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, notification, txStatus, isCordova, profileService, lodash) { $scope.openCopayersModal = function(copayers, copayerId) { var fc = profileService.focusedClient; var ModalInstanceCtrl = function($scope, $modalInstance) { $scope.copayers = copayers; $scope.copayerId = copayerId; $scope.color = fc.backgroundColor; $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; }; $modal.open({ templateUrl: 'views/modals/copayers.html', windowClass: 'full', controller: ModalInstanceCtrl, }); }; $scope.openTxModal = function(tx, copayers) { var fc = profileService.focusedClient; var ModalInstanceCtrl = function($scope, $modalInstance) { $scope.error = null; $scope.tx = tx; $scope.amountStr = tx.amountStr; $scope.alternativeAmountStr = tx.alternativeAmountStr; $scope.copayers = copayers $scope.loading = null; $scope.color = fc.backgroundColor; $scope.getShortNetworkName = function() { return fc.credentials.networkName.substring(0, 4); }; lodash.each(['TxProposalRejectedBy', 'TxProposalAcceptedBy', 'transactionProposalRemoved', 'TxProposalRemoved'], function(eventName) { $rootScope.$on(eventName, function() { fc.getTx($scope.tx.id, function(err, tx) { if (err) { if (err.code && err.code == 'BADREQUEST' && (eventName == 'transactionProposalRemoved' || eventName == 'TxProposalRemoved')) { $scope.tx.removed = true; $scope.tx.couldRemove = false; $scope.tx.pendingForUs = false; $scope.$apply(); return; } return; } var action = lodash.find(tx.actions, { copayerId: fc.credentials.copayerId }); $scope.tx = tx; if (!action && tx.status == 'pending') $scope.tx.pendingForUs = true; $scope.updateCopayerList(); $scope.$apply(); }); }); }); $scope.updateCopayerList = function() { lodash.map($scope.copayers, function(cp) { lodash.each($scope.tx.actions, function(ac) { if (cp.id == ac.copayerId) { cp.action = ac.type; } }); }); }; $scope.sign = function(txp) { var fc = profileService.focusedClient; if (fc.isPrivKeyEncrypted()) { profileService.unlockFC(function(err) { if (err) { $scope.error = err; return; } return $scope.sign(txp); }); return; }; if (isCordova) { window.plugins.spinnerDialog.show(null, 'Signing transaction...', true); } $scope.loading = true; $scope.error = null; $timeout(function() { fc.signTxProposal(txp, function(err, txpsi) { profileService.lockFC(); if (isCordova) { window.plugins.spinnerDialog.hide(); } $scope.loading = false; if (err) { $scope.error = err.message || 'Transaction not signed. Please try again.'; $scope.$digest(); } else { //if txp has required signatures then broadcast it var txpHasRequiredSignatures = txpsi.status == 'accepted'; if (txpHasRequiredSignatures) { fc.broadcastTxProposal(txpsi, function(err, txpsb) { if (err) { $scope.error = 'Transaction not broadcasted. Please try again.'; $scope.$digest(); } else { $modalInstance.close(txpsb); } }); } else { $modalInstance.close(txpsi); } } }); }, 100); }; $scope.reject = function(txp) { if (isCordova) { window.plugins.spinnerDialog.show(null, 'Rejecting transaction...', true); } $scope.loading = true; $scope.error = null; $timeout(function() { fc.rejectTxProposal(txp, null, function(err, txpr) { if (isCordova) { window.plugins.spinnerDialog.hide(); } $scope.loading = false; if (err) { $scope.error = err.message || 'Transaction not rejected. Please try again.'; $scope.$digest(); } else { $modalInstance.close(txpr); } }); }, 100); }; $scope.remove = function(txp) { if (isCordova) { window.plugins.spinnerDialog.show(null, 'Deleting transaction...', true); } $scope.loading = true; $scope.error = null; $timeout(function() { fc.removeTxProposal(txp, function(err, txpb) { if (isCordova) { window.plugins.spinnerDialog.hide(); } $scope.loading = false; // Hacky: request tries to parse an empty response if (err && !(err.message && err.message.match(/Unexpected/)) ) { $scope.error = err.message || 'Transaction could not be deleted. Please try again.'; $scope.$digest(); return; } $modalInstance.close(); }); }, 100); }; $scope.broadcast = function(txp) { if (isCordova) { window.plugins.spinnerDialog.show(null, 'Sending transaction...', true); } $scope.loading = true; $scope.error = null; $timeout(function() { fc.broadcastTxProposal(txp, function(err, txpb) { if (isCordova) { window.plugins.spinnerDialog.hide(); } $scope.loading = false; if (err) { $scope.error = err.message || 'Transaction not sent. Please try again.'; $scope.$digest(); } else { $modalInstance.close(txpb); } }); }, 100); }; $scope.cancel = function() { $modalInstance.close(); }; }; var modalInstance = $modal.open({ templateUrl: 'views/modals/txp-details.html', windowClass: 'full', controller: ModalInstanceCtrl, }); modalInstance.result.then(function(txp) { if (txp) { txStatus.notify(txp); } $scope.$emit('Local/TxProposalAction'); }); }; });