Merge pull request #4276 from matiu/feat/derivation-test-cache

Feat/derivation test cache
This commit is contained in:
Matias Alejo Garcia 2016-06-03 12:48:52 -03:00
commit bab8a26e1e
6 changed files with 43 additions and 72 deletions

View file

@ -6,31 +6,9 @@ bwcModule.constant('MODULE_VERSION', '1.0.0');
bwcModule.provider("bwcService", function() { bwcModule.provider("bwcService", function() {
var provider = {}; var provider = {};
var config = {
baseUrl: 'https://bws.bitpay.com/bws/api',
verbose: null,
transports: null
};
provider.setBaseUrl = function(url) {
config.baseUrl = url;
};
provider.setVerbose = function(v) {
config.verbose = v ? true : false;
};
provider.$get = function() { provider.$get = function() {
var service = {}; var service = {};
service.setBaseUrl = function(url) {
config.baseUrl = url;
};
service.setTransports = function(transports) {
config.transports = transports;
};
service.getBitcore = function() { service.getBitcore = function() {
return Client.Bitcore; return Client.Bitcore;
}; };
@ -46,20 +24,21 @@ bwcModule.provider("bwcService", function() {
service.buildTx = Client.buildTx; service.buildTx = Client.buildTx;
service.parseSecret = Client.parseSecret; service.parseSecret = Client.parseSecret;
service.Client = Client; service.Client = Client;
service.config = config;
service.getUtils = function() { service.getUtils = function() {
return Client.Utils; return Client.Utils;
}; };
service.getClient = function(walletData) { service.getClient = function(walletData, opts) {
//note opts use `baseurl` all lowercase;
var bwc = new Client({ var bwc = new Client({
baseUrl: config.baseUrl, baseUrl: opts.baseurl || 'https://bws.bitpay.com/bws/api',
verbose: config.verbose, verbose: opts.verbose,
transports: config.transports transports: ['polling'],
}); });
if (walletData) if (walletData)
bwc.import(walletData); bwc.import(walletData, opts);
return bwc; return bwc;
}; };
return service; return service;

View file

@ -42,7 +42,7 @@
"url": "https://github.com/bitpay/copay/issues" "url": "https://github.com/bitpay/copay/issues"
}, },
"dependencies": { "dependencies": {
"bitcore-wallet-client": "https://github.com/bitpay/bitcore-wallet-client#baf7b8e50f53d3a62d0f5804919f0f0218872daf", "bitcore-wallet-client": "2.5.0",
"express": "^4.11.2", "express": "^4.11.2",
"fs": "0.0.2", "fs": "0.0.2",
"grunt": "^0.4.5", "grunt": "^0.4.5",

View file

@ -16,6 +16,7 @@ Profile.create = function(opts) {
x.createdOn = Date.now(); x.createdOn = Date.now();
x.credentials = opts.credentials || []; x.credentials = opts.credentials || [];
x.disclaimerAccepted = false; x.disclaimerAccepted = false;
x.checked = {};
return x; return x;
}; };
@ -25,6 +26,7 @@ Profile.fromObj = function(obj) {
x.createdOn = obj.createdOn; x.createdOn = obj.createdOn;
x.credentials = obj.credentials; x.credentials = obj.credentials;
x.disclaimerAccepted = obj.disclaimerAccepted; x.disclaimerAccepted = obj.disclaimerAccepted;
x.checked = obj.checked || {};
if (x.credentials[0] && typeof x.credentials[0] != 'object') if (x.credentials[0] && typeof x.credentials[0] != 'object')
throw ("credentials should be an object"); throw ("credentials should be an object");

View file

@ -62,29 +62,34 @@ angular.module('copayApp.services')
}); });
}; };
root.setBaseURL = function(walletId) {
var config = configService.getSync();
var defaults = configService.getDefaults();
bwcService.setBaseUrl((config.bwsFor && config.bwsFor[walletId]) || defaults.bws.url);
bwcService.setTransports(['polling']);
}
root.setWalletClient = function(credentials) { root.setWalletClient = function(credentials) {
if (root.walletClients[credentials.walletId] && if (root.walletClients[credentials.walletId] &&
root.walletClients[credentials.walletId].started) { root.walletClients[credentials.walletId].started) {
return; return;
} }
root.setBaseURL(credentials.walletId);
var getBaseURL = function(walletId) {
var config = configService.getSync();
var defaults = configService.getDefaults();
return ((config.bwsFor && config.bwsFor[walletId]) || defaults.bws.url);
};
$log.debug('Importing wallet:' + credentials.walletId); $log.debug('Importing wallet:' + credentials.walletId);
var client = bwcService.getClient(JSON.stringify(credentials)); var skipKeyValidation = root.profile.checked[credentials.walletId] == platformInfo.ua;
var client = bwcService.getClient(JSON.stringify(credentials), {
baseurl: getBaseURL(credentials.walletId),
skipKeyValidation: skipKeyValidation,
});
root.walletClients[credentials.walletId] = client; root.walletClients[credentials.walletId] = client;
if (client.incorrectDerivation) { if (client.incorrectDerivation) {
$log.warn('Key Derivation failed for wallet:' + credentials.walletId); $log.warn('Key Derivation failed for wallet:' + credentials.walletId);
storageService.clearLastAddress(credentials.walletId, function() {}); storageService.clearLastAddress(credentials.walletId, function() {});
} else if (!skipKeyValidation) {
root.profile.checked[credentials.walletId] = platformInfo.ua;
storageService.storeProfileThrottled(root.profile, function() {});
} }
client.removeAllListeners(); client.removeAllListeners();
@ -219,10 +224,7 @@ angular.module('copayApp.services')
root._seedWallet = function(opts, cb) { root._seedWallet = function(opts, cb) {
opts = opts || {}; opts = opts || {};
if (opts.bwsurl) var walletClient = bwcService.getClient(null, opts);
bwcService.setBaseUrl(opts.bwsurl);
var walletClient = bwcService.getClient();
var network = opts.networkName || 'livenet'; var network = opts.networkName || 'livenet';
@ -331,8 +333,8 @@ angular.module('copayApp.services')
// check if exist // check if exist
if (lodash.find(root.profile.credentials, { if (lodash.find(root.profile.credentials, {
'walletId': walletData.walletId 'walletId': walletData.walletId
})) { })) {
return cb(gettext('Cannot join the same wallet more that once')); return cb(gettext('Cannot join the same wallet more that once'));
} }
} catch (ex) { } catch (ex) {
@ -471,10 +473,8 @@ angular.module('copayApp.services')
}; };
root.importWallet = function(str, opts, cb) { root.importWallet = function(str, opts, cb) {
if (opts.bwsurl)
bwcService.setBaseUrl(opts.bwsurl);
var walletClient = bwcService.getClient(); var walletClient = bwcService.getClient(null, opts);
$log.debug('Importing Wallet:', opts); $log.debug('Importing Wallet:', opts);
try { try {
@ -501,10 +501,7 @@ angular.module('copayApp.services')
}; };
root.importExtendedPrivateKey = function(xPrivKey, opts, cb) { root.importExtendedPrivateKey = function(xPrivKey, opts, cb) {
if (opts.bwsurl) var walletClient = bwcService.getClient(null, opts);
bwcService.setBaseUrl(opts.bwsurl);
var walletClient = bwcService.getClient();
$log.debug('Importing Wallet xPrivKey'); $log.debug('Importing Wallet xPrivKey');
walletClient.importFromExtendedPrivateKey(xPrivKey, opts, function(err) { walletClient.importFromExtendedPrivateKey(xPrivKey, opts, function(err) {
@ -523,10 +520,7 @@ angular.module('copayApp.services')
}; };
root.importMnemonic = function(words, opts, cb) { root.importMnemonic = function(words, opts, cb) {
if (opts.bwsurl) var walletClient = bwcService.getClient(null, opts);
bwcService.setBaseUrl(opts.bwsurl);
var walletClient = bwcService.getClient();
$log.debug('Importing Wallet Mnemonic'); $log.debug('Importing Wallet Mnemonic');
@ -544,10 +538,7 @@ angular.module('copayApp.services')
}; };
root.importExtendedPublicKey = function(opts, cb) { root.importExtendedPublicKey = function(opts, cb) {
if (opts.bwsurl) var walletClient = bwcService.getClient(null, opts);
bwcService.setBaseUrl(opts.bwsurl);
var walletClient = bwcService.getClient();
$log.debug('Importing Wallet XPubKey'); $log.debug('Importing Wallet XPubKey');
walletClient.importFromExtendedPublicKey(opts.extendedPublicKey, opts.externalSource, opts.entropySource, { walletClient.importFromExtendedPublicKey(opts.extendedPublicKey, opts.externalSource, opts.entropySource, {
@ -572,8 +563,6 @@ angular.module('copayApp.services')
var defaults = configService.getDefaults(); var defaults = configService.getDefaults();
configService.get(function(err) { configService.get(function(err) {
bwcService.setBaseUrl(defaults.bws.url);
bwcService.setTransports(['polling']);
root._createNewProfile(opts, function(err, p) { root._createNewProfile(opts, function(err, p) {
if (err) return cb(err); if (err) return cb(err);
@ -589,7 +578,7 @@ angular.module('copayApp.services')
root.setDisclaimerAccepted = function(cb) { root.setDisclaimerAccepted = function(cb) {
root.profile.disclaimerAccepted = true; root.profile.disclaimerAccepted = true;
storageService.storeProfile(root.profile, function(err) { storageService.storeProfileThrottled(root.profile, function(err) {
return cb(err); return cb(err);
}); });
}; };
@ -641,7 +630,7 @@ angular.module('copayApp.services')
newCredentials.push(JSON.parse(fc.export())); newCredentials.push(JSON.parse(fc.export()));
root.profile.credentials = newCredentials; root.profile.credentials = newCredentials;
storageService.storeProfile(root.profile, cb); storageService.storeProfileThrottled(root.profile, cb);
}; };

View file

@ -118,6 +118,8 @@ angular.module('copayApp.services')
}); });
}; };
root.storeProfileThrottled = lodash.throttle(root.storeProfile, 5000);
root.getProfile = function(cb) { root.getProfile = function(cb) {
storage.get('profile', function(err, str) { storage.get('profile', function(err, str) {
if (err || !str) if (err || !str)

View file

@ -85,21 +85,20 @@ mocks.init = function(fixtures, controllerName, opts, done) {
module('bwcModule', function($provide) { module('bwcModule', function($provide) {
$provide.decorator('bwcService', function($delegate, lodash) { $provide.decorator('bwcService', function($delegate, lodash) {
var getClient = $delegate.getClient; var getClient = $delegate.getClient;
var config = $delegate.config;
// Fix Encryption IVs // Fix Encryption IVs
var utils = $delegate.getUtils(); var utils = $delegate.getUtils();
utils.SJCL.iv = 'BZQVWAP6d1e4G8Fq1rQKbA=='; utils.SJCL.iv = 'BZQVWAP6d1e4G8Fq1rQKbA==';
$delegate.getClient = function(walletData) { $delegate.getClient = function(walletData, opts) {
var bwc = new $delegate.Client({ var bwc = new $delegate.Client();
baseUrl: config.baseUrl,
verbose: config.verbose,
transports: config.transports
});
if (walletData) if (walletData)
bwc.import(walletData); bwc.import(walletData, {
baseUrl: opts.baseurl || 'https://bws.bitpay.com/bws/api',
verbose: opts.verbose,
transports: ['polling'],
});
function createHash(method, url, args) { function createHash(method, url, args) {
var headers = JSON.stringify(bwc._getHeaders(method, url, args)); var headers = JSON.stringify(bwc._getHeaders(method, url, args));