2016-08-12 12:44:16 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2018-07-10 14:27:51 +02:00
|
|
|
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $rootScope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, bwcError, gettextCatalog, scannerService, configService, bitcoinCashJsService, $ionicNavBarDelegate, clipboardService) {
|
2018-07-10 11:11:11 +02:00
|
|
|
var clipboardHasAddress = false;
|
|
|
|
|
var clipboardHasContent = false;
|
2018-07-10 15:11:17 +02:00
|
|
|
var originalList;
|
2018-07-12 15:25:11 +02:00
|
|
|
$scope.displayBalanceAsFiat = true;
|
|
|
|
|
$scope.walletSelectorTitleForce = true;
|
2018-07-10 11:11:11 +02:00
|
|
|
|
2018-07-12 17:40:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
$scope.walletHide = function() {
|
|
|
|
|
console.log("wallet HIDE");
|
|
|
|
|
alert('test');
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-10 11:11:11 +02:00
|
|
|
$scope.addContact = function() {
|
|
|
|
|
$state.go('tabs.settings').then(function() {
|
|
|
|
|
$state.go('tabs.addressbook').then(function() {
|
|
|
|
|
$state.go('tabs.addressbook.add');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.pasteClipboard = function() {
|
|
|
|
|
if ($scope.clipboardHasAddress || $scope.clipboardHasContent) {
|
|
|
|
|
clipboardService.readFromClipboard(function(text) {
|
2018-07-13 17:18:41 +12:00
|
|
|
$scope.$apply(function() {
|
|
|
|
|
$scope.formData.search = text;
|
|
|
|
|
});
|
2018-07-10 11:11:11 +02:00
|
|
|
$scope.findContact($scope.formData.search);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.enter", function(event, data) {
|
|
|
|
|
clipboardService.readFromClipboard(function(text) {
|
|
|
|
|
if (text.length > 200) {
|
|
|
|
|
text = text.substring(0, 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.clipboardHasAddress = false;
|
|
|
|
|
$scope.clipboardHasContent = false;
|
|
|
|
|
if ((text.indexOf('bitcoincash:') === 0 || text[0] === 'C' || text[0] === 'H' || text[0] === 'p' || text[0] === 'q') && text.replace('bitcoincash:', '').length === 42) { // CashAddr
|
|
|
|
|
$scope.clipboardHasAddress = true;
|
|
|
|
|
} else if ((text[0] === "1" || text[0] === "3" || text.substring(0, 3) === "bc1") && text.length >= 26 && text.length <= 35) { // Legacy Addresses
|
|
|
|
|
$scope.clipboardHasAddress = true;
|
|
|
|
|
} else if (text.length > 1) {
|
|
|
|
|
$scope.clipboardHasContent = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$ionicNavBarDelegate.showBar(true);
|
|
|
|
|
if (!$scope.hasWallets) {
|
|
|
|
|
$scope.checkingBalance = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
updateHasFunds();
|
|
|
|
|
updateContactsList(function() {
|
|
|
|
|
updateList();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-07-12 15:25:11 +02:00
|
|
|
var wallets;
|
|
|
|
|
var walletsBch;
|
|
|
|
|
var walletsBtc;
|
2018-07-11 17:31:58 +02:00
|
|
|
var walletToWalletFrom = false;
|
|
|
|
|
|
|
|
|
|
$scope.onWalletSelect = function(wallet) {
|
|
|
|
|
if (!$scope.walletToWalletFrom) {
|
|
|
|
|
$scope.walletToWalletFrom = wallet;
|
2018-07-12 15:25:11 +02:00
|
|
|
if (wallet.coin === 'bch') {
|
|
|
|
|
$scope.showWalletsBch = true;
|
|
|
|
|
} else if (wallet.coin === 'btc') {
|
|
|
|
|
$scope.showWalletsBtc = true;
|
|
|
|
|
}
|
|
|
|
|
$scope.walletSelectorTitleTo = gettextCatalog.getString('Send to');
|
2018-07-11 17:31:58 +02:00
|
|
|
} else {
|
2018-07-12 15:25:11 +02:00
|
|
|
walletService.getAddress(wallet, true, function(err, addr) {
|
2018-07-11 17:31:58 +02:00
|
|
|
return $state.transitionTo('tabs.send.amount', {
|
|
|
|
|
displayAddress: $scope.walletToWalletFrom.coin === 'bch' ? bitcoinCashJsService.translateAddresses(addr).cashaddr : addr,
|
2018-07-12 15:25:11 +02:00
|
|
|
recipientType: 'wallet',
|
2018-07-11 17:31:58 +02:00
|
|
|
fromWalletId: $scope.walletToWalletFrom.walletId,
|
|
|
|
|
toAddress: addr,
|
|
|
|
|
coin: $scope.walletToWalletFrom.coin
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.showWalletSelector = function() {
|
|
|
|
|
$scope.walletToWalletFrom = false;
|
2018-07-12 15:25:11 +02:00
|
|
|
$scope.walletSelectorTitleFrom = gettextCatalog.getString('Send from');
|
2018-07-11 17:31:58 +02:00
|
|
|
$scope.showWallets = true;
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-10 15:11:17 +02:00
|
|
|
$scope.findContact = function(search) {
|
2018-07-10 11:11:11 +02:00
|
|
|
|
2018-07-10 15:11:17 +02:00
|
|
|
if (incomingData.redir(search)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-07-10 11:11:11 +02:00
|
|
|
|
2018-07-10 15:11:17 +02:00
|
|
|
if (!search || search.length < 1) {
|
|
|
|
|
$scope.list = originalList;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-07-10 11:11:11 +02:00
|
|
|
|
2018-07-10 15:11:17 +02:00
|
|
|
var result = lodash.filter(originalList, function(item) {
|
|
|
|
|
var val = item.name;
|
|
|
|
|
return lodash.startsWith(val.toLowerCase(), search.toLowerCase());
|
|
|
|
|
});
|
2018-07-10 11:11:11 +02:00
|
|
|
|
2018-07-10 15:11:17 +02:00
|
|
|
$scope.list = result;
|
2018-05-08 11:39:32 +09:00
|
|
|
};
|
2016-08-12 16:55:14 -03:00
|
|
|
|
2017-02-21 11:33:31 -05:00
|
|
|
var hasWallets = function() {
|
|
|
|
|
$scope.wallets = profileService.getWallets({
|
2018-07-13 14:02:48 +02:00
|
|
|
onlyComplete: true,
|
|
|
|
|
hasFunds: true
|
2016-09-08 16:54:35 -03:00
|
|
|
});
|
2018-07-12 15:25:11 +02:00
|
|
|
$scope.walletsBch = profileService.getWallets({
|
|
|
|
|
onlyComplete: true,
|
|
|
|
|
coin: 'bch'
|
|
|
|
|
});
|
|
|
|
|
$scope.walletsBtc = profileService.getWallets({
|
|
|
|
|
onlyComplete: true,
|
|
|
|
|
coin: 'btc'
|
|
|
|
|
});
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.hasWallets = lodash.isEmpty($scope.wallets) ? false : true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var updateHasFunds = function() {
|
|
|
|
|
|
|
|
|
|
$scope.hasFunds = false;
|
|
|
|
|
var index = 0;
|
|
|
|
|
lodash.each($scope.wallets, function(w) {
|
|
|
|
|
walletService.getStatus(w, {}, function(err, status) {
|
|
|
|
|
|
|
|
|
|
++index;
|
|
|
|
|
if (err && !status) {
|
|
|
|
|
$log.error(err);
|
|
|
|
|
// error updating the wallet. Probably a network error, do not show
|
|
|
|
|
// the 'buy bitcoins' message.
|
|
|
|
|
|
|
|
|
|
$scope.hasFunds = true;
|
|
|
|
|
} else if (status.availableBalanceSat > 0) {
|
|
|
|
|
$scope.hasFunds = true;
|
|
|
|
|
}
|
2016-08-15 18:11:36 -03:00
|
|
|
|
2018-07-13 15:03:57 +12:00
|
|
|
if (index === $scope.wallets.length) {
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.checkingBalance = false;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-03-02 16:56:44 -03:00
|
|
|
var updateContactsList = function(cb) {
|
2017-11-02 14:55:24 +09:00
|
|
|
var config = configService.getSync();
|
2017-11-02 12:55:55 +09:00
|
|
|
var defaults = configService.getDefaults();
|
2016-08-15 18:11:36 -03:00
|
|
|
addressbookService.list(function(err, ab) {
|
|
|
|
|
if (err) $log.error(err);
|
2016-08-12 15:11:52 -03:00
|
|
|
|
2016-09-23 17:10:34 -03:00
|
|
|
$scope.hasContacts = lodash.isEmpty(ab) ? false : true;
|
2017-03-02 16:56:44 -03:00
|
|
|
if (!$scope.hasContacts) return cb();
|
2017-02-21 11:33:31 -05:00
|
|
|
|
2016-09-27 12:26:58 -03:00
|
|
|
var completeContacts = [];
|
2016-08-15 18:11:36 -03:00
|
|
|
lodash.each(ab, function(v, k) {
|
2016-09-27 12:26:58 -03:00
|
|
|
completeContacts.push({
|
2016-09-12 18:04:56 -03:00
|
|
|
name: lodash.isObject(v) ? v.name : v,
|
2016-08-18 19:25:30 -03:00
|
|
|
address: k,
|
2016-09-14 10:04:16 -03:00
|
|
|
email: lodash.isObject(v) ? v.email : null,
|
2017-02-20 13:06:42 -05:00
|
|
|
recipientType: 'contact',
|
2017-11-02 15:40:21 +09:00
|
|
|
coin: v.coin,
|
|
|
|
|
displayCoin: (v.coin == 'bch'
|
2018-07-10 15:11:17 +02:00
|
|
|
? (config.bitcoinCashAlias || defaults.bitcoinCashAlias)
|
|
|
|
|
: (config.bitcoinAlias || defaults.bitcoinAlias)).toUpperCase(),
|
2016-08-16 18:38:18 -03:00
|
|
|
getAddress: function(cb) {
|
2016-09-08 16:54:35 -03:00
|
|
|
return cb(null, k);
|
2016-08-16 18:38:18 -03:00
|
|
|
},
|
2016-08-15 18:11:36 -03:00
|
|
|
});
|
|
|
|
|
});
|
2018-07-10 15:11:17 +02:00
|
|
|
originalList = completeContacts;
|
2017-03-02 16:56:44 -03:00
|
|
|
return cb();
|
2016-08-15 18:11:36 -03:00
|
|
|
});
|
2016-08-12 16:30:50 -03:00
|
|
|
};
|
|
|
|
|
|
2017-03-02 16:56:44 -03:00
|
|
|
var updateList = function() {
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.list = lodash.clone(originalList);
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$ionicScrollDelegate.resize();
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 10);
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-08 17:43:06 -04:00
|
|
|
$scope.searchInFocus = function() {
|
|
|
|
|
$scope.searchFocus = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.searchBlurred = function() {
|
2017-06-16 12:45:11 -04:00
|
|
|
if ($scope.formData.search == null || $scope.formData.search.length == 0) {
|
2017-06-08 17:43:06 -04:00
|
|
|
$scope.searchFocus = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-16 18:38:18 -03:00
|
|
|
$scope.goToAmount = function(item) {
|
2016-10-07 11:27:53 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
item.getAddress(function(err, addr) {
|
|
|
|
|
if (err || !addr) {
|
2017-01-16 16:58:42 -03:00
|
|
|
//Error is already formated
|
|
|
|
|
return popupService.showAlert(err);
|
2016-10-07 11:27:53 -03:00
|
|
|
}
|
2018-01-31 15:35:52 -04:00
|
|
|
|
|
|
|
|
if (item.recipientType && item.recipientType == 'contact') {
|
|
|
|
|
if (addr.indexOf('bch') == 0 || addr.indexOf('btc') == 0) {
|
|
|
|
|
addr = addr.substring(3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 11:27:53 -03:00
|
|
|
$log.debug('Got toAddress:' + addr + ' | ' + item.name);
|
|
|
|
|
return $state.transitionTo('tabs.send.amount', {
|
2017-02-20 13:06:42 -05:00
|
|
|
recipientType: item.recipientType,
|
2018-01-31 16:10:19 -04:00
|
|
|
displayAddress: item.coin == 'bch' ? bitcoinCashJsService.translateAddresses(addr).cashaddr : addr,
|
2016-10-07 11:27:53 -03:00
|
|
|
toAddress: addr,
|
|
|
|
|
toName: item.name,
|
2016-10-13 17:11:19 -03:00
|
|
|
toEmail: item.email,
|
2017-08-30 16:51:24 -03:00
|
|
|
toColor: item.color,
|
|
|
|
|
coin: item.coin
|
2018-01-31 15:35:52 -04:00
|
|
|
});
|
2016-10-07 11:27:53 -03:00
|
|
|
});
|
2016-08-12 12:44:16 -03:00
|
|
|
});
|
|
|
|
|
};
|
2016-08-18 19:25:30 -03:00
|
|
|
|
2016-10-11 18:30:40 -03:00
|
|
|
// This could probably be enhanced refactoring the routes abstract states
|
|
|
|
|
$scope.createWallet = function() {
|
|
|
|
|
$state.go('tabs.home').then(function() {
|
|
|
|
|
$state.go('tabs.add.create-personal');
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.buyBitcoin = function() {
|
|
|
|
|
$state.go('tabs.home').then(function() {
|
2017-02-21 11:33:31 -05:00
|
|
|
$state.go('tabs.buyandsell');
|
2016-10-11 18:30:40 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
2018-07-13 11:44:24 +02:00
|
|
|
$scope.isIOS = platformInfo.isIOS && platformInfo.isCordova;
|
2018-07-12 15:25:11 +02:00
|
|
|
$scope.showWalletsBch = $scope.showWalletsBtc = $scope.showWallets = false;
|
|
|
|
|
|
2017-02-21 11:33:31 -05:00
|
|
|
$scope.checkingBalance = true;
|
|
|
|
|
$scope.formData = {
|
|
|
|
|
search: null
|
|
|
|
|
};
|
|
|
|
|
originalList = [];
|
|
|
|
|
hasWallets();
|
2018-07-12 15:25:11 +02:00
|
|
|
|
|
|
|
|
configService.whenAvailable(function(_config) {
|
|
|
|
|
$scope.displayBalanceAsFiat = _config.wallet.settings.priceDisplay === 'fiat';
|
|
|
|
|
});
|
|
|
|
|
|
2017-02-21 11:33:31 -05:00
|
|
|
});
|
2016-08-12 12:44:16 -03:00
|
|
|
});
|