Wallet/src/js/controllers/customAmount.js

99 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-11-24 11:42:25 -03:00
'use strict';
2018-01-15 17:45:06 +09:00
angular.module('copayApp.controllers').controller('customAmountController', function($scope, $ionicHistory, txFormatService, platformInfo, configService, profileService, walletService, popupService, bitcoinCashJsService) {
var showErrorAndBack = function(title, msg) {
popupService.showAlert(title, msg, function() {
$scope.close();
});
};
2016-11-24 11:42:25 -03:00
var setProtocolHandler = function() {
$scope.protocolHandler = walletService.getProtocolHandler($scope.wallet);
}
2016-11-24 11:42:25 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
var walletId = data.stateParams.id;
if (!walletId) {
showErrorAndBack('Error', 'No wallet selected');
return;
}
2017-05-24 16:24:52 -03:00
$scope.showShareButton = platformInfo.isCordova ? (platformInfo.isIOS ? 'iOS' : 'Android') : null;
$scope.wallet = profileService.getWallet(walletId);
setProtocolHandler();
walletService.getAddress($scope.wallet, false, function(err, addr) {
if (!addr) {
showErrorAndBack('Error', 'Could not get the address');
return;
}
2018-01-15 17:45:06 +09:00
$scope.bchAddressType = 'cashaddr';
var bchAddresses = {};
if ($scope.wallet.coin == 'bch') {
bchAddresses = bitcoinCashJsService.translateAddresses(addr);
$scope.address = bchAddresses[$scope.bchAddressType];
} else {
$scope.address = addr;
}
$scope.displayAddress = function(type) {
$scope.bchAddressType = type;
$scope.address = bchAddresses[$scope.bchAddressType];
}
2017-08-28 15:51:13 -03:00
$scope.coin = data.stateParams.coin;
var parsedAmount = txFormatService.parseAmount(
2017-08-28 15:51:13 -03:00
$scope.wallet.coin,
data.stateParams.amount,
data.stateParams.currency);
// Amount in USD or BTC
var amount = parsedAmount.amount;
var currency = parsedAmount.currency;
$scope.amountUnitStr = parsedAmount.amountUnitStr;
if (currency != 'BTC' && currency != 'BCH') {
// Convert to BTC or BCH
var config = configService.getSync().wallet.settings;
var amountUnit = txFormatService.satToUnit(parsedAmount.amountSat);
2017-08-28 15:51:13 -03:00
var btcParsedAmount = txFormatService.parseAmount($scope.wallet.coin, amountUnit, $scope.wallet.coin);
$scope.amountBtc = btcParsedAmount.amount;
$scope.altAmountStr = btcParsedAmount.amountUnitStr;
} else {
$scope.amountBtc = amount; // BTC or BCH
2017-08-28 15:51:13 -03:00
$scope.altAmountStr = txFormatService.formatAlternativeStr($scope.wallet.coin, parsedAmount.amountSat);
}
});
2016-11-24 11:42:25 -03:00
});
$scope.close = function() {
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$ionicHistory.goBack(-2);
};
2017-05-24 16:24:52 -03:00
$scope.shareAddress = function() {
if (!platformInfo.isCordova) return;
2017-09-08 16:55:04 -03:00
var protocol = 'bitcoin';
if ($scope.wallet.coin == 'bch') protocol += 'cash';
var data = protocol + ':' + $scope.address + '?amount=' + $scope.amountBtc;
2017-05-24 16:24:52 -03:00
window.plugins.socialsharing.share(data, null, null, null);
}
$scope.copyToClipboard = function() {
2018-01-15 17:45:06 +09:00
var protocol = '';
if ($scope.wallet.coin == 'bch' && $scope.bchAddressType == 'cashaddr') {
protocol = 'bitcoincash:';
}
return protocol + $scope.address + '?amount=' + $scope.amountBtc;
};
2016-11-24 11:42:25 -03:00
});