Merge pull request #1930 from cmgustavo/feature/pin-01
PIN for mobile devices
This commit is contained in:
commit
6f1342ea27
21 changed files with 589 additions and 130 deletions
|
|
@ -1,23 +1,80 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, $timeout, notification, pluginManager, identityService) {
|
||||
identityService.goWalletHome();
|
||||
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, $timeout, notification, pluginManager, identityService, pinService) {
|
||||
|
||||
var _credentials, _firstpin;
|
||||
|
||||
$scope.init = function() {
|
||||
identityService.goWalletHome();
|
||||
|
||||
pinService.makePinInput($scope, 'newpin', function(newValue) {
|
||||
_firstpin = newValue;
|
||||
$scope.askForPin = 2;
|
||||
});
|
||||
|
||||
pinService.makePinInput($scope, 'repeatpin', function(newValue) {
|
||||
if (newValue === _firstpin) {
|
||||
_firstpin = null;
|
||||
$scope.createPin(newValue);
|
||||
} else {
|
||||
$scope.askForPin = 1;
|
||||
_firstpin = null;
|
||||
|
||||
$scope.setPinForm.newpin.$setViewValue('');
|
||||
$scope.setPinForm.newpin.$render();
|
||||
$scope.setPinForm.repeatpin.$setViewValue('');
|
||||
$scope.setPinForm.repeatpin.$render();
|
||||
$scope.setPinForm.$setPristine();
|
||||
|
||||
$scope.error = 'Entered PINs were not equal. Try again';
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
$scope.createPin = function(pin) {
|
||||
preconditions.checkArgument(pin);
|
||||
preconditions.checkState($rootScope.iden);
|
||||
preconditions.checkState(_credentials && _credentials.email);
|
||||
|
||||
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
|
||||
_credentials.password = '';
|
||||
_credentials = null;
|
||||
$scope.askForPin = 0;
|
||||
$rootScope.hasPin = true;
|
||||
$scope.createDefaultWallet();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.createDefaultWallet = function() {
|
||||
$rootScope.hideNavigation = false;
|
||||
identityService.createDefaultWallet(function(err) {
|
||||
$scope.askForPin =0 ;
|
||||
$scope.loading = false;
|
||||
|
||||
if (err) {
|
||||
var msg = err.toString();
|
||||
$scope.error = msg;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
$scope.createProfile = function(form) {
|
||||
$rootScope.hideNavigation = false;
|
||||
if (form && form.$invalid) {
|
||||
$scope.error('Error', 'Please enter the required fields');
|
||||
$scope.error = 'Please enter the required fields';
|
||||
return;
|
||||
}
|
||||
$rootScope.starting = true;
|
||||
identityService.create(
|
||||
form.email.$modelValue, form.password.$modelValue, function(err) {
|
||||
$rootScope.starting = false;
|
||||
if (err) {
|
||||
var msg = err.toString();
|
||||
if (msg.indexOf('EEXIST')>=0 || msg.indexOf('BADC')>=0 ) {
|
||||
msg = 'This profile already exists'
|
||||
}
|
||||
$timeout(function() {
|
||||
$scope.loading = true;
|
||||
identityService.create(form.email.$modelValue, form.password.$modelValue, function(err) {
|
||||
$scope.loading = false;
|
||||
|
||||
if (err) {
|
||||
var msg = err.toString();
|
||||
if (msg.indexOf('EEXIST') >= 0 || msg.indexOf('BADC') >= 0) {
|
||||
msg = 'This profile already exists'
|
||||
}
|
||||
$timeout(function() {
|
||||
form.email.$setViewValue('');
|
||||
form.email.$render();
|
||||
form.password.$setViewValue('');
|
||||
|
|
@ -27,7 +84,26 @@ angular.module('copayApp.controllers').controller('CreateProfileController', fun
|
|||
form.$setPristine();
|
||||
$scope.error = msg;
|
||||
},1);
|
||||
}
|
||||
$scope.error = msg;
|
||||
} else {
|
||||
$scope.error = null;
|
||||
// mobile
|
||||
if (isMobile.any()) {
|
||||
_credentials = {
|
||||
email: form.email.$modelValue,
|
||||
password: form.password.$modelValue,
|
||||
};
|
||||
$scope.askForPin = 1;
|
||||
$rootScope.hideNavigation = true;
|
||||
$timeout(function() {
|
||||
$rootScope.$digest();
|
||||
}, 1);
|
||||
|
||||
return;
|
||||
} else {
|
||||
$scope.createDefaultWallet();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -39,9 +39,10 @@ angular.module('copayApp.controllers').controller('HeadController', function($sc
|
|||
window.onbeforeunload = undefined;
|
||||
});
|
||||
|
||||
if ($rootScope.wallet) {
|
||||
$scope.$on('$idleStart', function() {
|
||||
});
|
||||
$scope.init = function() {
|
||||
if (!$rootScope.wallet) return;
|
||||
|
||||
$scope.$on('$idleStart', function() {});
|
||||
$scope.$on('$idleWarn', function(a, countdown) {
|
||||
$rootScope.countdown = countdown;
|
||||
$rootScope.sessionExpired = true;
|
||||
|
|
@ -64,8 +65,5 @@ angular.module('copayApp.controllers').controller('HeadController', function($sc
|
|||
$rootScope.$watch('title', function(newTitle, oldTitle) {
|
||||
$scope.title = newTitle;
|
||||
});
|
||||
$rootScope.$on('signout', function() {
|
||||
$scope.signout();
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,20 +1,57 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, $timeout, notification, identityService, Compatibility) {
|
||||
// This is only for backwards compat, insight api should link to #!/confirmed directly
|
||||
if (getParam('confirmed')) {
|
||||
var hashIndex = window.location.href.indexOf('/?');
|
||||
window.location = window.location.href.substr(0, hashIndex) + '#!/confirmed';
|
||||
return;
|
||||
}
|
||||
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, $timeout, notification, identityService, Compatibility, pinService, applicationService, isMobile) {
|
||||
|
||||
|
||||
if ($rootScope.fromEmailConfirmation) {
|
||||
$scope.confirmedEmail = true;
|
||||
$rootScope.fromEmailConfirmation = false;
|
||||
}
|
||||
var _credentials, _firstpin;
|
||||
|
||||
Compatibility.check($scope);
|
||||
$scope.init = function() {
|
||||
// This is only for backwards compat, insight api should link to #!/confirmed directly
|
||||
if (getParam('confirmed')) {
|
||||
var hashIndex = window.location.href.indexOf('/?');
|
||||
window.location = window.location.href.substr(0, hashIndex) + '#!/confirmed';
|
||||
return;
|
||||
}
|
||||
|
||||
if ($rootScope.fromEmailConfirmation) {
|
||||
$scope.confirmedEmail = true;
|
||||
$rootScope.fromEmailConfirmation = false;
|
||||
}
|
||||
|
||||
if ($rootScope.iden) {
|
||||
identityService.goWalletHome();
|
||||
}
|
||||
|
||||
Compatibility.check($scope);
|
||||
pinService.check(function(err, value) {
|
||||
$rootScope.hasPin = value;
|
||||
});
|
||||
};
|
||||
|
||||
pinService.makePinInput($scope, 'pin', function(newValue) {
|
||||
$scope.openWithPin(newValue);
|
||||
});
|
||||
|
||||
pinService.makePinInput($scope, 'newpin', function(newValue) {
|
||||
_firstpin = newValue;
|
||||
$scope.askForPin = 2;
|
||||
});
|
||||
|
||||
pinService.makePinInput($scope, 'repeatpin', function(newValue) {
|
||||
if (newValue === _firstpin) {
|
||||
_firstpin = null;
|
||||
$scope.createPin(newValue);
|
||||
} else {
|
||||
$scope.$$childTail.setPinForm.newpin.$setViewValue('');
|
||||
$scope.$$childTail.setPinForm.newpin.$render();
|
||||
$scope.$$childTail.setPinForm.repeatpin.$setViewValue('');
|
||||
$scope.$$childTail.setPinForm.repeatpin.$render();
|
||||
|
||||
_firstpin = null;
|
||||
$scope.askForPin = 1;
|
||||
$scope.error = 'Entered PINs were not equal. Try again';
|
||||
}
|
||||
});
|
||||
|
||||
$scope.done = function() {
|
||||
$rootScope.starting = false;
|
||||
|
|
@ -22,28 +59,87 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
|||
};
|
||||
|
||||
|
||||
$scope.$on("$destroy", function(){
|
||||
$scope.$on("$destroy", function() {
|
||||
var iden = $rootScope.iden;
|
||||
if (iden) {
|
||||
iden.removeListener('newWallet', $scope.done );
|
||||
iden.removeListener('noWallets', $scope.done );
|
||||
iden.removeListener('newWallet', $scope.done);
|
||||
iden.removeListener('noWallets', $scope.done);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
$scope.openProfile = function(form) {
|
||||
$scope.confirmedEmail = false;
|
||||
$scope.openWithPin = function(pin) {
|
||||
|
||||
if (!pin) {
|
||||
$scope.error = 'Please enter the required fields';
|
||||
return;
|
||||
}
|
||||
|
||||
var credentials = pinService.get(pin, function(err, credentials) {
|
||||
if (err || !credentials) {
|
||||
$scope.error = 'Wrong PIN';
|
||||
return;
|
||||
}
|
||||
$rootScope.starting = true;
|
||||
$scope.open(credentials.email, credentials.password);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
$scope.openWallets = function() {
|
||||
preconditions.checkState($rootScope.iden);
|
||||
var iden = $rootScope.iden;
|
||||
|
||||
$rootScope.hideNavigation = false;
|
||||
$rootScope.starting = true;
|
||||
iden.on('newWallet', $scope.done);
|
||||
iden.on('noWallets', $scope.done);
|
||||
iden.openWallets();
|
||||
};
|
||||
|
||||
$scope.createPin = function(pin) {
|
||||
preconditions.checkArgument(pin);
|
||||
preconditions.checkState($rootScope.iden);
|
||||
preconditions.checkState(_credentials && _credentials.email);
|
||||
|
||||
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
|
||||
_credentials.password = '';
|
||||
_credentials = null;
|
||||
$scope.askForPin = 0;
|
||||
$rootScope.hasPin = true;
|
||||
$scope.openWallets();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.openWithCredentials = function(form) {
|
||||
if (form && form.$invalid) {
|
||||
$scope.error = 'Please enter the required fields';
|
||||
return;
|
||||
}
|
||||
|
||||
$scope.open(form.email.$modelValue, form.password.$modelValue);
|
||||
};
|
||||
|
||||
|
||||
$scope.pinLogout = function() {
|
||||
pinService.clear(function() {
|
||||
copay.logger.debug('PIN erased');
|
||||
delete $rootScope['hasPin'];
|
||||
applicationService.reload();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.open = function(email, password) {
|
||||
$rootScope.starting = true;
|
||||
identityService.open(form.email.$modelValue, form.password.$modelValue, function(err, iden) {
|
||||
if (err) {
|
||||
identityService.open(email, password, function(err, iden) {
|
||||
if (err) {
|
||||
$rootScope.starting = false;
|
||||
copay.logger.warn(err);
|
||||
if ((err.toString() || '').match('PNOTFOUND')) {
|
||||
$scope.error = 'Invalid email or password';
|
||||
pinService.clear(function() {
|
||||
copay.logger.debug('PIN erased');
|
||||
});
|
||||
} else if ((err.toString() || '').match('Connection')) {
|
||||
$scope.error = 'Could not connect to Insight Server';
|
||||
} else if ((err.toString() || '').match('Unable')) {
|
||||
|
|
@ -51,13 +147,33 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
|
|||
} else {
|
||||
$scope.error = 'Unknown error';
|
||||
}
|
||||
return $scope.done();
|
||||
$rootScope.starting = false;
|
||||
$rootScope.$digest();
|
||||
return;
|
||||
}
|
||||
|
||||
// Open successfully?
|
||||
if (iden) {
|
||||
iden.on('newWallet', $scope.done);
|
||||
iden.on('noWallets', $scope.done);
|
||||
iden.openWallets();
|
||||
$scope.error = null;
|
||||
$scope.confirmedEmail = false;
|
||||
|
||||
// mobile
|
||||
if (isMobile.any() && !$rootScope.hasPin) {
|
||||
$scope.done();
|
||||
_credentials = {
|
||||
email: email,
|
||||
password: password,
|
||||
};
|
||||
$scope.askForPin = 1;
|
||||
$rootScope.starting = false;
|
||||
$rootScope.hideNavigation = true;
|
||||
$rootScope.$digest();
|
||||
return;
|
||||
}
|
||||
// no mobile
|
||||
else {
|
||||
$scope.openWallets();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,13 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
|
|||
'link': 'more'
|
||||
}];
|
||||
|
||||
$scope.go = function (path) {
|
||||
$location.path(path);
|
||||
};
|
||||
|
||||
$scope.signout = function() {
|
||||
$scope.$emit('signout');
|
||||
$rootScope.signingOut = true;
|
||||
identityService.signout();
|
||||
};
|
||||
|
||||
$scope.isActive = function(item) {
|
||||
|
|
@ -47,7 +52,7 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
|
|||
|
||||
$scope.init = function() {
|
||||
// This should be called only once.
|
||||
|
||||
|
||||
// focused wallet change
|
||||
if ($rootScope.wallet) {
|
||||
$rootScope.$watch('wallet', function() {
|
||||
|
|
@ -72,16 +77,14 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
|
|||
if (newWid && $rootScope.iden.getWalletById(newWid)) {
|
||||
identityService.setFocusedWallet(newWid);
|
||||
} else {
|
||||
copay.logger.debug('No wallets');
|
||||
copay.logger.debug('No wallets');
|
||||
identityService.noFocusedWallet(newWid);
|
||||
}
|
||||
}
|
||||
$scope.walletSelection = false;
|
||||
$scope.setWallets();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$scope.setWallets = function() {
|
||||
|
|
|
|||
|
|
@ -286,10 +286,20 @@ angular.module('copayApp.directives')
|
|||
link: function(_scope, _element) {
|
||||
$timeout(function() {
|
||||
_element[0].focus();
|
||||
}, 0);
|
||||
});
|
||||
}
|
||||
};
|
||||
})
|
||||
.directive('showFocus', function($timeout) {
|
||||
return function(scope, element, attrs) {
|
||||
scope.$watch(attrs.showFocus,
|
||||
function (newValue) {
|
||||
$timeout(function() {
|
||||
newValue && element[0].focus();
|
||||
});
|
||||
},true);
|
||||
};
|
||||
})
|
||||
.directive('match', function() {
|
||||
return {
|
||||
require: 'ngModel',
|
||||
|
|
|
|||
|
|
@ -355,17 +355,17 @@ Identity.prototype.remove = function(opts, cb) {
|
|||
};
|
||||
|
||||
Identity.prototype._cleanUp = function() {
|
||||
// NOP
|
||||
_.each(this.wallets, function(w){
|
||||
w.close();
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @desc Closes the wallet and disconnects all services
|
||||
*/
|
||||
Identity.prototype.close = function() {
|
||||
var self = this;
|
||||
self.store({}, function(err) {
|
||||
self.emitAndKeepAlive('closed');
|
||||
});
|
||||
this._cleanUp();
|
||||
this.emitAndKeepAlive('closed');
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,11 @@ angular.module('copayApp.services')
|
|||
.factory('identityService', function($rootScope, $location, $timeout, $filter, pluginManager, notification, pendingTxsService, balanceService, applicationService) {
|
||||
notification.enableHtml5Mode(); // for chrome: if support, enable it
|
||||
|
||||
// TODO:
|
||||
// * remove iden from rootScope
|
||||
// * remove wallet from rootScope
|
||||
// * create walletService
|
||||
|
||||
var root = {};
|
||||
root.check = function(scope) {
|
||||
copay.Identity.checkIfExistsAny({
|
||||
|
|
@ -22,6 +27,7 @@ angular.module('copayApp.services')
|
|||
});
|
||||
};
|
||||
|
||||
// TODO should be on 'walletService'
|
||||
root.goWalletHome = function() {
|
||||
var w = $rootScope.wallet;
|
||||
if (w) {
|
||||
|
|
@ -53,19 +59,24 @@ angular.module('copayApp.services')
|
|||
preconditions.checkState(iden);
|
||||
root.bind(iden);
|
||||
|
||||
var walletOptions = {
|
||||
nickname: iden.fullName,
|
||||
networkName: config.networkName,
|
||||
requiredCopayers: 1,
|
||||
totalCopayers: 1,
|
||||
password: iden.password,
|
||||
name: 'My wallet',
|
||||
};
|
||||
iden.createWallet(walletOptions, function(err, wallet) {
|
||||
return cb(err);
|
||||
});
|
||||
return cb(null);
|
||||
});
|
||||
};
|
||||
|
||||
root.createDefaultWallet = function(cb) {
|
||||
var iden = $rootScope.iden;
|
||||
|
||||
var walletOptions = {
|
||||
nickname: iden.fullName,
|
||||
networkName: config.networkName,
|
||||
requiredCopayers: 1,
|
||||
totalCopayers: 1,
|
||||
password: iden.password,
|
||||
name: 'My wallet',
|
||||
};
|
||||
iden.createWallet(walletOptions, function(err, wallet) {
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
||||
root.setServerStatus = function(headers) {
|
||||
|
|
|
|||
71
js/services/pinService.js
Normal file
71
js/services/pinService.js
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services')
|
||||
.factory('pinService', function($rootScope, localstorageService) {
|
||||
|
||||
var KEY = 'pinDATA';
|
||||
var SALT = '4gllotIKguqi0EkIslC0';
|
||||
var ITER = 2000;
|
||||
|
||||
var ls = localstorageService;
|
||||
var root = {};
|
||||
|
||||
root.check = function(cb) {
|
||||
ls.getItem(KEY, function(err, value) {
|
||||
return cb(err, value ? true : false);
|
||||
});
|
||||
};
|
||||
|
||||
root.get = function(pin, cb) {
|
||||
ls.getItem(KEY, function(err, value) {
|
||||
if (!value) return cb(null);
|
||||
var enc = value;
|
||||
var data = copay.crypto.decrypt('' + parseInt(pin), enc);
|
||||
var err = new Error('Could not decrypt');
|
||||
if (data) {
|
||||
var obj;
|
||||
try {
|
||||
obj = JSON.parse(data);
|
||||
err = null;
|
||||
} catch (e) {};
|
||||
}
|
||||
return cb(err, obj);
|
||||
});
|
||||
};
|
||||
|
||||
root.save = function(pin, email, password, cb) {
|
||||
var credentials = {
|
||||
email: email,
|
||||
password: password,
|
||||
};
|
||||
var enc = copay.crypto.encrypt('' + parseInt(pin), credentials, SALT, ITER);
|
||||
ls.setItem(KEY, enc, function(err) {
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
||||
root.clear = function(cb) {
|
||||
ls.removeItem(KEY, cb);
|
||||
};
|
||||
|
||||
|
||||
root.makePinInput = function(scope, name, cb) {
|
||||
Object.defineProperty(scope, name, {
|
||||
get: function() {
|
||||
return this['_' + name];
|
||||
},
|
||||
set: function(newValue) {
|
||||
this['_' + name] = newValue;
|
||||
scope.error = null;
|
||||
if (newValue && newValue.length == 4) {
|
||||
return cb(newValue);
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
@ -59,22 +59,22 @@ module.exports = {
|
|||
/**
|
||||
* Encrypts symmetrically using a passphrase
|
||||
*/
|
||||
encrypt: function(key, message) {
|
||||
encrypt: function(key, message, salt, iter) {
|
||||
if (!_.isString(message)) {
|
||||
message = JSON.stringify(message);
|
||||
}
|
||||
sjcl.json.defaults.salt = defaultSalt;
|
||||
sjcl.json.defaults.iter = defaultIterations;
|
||||
sjcl.json.defaults.salt = salt || defaultSalt;
|
||||
sjcl.json.defaults.iter = iter || defaultIterations;
|
||||
return sjcl.encrypt(key, message);
|
||||
},
|
||||
|
||||
/**
|
||||
* Decrypts symmetrically using a passphrase
|
||||
*/
|
||||
decrypt: function(key, cyphertext) {
|
||||
decrypt: function(key, sjclEncryptedJson) {
|
||||
var output = {};
|
||||
try {
|
||||
return sjcl.decrypt(key, cyphertext);
|
||||
return sjcl.decrypt(key, sjclEncryptedJson);
|
||||
} catch (e) {
|
||||
log.info('Decryption failed due to error: ' + e.message);
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue