rename walletFactory to identity. Test passing

This commit is contained in:
Matias Alejo Garcia 2014-09-26 05:00:43 -03:00
commit eb9ab115d7
14 changed files with 652 additions and 115 deletions

View file

@ -13,7 +13,7 @@ var Async = module.exports.Async = require('./js/models/Async');
var Insight = module.exports.Insight = require('./js/models/Insight'); var Insight = module.exports.Insight = require('./js/models/Insight');
var Storage = module.exports.Storage = require('./js/models/Storage'); var Storage = module.exports.Storage = require('./js/models/Storage');
module.exports.WalletFactory = require('./js/models/WalletFactory'); module.exports.Identity = require('./js/models/Identity');
module.exports.Wallet = require('./js/models/Wallet'); module.exports.Wallet = require('./js/models/Wallet');
module.exports.WalletLock = require('./js/models/WalletLock'); module.exports.WalletLock = require('./js/models/WalletLock');
module.exports.PluginManager = require('./js/models/PluginManager'); module.exports.PluginManager = require('./js/models/PluginManager');

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('CopayersController', angular.module('copayApp.controllers').controller('CopayersController',
function($scope, $rootScope, $location, backupService, walletFactory, controllerUtils) { function($scope, $rootScope, $location, backupService, identity, controllerUtils) {
$scope.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; $scope.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
$scope.hideAdv = true; $scope.hideAdv = true;
@ -36,7 +36,7 @@ angular.module('copayApp.controllers').controller('CopayersController',
$scope.deleteWallet = function() { $scope.deleteWallet = function() {
var w = $rootScope.wallet; var w = $rootScope.wallet;
walletFactory.delete(w.id, function() { identity.delete(w.id, function() {
controllerUtils.logout(); controllerUtils.logout();
}); });
}; };

View file

@ -1,71 +0,0 @@
'use strict';
angular.module('copayApp.controllers').controller('CreateController',
function($scope, $rootScope, $location, $timeout, walletFactory, controllerUtils, Passphrase, backupService, notification, defaults) {
controllerUtils.redirIfLogged();
$rootScope.fromSetup = true;
$scope.loading = false;
$scope.walletPassword = $rootScope.walletPassword;
$scope.isMobile = !!window.cordova;
$scope.hideAdv = true;
$scope.networkName = config.networkName;
$scope.networkUrl = config.network[$scope.networkName].url;
// ng-repeat defined number of times instead of repeating over array?
$scope.getNumber = function(num) {
return new Array(num);
}
$scope.totalCopayers = config.wallet.totalCopayers;
$scope.TCValues = _.range(1, config.limits.totalCopayers + 1);
var updateRCSelect = function(n) {
var maxReq = copay.Wallet.getMaxRequiredCopayers(n);
$scope.RCValues = _.range(1, maxReq + 1);
$scope.requiredCopayers = Math.min(parseInt(n / 2 + 1), maxReq);
};
updateRCSelect($scope.totalCopayers);
$scope.$watch('totalCopayers', function(tc) {
updateRCSelect(tc);
});
$scope.$watch('networkName', function(tc) {
$scope.networkUrl = config.network[$scope.networkName].url;
});
$scope.showNetwork = function(){
return $scope.networkUrl != defaults.network.livenet.url && $scope.networkUrl != defaults.network.testnet.url;
};
$scope.create = function(form) {
if (form && form.$invalid) {
notification.error('Error', 'Please enter the required fields');
return;
}
$scope.loading = true;
Passphrase.getBase64Async($scope.walletPassword, function(passphrase) {
var opts = {
requiredCopayers: $scope.requiredCopayers,
totalCopayers: $scope.totalCopayers,
name: $scope.walletName,
nickname: $scope.myNickname,
passphrase: passphrase,
privateKeyHex: $scope.private,
networkName: $scope.networkName,
};
walletFactory.create(opts, function(err, w) {
controllerUtils.startNetwork(w, $scope);
});
});
};
$scope.isSetupWalletPage = 0;
$scope.setupWallet = function() {
$scope.isSetupWalletPage = !$scope.isSetupWalletPage;
};
});

View file

@ -1,11 +1,11 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, walletFactory, notification, controllerUtils) { angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, identity, notification, controllerUtils) {
controllerUtils.redirIfLogged(); controllerUtils.redirIfLogged();
$scope.retreiving = true; $scope.retreiving = true;
walletFactory.getWallets(function(err,ret) { identity.getWallets(function(err,ret) {
$scope.retreiving = false; $scope.retreiving = false;
$scope.hasWallets = (ret && ret.length > 0) ? true : false; $scope.hasWallets = (ret && ret.length > 0) ? true : false;
}); });

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('ImportController', angular.module('copayApp.controllers').controller('ImportController',
function($scope, $rootScope, $location, walletFactory, controllerUtils, Passphrase, notification, isMobile) { function($scope, $rootScope, $location, identity, controllerUtils, Passphrase, notification, isMobile) {
controllerUtils.redirIfLogged(); controllerUtils.redirIfLogged();
$scope.title = 'Import a backup'; $scope.title = 'Import a backup';
@ -30,7 +30,7 @@ angular.module('copayApp.controllers').controller('ImportController',
// try to import encrypted wallet with passphrase // try to import encrypted wallet with passphrase
try { try {
w = walletFactory.import(encryptedObj, passphrase, skipFields); w = identity.import(encryptedObj, passphrase, skipFields);
} catch (e) { } catch (e) {
errMsg = e.message; errMsg = e.message;
} }

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('JoinController', angular.module('copayApp.controllers').controller('JoinController',
function($scope, $rootScope, $timeout, walletFactory, controllerUtils, Passphrase, notification) { function($scope, $rootScope, $timeout, identity, controllerUtils, Passphrase, notification) {
controllerUtils.redirIfLogged(); controllerUtils.redirIfLogged();
$rootScope.fromSetup = false; $rootScope.fromSetup = false;
$scope.loading = false; $scope.loading = false;
@ -121,12 +121,13 @@ angular.module('copayApp.controllers').controller('JoinController',
$scope.loading = true; $scope.loading = true;
Passphrase.getBase64Async($scope.joinPassword, function(passphrase) { Passphrase.getBase64Async($scope.joinPassword, function(passphrase) {
walletFactory.joinCreateSession({ identity.joinCreateSession({
secret: $scope.connectionId, secret: $scope.connectionId,
nickname: $scope.nickname, nickname: $scope.nickname,
passphrase: passphrase, passphrase: passphrase,
privateHex: $scope.private, privateHex: $scope.private,
}, function(err, w) { }, function(err, w) {
$scope.loading = false; $scope.loading = false;
if (err || !w) { if (err || !w) {
if (err === 'joinError') if (err === 'joinError')
@ -137,8 +138,6 @@ angular.module('copayApp.controllers').controller('JoinController',
notification.error('Network Error', 'Wallet network configuration missmatch'); notification.error('Network Error', 'Wallet network configuration missmatch');
else if (err === 'badSecret') else if (err === 'badSecret')
notification.error('Bad secret', 'The secret string you entered is invalid'); notification.error('Bad secret', 'The secret string you entered is invalid');
else if (err === 'connectionError')
notification.error('Networking Error', 'Could not connect to the Insight server. Check your settings and network configuration');
else else
notification.error('Unknown error'); notification.error('Unknown error');
controllerUtils.onErrorDigest(); controllerUtils.onErrorDigest();

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('MoreController', angular.module('copayApp.controllers').controller('MoreController',
function($scope, $rootScope, $location, $filter, backupService, walletFactory, controllerUtils, notification, rateService) { function($scope, $rootScope, $location, $filter, backupService, identity, controllerUtils, notification, rateService) {
var w = $rootScope.wallet; var w = $rootScope.wallet;
$scope.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; $scope.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
@ -81,7 +81,7 @@ angular.module('copayApp.controllers').controller('MoreController',
}; };
$scope.deleteWallet = function() { $scope.deleteWallet = function() {
walletFactory.delete(w.id, function() { identity.delete(w.id, function() {
controllerUtils.logout(); controllerUtils.logout();
}); });
}; };

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('OpenController', function($scope, $rootScope, $location, walletFactory, controllerUtils, Passphrase, notification) { angular.module('copayApp.controllers').controller('OpenController', function($scope, $rootScope, $location, identity, controllerUtils, Passphrase, notification) {
controllerUtils.redirIfLogged(); controllerUtils.redirIfLogged();
if ($rootScope.pendingPayment) { if ($rootScope.pendingPayment) {
@ -16,7 +16,7 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
$scope.loading = false; $scope.loading = false;
$scope.retreiving = true; $scope.retreiving = true;
walletFactory.getWallets(function(err, wallets) { identity.getWallets(function(err, wallets) {
if (err || !wallets || !wallets.length) { if (err || !wallets || !wallets.length) {
$location.path('/'); $location.path('/');
@ -48,7 +48,7 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
Passphrase.getBase64Async(password, function(passphrase) { Passphrase.getBase64Async(password, function(passphrase) {
var w, errMsg; var w, errMsg;
walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) { identity.open($scope.selectedWalletId, passphrase, function(err, w) {
if (!w) { if (!w) {
$scope.loading = false; $scope.loading = false;
notification.error('Error', err.errMsg || 'Wrong password'); notification.error('Error', err.errMsg || 'Wrong password');

View file

@ -15,7 +15,7 @@ var Storage = module.exports.Storage = require('./Storage');
/** /**
* @desc * @desc
* WalletFactory - stores the state for a wallet in creation * Identity - stores the state for a wallet in creation
* *
* @param {Object} config - configuration for this wallet * @param {Object} config - configuration for this wallet
* *
@ -35,7 +35,7 @@ var Storage = module.exports.Storage = require('./Storage');
* @constructor * @constructor
*/ */
function WalletFactory(config, version, pluginManager) { function Identity(config, version, pluginManager) {
var self = this; var self = this;
preconditions.checkArgument(config); preconditions.checkArgument(config);
preconditions.checkArgument(config.network); preconditions.checkArgument(config.network);
@ -73,7 +73,7 @@ function WalletFactory(config, version, pluginManager) {
* @param {Object} wallet object * @param {Object} wallet object
* @return {string} network name * @return {string} network name
*/ */
WalletFactory.prototype.obtainNetworkName = function(obj) { Identity.prototype.obtainNetworkName = function(obj) {
return obj.networkName || return obj.networkName ||
obj.opts.networkName || obj.opts.networkName ||
obj.publicKeyRing.networkName || obj.publicKeyRing.networkName ||
@ -86,7 +86,7 @@ WalletFactory.prototype.obtainNetworkName = function(obj) {
* @param {string[]} skipFields - fields to skip when importing * @param {string[]} skipFields - fields to skip when importing
* @return {Wallet} * @return {Wallet}
*/ */
WalletFactory.prototype.fromObj = function(inObj, skipFields) { Identity.prototype.fromObj = function(inObj, skipFields) {
var networkName = this.obtainNetworkName(inObj); var networkName = this.obtainNetworkName(inObj);
preconditions.checkState(networkName); preconditions.checkState(networkName);
preconditions.checkArgument(inObj); preconditions.checkArgument(inObj);
@ -118,7 +118,7 @@ WalletFactory.prototype.fromObj = function(inObj, skipFields) {
* @param {string[]} skipFields - fields to ignore when importing * @param {string[]} skipFields - fields to ignore when importing
* @return {Wallet} * @return {Wallet}
*/ */
WalletFactory.prototype.fromEncryptedObj = function(base64, passphrase, skipFields) { Identity.prototype.fromEncryptedObj = function(base64, passphrase, skipFields) {
this.storage.setPassphrase(passphrase); this.storage.setPassphrase(passphrase);
var walletObj = this.storage.import(base64); var walletObj = this.storage.import(base64);
if (!walletObj) return false; if (!walletObj) return false;
@ -127,19 +127,19 @@ WalletFactory.prototype.fromEncryptedObj = function(base64, passphrase, skipFiel
/** /**
* @TODO: import is a reserved keyword! DONT USE IT * @TODO: import is a reserved keyword! DONT USE IT
* @TODO: this is essentialy the same method as {@link WalletFactory#fromEncryptedObj}! * @TODO: this is essentialy the same method as {@link Identity#fromEncryptedObj}!
* @desc Imports a wallet from an encrypted base64 object * @desc Imports a wallet from an encrypted base64 object
* @param {string} base64 - the base64 encoded object * @param {string} base64 - the base64 encoded object
* @param {string} passphrase - passphrase to decrypt it * @param {string} passphrase - passphrase to decrypt it
* @param {string[]} skipFields - fields to ignore when importing * @param {string[]} skipFields - fields to ignore when importing
* @return {Wallet} * @return {Wallet}
*/ */
WalletFactory.prototype.import = function(base64, passphrase, skipFields) { Identity.prototype.import = function(base64, passphrase, skipFields) {
var self = this; var self = this;
return self.fromEncryptedObj(base64, passphrase, skipFields); return self.fromEncryptedObj(base64, passphrase, skipFields);
}; };
WalletFactory.prototype.migrateWallet = function(walletId, passphrase, cb) { Identity.prototype.migrateWallet = function(walletId, passphrase, cb) {
var self = this; var self = this;
self.storage.setPassphrase(passphrase); self.storage.setPassphrase(passphrase);
@ -168,7 +168,7 @@ WalletFactory.prototype.migrateWallet = function(walletId, passphrase, cb) {
* @param {string[]} skipFields - parameters to ignore when importing * @param {string[]} skipFields - parameters to ignore when importing
* @param {function} callback - {err, Wallet} * @param {function} callback - {err, Wallet}
*/ */
WalletFactory.prototype.read = function(walletId, skipFields, cb) { Identity.prototype.read = function(walletId, skipFields, cb) {
var self = this, var self = this,
err; err;
var obj = {}; var obj = {};
@ -199,7 +199,7 @@ WalletFactory.prototype.read = function(walletId, skipFields, cb) {
}); });
}; };
WalletFactory.prototype.read_Old = function(walletId, skipFields, cb) { Identity.prototype.read_Old = function(walletId, skipFields, cb) {
var self = this, var self = this,
err; err;
var obj = {}; var obj = {};
@ -238,7 +238,7 @@ WalletFactory.prototype.read_Old = function(walletId, skipFields, cb) {
*/ */
WalletFactory.prototype._getWallet = function(opts) { Identity.prototype._getWallet = function(opts) {
return new Wallet(opts); return new Wallet(opts);
}; };
@ -262,7 +262,7 @@ WalletFactory.prototype._getWallet = function(opts) {
* @param {callback} opts.version * @param {callback} opts.version
* @return {Wallet} * @return {Wallet}
*/ */
WalletFactory.prototype.create = function(opts, cb) { Identity.prototype.create = function(opts, cb) {
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
opts = opts || {}; opts = opts || {};
@ -327,7 +327,7 @@ WalletFactory.prototype.create = function(opts, cb) {
* @param {string} inVersion - a version, with major, minor, and revision, period-separated (x.y.z) * @param {string} inVersion - a version, with major, minor, and revision, period-separated (x.y.z)
* @throws {Error} if there's a major version difference * @throws {Error} if there's a major version difference
*/ */
WalletFactory.prototype._checkVersion = function(inVersion) { Identity.prototype._checkVersion = function(inVersion) {
var thisV = this.version.split('.'); var thisV = this.version.split('.');
var thisV0 = parseInt(thisV[0]); var thisV0 = parseInt(thisV[0]);
var inV = inVersion.split('.'); var inV = inVersion.split('.');
@ -349,7 +349,7 @@ WalletFactory.prototype._checkVersion = function(inVersion) {
* @param {function} callback (err, {Wallet}) * @param {function} callback (err, {Wallet})
* @return * @return
*/ */
WalletFactory.prototype.open = function(walletId, passphrase, cb) { Identity.prototype.open = function(walletId, passphrase, cb) {
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
var self = this; var self = this;
self.storage.setPassphrase(passphrase); self.storage.setPassphrase(passphrase);
@ -367,7 +367,7 @@ WalletFactory.prototype.open = function(walletId, passphrase, cb) {
}); });
}; };
WalletFactory.prototype.getWallets = function(cb) { Identity.prototype.getWallets = function(cb) {
var self = this; var self = this;
this.storage.getWallets(function(wallets) { this.storage.getWallets(function(wallets) {
wallets.forEach(function(i) { wallets.forEach(function(i) {
@ -392,7 +392,7 @@ WalletFactory.prototype.getWallets = function(cb) {
* @callback cb * @callback cb
* @return {?} the result of the callback * @return {?} the result of the callback
*/ */
WalletFactory.prototype.delete = function(walletId, cb) { Identity.prototype.delete = function(walletId, cb) {
var self = this; var self = this;
self.storage.deleteWallet(walletId, function(err) { self.storage.deleteWallet(walletId, function(err) {
if (err) return cb(err); if (err) return cb(err);
@ -405,7 +405,7 @@ WalletFactory.prototype.delete = function(walletId, cb) {
/** /**
* @desc Pass through to {@link Wallet#secret} * @desc Pass through to {@link Wallet#secret}
*/ */
WalletFactory.prototype.decodeSecret = function(secret) { Identity.prototype.decodeSecret = function(secret) {
try { try {
return Wallet.decodeSecret(secret); return Wallet.decodeSecret(secret);
} catch (e) { } catch (e) {
@ -434,7 +434,7 @@ WalletFactory.prototype.decodeSecret = function(secret) {
* @param {string} opts.privateHex - the private extended master key * @param {string} opts.privateHex - the private extended master key
* @param {walletCreationCallback} cb - a callback * @param {walletCreationCallback} cb - a callback
*/ */
WalletFactory.prototype.joinCreateSession = function(opts, cb) { Identity.prototype.joinCreateSession = function(opts, cb) {
preconditions.checkArgument(opts); preconditions.checkArgument(opts);
preconditions.checkArgument(opts.secret); preconditions.checkArgument(opts.secret);
preconditions.checkArgument(opts.passphrase); preconditions.checkArgument(opts.passphrase);
@ -513,4 +513,4 @@ WalletFactory.prototype.joinCreateSession = function(opts, cb) {
}); });
}; };
module.exports = WalletFactory; module.exports = Identity;

5
js/services/identity.js Normal file
View file

@ -0,0 +1,5 @@
'use strict';
angular.module('copayApp.services').factory('identity', function(pluginManager){
return new copay.Identity(config, copay.version, pluginManager);
});

View file

@ -1,5 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('walletFactory', function(pluginManager){
return new copay.WalletFactory(config, copay.version, pluginManager);
});

609
test/test.Identity.js Normal file

File diff suppressed because one or more lines are too long

View file

@ -10,8 +10,8 @@ beforeEach(angular.mock.module('copayApp'));
describe("Unit: Walletfactory Service", function() { describe("Unit: Walletfactory Service", function() {
beforeEach(angular.mock.module('copayApp.services')); beforeEach(angular.mock.module('copayApp.services'));
it('should contain a walletFactory service', inject(function(walletFactory) { it('should contain a identity service', inject(function(identity) {
expect(walletFactory).not.to.equal(null); expect(identity).not.to.equal(null);
})); }));
}); });

View file

@ -58,8 +58,8 @@ var createBundle = function(opts) {
expose: '../js/log' expose: '../js/log'
}); });
// b.external('bitcore'); // b.external('bitcore');
b.require('./js/models/WalletFactory', { b.require('./js/models/Identity', {
expose: '../js/models/WalletFactory' expose: '../js/models/Identity'
}); });
b.require('./js/models/Wallet'); b.require('./js/models/Wallet');
b.require('./js/models/Wallet', { b.require('./js/models/Wallet', {