From 70d306242e1c24b06cf5a3c9b04a9c0a1e13e08f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 30 Sep 2014 20:12:02 -0300 Subject: [PATCH] refactors to UX --- js/models/Identity.js | 29 +++++++++++++++-------------- js/models/Profile.js | 8 +++++++- js/models/Storage.js | 5 +++++ js/models/Wallet.js | 15 +++++++++++---- test/test.Identity.js | 6 +++--- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index ebedda4ac..c7e5f9a05 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -11,6 +11,7 @@ var version = require('../../version').version; var PluginManager = require('./PluginManager'); var Profile = require('./Profile'); var Insight = module.exports.Insight = require('./Insight'); +var Async = module.exports.Async = require('./Async'); var preconditions = require('preconditions').singleton(); var Storage = module.exports.Storage = require('./Storage'); @@ -44,8 +45,8 @@ function Identity(email, password, opts) { this.storage = Identity._newStorage(storageOpts); this.networks = { - 'livenet': Identity._newInsight(opts.network.livenet), - 'testnet': Identity._newInsight(opts.network.testnet), + 'livenet': Identity._newAsync(opts.network.livenet), + 'testnet': Identity._newAsync(opts.network.testnet), }; this.blockchains = { 'livenet': Identity._newInsight(opts.network.livenet), @@ -69,6 +70,12 @@ Identity._newInsight = function(opts) { return new Insight(opts); }; +Identity._newAsync = function(opts) { + return new Async(opts); +}; + + + Identity._newStorage = function(opts) { return new Storage(opts); }; @@ -89,13 +96,6 @@ Identity._walletDelete = function(id, cb) { return Wallet.delete(id, cb); }; -Identity._profileOpen = function(e, p, s, cb) { - Profile.create(e, p, s, cb); -}; - - - - /** * creates and Identity * @@ -113,18 +113,18 @@ Identity.create = function(email, password, opts, cb) { Identity._createProfile(email, password, iden.storage, function(err, profile) { if (err) return cb(err); iden.profile = profile; +console.log('[Identity.js.115:profile:]',profile); //TODO if (opts.noWallets) cb(null, iden); // default wallet - var wopts = { + var wopts = _.extend(opts.walletDefaults,{ nickname: email, networkName: opts.networkName, requiredCopayers: 1, totalCopayers: 1, - }; - + }); iden.createWallet(wopts, function(err, w) { return cb(null, iden, w); }); @@ -158,10 +158,9 @@ Identity.prototype.validate = function(authcode, cb) { Identity.open = function(email, password, opts, cb) { 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); iden.profile = profile; - return cb(null, iden); }); }; @@ -254,6 +253,7 @@ Identity.prototype.importWallet = function(base64, passphrase, skipFields, cb) { */ Identity.prototype.createWallet = function(opts, cb) { preconditions.checkArgument(cb); + preconditions.checkState(this.profile); opts = opts || {}; opts.networkName = opts.networkName || 'testnet'; @@ -318,6 +318,7 @@ Identity.prototype.addWallet = function(wallet, cb) { preconditions.checkArgument(wallet); preconditions.checkArgument(wallet.getId); preconditions.checkArgument(cb); + preconditions.checkState(this.profile); var self = this; self.profile.addWallet(wallet.id, function(err) { diff --git a/js/models/Profile.js b/js/models/Profile.js index 540149e80..4c857c2c1 100644 --- a/js/models/Profile.js +++ b/js/models/Profile.js @@ -30,11 +30,17 @@ Profile.key = function(hash) { Profile.create = function(email, password, storage, cb) { preconditions.checkArgument(cb); + preconditions.checkArgument(storage.setPassphrase); + + preconditions.checkState(storage.hasPassphrase()); + var p = new Profile({ email: email, hash: Profile.hash(email,password), }, storage); - p.store({}, cb); + p.store({}, function(err) { + return cb(err,p); + }); }; Profile.open = function(email, password, storage, cb) { diff --git a/js/models/Storage.js b/js/models/Storage.js index faf46f1d4..9880d31d0 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -43,6 +43,11 @@ Storage.prototype._getPassphrase = function() { return pps[this.__uniqueid]; } + +Storage.prototype.hasPassphrase = function() { + return pps[this.__uniqueid] ? true : false; +}; + Storage.prototype.setPassphrase = function(password) { pps[this.__uniqueid] = password; } diff --git a/js/models/Wallet.js b/js/models/Wallet.js index f628e3f6f..fa96c8653 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -79,8 +79,6 @@ function Wallet(opts) { this.publicKeyRing.walletId = this.id; this.txProposals.walletId = this.id; - this.network.maxPeers = this.totalCopayers; - this.network.secretNumber = this.secretNumber; this.registeredPeerIds = []; this.addressBook = opts.addressBook || {}; this.publicKey = this.privateKey.publicHex; @@ -96,6 +94,15 @@ function Wallet(opts) { 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 //one nonce for oneself, and then one nonce for each copayer this.network.setHexNonce(opts.networkNonce); @@ -1064,8 +1071,8 @@ Wallet.fromObj = function(o, storage, network, blockchain, skipFields) { opts.lastTimestamp = o.lastTimestamp; opts.storage = storage; - opts.network = _.isArray(network)? network[networkName] : network; - opts.blockchain = _.isArray(blockchain) ? blockchain[networkName] : blockchain; + opts.network = network; + opts.blockchain = blockchain; opts.isImported = true; return new Wallet(opts); diff --git a/test/test.Identity.js b/test/test.Identity.js index 4b361f2ed..25aabe7ee 100644 --- a/test/test.Identity.js +++ b/test/test.Identity.js @@ -127,14 +127,14 @@ describe('Identity model', function() { describe('#open', function(done) { 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) { should.not.exist(err); iden.profile.should.equal('kk'); - Identity._profileOpen.calledOnce.should.equal(true); + Identity._createProfile.calledOnce.should.equal(true); done(); }); });