From d08d4e365013b485cd82a6153c742e1248957a5a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 1 Jun 2016 19:56:47 -0300 Subject: [PATCH] implement cache for key derivation, using user-agent --- package.json | 2 +- src/js/models/profile.js | 2 ++ src/js/services/profileService.js | 13 ++++++++----- src/js/services/storageService.js | 11 +++-------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 57b20330b..3630ea293 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "url": "https://github.com/bitpay/copay/issues" }, "dependencies": { - "bitcore-wallet-client": "https://github.com/bitpay/bitcore-wallet-client#baf7b8e50f53d3a62d0f5804919f0f0218872daf", + "bitcore-wallet-client": "2.5.0", "express": "^4.11.2", "fs": "0.0.2", "grunt": "^0.4.5", diff --git a/src/js/models/profile.js b/src/js/models/profile.js index 4b57a0f66..4e808fdee 100644 --- a/src/js/models/profile.js +++ b/src/js/models/profile.js @@ -16,6 +16,7 @@ Profile.create = function(opts) { x.createdOn = Date.now(); x.credentials = opts.credentials || []; x.disclaimerAccepted = false; + x.checked = {}; return x; }; @@ -25,6 +26,7 @@ Profile.fromObj = function(obj) { x.createdOn = obj.createdOn; x.credentials = obj.credentials; x.disclaimerAccepted = obj.disclaimerAccepted; + x.checked = obj.checked || {}; if (x.credentials[0] && typeof x.credentials[0] != 'object') throw ("credentials should be an object"); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index a8f44ad7e..e3238db97 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -66,7 +66,7 @@ angular.module('copayApp.services') var config = configService.getSync(); var defaults = configService.getDefaults(); - bwcService.setBaseUrl((config.bwsFor && config.bwsFor[walletId]) || defaults.bws.url); + return ((config.bwsFor && config.bwsFor[walletId]) || defaults.bws.url); } root.setWalletClient = function(credentials) { @@ -75,17 +75,20 @@ angular.module('copayApp.services') return; } - - $log.debug('Importing wallet:' + credentials.walletId); + var skipKeyValidation = root.profile.checked[credentials.walletId] == platformInfo.ua; var client = bwcService.getClient(JSON.stringify(credentials), { baseurl: root._getBaseURL(credentials.walletId), + skipKeyValidation: skipKeyValidation, }); root.walletClients[credentials.walletId] = client; if (client.incorrectDerivation) { $log.warn('Key Derivation failed for wallet:' + credentials.walletId); storageService.clearLastAddress(credentials.walletId, function() {}); + } else if (!skipKeyValidation) { + root.profile.checked[credentials.walletId] = platformInfo.ua; + storageService.storeProfileThrottled(root.profile, function() {}); } client.removeAllListeners(); @@ -574,7 +577,7 @@ angular.module('copayApp.services') root.setDisclaimerAccepted = function(cb) { root.profile.disclaimerAccepted = true; - storageService.storeProfile(root.profile, function(err) { + storageService.storeProfileThrottled(root.profile, function(err) { return cb(err); }); }; @@ -626,7 +629,7 @@ angular.module('copayApp.services') newCredentials.push(JSON.parse(fc.export())); root.profile.credentials = newCredentials; - storageService.storeProfile(root.profile, cb); + storageService.storeProfileThrottled(root.profile, cb); }; diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index 877933351..4b9b91b33 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -113,11 +113,14 @@ angular.module('copayApp.services') }; root.storeProfile = function(profile, cb) { +console.log('[storageService.js.115:storeProfile:]',profile); //TODO encryptOnMobile(profile.toObj(), function(err, x) { storage.set('profile', x, cb); }); }; + root.storeProfileThrottled = lodash.throttle(root.storeProfile, 5000); + root.getProfile = function(cb) { storage.get('profile', function(err, str) { if (err || !str) @@ -173,14 +176,6 @@ angular.module('copayApp.services') storage.remove('backup-' + walletId, cb); }; - root.setDerivationTestFlag = function(walletId, ua, cb) { - storage.set('DerivationTest-'+walletId+ua.replace(' ', ''), true, cb); - }; - - root.getDerivationTestFlag = function(walletId, ua, cb) { - storage.get('DerivationTest-'+walletId+ua.replace(' ', ''), cb); - }; - root.setCleanAndScanAddresses = function(walletId, cb) { storage.set('CleanAndScanAddresses', walletId, cb); };