2016-08-16 18:38:18 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-08-18 10:08:23 -03:00
|
|
|
angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $filter, $timeout, $ionicScrollDelegate, walletService, platformInfo, lodash, configService, go, rateService, $stateParams, $window, $state, $log, profileService, bitcore, $ionicPopup, txStatus, gettext) {
|
2016-08-17 13:16:06 -03:00
|
|
|
|
|
|
|
|
// An alert dialog
|
|
|
|
|
var showAlert = function(title, msg, cb) {
|
2016-08-17 17:31:45 -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-17 17:31:45 -03:00
|
|
|
|
|
|
|
|
|
2016-08-16 18:38:18 -03:00
|
|
|
var unitToSatoshi;
|
|
|
|
|
var satToUnit;
|
|
|
|
|
var unitDecimals;
|
|
|
|
|
var satToBtc;
|
|
|
|
|
var SMALL_FONT_SIZE_LIMIT = 13;
|
|
|
|
|
var LENGTH_EXPRESSION_LIMIT = 19;
|
2016-08-17 13:16:06 -03:00
|
|
|
var config;
|
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-17 13:16:06 -03:00
|
|
|
config = configService.getSync().wallet;
|
2016-08-17 15:36:19 -03:00
|
|
|
$scope.feeLevel = config.feeLevel;
|
2016-08-17 13:16:06 -03:00
|
|
|
|
|
|
|
|
$scope.unitName = config.settings.unitName;
|
|
|
|
|
$scope.alternativeIsoCode = config.settings.alternativeIsoCode;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-17 13:16:06 -03:00
|
|
|
unitToSatoshi = config.settings.unitToSatoshi;
|
2016-08-16 18:38:18 -03:00
|
|
|
satToUnit = 1 / unitToSatoshi;
|
|
|
|
|
satToBtc = 1 / 100000000;
|
|
|
|
|
|
2016-08-17 15:36:19 -03:00
|
|
|
$scope.toAmount = parseInt($stateParams.toAmount);
|
2016-08-17 17:31:45 -03:00
|
|
|
$scope.amount = (($stateParams.toAmount) * satToUnit).toFixed(unitDecimals);
|
2016-08-16 18:38:18 -03:00
|
|
|
$scope.toAddress = $stateParams.toAddress;
|
|
|
|
|
$scope.toName = $stateParams.toName;
|
|
|
|
|
|
|
|
|
|
var network = (new bitcore.Address($scope.toAddress)).network.name;
|
2016-08-18 11:45:30 -03:00
|
|
|
$scope.network = network;
|
|
|
|
|
|
2016-08-16 18:38:18 -03:00
|
|
|
$scope.setWallets(network);
|
|
|
|
|
|
2016-08-17 15:36:19 -03:00
|
|
|
toFiat($scope.amount, function(v) {
|
|
|
|
|
$scope.alternativeAmount = v;
|
|
|
|
|
});
|
|
|
|
|
|
2016-08-17 13:16:06 -03:00
|
|
|
unitDecimals = config.settings.unitDecimals;
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicSlides.slideChangeEnd", function(event, data) {
|
|
|
|
|
$scope.wallet = $scope.wallets[data.slider.activeIndex];
|
2016-08-18 11:45:30 -03:00
|
|
|
|
2016-08-17 13:16:06 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
createTx($scope.toAddress, $scope.toAmount);
|
|
|
|
|
|
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-17 13:16:06 -03:00
|
|
|
var createTx = function(toAddress, toAmount, comment) {
|
2016-08-18 10:08:23 -03:00
|
|
|
console.log('[confirm.js.78:toAddress:]',toAddress); //TODO
|
2016-08-17 13:16:06 -03:00
|
|
|
//
|
|
|
|
|
var currentSpendUnconfirmed = config.spendUnconfirmed;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
|
|
|
|
|
2016-08-17 17:31:45 -03:00
|
|
|
////
|
2016-08-17 13:16:06 -03:00
|
|
|
var wallet = $scope.wallet;
|
|
|
|
|
if (!wallet) {
|
|
|
|
|
$log.error('No wallet selected')
|
|
|
|
|
return;
|
|
|
|
|
};
|
2016-08-18 11:45:30 -03:00
|
|
|
console.log('[confirm.js.85:wallet:]',wallet); //TODO
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-17 13:16:06 -03:00
|
|
|
var outputs = [];
|
|
|
|
|
var comment = $scope.comment;
|
|
|
|
|
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-17 13:16:06 -03:00
|
|
|
if (comment && !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-17 17:31:45 -03:00
|
|
|
outputs.push({
|
|
|
|
|
'toAddress': toAddress,
|
|
|
|
|
'amount': toAmount,
|
|
|
|
|
'message': comment
|
|
|
|
|
});
|
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;
|
|
|
|
|
txp.message = comment;
|
|
|
|
|
txp.payProUrl = paypro ? paypro.url : null;
|
|
|
|
|
txp.excludeUnconfirmedUtxos = config.spendUnconfirmed ? false : true;
|
|
|
|
|
txp.feeLevel = config.feeLevel || 'normal';
|
2016-08-16 18:38:18 -03:00
|
|
|
|
|
|
|
|
|
2016-08-18 10:08:23 -03:00
|
|
|
console.log('[confirm.js.129]'); //TODO
|
|
|
|
|
walletService.createTx(wallet, txp, function(err, createdTxp) {
|
2016-08-17 13:16:06 -03:00
|
|
|
|
2016-08-18 10:08:23 -03:00
|
|
|
console.log('[confirm.js.132]', err); //TODO
|
|
|
|
|
if (err) {
|
|
|
|
|
return setSendError(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.fee = ((createdTxp.fee) * satToUnit).toFixed(unitDecimals);
|
|
|
|
|
$scope.txp = createdTxp;
|
|
|
|
|
$scope.$apply();
|
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
|
|
|
|
|
|
|
|
return walletService.onlyPublish(wallet,txp, function(err, txp){
|
|
|
|
|
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) {
|
|
|
|
|
if (err) return setSendError(err);
|
|
|
|
|
$state.transitionTo('tabs.home');
|
2016-08-17 17:31:45 -03:00
|
|
|
});
|
2016-08-16 18:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
2016-08-17 15:36:19 -03:00
|
|
|
function toFiat(val, cb) {
|
|
|
|
|
rateService.whenAvailable(function() {
|
|
|
|
|
return cb(parseFloat((rateService.toFiat(val * unitToSatoshi, $scope.alternativeIsoCode)).toFixed(2), 10));
|
|
|
|
|
});
|
2016-08-16 18:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.cancel = function() {
|
|
|
|
|
$state.transitionTo('tabs.send');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.setWallets = function(network) {
|
2016-08-17 17:31:45 -03:00
|
|
|
$scope.wallets = profileService.getWallets({
|
|
|
|
|
onlyComplete: true,
|
|
|
|
|
network: network
|
|
|
|
|
});
|
2016-08-17 13:16:06 -03:00
|
|
|
$scope.wallet = $scope.wallets[0];
|
2016-08-16 18:38:18 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|