copay/js/services/pinService.js

116 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-12-02 11:16:31 -08:00
'use strict';
angular.module('copayApp.services')
.factory('pinService', function($rootScope, $timeout, localstorageService) {
2014-12-02 17:38:33 -08:00
var KEY = 'pinDATA';
var SALT = '4gllotIKguqi0EkIslC0';
var ITER = 5000;
2014-12-02 17:38:33 -08:00
var ls = localstorageService;
2014-12-02 11:16:31 -08:00
var root = {};
2015-01-28 20:56:32 -08:00
var _firstpin;
2014-12-02 17:38:33 -08:00
root.check = function(cb) {
ls.getItem(KEY, function(err, value) {
return cb(err, value ? true : false);
});
2014-12-02 11:16:31 -08:00
};
2014-12-02 17:38:33 -08:00
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) {};
2014-12-02 11:16:31 -08:00
}
2014-12-02 17:38:33 -08:00
return cb(err, obj);
});
};
root.save = function(pin, email, password, cb) {
var credentials = {
email: email,
password: password,
2014-12-02 11:16:31 -08:00
};
2014-12-02 17:38:33 -08:00
var enc = copay.crypto.encrypt('' + parseInt(pin), credentials, SALT, ITER);
ls.setItem(KEY, enc, function(err) {
return cb(err);
});
2014-12-02 11:16:31 -08:00
};
2014-12-02 17:38:33 -08:00
root.clear = function(cb) {
ls.removeItem(KEY, cb);
2014-12-02 11:16:31 -08:00
};
2014-12-02 17:38:33 -08:00
2015-01-28 20:56:32 -08:00
root.clearPin = function(scope) {
scope.digits = [];
scope.defined = [];
};
2014-12-03 08:44:55 -08:00
2015-01-28 20:56:32 -08:00
root.pressPin = function(scope, digit, skipOpenWithPin) {
scope.error = null;
scope.digits.push(digit);
scope.defined.push(true);
if (scope.digits.length == 4) {
var pin = scope.digits.join('');
if (!$rootScope.hasPin) {
if (!_firstpin) {
_firstpin = pin;
2015-01-28 22:14:32 -08:00
scope.askForPin = 2;
2015-01-28 20:56:32 -08:00
$timeout(function() {
scope.clear();
}, 100);
return;
}
else {
if (pin === _firstpin) {
_firstpin = null;
2015-01-28 22:14:32 -08:00
scope.askForPin = null;
2015-01-28 20:56:32 -08:00
scope.createPin(pin);
2015-02-03 08:00:17 -08:00
return;
2015-01-28 20:56:32 -08:00
}
else {
_firstpin = null;
2015-01-28 22:14:32 -08:00
scope.askForPin = 1;
$timeout(function() {
2015-01-28 20:56:32 -08:00
scope.clear();
scope.error = 'Entered PINs were not equal. Try again';
$timeout(function() {
scope.error = null;
}, 2000);
}, 100);
return;
2014-12-03 08:44:55 -08:00
}
2015-01-28 20:56:32 -08:00
}
}
if (!skipOpenWithPin) {
scope.openWithPin(pin);
}
}
2014-12-03 08:44:55 -08:00
};
2015-01-28 20:56:32 -08:00
root.skipPin = function(scope, creatingProfile) {
if (!$rootScope.hasPin) {
if (!creatingProfile) {
scope.openWallets();
}
else {
scope.createDefaultWallet()
}
}
else {
scope.pinLogout();
}
};
2014-12-03 08:44:55 -08:00
2014-12-02 11:16:31 -08:00
return root;
});