refactor sign

This commit is contained in:
Matias Alejo Garcia 2016-01-21 14:52:58 -03:00 committed by Gustavo Maximiliano Cortez
commit bef3e5789c
4 changed files with 139 additions and 340 deletions

View file

@ -1463,20 +1463,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.setTab(tab, reset); self.setTab(tab, reset);
}); });
$rootScope.$on('Local/RequestTouchid', function(event, cb) {
window.plugins.touchid.verifyFingerprint(
gettextCatalog.getString('Scan your fingerprint please'),
function(msg) {
// OK
return cb();
},
function(msg) {
// ERROR
return cb(gettext('Error with Touch ID:') + msg);
}
);
});
$rootScope.$on('Local/NeedsPassword', function(event, isSetup, cb) { $rootScope.$on('Local/NeedsPassword', function(event, isSetup, cb) {
self.askPassword = { self.askPassword = {
isSetup: isSetup, isSetup: isSetup,

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('sellGlideraController', angular.module('copayApp.controllers').controller('sellGlideraController',
function($scope, $timeout, $log, $modal, configService, profileService, addressService, feeService, glideraService, bwsError, lodash, isChromeApp, animationService) { function($scope, $timeout, $log, $modal, configService, profileService, addressService, feeService, glideraService, bwsError, lodash, isChromeApp, animationService, txSignService) {
var self = this; var self = this;
var config = configService.getSync(); var config = configService.getSync();
@ -57,7 +57,9 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
$scope.selectWallet = function(walletId, walletName) { $scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) { if (!profileService.getClient(walletId).isComplete()) {
self.error = bwsError.msg({'code': 'WALLET_NOT_COMPLETE'}, 'Could not choose the wallet'); self.error = bwsError.msg({
'code': 'WALLET_NOT_COMPLETE'
}, 'Could not choose the wallet');
$modalInstance.dismiss('cancel'); $modalInstance.dismiss('cancel');
return; return;
} }
@ -101,8 +103,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
self.gettingSellPrice = false; self.gettingSellPrice = false;
if (err) { if (err) {
self.error = 'Could not get exchange information. Please, try again.'; self.error = 'Could not get exchange information. Please, try again.';
} } else {
else {
self.error = null; self.error = null;
self.sellPrice = sellPrice; self.sellPrice = sellPrice;
} }
@ -117,8 +118,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
self.loading = null; self.loading = null;
if (err) { if (err) {
self.error = 'Could not send confirmation code to your phone'; self.error = 'Could not send confirmation code to your phone';
} } else {
else {
self.show2faCodeInput = sent; self.show2faCodeInput = sent;
} }
}); });
@ -129,6 +129,12 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
var self = this; var self = this;
self.error = null; self.error = null;
txSignService.prepare(function(err) {
if (err) {
self.error = err;
return;
}
this.loading = 'Selling Bitcoin...'; this.loading = 'Selling Bitcoin...';
$timeout(function() { $timeout(function() {
addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) { addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) {
@ -151,7 +157,9 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
toAddress: sellAddress, toAddress: sellAddress,
amount: amount, amount: amount,
message: 'Glidera transaction', message: 'Glidera transaction',
customData: {'glideraToken': token}, customData: {
'glideraToken': token
},
payProUrl: null, payProUrl: null,
feePerKb: feePerKb, feePerKb: feePerKb,
excludeUnconfirmedUtxos: self.currentSpendUnconfirmed ? false : true excludeUnconfirmedUtxos: self.currentSpendUnconfirmed ? false : true
@ -166,20 +174,13 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
return; return;
} }
if (!fc.canSign()) { txSignService.sign(tx, function(err, txp) {
self.loading = null;
$log.info('No signing proposal: No private key');
return;
}
_signTx(txp, function(err, txp, rawTx) {
profileService.lockFC();
if (err) { if (err) {
self.loading = null; self.loading = null;
self.error = err; self.error = err;
$scope.$apply(); $scope.$apply();
} } else {
else { var rawTx = txp.raw;
var data = { var data = {
refundAddress: refundAddress, refundAddress: refundAddress,
signedTransaction: rawTx, signedTransaction: rawTx,
@ -196,8 +197,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
$scope.$emit('Local/GlideraError'); $scope.$emit('Local/GlideraError');
}, 100); }, 100);
}); });
} } else {
else {
self.success = data; self.success = data;
$scope.$emit('Local/GlideraTx'); $scope.$emit('Local/GlideraTx');
} }
@ -208,28 +208,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
}); });
}); });
}); });
}, 100); }, 100);
};
var _signTx = function(txp, cb) {
var self = this;
fc.signTxProposal(txp, function(err, signedTx) {
profileService.lockFC();
if (err) {
err = bwsError.msg(err, 'Could not accept payment');
return cb(err);
}
else {
if (signedTx.status == 'accepted') {
return cb(null, txp, signedTx.raw);
} else {
return cb('The transaction could not be signed');
}
}
}); });
}; };
}); });

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, isMobile, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService, ledger, bwsError, confirmDialog, txFormatService, animationService, addressbookService, go, feeService) { angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, isMobile, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService, ledger, bwsError, confirmDialog, txFormatService, animationService, addressbookService, go, feeService, txSignService) {
var self = this; var self = this;
window.ignoreMobilePause = false; window.ignoreMobilePause = false;
@ -87,16 +87,6 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
$rootScope.hideMenuBar = false; $rootScope.hideMenuBar = false;
}); });
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();
}
};
this.onQrCodeScanned = function(data) { this.onQrCodeScanned = function(data) {
if (data) go.send(); if (data) go.send();
$rootScope.$emit('dataScanned', data); $rootScope.$emit('dataScanned', data);
@ -336,72 +326,25 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
$scope.sign = function(txp) { $scope.sign = function(txp) {
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
$scope.error = null; $scope.error = null;
if (!fc.canSign() && !fc.isPrivKeyExternal())
return;
$scope.loading = true; $scope.loading = true;
$timeout(function() {
requestTouchid(function(err) { txSignService.prepareAndSignAndBroadcast(txp, {
if (err) { reporterFn: self.setOngoingProcess.bind(self)
}, function(err, txp) {
$scope.loading = false; $scope.loading = false;
$scope.$emit('UpdateTx');
if (err) {
$scope.error = err; $scope.error = err;
$timeout(function() { $timeout(function() {
$scope.$digest(); $scope.$digest();
}); });
return; return;
} }
profileService.unlockFC(function(err) {
if (err) {
$scope.loading = false;
$scope.error = bwsError.msg(err);
$timeout(function() {
$scope.$digest();
});
return;
}
self._setOngoingForSigning();
profileService.signTxProposal(txp, function(err, txpsi) {
self.setOngoingProcess();
if (err) {
$scope.$emit('UpdateTx');
$scope.loading = false;
$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(gettextCatalog.getString('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; refreshUntilItChanges = true;
$modalInstance.close(txpsb); $modalInstance.close(txp);
} return;
}); });
} else {
$scope.loading = false;
$modalInstance.close(txpsi);
}
}
});
});
});
}, 100);
}; };
$scope.reject = function(txp) { $scope.reject = function(txp) {
@ -871,20 +814,10 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
address = form.address.$modelValue; address = form.address.$modelValue;
amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0)); amount = parseInt((form.amount.$modelValue * unitToSat).toFixed(0));
requestTouchid(function(err) { txSignService.prepare(function(err) {
if (err) {
self.error = err;
$timeout(function() {
$scope.$digest();
}, 1);
return;
}
profileService.unlockFC(function(err) {
if (err) { if (err) {
return self.setSendError(err); return self.setSendError(err);
} }
self.setOngoingProcess(gettextCatalog.getString('Creating transaction')); self.setOngoingProcess(gettextCatalog.getString('Creating transaction'));
getFee(function(err, feePerKb) { getFee(function(err, feePerKb) {
if (err) $log.debug(err); if (err) $log.debug(err);
@ -902,8 +835,8 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
} }
if (!fc.canSign() && !fc.isPrivKeyExternal()) { if (!fc.canSign() && !fc.isPrivKeyExternal()) {
$log.info('No signing proposal: No private key')
self.setOngoingProcess(); self.setOngoingProcess();
$log.info('No signing proposal: No private key')
self.resetForm(); self.resetForm();
txStatus.notify(txp, function() { txStatus.notify(txp, function() {
return $scope.$emit('Local/TxProposalAction'); return $scope.$emit('Local/TxProposalAction');
@ -911,8 +844,9 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
return; return;
} }
self.signAndBroadcast(txp, function(err) { txSignService.signAndBroadcast(txp, {
self.setOngoingProcess(); reporterFn: self.setOngoingProcess.bind(self)
}, function(err, txp) {
self.resetForm(); self.resetForm();
if (err) { if (err) {
self.error = err.message ? err.message : gettext('The payment was created but could not be completed. Please try again from home screen'); self.error = err.message ? err.message : gettext('The payment was created but could not be completed. Please try again from home screen');
@ -920,8 +854,12 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
$timeout(function() { $timeout(function() {
$scope.$digest(); $scope.$digest();
}, 1); }, 1);
} else go.walletHome(); } else {
go.walletHome();
txStatus.notify(txp, function() {
$scope.$emit('Local/TxProposalAction', true);
}); });
};
}); });
}); });
}); });
@ -929,56 +867,6 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
}, 100); }, 100);
}; };
this._setOngoingForSigning = function() {
var fc = profileService.focusedClient;
if (fc.isPrivKeyExternal() && fc.getPrivKeyExternalSourceName() == 'ledger') {
self.setOngoingProcess(gettextCatalog.getString('Requesting Ledger Wallet to sign'));
} else {
self.setOngoingProcess(gettextCatalog.getString('Signing payment'));
}
};
this.signAndBroadcast = function(txp, cb) {
var fc = profileService.focusedClient;
this._setOngoingForSigning();
profileService.signTxProposal(txp, function(err, signedTx) {
self.setOngoingProcess();
if (err) {
if (!lodash.isObject(err)) {
err = { message: err};
}
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);
}
if (signedTx.status == 'accepted') {
self.setOngoingProcess(gettextCatalog.getString('Broadcasting transaction'));
fc.broadcastTxProposal(signedTx, function(err, btx, memo) {
self.setOngoingProcess();
if (err) {
err.message = bwsError.msg(err, gettextCatalog.getString('The payment was signed but could not be broadcasted. Please try again from home screen'));
return cb(err);
}
if (memo)
$log.info(memo);
txStatus.notify(btx, function() {
$scope.$emit('Local/TxProposalAction', true);
return cb();
});
});
} else {
self.setOngoingProcess();
txStatus.notify(signedTx, function() {
$scope.$emit('Local/TxProposalAction');
return cb();
});
}
});
};
this.setForm = function(to, amount, comment) { this.setForm = function(to, amount, comment) {
var form = $scope.sendForm; var form = $scope.sendForm;
if (to) { if (to) {

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.services') angular.module('copayApp.services')
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, ledger, bitcore, trezor) { .factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, bitcore) {
var root = {}; var root = {};
@ -683,59 +683,5 @@ angular.module('copayApp.services')
return lodash.sortBy(ret, 'name'); return lodash.sortBy(ret, 'name');
}; };
root._signWithLedger = function(txp, cb) {
var fc = root.focusedClient;
$log.info('Requesting Ledger Chrome app to sign the transaction');
ledger.signTx(txp, fc.credentials.account, function(result) {
$log.debug('Ledger response', result);
if (!result.success)
return cb(result.message || result.error);
txp.signatures = lodash.map(result.signatures, function(s) {
return s.substring(0, s.length - 2);
});
return fc.signTxProposal(txp, cb);
});
};
root._signWithTrezor = function(txp, cb) {
var fc = root.focusedClient;
$log.info('Requesting Trezor to sign the transaction');
var xPubKeys = lodash.pluck(fc.credentials.publicKeyRing, 'xPubKey');
trezor.signTx(xPubKeys, txp, fc.credentials.account, function(err, result) {
if (err) return cb(err);
$log.debug('Trezor response', result);
txp.signatures = result.signatures;
return fc.signTxProposal(txp, cb);
});
};
root.signTxProposal = function(txp, cb) {
var fc = root.focusedClient;
if (fc.isPrivKeyExternal()) {
switch (fc.getPrivKeyExternalSourceName()) {
case 'ledger':
return root._signWithLedger(txp, cb);
case 'trezor':
return root._signWithTrezor(txp, cb);
default:
var msg = 'Unsupported External Key:' + fc.getPrivKeyExternalSourceName();
$log.error(msg);
return cb(msg);
}
} else {
return fc.signTxProposal(txp, function(err, signedTxp) {
root.lockFC();
return cb(err, signedTxp);
});
}
};
return root; return root;
}); });