From 282f549ed0c584ac6bae48f14ddc05f29a5a3b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Tue, 9 May 2017 12:19:08 -0300 Subject: [PATCH] adding to pin modal to resume --- src/js/controllers/modals/pin.js | 224 ++++++++++++++++++++++++++++++ src/js/routes.js | 1 + src/js/services/glideraService.js | 28 ++-- www/views/modals/pin.html | 69 +++++++++ 4 files changed, 310 insertions(+), 12 deletions(-) create mode 100644 src/js/controllers/modals/pin.js create mode 100644 www/views/modals/pin.html diff --git a/src/js/controllers/modals/pin.js b/src/js/controllers/modals/pin.js new file mode 100644 index 000000000..5d3f23a27 --- /dev/null +++ b/src/js/controllers/modals/pin.js @@ -0,0 +1,224 @@ +'use strict'; + +angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService, applicationService) { + var ATTEMPT_LIMIT = 3; + var ATTEMPT_LOCK_OUT_TIME = 5 * 60; + var currentPin; + + $scope.$on("$ionicView.beforeEnter", function(event) { + currentPin = $scope.confirmPin = ''; + $scope.action = $stateParams.action; + $scope.match = $scope.error = $scope.disableButtons = false; + $scope.currentAttempts = 0; + $scope.appName = appConfigService.name; + }); + + $scope.$on("$ionicView.enter", function(event) { + configService.whenAvailable(function(config) { + if (!config.lock) return; + $scope.bannedUntil = config.lock.bannedUntil || null; + if ($scope.bannedUntil) { + var now = Math.floor(Date.now() / 1000); + if (now < $scope.bannedUntil) { + $scope.error = $scope.disableButtons = true; + lockTimeControl($scope.bannedUntil); + } + } + }); + }); + + function getSavedMethod() { + var config = configService.getSync(); + if (config.lock) return config.lock.method; + return 'none'; + }; + + function checkAttempts() { + $scope.currentAttempts += 1; + $log.debug('Attempts to unlock:', $scope.currentAttempts); + if ($scope.currentAttempts === ATTEMPT_LIMIT) { + $scope.currentAttempts = 0; + var bannedUntil = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME; + saveFailedAttempt(bannedUntil); + } + }; + + function lockTimeControl(bannedUntil) { + setExpirationTime(); + + var countDown = $interval(function() { + setExpirationTime(); + }, 1000); + + function setExpirationTime() { + var now = Math.floor(Date.now() / 1000); + if (now > bannedUntil) { + if (countDown) reset(); + } else { + $scope.disableButtons = true; + var totalSecs = bannedUntil - now; + var m = Math.floor(totalSecs / 60); + var s = totalSecs % 60; + $scope.expires = ('0' + m).slice(-2) + ":" + ('0' + s).slice(-2); + } + }; + + function reset() { + $scope.expires = $scope.error = $scope.disableButtons = null; + currentPin = $scope.confirmPin = ''; + $interval.cancel(countDown); + $timeout(function() { + $scope.$apply(); + }); + return; + }; + }; + + $scope.getFilledClass = function(limit) { + return currentPin && currentPin.length >= limit ? 'filled-' + $scope.appName : null; + }; + + $scope.delete = function() { + if ($scope.disableButtons) return; + if (currentPin.length > 0) { + currentPin = currentPin.substring(0, currentPin.length - 1); + $scope.error = false; + $scope.updatePin(); + } + }; + + $scope.isComplete = function() { + if (currentPin.length < 4) return false; + else return true; + }; + + $scope.updatePin = function(value) { + if ($scope.disableButtons) return; + $scope.error = false; + if (value && !$scope.isComplete()) { + currentPin = currentPin + value; + $timeout(function() { + $scope.$apply(); + }); + } + $scope.save(); + }; + + function isMatch(pin) { + var config = configService.getSync(); + return config.lock.value == pin; + }; + + $scope.save = function() { + if (!$scope.isComplete()) return; + var savedMethod = getSavedMethod(); + + switch ($scope.action) { + case 'setup': + applyAndCheckPin(); + break; + case 'disable': + if (isMatch(currentPin)) { + deletePin(); + } else { + showError(); + checkAttempts(); + } + break; + case 'check': + if (isMatch(currentPin)) { + console.log("##################################### CHECKING"); + applicationService.successfullUnlocked = true; + $scope.pinModal.hide(); + return; + } + showError(); + checkAttempts(); + break; + } + }; + + function showError() { + $timeout(function() { + $scope.confirmPin = currentPin = ''; + $scope.error = true; + }, 200); + + $timeout(function() { + $scope.$apply(); + }); + }; + + function applyAndCheckPin() { + if (!$scope.confirmPin) { + $timeout(function() { + $scope.confirmPin = currentPin; + currentPin = ''; + }, 200); + } else { + if ($scope.confirmPin == currentPin) + savePin($scope.confirmPin); + else { + $scope.confirmPin = currentPin = ''; + $scope.error = true; + } + } + $timeout(function() { + $scope.$apply(); + }); + }; + + function deletePin() { + var opts = { + lock: { + method: 'none', + value: null, + bannedUntil: null, + } + }; + + configService.set(opts, function(err) { + if (err) $log.debug(err); + $scope.close(); + }); + }; + + function savePin(value) { + var opts = { + lock: { + method: 'pin', + value: value, + bannedUntil: null, + } + }; + + configService.set(opts, function(err) { + if (err) $log.debug(err); + $scope.close(); + }); + }; + + function saveFailedAttempt(bannedUntil) { + var opts = { + lock: { + bannedUntil: bannedUntil, + } + }; + + configService.set(opts, function(err) { + if (err) $log.debug(err); + lockTimeControl(bannedUntil); + }); + }; + + $scope.close = function(delay) { + $timeout(function() { + var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting'; + + if (shouldReturn) + $ionicHistory.goBack() + else + $state.go('tabs.home'); + }, delay || 1); + }; +}); diff --git a/src/js/routes.js b/src/js/routes.js index 70c32b5bb..437868ccb 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -1245,6 +1245,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr $ionicPlatform.on('resume', function() { applicationService.successfullUnlocked = false; + applicationService.pinModal(); // checkAndApplyLock(true); }); diff --git a/src/js/services/glideraService.js b/src/js/services/glideraService.js index f60230cdf..3c8f26dab 100644 --- a/src/js/services/glideraService.js +++ b/src/js/services/glideraService.js @@ -17,7 +17,7 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l * Development: 'testnet' * Production: 'livenet' */ - credentials.NETWORK = 'livenet'; + credentials.NETWORK = 'testnet'; //credentials.NETWORK = 'testnet'; if (credentials.NETWORK == 'testnet') { @@ -127,7 +127,7 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l }); }); }); - }); + }); }; var _get = function(endpoint, token) { @@ -365,7 +365,7 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l getPermissions(accessToken, credentials.NETWORK, true, function(err, permissions) { if (err) return cb(err); - + storageService.getGlideraStatus(credentials.NETWORK, function(err, status) { if (lodash.isString(status)) status = JSON.parse(status); storageService.getGlideraTxs(credentials.NETWORK, function(err, txs) { @@ -386,17 +386,21 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l root.updateStatus = function(data) { storageService.getGlideraToken(credentials.NETWORK, function(err, accessToken) { if (err) return; - + getPermissions(accessToken, credentials.NETWORK, false, function(err, permissions) { if (err) return; data.permissions = permissions; data.price = {}; - root.buyPrice(accessToken, {qty: 1}, function(err, buy) { + root.buyPrice(accessToken, { + qty: 1 + }, function(err, buy) { if (err) return; data.price['buy'] = buy.price; }); - root.sellPrice(accessToken, {qty: 1}, function(err, sell) { + root.sellPrice(accessToken, { + qty: 1 + }, function(err, sell) { if (err) return; data.price['sell'] = sell.price; }); @@ -405,12 +409,12 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l if (err) return; data.status = status; storageService.setGlideraStatus(credentials.NETWORK, JSON.stringify(status), function() {}); - }); - + }); + root.getLimits(accessToken, function(err, limits) { data.limits = limits; - }); - + }); + if (permissions.transaction_history) { root.getTransactions(accessToken, function(err, txs) { if (err) return; @@ -431,8 +435,8 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l data.personalInfo = info; }); } - }); - }); + }); + }); }; var register = function() { diff --git a/www/views/modals/pin.html b/www/views/modals/pin.html new file mode 100644 index 000000000..df0e82534 --- /dev/null +++ b/www/views/modals/pin.html @@ -0,0 +1,69 @@ + +
+
+
Please enter your PIN
+
Confirm your PIN
+
+
Incorrect PIN, try again.
+ +
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
1
+
+
+
2
+
+
+
3
+
+
+
+
+
4
+
+
+
5
+
+
+
6
+
+
+
+
+
7
+
+
+
8
+
+
+
9
+
+
+
+
+
+
0
+
+
+
+
+
+
+
+