copay/src/js/controllers/walletHome.js

1290 lines
38 KiB
JavaScript
Raw Normal View History

2015-03-06 07:00:10 -08:00
'use strict';
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService, ledger, bwsError, confirmDialog, txFormatService, animationService, addressbookService, go) {
2015-03-06 07:00:10 -08:00
2015-04-23 08:27:43 -07:00
var self = this;
2015-04-21 09:06:44 -07:00
$rootScope.hideMenuBar = false;
2015-04-23 08:27:43 -07:00
$rootScope.wpInputFocused = false;
2015-10-07 12:17:19 -07:00
var config = configService.getSync();
var configWallet = config.wallet;
2015-04-23 08:27:43 -07:00
// INIT
2015-10-07 12:17:19 -07:00
var walletSettings = configWallet.settings;
this.unitToSatoshi = walletSettings.unitToSatoshi;
2015-04-23 08:27:43 -07:00
this.satToUnit = 1 / this.unitToSatoshi;
2015-10-07 12:17:19 -07:00
this.unitName = walletSettings.unitName;
this.alternativeIsoCode = walletSettings.alternativeIsoCode;
this.alternativeName = walletSettings.alternativeName;
2015-04-23 08:27:43 -07:00
this.alternativeAmount = 0;
2015-10-07 12:17:19 -07:00
this.unitDecimals = walletSettings.unitDecimals;
2015-04-23 08:27:43 -07:00
this.isCordova = isCordova;
this.addresses = [];
this.isMobile = isMobile.any();
this.isWindowsPhoneApp = isMobile.Windows() && isCordova;
this.blockUx = false;
this.isRateAvailable = false;
this.showScanner = false;
this.isMobile = isMobile.any();
2015-05-30 19:15:43 -07:00
this.addr = {};
2015-04-23 08:27:43 -07:00
var disableScannerListener = $rootScope.$on('dataScanned', function(event, data) {
self.setForm(data);
2015-04-28 14:11:06 -07:00
$rootScope.$emit('Local/SetTab', 'send');
2015-07-15 12:15:05 -07:00
var form = $scope.sendForm;
2015-09-18 11:29:55 -07:00
if (form.address.$invalid && !self.blockUx) {
2015-07-15 12:15:05 -07:00
self.resetForm();
self.error = gettext('Could not recognize a valid Bitcoin QR Code');
}
2015-04-23 08:27:43 -07:00
});
var disablePaymentUriListener = $rootScope.$on('paymentUri', function(event, uri) {
$timeout(function() {
2015-04-28 14:11:06 -07:00
$rootScope.$emit('Local/SetTab', 'send');
2015-04-23 08:27:43 -07:00
self.setForm(uri);
}, 100);
});
var disableAddrListener = $rootScope.$on('Local/NeedNewAddress', function() {
2015-06-27 09:22:56 -07:00
self.setAddress(true);
2015-04-23 08:27:43 -07:00
});
2015-04-23 22:42:10 -07:00
var disableFocusListener = $rootScope.$on('Local/NewFocusedWallet', function() {
2015-05-30 19:15:43 -07:00
self.addr = {};
2015-04-23 22:42:10 -07:00
self.resetForm();
});
2015-04-23 11:19:30 -07:00
2015-04-28 14:00:49 -07:00
var disableResumeListener = $rootScope.$on('Local/Resume', function() {
2015-04-28 15:26:22 -07:00
// This is needed then the apps go to sleep
self.bindTouchDown();
2015-04-28 14:00:49 -07:00
});
2015-04-23 22:42:10 -07:00
var disableTabListener = $rootScope.$on('Local/TabChanged', function(e, tab) {
2015-04-28 12:58:40 -07:00
// This will slow down switch, do not add things here!
2015-04-23 22:42:10 -07:00
switch (tab) {
2015-04-23 11:19:30 -07:00
case 'receive':
2015-04-28 12:58:40 -07:00
// just to be sure we have an address
self.setAddress();
2015-04-23 11:19:30 -07:00
break;
2015-04-28 12:58:40 -07:00
case 'send':
self.resetError();
2015-04-23 11:19:30 -07:00
};
});
var disableOngoingProcessListener = $rootScope.$on('Addon/OngoingProcess', function(e, name) {
self.setOngoingProcess(name);
});
2015-04-23 08:27:43 -07:00
$scope.$on('$destroy', function() {
disableAddrListener();
disableScannerListener();
disablePaymentUriListener();
2015-04-23 11:19:30 -07:00
disableTabListener();
2015-04-23 22:42:10 -07:00
disableFocusListener();
2015-04-28 14:00:49 -07:00
disableResumeListener();
disableOngoingProcessListener();
2015-04-23 08:27:43 -07:00
$rootScope.hideMenuBar = false;
});
2015-10-07 12:17:19 -07:00
var requestTouchid = function(cb) {
var fc = profileService.focusedClient;
config.touchIdFor = config.touchIdFor || {};
if (window.touchidAvailable && config.touchIdFor[fc.credentials.walletId]) {
$rootScope.$emit('Local/RequestTouchid', cb);
} else {
return cb();
}
};
2015-12-01 12:16:39 -08:00
this.onQrCodeScanned = function(data) {
$rootScope.$emit('dataScanned', data);
};
this.openSendScreen = function() {
go.send();
};
2015-04-23 08:27:43 -07:00
rateService.whenAvailable(function() {
self.isRateAvailable = true;
$rootScope.$digest();
});
2015-07-29 08:37:51 -07:00
var accept_msg = gettextCatalog.getString('Accept');
var cancel_msg = gettextCatalog.getString('Cancel');
var confirm_msg = gettextCatalog.getString('Confirm');
$scope.openDestinationAddressModal = function(wallets, address) {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = true;
2015-10-22 14:43:32 -07:00
var fc = profileService.focusedClient;
self.resetForm();
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.wallets = wallets;
2015-10-22 14:43:32 -07:00
$scope.editAddressbook = false;
$scope.addAddressbookEntry = false;
$scope.selectedAddressbook = {};
$scope.newAddress = address;
2015-12-01 12:33:53 -08:00
$scope.walletName = fc.credentials.walletName;
$scope.addressbook = {
'address': ($scope.newAddress || ''),
'label': ''
};
2015-10-22 14:43:32 -07:00
$scope.beforeQrCodeScann = function() {
$scope.error = null;
$scope.addAddressbookEntry = true;
$scope.editAddressbook = false;
};
$scope.onQrCodeScanned = function(data, addressbookForm) {
$timeout(function() {
var form = addressbookForm;
if (data && form) {
data = data.replace('bitcoin:', '');
form.address.$setViewValue(data);
form.address.$isValid = true;
form.address.$render();
}
$scope.$digest();
}, 100);
};
2015-10-22 14:43:32 -07:00
$scope.selectAddressbook = function(addr) {
$modalInstance.close(addr);
};
$scope.toggleEditAddressbook = function() {
$scope.editAddressbook = !$scope.editAddressbook;
$scope.selectedAddressbook = {};
$scope.addAddressbookEntry = false;
};
$scope.toggleSelectAddressbook = function(addr) {
$scope.selectedAddressbook[addr] = $scope.selectedAddressbook[addr] ? false : true;
};
$scope.toggleAddAddressbookEntry = function() {
$scope.error = null;
$scope.addressbook = {
'address': ($scope.newAddress || ''),
'label': ''
};
2015-10-22 14:43:32 -07:00
$scope.addAddressbookEntry = !$scope.addAddressbookEntry;
};
$scope.list = function() {
$scope.error = null;
addressbookService.list(function(err, ab) {
if (err) {
2015-10-22 14:43:32 -07:00
$scope.error = err;
return;
}
$scope.list = ab;
});
};
$scope.add = function(addressbook) {
$scope.error = null;
$timeout(function() {
addressbookService.add(addressbook, function(err, ab) {
if (err) {
$scope.error = err;
return;
}
2015-11-02 07:04:18 -08:00
$rootScope.$emit('Local/AddressbookUpdated', ab);
$scope.list = ab;
$scope.editAddressbook = true;
$scope.toggleEditAddressbook();
$scope.$digest();
});
}, 100);
2015-10-22 14:43:32 -07:00
};
$scope.remove = function(addr) {
$scope.error = null;
$timeout(function() {
addressbookService.remove(addr, function(err, ab) {
if (err) {
$scope.error = err;
return;
}
2015-11-02 07:04:18 -08:00
$rootScope.$emit('Local/AddressbookUpdated', ab);
$scope.list = ab;
$scope.$digest();
});
}, 100);
2015-10-22 14:43:32 -07:00
};
2015-09-30 10:14:15 -07:00
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
2015-06-27 09:48:25 -07:00
$scope.selectWallet = function(walletId, walletName) {
$scope.gettingAddress = true;
$scope.selectedWalletName = walletName;
$timeout(function() {
2015-06-27 09:48:25 -07:00
$scope.$apply();
});
addressService.getAddress(walletId, false, function(err, addr) {
$scope.gettingAddress = false;
if (err) {
2015-06-27 09:48:25 -07:00
self.error = err;
$modalInstance.dismiss('cancel');
return;
2015-06-27 09:48:25 -07:00
}
$modalInstance.close(addr);
2015-06-27 09:48:25 -07:00
});
};
};
2015-06-27 09:48:25 -07:00
var modalInstance = $modal.open({
2015-10-22 14:43:32 -07:00
templateUrl: 'views/modals/destination-address.html',
2015-09-30 22:13:33 -07:00
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
2015-09-30 10:14:15 -07:00
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
modalInstance.result.finally(function() {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = false;
disableCloseModal();
var m = angular.element(document.getElementsByClassName('reveal-modal'));
2015-09-30 22:13:33 -07:00
m.addClass(animationService.modalAnimated.slideOutDown);
});
2015-06-27 09:48:25 -07:00
modalInstance.result.then(function(addr) {
if (addr) {
self.setForm(addr);
}
});
};
var GLIDERA_LOCK_TIME = 6 * 60 * 60;
// isGlidera flag is a security mesure so glidera status is not
// only determined by the tx.message
this.openTxpModal = function(tx, copayers, isGlidera) {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = true;
2015-03-06 07:00:10 -08:00
var fc = profileService.focusedClient;
2015-05-30 18:38:08 -07:00
var refreshUntilItChanges = false;
var currentSpendUnconfirmed = configWallet.spendUnconfirmed;
2015-03-06 07:00:10 -08:00
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.error = null;
$scope.copayers = copayers
2015-05-13 06:56:08 -07:00
$scope.copayerId = fc.credentials.copayerId;
2015-09-04 06:17:59 -07:00
$scope.canSign = fc.canSign() || fc.isPrivKeyExternal();
2015-03-06 07:00:10 -08:00
$scope.loading = null;
$scope.color = fc.backgroundColor;
$scope.isShared = fc.credentials.n > 1;
2015-09-08 17:11:13 -07:00
// ToDo: use tx.customData instead of tx.message
if (tx.message === 'Glidera transaction' && isGlidera) {
tx.isGlidera = true;
if (tx.canBeRemoved) {
tx.canBeRemoved = (Date.now() / 1000 - (tx.ts || tx.createdOn)) > GLIDERA_LOCK_TIME;
}
2015-09-08 17:11:13 -07:00
}
$scope.tx = tx;
2015-05-30 18:38:08 -07:00
refreshUntilItChanges = false;
2015-08-11 13:59:41 -07:00
$scope.currentSpendUnconfirmed = currentSpendUnconfirmed;
2015-03-06 07:00:10 -08:00
$scope.getShortNetworkName = function() {
return fc.credentials.networkName.substring(0, 4);
};
2015-09-03 14:14:38 -07:00
lodash.each(['TxProposalRejectedBy', 'TxProposalAcceptedBy', 'transactionProposalRemoved', 'TxProposalRemoved', 'NewOutgoingTx', 'UpdateTx'], function(eventName) {
2015-03-06 07:00:10 -08:00
$rootScope.$on(eventName, function() {
fc.getTx($scope.tx.id, function(err, tx) {
if (err) {
2015-09-03 13:34:42 -07:00
if (err.code && err.code == 'TX_NOT_FOUND' &&
2015-03-06 07:00:10 -08:00
(eventName == 'transactionProposalRemoved' || eventName == 'TxProposalRemoved')) {
$scope.tx.removed = true;
2015-06-18 07:17:35 -07:00
$scope.tx.canBeRemoved = false;
2015-03-06 07:00:10 -08:00
$scope.tx.pendingForUs = false;
$scope.$apply();
return;
}
return;
}
var action = lodash.find(tx.actions, {
copayerId: fc.credentials.copayerId
});
2015-09-09 12:17:08 -07:00
$scope.tx = txFormatService.processTx(tx);
2015-03-06 07:00:10 -08:00
if (!action && tx.status == 'pending')
$scope.tx.pendingForUs = true;
$scope.updateCopayerList();
$scope.$apply();
});
});
});
$scope.updateCopayerList = function() {
lodash.map($scope.copayers, function(cp) {
lodash.each($scope.tx.actions, function(ac) {
if (cp.id == ac.copayerId) {
cp.action = ac.type;
}
});
});
};
$scope.sign = function(txp) {
var fc = profileService.focusedClient;
2015-09-04 06:17:59 -07:00
if (!fc.canSign() && !fc.isPrivKeyExternal())
return;
2015-03-06 07:00:10 -08:00
if (fc.isPrivKeyEncrypted()) {
profileService.unlockFC(function(err) {
if (err) {
$scope.error = bwsError.msg(err);
2015-03-06 07:00:10 -08:00
return;
}
return $scope.sign(txp);
});
return;
};
2015-10-07 12:17:19 -07:00
self._setOngoingForSigning();
2015-03-06 07:00:10 -08:00
$scope.loading = true;
$scope.error = null;
2015-03-06 07:00:10 -08:00
$timeout(function() {
2015-10-07 12:17:19 -07:00
requestTouchid(function(err) {
2015-03-06 07:00:10 -08:00
if (err) {
2015-10-07 12:17:19 -07:00
self.setOngoingProcess();
$scope.loading = false;
2015-10-07 12:17:19 -07:00
profileService.lockFC();
$scope.error = err;
2015-03-06 07:00:10 -08:00
$scope.$digest();
2015-10-07 12:17:19 -07:00
return;
}
profileService.signTxProposal(txp, function(err, txpsi) {
self.setOngoingProcess();
if (err) {
$scope.$emit('UpdateTx');
$scope.loading = false;
2015-10-07 12:17:19 -07:00
$scope.error = bwsError.msg(err, gettextCatalog.getString('Could not accept payment'));
$scope.$digest();
} else {
//if txp has required signatures then broadcast it
var txpHasRequiredSignatures = txpsi.status == 'accepted';
if (txpHasRequiredSignatures) {
self.setOngoingProcess(gettext('Broadcasting transaction'));
$scope.loading = true;
fc.broadcastTxProposal(txpsi, function(err, txpsb, memo) {
self.setOngoingProcess();
$scope.loading = false;
if (err) {
$scope.$emit('UpdateTx');
$scope.error = bwsError.msg(err, gettextCatalog.getString('Could not broadcast payment'));
$scope.$digest();
} else {
$log.debug('Transaction signed and broadcasted')
if (memo)
$log.info(memo);
refreshUntilItChanges = true;
$modalInstance.close(txpsb);
}
});
} else {
$scope.loading = false;
$modalInstance.close(txpsi);
}
2015-03-06 07:00:10 -08:00
}
2015-10-07 12:17:19 -07:00
});
2015-03-06 07:00:10 -08:00
});
}, 100);
};
$scope.reject = function(txp) {
self.setOngoingProcess(gettext('Rejecting payment'));
2015-03-06 07:00:10 -08:00
$scope.loading = true;
$scope.error = null;
$timeout(function() {
fc.rejectTxProposal(txp, null, function(err, txpr) {
self.setOngoingProcess();
2015-03-06 07:00:10 -08:00
$scope.loading = false;
if (err) {
2015-09-03 14:14:38 -07:00
$scope.$emit('UpdateTx');
2015-08-13 12:47:10 -07:00
$scope.error = bwsError.msg(err, gettextCatalog.getString('Could not reject payment'));
2015-03-06 07:00:10 -08:00
$scope.$digest();
} else {
$modalInstance.close(txpr);
}
});
}, 100);
};
$scope.remove = function(txp) {
self.setOngoingProcess(gettext('Deleting payment'));
2015-03-06 07:00:10 -08:00
$scope.loading = true;
$scope.error = null;
$timeout(function() {
fc.removeTxProposal(txp, function(err, txpb) {
self.setOngoingProcess();
2015-03-06 07:00:10 -08:00
$scope.loading = false;
// Hacky: request tries to parse an empty response
2015-04-21 09:06:44 -07:00
if (err && !(err.message && err.message.match(/Unexpected/))) {
2015-09-03 14:14:38 -07:00
$scope.$emit('UpdateTx');
2015-08-13 12:47:10 -07:00
$scope.error = bwsError.msg(err, gettextCatalog.getString('Could not delete payment proposal'));
2015-03-06 07:00:10 -08:00
$scope.$digest();
return;
2015-04-21 09:06:44 -07:00
}
2015-03-06 07:00:10 -08:00
$modalInstance.close();
});
}, 100);
};
$scope.broadcast = function(txp) {
self.setOngoingProcess(gettext('Broadcasting Payment'));
2015-03-06 07:00:10 -08:00
$scope.loading = true;
$scope.error = null;
$timeout(function() {
2015-05-22 13:16:55 -07:00
fc.broadcastTxProposal(txp, function(err, txpb, memo) {
self.setOngoingProcess();
2015-03-06 07:00:10 -08:00
$scope.loading = false;
if (err) {
2015-08-13 12:47:10 -07:00
$scope.error = bwsError.msg(err, gettextCatalog.getString('Could not broadcast payment'));
2015-03-06 07:00:10 -08:00
$scope.$digest();
} else {
2015-05-22 13:16:55 -07:00
if (memo)
$log.info(memo);
2015-05-29 08:39:17 -07:00
2015-05-30 18:38:08 -07:00
refreshUntilItChanges = true;
2015-05-29 08:39:17 -07:00
$modalInstance.close(txpb);
2015-03-06 07:00:10 -08:00
}
});
}, 100);
};
2015-04-26 22:31:07 -07:00
$scope.copyAddress = function(addr) {
if (!addr) return;
self.copyAddress(addr);
};
2015-03-06 07:00:10 -08:00
$scope.cancel = function() {
2015-04-21 22:48:00 -07:00
$modalInstance.dismiss('cancel');
2015-03-06 07:00:10 -08:00
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/txp-details.html',
2015-09-30 22:13:33 -07:00
windowClass: animationService.modalAnimated.slideRight,
2015-03-06 07:00:10 -08:00
controller: ModalInstanceCtrl,
});
2015-09-30 10:14:15 -07:00
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
2015-05-07 15:02:38 -07:00
modalInstance.result.finally(function() {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = false;
disableCloseModal();
2015-05-07 15:02:38 -07:00
var m = angular.element(document.getElementsByClassName('reveal-modal'));
2015-09-30 22:13:33 -07:00
m.addClass(animationService.modalAnimated.slideOutRight);
2015-05-07 15:02:38 -07:00
});
2015-05-29 08:39:17 -07:00
modalInstance.result.then(function(txp) {
2015-05-12 06:59:10 -07:00
self.setOngoingProcess();
2015-03-06 07:00:10 -08:00
if (txp) {
2015-05-08 07:38:34 -07:00
txStatus.notify(txp, function() {
2015-05-30 18:38:08 -07:00
$scope.$emit('Local/TxProposalAction', refreshUntilItChanges);
2015-05-08 07:38:34 -07:00
});
2015-05-12 06:59:10 -07:00
} else {
$timeout(function() {
2015-05-30 18:38:08 -07:00
$scope.$emit('Local/TxProposalAction', refreshUntilItChanges);
2015-05-12 06:59:10 -07:00
}, 100);
2015-03-06 07:00:10 -08:00
}
});
};
2015-06-27 09:22:56 -07:00
this.setAddress = function(forceNew) {
2015-05-29 07:46:33 -07:00
self.addrError = null;
2015-04-23 08:27:43 -07:00
var fc = profileService.focusedClient;
2015-05-13 08:41:05 -07:00
if (!fc)
return;
2015-06-27 09:22:56 -07:00
// Address already set?
if (!forceNew && self.addr[fc.credentials.walletId]) {
2015-05-30 19:15:43 -07:00
return;
}
2015-06-27 09:22:56 -07:00
self.generatingAddress = true;
2015-04-23 08:27:43 -07:00
$timeout(function() {
addressService.getAddress(fc.credentials.walletId, forceNew, function(err, addr) {
2015-06-27 09:22:56 -07:00
self.generatingAddress = false;
2015-08-12 07:49:13 -07:00
if (err) {
self.addrError = err;
2015-08-12 07:49:13 -07:00
} else {
if (addr)
self.addr[fc.credentials.walletId] = addr;
2015-04-23 08:27:43 -07:00
}
2015-06-27 09:22:56 -07:00
$scope.$digest();
2015-04-23 08:27:43 -07:00
});
});
};
this.copyAddress = function(addr) {
if (isCordova) {
2015-06-08 07:15:30 -07:00
window.cordova.plugins.clipboard.copy(addr);
2015-07-29 08:42:05 -07:00
window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard'));
2015-05-28 06:52:33 -07:00
} else if (nodeWebkit.isDefined()) {
nodeWebkit.writeToClipboard(addr);
2015-04-23 08:27:43 -07:00
}
};
this.shareAddress = function(addr) {
if (isCordova) {
if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true;
}
window.plugins.socialsharing.share('bitcoin:' + addr, null, null, null);
}
};
this.openCustomizedAmountModal = function(addr) {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = true;
var self = this;
var fc = profileService.focusedClient;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.addr = addr;
$scope.color = fc.backgroundColor;
$scope.unitName = self.unitName;
$scope.alternativeAmount = self.alternativeAmount;
$scope.alternativeName = self.alternativeName;
$scope.alternativeIsoCode = self.alternativeIsoCode;
$scope.isRateAvailable = self.isRateAvailable;
$scope.unitToSatoshi = self.unitToSatoshi;
$scope.unitDecimals = self.unitDecimals;
var satToUnit = 1 / self.unitToSatoshi;
$scope.showAlternative = false;
$scope.isCordova = isCordova;
Object.defineProperty($scope,
"_customAlternative", {
get: function() {
return $scope.customAlternative;
},
set: function(newValue) {
$scope.customAlternative = newValue;
if (typeof(newValue) === 'number' && $scope.isRateAvailable) {
$scope.customAmount = parseFloat((rateService.fromFiat(newValue, $scope.alternativeIsoCode) * satToUnit).toFixed($scope.unitDecimals), 10);
2015-08-07 13:21:22 -07:00
} else {
$scope.customAmount = null;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty($scope,
"_customAmount", {
get: function() {
return $scope.customAmount;
},
set: function(newValue) {
$scope.customAmount = newValue;
if (typeof(newValue) === 'number' && $scope.isRateAvailable) {
$scope.customAlternative = parseFloat((rateService.toFiat(newValue * $scope.unitToSatoshi, $scope.alternativeIsoCode)).toFixed(2), 10);
} else {
2015-08-07 13:21:22 -07:00
$scope.customAlternative = null;
}
$scope.alternativeAmount = $scope.customAlternative;
},
enumerable: true,
configurable: true
});
$scope.submitForm = function(form) {
var satToBtc = 1 / 100000000;
var amount = form.amount.$modelValue;
2015-06-26 10:25:53 -07:00
var amountSat = parseInt((amount * $scope.unitToSatoshi).toFixed(0));
$timeout(function() {
$scope.customizedAmountUnit = amount + ' ' + $scope.unitName;
$scope.customizedAlternativeUnit = $filter('noFractionNumber')(form.alternative.$modelValue, 2) + ' ' + $scope.alternativeIsoCode;
if ($scope.unitName == 'bits') {
2015-06-26 11:08:59 -07:00
amount = (amountSat * satToBtc).toFixed(8);
2015-06-26 10:25:53 -07:00
}
$scope.customizedAmountBtc = amount;
}, 1);
};
$scope.toggleAlternative = function() {
$scope.showAlternative = !$scope.showAlternative;
};
$scope.shareAddress = function(uri) {
if (isCordova) {
if (isMobile.Android() || isMobile.Windows()) {
window.ignoreMobilePause = true;
}
window.plugins.socialsharing.share(uri, null, null, null);
}
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/customized-amount.html',
2015-09-30 22:13:33 -07:00
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
2015-09-30 10:14:15 -07:00
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
modalInstance.result.finally(function() {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = false;
disableCloseModal();
var m = angular.element(document.getElementsByClassName('reveal-modal'));
2015-09-30 22:13:33 -07:00
m.addClass(animationService.modalAnimated.slideOutDown);
});
};
2015-07-24 08:11:07 -07:00
// Send
2015-04-28 08:06:04 -07:00
this.canShowAlternative = function() {
return $scope.showAlternative;
};
this.showAlternative = function() {
$scope.showAlternative = true;
};
this.hideAlternative = function() {
$scope.showAlternative = false;
};
2015-04-23 08:27:43 -07:00
this.resetError = function() {
2015-05-29 08:39:17 -07:00
this.error = this.success = null;
2015-04-23 08:27:43 -07:00
};
2015-04-28 12:08:43 -07:00
this.bindTouchDown = function(tries) {
var self = this;
tries = tries || 0;
if (tries > 5) return;
var e = document.getElementById('menu-walletHome');
2015-04-28 16:13:28 -07:00
if (!e) return $timeout(function() {
2015-04-28 12:08:43 -07:00
self.bindTouchDown(++tries);
}, 500);
// on touchdown elements
$log.debug('Binding touchstart elements...');
2015-04-29 08:08:16 -07:00
['hamburger', 'menu-walletHome', 'menu-send', 'menu-receive', 'menu-history'].forEach(function(id) {
2015-04-28 12:08:43 -07:00
var e = document.getElementById(id);
if (e) e.addEventListener('touchstart', function() {
try {
event.preventDefault();
} catch (e) {};
2015-04-28 12:08:43 -07:00
angular.element(e).triggerHandler('click');
}, true);
2015-04-28 12:08:43 -07:00
});
}
2015-04-23 08:27:43 -07:00
2015-04-28 12:08:43 -07:00
this.hideMenuBar = lodash.debounce(function(hide) {
2015-04-23 08:27:43 -07:00
if (hide) {
$rootScope.hideMenuBar = true;
2015-04-28 12:08:43 -07:00
this.bindTouchDown();
2015-04-23 08:27:43 -07:00
} else {
$rootScope.hideMenuBar = false;
}
$rootScope.$digest();
}, 100);
this.formFocus = function(what) {
if (isCordova && !this.isWindowsPhoneApp) {
2015-04-28 12:08:43 -07:00
this.hideMenuBar(what);
2015-04-23 08:27:43 -07:00
}
if (!this.isWindowsPhoneApp) return
if (!what) {
this.hideAddress = false;
this.hideAmount = false;
} else {
if (what == 'amount') {
this.hideAddress = true;
} else if (what == 'msg') {
this.hideAddress = true;
this.hideAmount = true;
}
}
$timeout(function() {
$rootScope.$digest();
}, 1);
};
2015-04-28 12:58:40 -07:00
this.setSendFormInputs = function() {
2015-04-23 08:27:43 -07:00
var unitToSat = this.unitToSatoshi;
var satToUnit = 1 / unitToSat;
/**
* Setting the two related amounts as properties prevents an infinite
* recursion for watches while preserving the original angular updates
*
*/
Object.defineProperty($scope,
"_alternative", {
get: function() {
return $scope.__alternative;
},
set: function(newValue) {
$scope.__alternative = newValue;
if (typeof(newValue) === 'number' && self.isRateAvailable) {
$scope._amount = parseFloat((rateService.fromFiat(newValue, self.alternativeIsoCode) * satToUnit).toFixed(self.unitDecimals), 10);
2015-08-07 13:21:22 -07:00
} else {
$scope.__amount = null;
2015-04-23 08:27:43 -07:00
}
},
enumerable: true,
configurable: true
});
Object.defineProperty($scope,
"_amount", {
get: function() {
return $scope.__amount;
},
set: function(newValue) {
$scope.__amount = newValue;
if (typeof(newValue) === 'number' && self.isRateAvailable) {
$scope.__alternative = parseFloat((rateService.toFiat(newValue * self.unitToSatoshi, self.alternativeIsoCode)).toFixed(2), 10);
} else {
2015-08-07 13:21:22 -07:00
$scope.__alternative = null;
2015-04-23 08:27:43 -07:00
}
self.alternativeAmount = $scope.__alternative;
self.resetError();
},
enumerable: true,
configurable: true
});
Object.defineProperty($scope,
"_address", {
get: function() {
return $scope.__address;
},
set: function(newValue) {
$scope.__address = self.onAddressChange(newValue);
2015-10-22 14:43:32 -07:00
if ($scope.sendForm && $scope.sendForm.address.$valid) {
self.lockAddress = true;
}
2015-04-23 08:27:43 -07:00
},
enumerable: true,
configurable: true
});
var fc = profileService.focusedClient;
// ToDo: use a credential's (or fc's) function for this
this.hideNote = !fc.credentials.sharedEncryptingKey;
2015-04-23 08:27:43 -07:00
};
2015-05-12 06:48:00 -07:00
this.setSendError = function(err) {
2015-04-23 11:23:21 -07:00
var fc = profileService.focusedClient;
var prefix =
2015-08-13 12:47:10 -07:00
fc.credentials.m > 1 ? gettextCatalog.getString('Could not create payment proposal') : gettextCatalog.getString('Could not send payment');
2015-04-23 08:27:43 -07:00
2015-08-12 07:49:13 -07:00
this.error = bwsError.msg(err, prefix);
2015-04-23 08:27:43 -07:00
$timeout(function() {
$scope.$digest();
}, 1);
};
this.setOngoingProcess = function(name) {
var self = this;
2015-04-23 22:42:10 -07:00
self.blockUx = !!name;
if (isCordova) {
if (name) {
2015-04-28 13:49:09 -07:00
window.plugins.spinnerDialog.hide();
2015-04-23 22:42:10 -07:00
window.plugins.spinnerDialog.show(null, name + '...', true);
} else {
window.plugins.spinnerDialog.hide();
}
} else {
2015-04-28 13:19:22 -07:00
self.onGoingProcess = name;
2015-04-28 13:28:49 -07:00
$timeout(function() {
$rootScope.$apply();
});
2015-04-28 13:19:22 -07:00
};
2015-04-23 08:27:43 -07:00
};
this.submitForm = function(currentFeePerKb) {
2015-04-23 11:23:21 -07:00
var fc = profileService.focusedClient;
2015-04-23 08:27:43 -07:00
var unitToSat = this.unitToSatoshi;
var currentSpendUnconfirmed = configWallet.spendUnconfirmed;
2015-08-12 07:49:13 -07:00
2015-07-02 10:42:53 -07:00
if (isCordova && this.isWindowsPhoneApp) {
this.hideAddress = false;
this.hideAmount = false;
}
2015-04-23 08:27:43 -07:00
2015-04-23 22:42:10 -07:00
var form = $scope.sendForm;
2015-04-23 08:27:43 -07:00
if (form.$invalid) {
this.error = gettext('Unable to send transaction proposal');
2015-04-23 08:27:43 -07:00
return;
}
if (fc.isPrivKeyEncrypted()) {
profileService.unlockFC(function(err) {
2015-05-12 06:48:00 -07:00
if (err) return self.setSendError(err);
2015-04-23 22:42:10 -07:00
return self.submitForm();
2015-04-23 08:27:43 -07:00
});
return;
};
var comment = form.comment.$modelValue;
// ToDo: use a credential's (or fc's) function for this
if (comment && !fc.credentials.sharedEncryptingKey) {
var msg = 'Could not add message to imported wallet without shared encrypting key';
$log.warn(msg);
return self.setSendError(gettext(msg));
}
self.setOngoingProcess(gettext('Creating transaction'));
2015-04-23 08:27:43 -07:00
$timeout(function() {
var paypro = self._paypro;
var address, amount;
address = form.address.$modelValue;
amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0));
2015-10-07 12:17:19 -07:00
requestTouchid(function(err) {
if (err) {
profileService.lockFC();
self.setOngoingProcess();
self.error = err;
$timeout(function() {
$scope.$digest();
}, 1);
return;
}
2015-07-24 08:11:07 -07:00
fc.sendTxProposal({
toAddress: address,
amount: amount,
message: comment,
payProUrl: paypro ? paypro.url : null,
feePerKb: currentFeePerKb,
excludeUnconfirmedUtxos: currentSpendUnconfirmed ? false : true
}, function(err, txp) {
if (err) {
self.setOngoingProcess();
profileService.lockFC();
return self.setSendError(err);
}
2015-10-07 12:17:19 -07:00
if (!fc.canSign() && !fc.isPrivKeyExternal()) {
$log.info('No signing proposal: No private key')
self.setOngoingProcess();
self.resetForm();
txStatus.notify(txp, function() {
return $scope.$emit('Local/TxProposalAction');
2015-10-07 12:17:19 -07:00
});
return;
}
self.signAndBroadcast(txp, function(err) {
self.setOngoingProcess();
self.resetForm();
if (err) {
self.error = err.message ? err.message : gettext('The payment was created but could not be completed. Please try again from home screen');
$scope.$emit('Local/TxProposalAction');
$timeout(function() {
$scope.$digest();
}, 1);
}
2015-07-24 08:11:07 -07:00
});
2015-04-23 08:27:43 -07:00
});
});
}, 100);
};
this._setOngoingForSigning = function() {
2015-04-23 11:23:21 -07:00
var fc = profileService.focusedClient;
2015-07-17 06:53:50 -07:00
if (fc.isPrivKeyExternal() && fc.getPrivKeyExternalSourceName() == 'ledger') {
self.setOngoingProcess(gettext('Requesting Ledger Wallet to sign'));
2015-07-17 06:53:50 -07:00
} else {
self.setOngoingProcess(gettext('Signing payment'));
2015-07-17 06:53:50 -07:00
}
};
this.signAndBroadcast = function(txp, cb) {
2015-07-17 06:53:50 -07:00
var fc = profileService.focusedClient;
this._setOngoingForSigning();
profileService.signTxProposal(txp, function(err, signedTx) {
2015-05-30 08:46:38 -07:00
self.setOngoingProcess();
2015-04-26 14:41:21 -07:00
if (err) {
if (!lodash.isObject(err)) {
err = { message: err};
}
2015-08-13 12:47:10 -07:00
err.message = bwsError.msg(err, gettextCatalog.getString('The payment was created but could not be signed. Please try again from home screen'));
return cb(err);
}
2015-04-23 08:27:43 -07:00
if (signedTx.status == 'accepted') {
self.setOngoingProcess(gettext('Broadcasting transaction'));
2015-05-22 13:16:55 -07:00
fc.broadcastTxProposal(signedTx, function(err, btx, memo) {
2015-04-28 13:28:49 -07:00
self.setOngoingProcess();
2015-04-23 08:27:43 -07:00
if (err) {
2015-08-13 12:47:10 -07:00
err.message = bwsError.msg(err, gettextCatalog.getString('The payment was signed but could not be broadcasted. Please try again from home screen'));
2015-04-28 13:28:49 -07:00
return cb(err);
2015-04-23 08:27:43 -07:00
}
2015-05-22 13:16:55 -07:00
if (memo)
$log.info(memo);
2015-04-26 22:38:48 -07:00
txStatus.notify(btx, function() {
2015-05-28 09:14:54 -07:00
$scope.$emit('Local/TxProposalAction', true);
2015-04-26 22:38:48 -07:00
return cb();
});
2015-04-23 08:27:43 -07:00
});
} else {
2015-04-28 11:17:13 -07:00
self.setOngoingProcess();
2015-04-26 22:38:48 -07:00
txStatus.notify(signedTx, function() {
2015-05-08 07:38:34 -07:00
$scope.$emit('Local/TxProposalAction');
2015-04-26 22:38:48 -07:00
return cb();
});
2015-04-23 08:27:43 -07:00
}
});
};
2015-08-13 10:54:53 -07:00
this.setForm = function(to, amount, comment, feeRate) {
2015-04-23 08:27:43 -07:00
var form = $scope.sendForm;
if (to) {
form.address.$setViewValue(to);
form.address.$isValid = true;
form.address.$render();
this.lockAddress = true;
}
if (amount) {
form.amount.$setViewValue("" + amount);
form.amount.$isValid = true;
form.amount.$render();
this.lockAmount = true;
}
if (comment) {
form.comment.$setViewValue(comment);
form.comment.$isValid = true;
form.comment.$render();
}
2015-08-13 10:54:53 -07:00
if (feeRate) {
form.feeRate = feeRate;
}
2015-04-23 08:27:43 -07:00
};
2015-04-23 22:42:10 -07:00
this.resetForm = function() {
2015-04-23 08:27:43 -07:00
this.resetError();
this._paypro = null;
this.lockAddress = false;
this.lockAmount = false;
this._amount = this._address = null;
2015-04-23 22:42:10 -07:00
var form = $scope.sendForm;
2015-08-13 10:54:53 -07:00
if (form && form.feeRate) {
form.feeRate = null;
}
2015-04-23 08:27:43 -07:00
if (form && form.amount) {
form.amount.$pristine = true;
form.amount.$setViewValue('');
form.amount.$render();
form.comment.$setViewValue('');
form.comment.$render();
form.$setPristine();
if (form.address) {
form.address.$pristine = true;
form.address.$setViewValue('');
form.address.$render();
}
}
$timeout(function() {
$rootScope.$digest();
}, 1);
};
this.openPPModal = function(paypro) {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = true;
2015-04-23 08:27:43 -07:00
var ModalInstanceCtrl = function($scope, $modalInstance) {
2015-04-23 11:23:21 -07:00
var fc = profileService.focusedClient;
2015-04-23 08:27:43 -07:00
var satToUnit = 1 / self.unitToSatoshi;
$scope.paypro = paypro;
$scope.alternative = self.alternativeAmount;
$scope.alternativeIsoCode = self.alternativeIsoCode;
$scope.isRateAvailable = self.isRateAvailable;
$scope.unitTotal = (paypro.amount * satToUnit).toFixed(self.unitDecimals);
$scope.unitName = self.unitName;
$scope.color = fc.backgroundColor;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
2015-05-07 15:02:38 -07:00
var modalInstance = $modal.open({
2015-04-23 08:27:43 -07:00
templateUrl: 'views/modals/paypro.html',
2015-09-30 22:13:33 -07:00
windowClass: animationService.modalAnimated.slideUp,
2015-04-23 08:27:43 -07:00
controller: ModalInstanceCtrl,
});
2015-05-07 15:02:38 -07:00
2015-09-30 10:14:15 -07:00
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
2015-05-07 15:02:38 -07:00
modalInstance.result.finally(function() {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = false;
disableCloseModal();
2015-05-07 15:02:38 -07:00
var m = angular.element(document.getElementsByClassName('reveal-modal'));
2015-09-30 22:13:33 -07:00
m.addClass(animationService.modalAnimated.slideOutDown);
2015-05-07 15:02:38 -07:00
});
2015-04-23 08:27:43 -07:00
};
this.setFromPayPro = function(uri, cb) {
if (!cb) cb = function() {};
2015-04-23 11:23:21 -07:00
var fc = profileService.focusedClient;
2015-04-23 08:27:43 -07:00
if (isChromeApp) {
this.error = gettext('Payment Protocol not supported on Chrome App');
return cb(true);
2015-04-23 08:27:43 -07:00
}
var satToUnit = 1 / this.unitToSatoshi;
var self = this;
/// Get information of payment if using Payment Protocol
self.setOngoingProcess(gettext('Fetching Payment Information'));
2015-04-23 08:27:43 -07:00
$log.debug('Fetch PayPro Request...', uri);
$timeout(function() {
fc.fetchPayPro({
payProUrl: uri,
}, function(err, paypro) {
2015-04-23 22:42:10 -07:00
self.setOngoingProcess();
2015-04-23 08:27:43 -07:00
if (err) {
$log.warn('Could not fetch payment request:', err);
2015-04-23 22:42:10 -07:00
self.resetForm();
2015-04-23 08:27:43 -07:00
var msg = err.toString();
if (msg.match('HTTP')) {
msg = gettext('Could not fetch payment information');
2015-04-23 08:27:43 -07:00
}
self.error = msg;
2015-09-18 08:07:51 -07:00
$timeout(function() {
$rootScope.$digest();
}, 1);
return cb(true);
}
if (!paypro.verified) {
self.resetForm();
$log.warn('Failed to verified payment protocol signatured');
self.error = gettext('Payment Protocol Invalid');
$timeout(function() {
$rootScope.$digest();
}, 1);
return cb(true);
2015-04-23 08:27:43 -07:00
}
self._paypro = paypro;
self.setForm(paypro.toAddress, (paypro.amount * satToUnit).toFixed(self.unitDecimals), paypro.memo);
return cb();
2015-04-23 08:27:43 -07:00
});
}, 1);
};
this.setFromUri = function(uri) {
var self = this;
2015-04-23 08:27:43 -07:00
function sanitizeUri(uri) {
// Fixes when a region uses comma to separate decimals
var regex = /[\?\&]amount=(\d+([\,\.]\d+)?)/i;
var match = regex.exec(uri);
if (!match || match.length === 0) {
return uri;
}
var value = match[0].replace(',', '.');
var newUri = uri.replace(regex, value);
return newUri;
};
var satToUnit = 1 / this.unitToSatoshi;
2015-09-29 08:51:08 -07:00
// URI extensions for Payment Protocol with non-backwards-compatible request
if ((/^bitcoin:\?r=[\w+]/).exec(uri)) {
uri = decodeURIComponent(uri.replace('bitcoin:?r=', ''));
this.setFromPayPro(uri, function(err) {
if (err) {
return err;
}
});
} else {
uri = sanitizeUri(uri);
2015-04-23 08:27:43 -07:00
if (!bitcore.URI.isValid(uri)) {
return uri;
}
var parsed = new bitcore.URI(uri);
var addr = parsed.address ? parsed.address.toString() : '';
var message = parsed.message;
2015-04-23 08:27:43 -07:00
var amount = parsed.amount ?
(parsed.amount.toFixed(0) * satToUnit).toFixed(this.unitDecimals) : 0;
2015-04-23 08:27:43 -07:00
if (parsed.r) {
this.setFromPayPro(parsed.r, function(err) {
if (err && addr && amount) {
self.setForm(addr, amount, message);
return addr;
}
});
} else {
this.setForm(addr, amount, message);
return addr;
}
}
2015-04-23 08:27:43 -07:00
};
this.onAddressChange = function(value) {
this.resetError();
if (!value) return '';
if (this._paypro)
return value;
if (value.indexOf('bitcoin:') === 0) {
return this.setFromUri(value);
} else if (/^https?:\/\//.test(value)) {
return this.setFromPayPro(value);
} else {
return value;
}
};
// History
function strip(number) {
return (parseFloat(number.toPrecision(12)));
}
this.getUnitName = function() {
return this.unitName;
};
this.getAlternativeIsoCode = function() {
return this.alternativeIsoCode;
};
this.openTxModal = function(btx) {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = true;
2015-04-23 08:27:43 -07:00
var self = this;
var fc = profileService.focusedClient;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.btx = btx;
2015-10-07 12:17:19 -07:00
$scope.settings = walletSettings;
2015-04-23 08:27:43 -07:00
$scope.color = fc.backgroundColor;
2015-05-13 06:56:08 -07:00
$scope.copayerId = fc.credentials.copayerId;
$scope.isShared = fc.credentials.n > 1;
2015-04-23 08:27:43 -07:00
$scope.getAmount = function(amount) {
return self.getAmount(amount);
};
$scope.getUnitName = function() {
return self.getUnitName();
};
$scope.getShortNetworkName = function() {
var n = fc.credentials.network;
return n.substring(0, 4);
};
2015-04-26 22:31:07 -07:00
$scope.copyAddress = function(addr) {
if (!addr) return;
self.copyAddress(addr);
};
2015-04-23 08:27:43 -07:00
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
2015-04-23 08:27:43 -07:00
};
2015-05-07 15:02:38 -07:00
var modalInstance = $modal.open({
2015-04-23 08:27:43 -07:00
templateUrl: 'views/modals/tx-details.html',
2015-09-30 22:13:33 -07:00
windowClass: animationService.modalAnimated.slideRight,
2015-04-23 08:27:43 -07:00
controller: ModalInstanceCtrl,
});
2015-05-07 15:02:38 -07:00
2015-09-30 10:14:15 -07:00
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
2015-05-07 15:02:38 -07:00
modalInstance.result.finally(function() {
2015-09-30 10:14:15 -07:00
$rootScope.modalOpened = false;
disableCloseModal();
2015-05-07 15:02:38 -07:00
var m = angular.element(document.getElementsByClassName('reveal-modal'));
2015-09-30 22:13:33 -07:00
m.addClass(animationService.modalAnimated.slideOutRight);
2015-05-07 15:02:38 -07:00
});
2015-04-23 08:27:43 -07:00
};
this.hasAction = function(actions, action) {
return actions.hasOwnProperty('create');
};
2015-08-13 10:54:53 -07:00
this._doSendAll = function(amount, feeRate) {
this.setForm(null, amount, null, feeRate);
2015-06-19 11:00:27 -07:00
};
this.sendAll = function(amount, feeStr, feeRate) {
var self = this;
2015-08-24 13:09:59 -07:00
var msg = gettextCatalog.getString("{{fee}} will be deducted for bitcoin networking fees", {
fee: feeStr
});
2015-09-25 09:39:41 -07:00
confirmDialog.show(msg, function(confirmed) {
2015-11-23 05:47:24 -08:00
if (confirmed) {
2015-09-25 09:39:41 -07:00
self._doSendAll(amount, feeRate);
2015-11-23 05:47:24 -08:00
}
2015-09-25 09:39:41 -07:00
});
2015-08-12 07:49:13 -07:00
};
2015-06-19 11:00:27 -07:00
2015-06-27 09:22:56 -07:00
/* Start setup */
2015-06-19 11:00:27 -07:00
2015-05-11 11:25:42 -07:00
this.bindTouchDown();
2015-09-14 11:01:49 -07:00
if (profileService.focusedClient) {
this.setAddress();
this.setSendFormInputs();
}
2015-03-06 07:00:10 -08:00
});