refactors to UX

This commit is contained in:
Matias Alejo Garcia 2014-09-30 20:12:02 -03:00
commit 70d306242e
5 changed files with 41 additions and 22 deletions

View file

@ -11,6 +11,7 @@ var version = require('../../version').version;
var PluginManager = require('./PluginManager'); var PluginManager = require('./PluginManager');
var Profile = require('./Profile'); var Profile = require('./Profile');
var Insight = module.exports.Insight = require('./Insight'); var Insight = module.exports.Insight = require('./Insight');
var Async = module.exports.Async = require('./Async');
var preconditions = require('preconditions').singleton(); var preconditions = require('preconditions').singleton();
var Storage = module.exports.Storage = require('./Storage'); var Storage = module.exports.Storage = require('./Storage');
@ -44,8 +45,8 @@ function Identity(email, password, opts) {
this.storage = Identity._newStorage(storageOpts); this.storage = Identity._newStorage(storageOpts);
this.networks = { this.networks = {
'livenet': Identity._newInsight(opts.network.livenet), 'livenet': Identity._newAsync(opts.network.livenet),
'testnet': Identity._newInsight(opts.network.testnet), 'testnet': Identity._newAsync(opts.network.testnet),
}; };
this.blockchains = { this.blockchains = {
'livenet': Identity._newInsight(opts.network.livenet), 'livenet': Identity._newInsight(opts.network.livenet),
@ -69,6 +70,12 @@ Identity._newInsight = function(opts) {
return new Insight(opts); return new Insight(opts);
}; };
Identity._newAsync = function(opts) {
return new Async(opts);
};
Identity._newStorage = function(opts) { Identity._newStorage = function(opts) {
return new Storage(opts); return new Storage(opts);
}; };
@ -89,13 +96,6 @@ Identity._walletDelete = function(id, cb) {
return Wallet.delete(id, cb); return Wallet.delete(id, cb);
}; };
Identity._profileOpen = function(e, p, s, cb) {
Profile.create(e, p, s, cb);
};
/** /**
* creates and Identity * creates and Identity
* *
@ -113,18 +113,18 @@ Identity.create = function(email, password, opts, cb) {
Identity._createProfile(email, password, iden.storage, function(err, profile) { Identity._createProfile(email, password, iden.storage, function(err, profile) {
if (err) return cb(err); if (err) return cb(err);
iden.profile = profile; iden.profile = profile;
console.log('[Identity.js.115:profile:]',profile); //TODO
if (opts.noWallets) if (opts.noWallets)
cb(null, iden); cb(null, iden);
// default wallet // default wallet
var wopts = { var wopts = _.extend(opts.walletDefaults,{
nickname: email, nickname: email,
networkName: opts.networkName, networkName: opts.networkName,
requiredCopayers: 1, requiredCopayers: 1,
totalCopayers: 1, totalCopayers: 1,
}; });
iden.createWallet(wopts, function(err, w) { iden.createWallet(wopts, function(err, w) {
return cb(null, iden, w); return cb(null, iden, w);
}); });
@ -158,10 +158,9 @@ Identity.prototype.validate = function(authcode, cb) {
Identity.open = function(email, password, opts, cb) { Identity.open = function(email, password, opts, cb) {
var iden = new Identity(email, password, opts); var iden = new Identity(email, password, opts);
Identity._profileOpen(email, password, iden.storage, function(err, profile) { Identity._createProfile(email, password, iden.storage, function(err, profile) {
if (err) return cb(err); if (err) return cb(err);
iden.profile = profile; iden.profile = profile;
return cb(null, iden); return cb(null, iden);
}); });
}; };
@ -254,6 +253,7 @@ Identity.prototype.importWallet = function(base64, passphrase, skipFields, cb) {
*/ */
Identity.prototype.createWallet = function(opts, cb) { Identity.prototype.createWallet = function(opts, cb) {
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
preconditions.checkState(this.profile);
opts = opts || {}; opts = opts || {};
opts.networkName = opts.networkName || 'testnet'; opts.networkName = opts.networkName || 'testnet';
@ -318,6 +318,7 @@ Identity.prototype.addWallet = function(wallet, cb) {
preconditions.checkArgument(wallet); preconditions.checkArgument(wallet);
preconditions.checkArgument(wallet.getId); preconditions.checkArgument(wallet.getId);
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
preconditions.checkState(this.profile);
var self = this; var self = this;
self.profile.addWallet(wallet.id, function(err) { self.profile.addWallet(wallet.id, function(err) {

View file

@ -30,11 +30,17 @@ Profile.key = function(hash) {
Profile.create = function(email, password, storage, cb) { Profile.create = function(email, password, storage, cb) {
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
preconditions.checkArgument(storage.setPassphrase);
preconditions.checkState(storage.hasPassphrase());
var p = new Profile({ var p = new Profile({
email: email, email: email,
hash: Profile.hash(email,password), hash: Profile.hash(email,password),
}, storage); }, storage);
p.store({}, cb); p.store({}, function(err) {
return cb(err,p);
});
}; };
Profile.open = function(email, password, storage, cb) { Profile.open = function(email, password, storage, cb) {

View file

@ -43,6 +43,11 @@ Storage.prototype._getPassphrase = function() {
return pps[this.__uniqueid]; return pps[this.__uniqueid];
} }
Storage.prototype.hasPassphrase = function() {
return pps[this.__uniqueid] ? true : false;
};
Storage.prototype.setPassphrase = function(password) { Storage.prototype.setPassphrase = function(password) {
pps[this.__uniqueid] = password; pps[this.__uniqueid] = password;
} }

View file

@ -79,8 +79,6 @@ function Wallet(opts) {
this.publicKeyRing.walletId = this.id; this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id; this.txProposals.walletId = this.id;
this.network.maxPeers = this.totalCopayers;
this.network.secretNumber = this.secretNumber;
this.registeredPeerIds = []; this.registeredPeerIds = [];
this.addressBook = opts.addressBook || {}; this.addressBook = opts.addressBook || {};
this.publicKey = this.privateKey.publicHex; this.publicKey = this.privateKey.publicHex;
@ -96,6 +94,15 @@ function Wallet(opts) {
this.paymentRequests = opts.paymentRequests || {}; this.paymentRequests = opts.paymentRequests || {};
var networkName = Wallet.obtainNetworkName(this);
this.network = _.isArray(this.network)? this.network[networkName] : this.network;
this.blockchain = _.isArray(this.blockchain) ? this.blockchain[networkName] : this.blockchain;
this.network.maxPeers = this.totalCopayers;
this.network.secretNumber = this.secretNumber;
//network nonces are 8 byte buffers, representing a big endian number //network nonces are 8 byte buffers, representing a big endian number
//one nonce for oneself, and then one nonce for each copayer //one nonce for oneself, and then one nonce for each copayer
this.network.setHexNonce(opts.networkNonce); this.network.setHexNonce(opts.networkNonce);
@ -1064,8 +1071,8 @@ Wallet.fromObj = function(o, storage, network, blockchain, skipFields) {
opts.lastTimestamp = o.lastTimestamp; opts.lastTimestamp = o.lastTimestamp;
opts.storage = storage; opts.storage = storage;
opts.network = _.isArray(network)? network[networkName] : network; opts.network = network;
opts.blockchain = _.isArray(blockchain) ? blockchain[networkName] : blockchain; opts.blockchain = blockchain;
opts.isImported = true; opts.isImported = true;
return new Wallet(opts); return new Wallet(opts);

View file

@ -127,14 +127,14 @@ describe('Identity model', function() {
describe('#open', function(done) { describe('#open', function(done) {
beforeEach(function() { beforeEach(function() {
Identity._profileOpen = sinon.stub().callsArgWith(3, null, 'kk'); Identity._createProfile = sinon.stub().callsArgWith(3, null, 'kk');
}); });
it('should call ._profileOpen', function(done) { it('should call ._createProfile', function(done) {
Identity.open(email, password, config, function(err, iden) { Identity.open(email, password, config, function(err, iden) {
should.not.exist(err); should.not.exist(err);
iden.profile.should.equal('kk'); iden.profile.should.equal('kk');
Identity._profileOpen.calledOnce.should.equal(true); Identity._createProfile.calledOnce.should.equal(true);
done(); done();
}); });
}); });