2016-08-16 18:38:18 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-09-17 18:04:54 -03:00
|
|
|
angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $filter, $timeout, $ionicScrollDelegate, $ionicNavBarDelegate, gettextCatalog, walletService, platformInfo, lodash, configService, rateService, $stateParams, $window, $state, $log, profileService, bitcore, $ionicPopup, gettext, txFormatService, ongoingProcess, $ionicModal, $ionicHistory, popupService) {
|
2016-08-30 12:52:28 -03:00
|
|
|
$ionicNavBarDelegate.title(gettextCatalog.getString('Confirm'));
|
2016-08-18 14:51:35 -03:00
|
|
|
var cachedTxp = {};
|
2016-08-24 19:12:11 -03:00
|
|
|
var isChromeApp = platformInfo.isChromeApp;
|
2016-08-17 13:16:06 -03:00
|
|
|
|
2016-09-02 14:55:18 -03:00
|
|
|
|
|
|
|
|
$scope.$on('Wallet/Changed', function(event, wallet) {
|
|
|
|
|
if (lodash.isEmpty(wallet)) {
|
|
|
|
|
$log.debug('No wallet provided');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$log.debug('Wallet changed: ' + wallet.name);
|
|
|
|
|
setWallet(wallet, true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2016-08-24 16:53:14 -03:00
|
|
|
$scope.showDescriptionPopup = function() {
|
2016-09-16 21:01:19 -03:00
|
|
|
var title = gettextCatalog.getString('Add description');
|
|
|
|
|
var opts = {
|
|
|
|
|
defaultText: $scope.description
|
2016-09-07 16:48:16 -03:00
|
|
|
};
|
|
|
|
|
|
2016-09-16 21:01:19 -03:00
|
|
|
popupService.showPrompt(title, null, opts, function(res) {
|
|
|
|
|
if (res) $scope.description = res;
|
2016-08-24 15:47:36 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-24 19:12:11 -03:00
|
|
|
var setFromPayPro = function(uri, cb) {
|
|
|
|
|
if (!cb) cb = function() {};
|
|
|
|
|
|
|
|
|
|
var wallet = profileService.getWallets({
|
|
|
|
|
onlyComplete: true
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
if (!wallet) return cb();
|
|
|
|
|
|
|
|
|
|
if (isChromeApp) {
|
2016-09-01 11:11:40 -03:00
|
|
|
popupService.showAlert(gettextCatalog.getString('Payment Protocol not supported on Chrome App'));
|
2016-08-24 19:12:11 -03:00
|
|
|
return cb(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$log.debug('Fetch PayPro Request...', uri);
|
|
|
|
|
|
|
|
|
|
ongoingProcess.set('fetchingPayPro', true);
|
|
|
|
|
wallet.fetchPayPro({
|
|
|
|
|
payProUrl: uri,
|
|
|
|
|
}, function(err, paypro) {
|
|
|
|
|
|
|
|
|
|
ongoingProcess.set('fetchingPayPro', false);
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
$log.warn('Could not fetch payment request:', err);
|
|
|
|
|
var msg = err.toString();
|
|
|
|
|
if (msg.match('HTTP')) {
|
2016-09-01 11:11:40 -03:00
|
|
|
msg = gettextCatalog.getString('Could not fetch payment information');
|
2016-08-24 19:12:11 -03:00
|
|
|
}
|
2016-09-01 11:11:40 -03:00
|
|
|
popupService.showAlert(msg);
|
2016-08-24 19:12:11 -03:00
|
|
|
return cb(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!paypro.verified) {
|
|
|
|
|
$log.warn('Failed to verify payment protocol signatures');
|
2016-09-01 11:11:40 -03:00
|
|
|
popupService.showAlert(gettextCatalog.getString('Payment Protocol Invalid'));
|
2016-08-24 19:12:11 -03:00
|
|
|
return cb(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$stateParams.toAmount = paypro.amount;
|
|
|
|
|
$stateParams.toAddress = paypro.toAddress;
|
|
|
|
|
$stateParams.description = paypro.memo;
|
|
|
|
|
$stateParams.paypro = null;
|
|
|
|
|
|
|
|
|
|
$scope._paypro = paypro;
|
|
|
|
|
return $scope.init();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-16 18:38:18 -03:00
|
|
|
$scope.init = function() {
|
2016-09-17 18:04:54 -03:00
|
|
|
if ($stateParams.paypro) {
|
|
|
|
|
return setFromPayPro($stateParams.paypro, function(err) {
|
|
|
|
|
if (err && !isChromeApp) {
|
|
|
|
|
showAlert(gettext('Could not fetch payment'));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-08-24 19:32:49 -03:00
|
|
|
// TODO (URL , etc)
|
2016-08-16 18:38:18 -03:00
|
|
|
if (!$stateParams.toAddress || !$stateParams.toAmount) {
|
|
|
|
|
$log.error('Bad params at amount')
|
|
|
|
|
throw ('bad params');
|
|
|
|
|
}
|
|
|
|
|
$scope.isCordova = platformInfo.isCordova;
|
|
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
var config = configService.getSync().wallet;
|
|
|
|
|
$scope.feeLevel = config.settings ? config.settings.feeLevel : '';
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
var amount = $scope.toAmount = parseInt($stateParams.toAmount);
|
|
|
|
|
$scope.amountStr = txFormatService.formatAmountStr($scope.toAmount);
|
2016-08-16 18:38:18 -03:00
|
|
|
|
|
|
|
|
$scope.toAddress = $stateParams.toAddress;
|
|
|
|
|
$scope.toName = $stateParams.toName;
|
2016-09-14 10:04:16 -03:00
|
|
|
$scope.toEmail = $stateParams.toEmail;
|
2016-08-24 18:32:59 -03:00
|
|
|
$scope.description = $stateParams.description;
|
|
|
|
|
$scope.paypro = $stateParams.paypro;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-24 19:32:49 -03:00
|
|
|
var networkName = (new bitcore.Address($scope.toAddress)).network.name;
|
|
|
|
|
$scope.network = networkName;
|
|
|
|
|
|
|
|
|
|
$scope.notAvailable = false;
|
|
|
|
|
var wallets = profileService.getWallets({
|
|
|
|
|
onlyComplete: true,
|
|
|
|
|
network: networkName,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var filteredWallets = [];
|
|
|
|
|
var index = 0;
|
|
|
|
|
|
2016-08-25 10:19:39 -03:00
|
|
|
lodash.each(wallets, function(w) {
|
|
|
|
|
walletService.getStatus(w, {}, function(err, status) {
|
2016-09-14 11:03:43 -03:00
|
|
|
if (err || !status) {
|
|
|
|
|
$log.error(err);
|
|
|
|
|
} else {
|
|
|
|
|
if (!status.availableBalanceSat) $log.debug('No balance available in: ' + w.name);
|
|
|
|
|
if (status.availableBalanceSat > amount) filteredWallets.push(w);
|
|
|
|
|
}
|
2016-08-25 10:19:39 -03:00
|
|
|
|
|
|
|
|
if (++index == wallets.length) {
|
|
|
|
|
if (!lodash.isEmpty(filteredWallets)) {
|
|
|
|
|
$scope.wallets = lodash.clone(filteredWallets);
|
|
|
|
|
$scope.notAvailable = false;
|
|
|
|
|
} else {
|
|
|
|
|
$scope.notAvailable = true;
|
|
|
|
|
$log.warn('No wallet available to make the payment');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
2016-09-14 11:03:43 -03:00
|
|
|
});
|
2016-08-25 10:19:39 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2016-08-24 19:32:49 -03:00
|
|
|
});
|
2016-08-25 10:19:39 -03:00
|
|
|
});
|
2016-08-18 11:45:30 -03:00
|
|
|
|
2016-08-23 16:15:10 -03:00
|
|
|
txFormatService.formatAlternativeStr(amount, function(v) {
|
|
|
|
|
$scope.alternativeAmountStr = v;
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-08-17 15:36:19 -03:00
|
|
|
|
2016-08-23 16:15:10 -03:00
|
|
|
function setWallet(wallet, delayed) {
|
2016-08-18 14:51:35 -03:00
|
|
|
var stop;
|
2016-08-23 16:15:10 -03:00
|
|
|
$scope.wallet = wallet;
|
|
|
|
|
$scope.fee = $scope.txp = null;
|
2016-08-18 11:45:30 -03:00
|
|
|
|
2016-08-23 16:15:10 -03:00
|
|
|
$timeout(function() {
|
2016-08-25 10:19:39 -03:00
|
|
|
$ionicScrollDelegate.resize();
|
2016-08-23 16:15:10 -03:00
|
|
|
$scope.$apply();
|
|
|
|
|
}, 10);
|
2016-08-18 14:51:35 -03:00
|
|
|
|
2016-08-23 16:15:10 -03:00
|
|
|
if (stop) {
|
|
|
|
|
$timeout.cancel(stop);
|
|
|
|
|
stop = null;
|
|
|
|
|
}
|
2016-08-18 14:51:35 -03:00
|
|
|
|
2016-08-23 16:15:10 -03:00
|
|
|
if (cachedTxp[wallet.id]) {
|
|
|
|
|
apply(cachedTxp[wallet.id]);
|
|
|
|
|
} else {
|
|
|
|
|
stop = $timeout(function() {
|
2016-09-02 14:17:47 -03:00
|
|
|
createTx(wallet, true, function(err, txp) {
|
2016-08-23 16:15:10 -03:00
|
|
|
if (err) return;
|
|
|
|
|
cachedTxp[wallet.id] = txp;
|
|
|
|
|
apply(txp);
|
|
|
|
|
});
|
|
|
|
|
}, delayed ? 2000 : 1);
|
|
|
|
|
}
|
2016-08-16 18:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
2016-08-17 15:36:19 -03:00
|
|
|
var setSendError = function(msg) {
|
2016-09-01 11:11:40 -03:00
|
|
|
popupService.showAlert(gettextCatalog.getString('Error at confirm:'), msg);
|
2016-08-17 13:16:06 -03:00
|
|
|
};
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-24 16:53:14 -03:00
|
|
|
function apply(txp) {
|
|
|
|
|
$scope.fee = txFormatService.formatAmountStr(txp.fee);
|
|
|
|
|
$scope.txp = txp;
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-02 14:17:47 -03:00
|
|
|
var createTx = function(wallet, dryRun, cb) {
|
2016-08-18 14:51:35 -03:00
|
|
|
var config = configService.getSync().wallet;
|
2016-08-17 13:16:06 -03:00
|
|
|
var currentSpendUnconfirmed = config.spendUnconfirmed;
|
|
|
|
|
var outputs = [];
|
2016-08-18 14:51:35 -03:00
|
|
|
|
2016-08-17 13:16:06 -03:00
|
|
|
var paypro = $scope.paypro;
|
2016-08-24 19:12:11 -03:00
|
|
|
var toAddress = $scope.toAddress;
|
|
|
|
|
var toAmount = $scope.toAmount;
|
|
|
|
|
var description = $scope.description;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
|
|
|
|
// ToDo: use a credential's (or fc's) function for this
|
2016-08-24 16:53:14 -03:00
|
|
|
if (description && !wallet.credentials.sharedEncryptingKey) {
|
2016-08-16 18:38:18 -03:00
|
|
|
var msg = 'Could not add message to imported wallet without shared encrypting key';
|
|
|
|
|
$log.warn(msg);
|
2016-09-01 11:11:40 -03:00
|
|
|
return setSendError(msg);
|
2016-08-16 18:38:18 -03:00
|
|
|
}
|
|
|
|
|
|
2016-08-17 13:16:06 -03:00
|
|
|
if (toAmount > Number.MAX_SAFE_INTEGER) {
|
2016-08-16 18:38:18 -03:00
|
|
|
var msg = 'Amount too big';
|
|
|
|
|
$log.warn(msg);
|
2016-09-01 11:11:40 -03:00
|
|
|
return setSendError(msg);
|
2016-08-16 18:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
outputs.push({
|
|
|
|
|
'toAddress': toAddress,
|
|
|
|
|
'amount': toAmount,
|
2016-08-24 16:53:14 -03:00
|
|
|
'message': description
|
2016-08-18 14:51:35 -03:00
|
|
|
});
|
2016-08-17 13:16:06 -03:00
|
|
|
|
2016-08-18 10:08:23 -03:00
|
|
|
var txp = {};
|
2016-08-17 13:16:06 -03:00
|
|
|
|
2016-08-18 10:08:23 -03:00
|
|
|
// TODO
|
|
|
|
|
if (!lodash.isEmpty($scope.sendMaxInfo)) {
|
|
|
|
|
txp.sendMax = true;
|
|
|
|
|
txp.inputs = $scope.sendMaxInfo.inputs;
|
|
|
|
|
txp.fee = $scope.sendMaxInfo.fee;
|
|
|
|
|
}
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-18 10:08:23 -03:00
|
|
|
txp.outputs = outputs;
|
2016-08-24 16:53:14 -03:00
|
|
|
txp.message = description;
|
2016-08-24 19:12:11 -03:00
|
|
|
txp.payProUrl = paypro;
|
2016-08-18 10:08:23 -03:00
|
|
|
txp.excludeUnconfirmedUtxos = config.spendUnconfirmed ? false : true;
|
2016-08-18 14:51:35 -03:00
|
|
|
txp.feeLevel = config.settings.feeLevel || 'normal';
|
2016-09-02 14:17:47 -03:00
|
|
|
txp.dryRun = dryRun;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
walletService.createTx(wallet, txp, function(err, ctxp) {
|
2016-08-18 10:08:23 -03:00
|
|
|
if (err) {
|
|
|
|
|
return setSendError(err);
|
|
|
|
|
}
|
2016-08-18 14:51:35 -03:00
|
|
|
return cb(null, ctxp);
|
2016-08-17 13:16:06 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-24 19:12:11 -03:00
|
|
|
$scope.openPPModal = function() {
|
|
|
|
|
$ionicModal.fromTemplateUrl('views/modals/paypro.html', {
|
|
|
|
|
scope: $scope
|
|
|
|
|
}).then(function(modal) {
|
|
|
|
|
$scope.payproModal = modal;
|
|
|
|
|
$scope.payproModal.show();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-17 13:16:06 -03:00
|
|
|
$scope.approve = function() {
|
|
|
|
|
var wallet = $scope.wallet;
|
|
|
|
|
if (!wallet) {
|
2016-09-01 11:11:40 -03:00
|
|
|
return setSendError(gettextCatalog.getString('No wallet selected'));
|
2016-08-17 13:16:06 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!wallet.canSign() && !wallet.isPrivKeyExternal()) {
|
|
|
|
|
$log.info('No signing proposal: No private key');
|
2016-08-18 10:08:23 -03:00
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
return walletService.onlyPublish(wallet, txp, function(err, txp) {
|
2016-08-18 10:08:23 -03:00
|
|
|
if (err) return setSendError(err);
|
|
|
|
|
$state.transitionTo('tabs.home');
|
2016-08-16 18:38:18 -03:00
|
|
|
});
|
2016-08-17 13:16:06 -03:00
|
|
|
}
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-09-02 14:55:18 -03:00
|
|
|
ongoingProcess.set('creatingTx', true);
|
2016-09-02 14:17:47 -03:00
|
|
|
createTx(wallet, false, function(err, txp) {
|
2016-09-02 14:55:18 -03:00
|
|
|
ongoingProcess.set('creatingTx', false);
|
2016-09-02 14:17:47 -03:00
|
|
|
if (err) return;
|
|
|
|
|
walletService.publishAndSign(wallet, txp, function(err, txp) {
|
|
|
|
|
if (err) return setSendError(err);
|
2016-09-17 18:04:54 -03:00
|
|
|
$ionicHistory.clearHistory();
|
|
|
|
|
$state.go('tabs.home');
|
2016-09-02 14:17:47 -03:00
|
|
|
});
|
2016-08-17 15:36:19 -03:00
|
|
|
});
|
2016-08-16 18:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.cancel = function() {
|
|
|
|
|
$state.transitionTo('tabs.send');
|
|
|
|
|
};
|
|
|
|
|
});
|