Wallet/src/js/controllers/tab-receive.js

164 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-08-12 16:04:17 -03:00
'use strict';
angular.module('copayApp.controllers').controller('tabReceiveController', function($rootScope, $scope, $timeout, $log, $ionicModal, $state, $ionicHistory, $ionicPopover, storageService, platformInfo, walletService, profileService, configService, lodash, gettextCatalog, popupService, bwcError, bitcoinCashJsService) {
2016-08-15 11:56:59 -03:00
2016-11-14 10:09:11 -03:00
var listeners = [];
$scope.bchAddressType = { type: 'cashaddr' };
var bchAddresses = {};
2016-08-15 11:56:59 -03:00
$scope.isCordova = platformInfo.isCordova;
2016-09-21 17:12:25 -03:00
$scope.isNW = platformInfo.isNW;
2016-08-12 16:04:17 -03:00
2017-05-03 16:25:27 -03:00
$scope.requestSpecificAmount = function() {
$state.go('tabs.paymentRequest.amount', {
id: $scope.wallet.credentials.walletId,
2017-08-27 23:50:27 -03:00
coin: $scope.wallet.coin
2017-05-03 16:25:27 -03:00
});
2016-08-12 16:04:17 -03:00
};
$scope.setAddress = function(newAddr) {
2016-08-30 15:43:17 -03:00
$scope.addr = null;
if (!$scope.wallet || $scope.generatingAddress || !$scope.wallet.isComplete()) return;
2016-08-12 16:04:17 -03:00
$scope.generatingAddress = true;
walletService.getAddress($scope.wallet, newAddr, function(err, addr) {
2016-09-29 19:52:26 -03:00
$scope.generatingAddress = false;
2017-01-16 16:00:09 -03:00
if (err) {
2017-01-16 16:00:09 -03:00
//Error is already formated
popupService.showAlert(err);
2017-01-16 16:00:09 -03:00
}
if ($scope.wallet.coin == 'bch') {
bchAddresses = bitcoinCashJsService.translateAddresses(addr);
$scope.addr = bchAddresses[$scope.bchAddressType.type];
} else {
$scope.addr = addr;
}
2016-10-17 09:55:20 -03:00
$timeout(function() {
2016-10-15 10:28:30 -03:00
$scope.$apply();
2016-10-17 09:55:20 -03:00
}, 10);
2016-09-23 12:07:56 -03:00
});
2016-09-29 19:52:26 -03:00
};
2016-09-26 11:26:10 -03:00
$scope.displayAddress = function(type) {
$scope.bchAddressType.type = type;
$scope.addr = bchAddresses[$scope.bchAddressType.type];
}
2016-09-26 11:26:10 -03:00
$scope.goCopayers = function() {
$ionicHistory.removeBackView();
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home');
$timeout(function() {
$state.transitionTo('tabs.copayers', {
walletId: $scope.wallet.credentials.walletId
});
}, 100);
};
2016-09-26 16:11:45 -03:00
$scope.openBackupNeededModal = function() {
2016-09-27 10:59:08 -03:00
$ionicModal.fromTemplateUrl('views/includes/backupNeededPopup.html', {
scope: $scope,
backdropClickToClose: false,
hardwareBackButtonClose: false
}).then(function(modal) {
$scope.BackupNeededModal = modal;
$scope.BackupNeededModal.show();
});
2016-09-26 13:05:15 -03:00
};
2016-09-26 16:11:45 -03:00
$scope.close = function() {
$scope.BackupNeededModal.hide();
$scope.BackupNeededModal.remove();
};
$scope.doBackup = function() {
$scope.close();
$scope.goToBackupFlow();
2016-09-26 13:05:15 -03:00
};
$scope.goToBackupFlow = function() {
2016-09-26 16:11:45 -03:00
$state.go('tabs.receive.backupWarning', {
from: 'tabs.receive',
2016-09-26 13:05:15 -03:00
walletId: $scope.wallet.credentials.walletId
});
2016-09-29 19:52:26 -03:00
};
2016-12-05 17:33:46 -05:00
$scope.shouldShowReceiveAddressFromHardware = function() {
var wallet = $scope.wallet;
if (wallet.isPrivKeyExternal() && wallet.credentials.hwInfo) {
return (wallet.credentials.hwInfo.name == walletService.externalSource.intelTEE.id);
} else {
return false;
}
};
$scope.showReceiveAddressFromHardware = function() {
var wallet = $scope.wallet;
if (wallet.isPrivKeyExternal() && wallet.credentials.hwInfo) {
2017-05-03 16:25:27 -03:00
walletService.showReceiveAddressFromHardware(wallet, $scope.addr, function() {});
2016-12-05 17:33:46 -05:00
}
};
2016-09-29 19:52:26 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.wallets = profileService.getWallets();
2017-05-03 16:25:27 -03:00
$scope.singleWallet = $scope.wallets.length == 1;
2016-11-14 10:09:11 -03:00
if (!$scope.wallets[0]) return;
// select first wallet if no wallet selected previously
var selectedWallet = checkSelectedWallet($scope.wallet, $scope.wallets);
$scope.onWalletSelect(selectedWallet);
$scope.showShareButton = platformInfo.isCordova ? (platformInfo.isIOS ? 'iOS' : 'Android') : null;
2016-11-14 10:09:11 -03:00
listeners = [
$rootScope.$on('bwsEvent', function(e, walletId, type, n) {
// Update current address
if ($scope.wallet && walletId == $scope.wallet.id && type == 'NewIncomingTx') $scope.setAddress(true);
2016-11-14 10:09:11 -03:00
})
];
2016-11-14 10:09:11 -03:00
});
$scope.$on("$ionicView.leave", function(event, data) {
$ionicHistory.clearCache();
2016-11-14 10:09:11 -03:00
lodash.each(listeners, function(x) {
x();
});
2016-09-29 19:52:26 -03:00
});
2017-05-03 16:25:27 -03:00
var checkSelectedWallet = function(wallet, wallets) {
if (!wallet) return wallets[0];
var w = lodash.find(wallets, function(w) {
return w.id == wallet.id;
});
if (!w) return wallets[0];
return wallet;
}
var setProtocolHandler = function() {
$scope.protocolHandler = walletService.getProtocolHandler($scope.wallet);
}
2017-05-03 16:25:27 -03:00
$scope.onWalletSelect = function(wallet) {
$scope.wallet = wallet;
setProtocolHandler();
$scope.setAddress();
2017-05-03 16:25:27 -03:00
};
$scope.showWalletSelector = function() {
if ($scope.singleWallet) return;
2017-06-06 10:31:17 -03:00
$scope.walletSelectorTitle = gettextCatalog.getString('Select a wallet');
2017-05-03 16:25:27 -03:00
$scope.showWallets = true;
};
$scope.shareAddress = function() {
if (!$scope.isCordova) return;
2017-09-08 16:55:04 -03:00
var protocol = 'bitcoin';
if ($scope.wallet.coin == 'bch') protocol += 'cash';
window.plugins.socialsharing.share(protocol + ':' + $scope.addr, null, null, null);
2017-05-03 16:25:27 -03:00
}
2016-08-12 16:04:17 -03:00
});