2016-08-16 18:38:18 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-08-24 15:47:36 -03:00
|
|
|
angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $filter, $timeout, $ionicScrollDelegate, gettextCatalog, walletService, platformInfo, lodash, configService, rateService, $stateParams, $window, $state, $log, profileService, bitcore, $ionicPopup, txStatus, gettext, txFormatService) {
|
2016-08-18 14:51:35 -03:00
|
|
|
|
|
|
|
|
var cachedTxp = {};
|
2016-08-17 13:16:06 -03:00
|
|
|
|
|
|
|
|
// An alert dialog
|
|
|
|
|
var showAlert = function(title, msg, cb) {
|
2016-08-24 15:47:36 -03:00
|
|
|
$log.warn(title + ": " + msg);
|
2016-08-17 13:16:06 -03:00
|
|
|
var alertPopup = $ionicPopup.alert({
|
|
|
|
|
title: title,
|
|
|
|
|
template: msg
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!cb) cb = function() {};
|
|
|
|
|
|
|
|
|
|
alertPopup.then(cb);
|
|
|
|
|
};
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-24 16:53:14 -03:00
|
|
|
$scope.showDescriptionPopup = function() {
|
2016-08-24 15:47:36 -03:00
|
|
|
var commentPopup = $ionicPopup.show({
|
|
|
|
|
templateUrl: "views/includes/note.html",
|
2016-08-24 16:53:14 -03:00
|
|
|
title: gettextCatalog.getString('Set description'),
|
2016-08-24 15:47:36 -03:00
|
|
|
scope: $scope,
|
|
|
|
|
});
|
|
|
|
|
$scope.commentPopupClose = function() {
|
|
|
|
|
commentPopup.close();
|
|
|
|
|
};
|
|
|
|
|
$scope.commentPopupSave = function() {
|
2016-08-24 16:53:14 -03:00
|
|
|
$log.debug('Saving description: ' + $scope.data.comment);
|
|
|
|
|
$scope.description = $scope.data.comment;
|
|
|
|
|
$scope.txp = null;
|
|
|
|
|
|
|
|
|
|
createTx($scope.wallet, $scope.toAddress, $scope.toAmount, $scope.data.comment, function(err, txp) {
|
|
|
|
|
if (err) return;
|
|
|
|
|
cachedTxp[$scope.wallet.id] = txp;
|
|
|
|
|
apply(txp);
|
|
|
|
|
});
|
2016-08-24 15:47:36 -03:00
|
|
|
commentPopup.close();
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-16 18:38:18 -03:00
|
|
|
$scope.init = function() {
|
|
|
|
|
|
|
|
|
|
// TODO (URL , etc)
|
|
|
|
|
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-08-24 18:32:59 -03:00
|
|
|
$scope.description = $stateParams.description;
|
|
|
|
|
$scope.paypro = $stateParams.paypro;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
|
|
|
|
var network = (new bitcore.Address($scope.toAddress)).network.name;
|
2016-08-18 11:45:30 -03:00
|
|
|
$scope.network = network;
|
|
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
function setWallets() {
|
|
|
|
|
var w = profileService.getWallets({
|
|
|
|
|
onlyComplete: true,
|
|
|
|
|
network: network,
|
|
|
|
|
});
|
|
|
|
|
$scope.wallets = lodash.filter(w, function(x) {
|
|
|
|
|
if (!x.availableBalanceSat) return true;
|
|
|
|
|
return x.availableBalanceSat > amount;
|
|
|
|
|
});
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
$scope.someFiltered = $scope.wallets.length != w.length;
|
2016-08-17 15:36:19 -03:00
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
};
|
2016-08-17 13:16:06 -03:00
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
var stop;
|
2016-08-18 11:45:30 -03:00
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
function setWallet(wallet, delayed) {
|
|
|
|
|
$scope.wallet = wallet;
|
|
|
|
|
$scope.fee = $scope.txp = null;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
|
|
if (stop) {
|
|
|
|
|
$timeout.cancel(stop);
|
|
|
|
|
stop = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cachedTxp[wallet.id]) {
|
|
|
|
|
apply(cachedTxp[wallet.id]);
|
|
|
|
|
} else {
|
|
|
|
|
stop = $timeout(function() {
|
2016-08-24 16:53:14 -03:00
|
|
|
createTx(wallet, $scope.toAddress, $scope.toAmount, $scope.description, function(err, txp) {
|
2016-08-18 14:51:35 -03:00
|
|
|
if (err) return;
|
|
|
|
|
cachedTxp[wallet.id] = txp;
|
|
|
|
|
apply(txp);
|
|
|
|
|
});
|
|
|
|
|
}, delayed ? 2000 : 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
txFormatService.formatAlternativeStr(amount, function(v) {
|
|
|
|
|
$scope.alternativeAmountStr = v;
|
2016-08-17 13:16:06 -03:00
|
|
|
});
|
|
|
|
|
|
2016-08-18 14:51:35 -03:00
|
|
|
$scope.$on("$ionicSlides.slideChangeEnd", function(event, data) {
|
|
|
|
|
setWallet($scope.wallets[data.slider.activeIndex], true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setWallets();
|
|
|
|
|
setWallet($scope.wallets[0]);
|
2016-08-17 13:16:06 -03:00
|
|
|
|
2016-08-16 18:38:18 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$ionicScrollDelegate.resize();
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-17 15:36:19 -03:00
|
|
|
var setSendError = function(msg) {
|
|
|
|
|
showAlert(gettext('Error creating transaction'), 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();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var createTx = function(wallet, toAddress, toAmount, description, 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-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-08-17 13:16:06 -03:00
|
|
|
return setSendError(gettext(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-08-17 13:16:06 -03:00
|
|
|
return setSendError(gettext(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-18 10:08:23 -03:00
|
|
|
txp.payProUrl = paypro ? paypro.url : null;
|
|
|
|
|
txp.excludeUnconfirmedUtxos = config.spendUnconfirmed ? false : true;
|
2016-08-18 14:51:35 -03:00
|
|
|
txp.feeLevel = config.settings.feeLevel || 'normal';
|
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
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$scope.approve = function() {
|
|
|
|
|
var wallet = $scope.wallet;
|
2016-08-17 17:31:45 -03:00
|
|
|
var txp = $scope.txp;
|
2016-08-17 13:16:06 -03:00
|
|
|
if (!wallet) {
|
2016-08-17 15:36:19 -03:00
|
|
|
return setSendError(gettext('No wallet selected'));
|
2016-08-17 13:16:06 -03:00
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!txp) {
|
2016-08-17 15:36:19 -03:00
|
|
|
return setSendError(gettext('No transaction'));
|
2016-08-17 13:16:06 -03:00
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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-08-18 10:08:23 -03:00
|
|
|
walletService.publishAndSign(wallet, txp, function(err, txp) {
|
2016-08-18 14:51:35 -03:00
|
|
|
if (err) return setSendError(err);
|
|
|
|
|
$state.transitionTo('tabs.home');
|
2016-08-17 15:36:19 -03:00
|
|
|
});
|
2016-08-16 18:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.cancel = function() {
|
|
|
|
|
$state.transitionTo('tabs.send');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|