Wallet/src/js/controllers/walletSelectorController.js

116 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-07-23 16:58:32 +02:00
'use strict';
angular.module('copayApp.controllers').controller('walletSelectorController', function($scope, $rootScope, $state, $log, $ionicHistory, configService, gettextCatalog, profileService) {
2018-07-23 16:58:32 +02:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
var config = configService.getSync().wallet.settings;
switch($state.current.name) {
case 'tabs.send.wallet-to-wallet':
$scope.sendFlowTitle = gettextCatalog.getString('Wallet to Wallet Transfer');
break;
case 'tabs.send.destination':
if (data.stateParams.fromWalletId) {
$scope.sendFlowTitle = gettextCatalog.getString('Wallet to Wallet Transfer');
}
break;
default:
// nop
}
$scope.params = $state.params;
$scope.coin = false; // Wallets to show (for destination screen or contacts)
$scope.type = data.stateParams && data.stateParams['fromWalletId'] ? 'destination' : 'origin'; // origin || destination
if ($scope.params.coin) {
$scope.coin = $scope.params.coin; // Contacts have a coin embedded
}
if ($scope.params.amount) { // There is an amount, so presume that it is a payment request
$scope.sendFlowTitle = gettextCatalog.getString('Payment Request');
2018-07-25 11:26:33 +02:00
$scope.specificAmount = $scope.specificAlternativeAmount = '';
$scope.requestAmount = (($state.params.amount) * (1 / config.unitToSatoshi)).toFixed(config.unitDecimals);
2018-07-23 16:58:32 +02:00
$scope.isPaymentRequest = true;
}
if ($scope.params.thirdParty) {
$scope.thirdParty = JSON.parse($scope.params.thirdParty); // Parse stringified JSON-object
}
2018-07-23 16:58:32 +02:00
});
$scope.$on("$ionicView.enter", function(event, data) {
2018-07-25 11:26:33 +02:00
configService.whenAvailable(function(config) {
$scope.selectedPriceDisplay = config.wallet.settings.priceDisplay;
});
$scope.walletsEmpty = []; // empty wallets for origin screen
2018-07-23 16:58:32 +02:00
if ($scope.type === 'origin') {
$scope.headerTitle = gettextCatalog.getString('Choose a wallet to send from');
$scope.walletsEmpty = profileService.getWallets({coin: $scope.coin, hasNoFunds: true});
} else if ($scope.type === 'destination') {
$scope.fromWallet = profileService.getWallet(data.stateParams.fromWalletId);
2018-07-25 11:26:33 +02:00
$scope.coin = $scope.fromWallet.coin; // Only show wallets with the select origin wallet coin
2018-07-23 16:58:32 +02:00
$scope.headerTitle = gettextCatalog.getString('Choose a wallet to send to');
}
if ($scope.thirdParty) {
// Third party services specific logic
handleThirdPartyIfBip70PaymentProtocol();
handleThirdPartyIfShapeshift();
}
2018-07-25 11:26:33 +02:00
if (!$scope.coin || $scope.coin === 'bch') { // if no specific coin is set or coin is set to bch
2018-07-23 16:58:32 +02:00
$scope.walletsBch = profileService.getWallets({coin: 'bch', hasFunds: $scope.type==='origin'});
}
2018-07-25 11:26:33 +02:00
if (!$scope.coin || $scope.coin === 'btc') { // if no specific coin is set or coin is set btc
2018-07-23 16:58:32 +02:00
$scope.walletsBtc = profileService.getWallets({coin: 'btc', hasFunds: $scope.type === 'origin'});
}
});
function getNextStep() {
if ($scope.thirdParty) {
$scope.params.thirdParty = JSON.stringify($scope.thirdParty) // re-stringify JSON-object
}
2018-07-25 11:26:33 +02:00
if (!$scope.params.toWalletId && !$scope.params.toAddress) { // If we have no toAddress or fromWallet
return 'tabs.send.destination';
2018-07-25 11:26:33 +02:00
} else if (!$scope.params.amount) { // If we have no amount
return 'tabs.send.amount';
2018-07-25 11:26:33 +02:00
} else { // If we do have them
return 'tabs.send.confirm';
}
}
function handleThirdPartyIfBip70PaymentProtocol() {
if ($scope.thirdParty.id === 'bip70PaymentProtocol') {
$scope.coin = $scope.thirdParty.coin;
$scope.requestAmount = $scope.thirdParty.details.
console.log('paypro details:', $scope.thirdParty.details);
}
}
function handleThirdPartyIfShapeshift() {
if ($scope.thirdParty.id === 'shapeshift' && $scope.type === 'destination') { // Shapeshift wants to know the
if ($scope.coin === 'bch') {
$scope.coin = 'btc';
} else {
$scope.coin = 'bch';
}
}
}
2018-07-23 16:58:32 +02:00
$scope.useWallet = function(wallet) {
2018-07-25 11:26:33 +02:00
if ($scope.type === 'origin') { // we're on the origin screen, set wallet to send from
2018-07-23 16:58:32 +02:00
$scope.params['fromWalletId'] = wallet.id;
2018-07-25 11:26:33 +02:00
} else { // we're on the destination screen, set wallet to send to
2018-07-23 16:58:32 +02:00
$scope.params['toWalletId'] = wallet.id;
}
$state.transitionTo(getNextStep(), $scope.params);
2018-07-23 16:58:32 +02:00
};
$scope.goBack = function() {
$ionicHistory.goBack();
}
2018-07-23 16:58:32 +02:00
});