From 7abdc776119fb91212eb6efedd9ce6c84d4324c6 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 28 Sep 2014 21:22:53 -0300 Subject: [PATCH] add #addWallet to profile --- js/models/Identity.js | 19 +++++++---- js/models/Profile.js | 12 +++++++ test/test.Identity.js | 75 +++++++++++++++++++++++-------------------- test/test.Profile.js | 27 ++++++++++++++-- 4 files changed, 91 insertions(+), 42 deletions(-) diff --git a/js/models/Identity.js b/js/models/Identity.js index b774891c8..b26ac959c 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -144,7 +144,9 @@ Identity.isAvailable = function(email, opts, cb) { Identity.prototype.store = function(opts, cb) { var self = this; - self.profile.store(function() { + self.profile.store(opts, function(err) { + if (err) return cb(err); + var l = self.wallets.length, i = 0; if (!l) return cb(); @@ -169,7 +171,7 @@ Identity.prototype.store = function(opts, cb) { Identity.prototype.obtainNetworkName = function(obj) { return obj.networkName || (obj.opts ? obj.opts.networkName : null) || - (obj.publicKeyRing ? obj.publicKeyRing.networkName :null) || + (obj.publicKeyRing ? obj.publicKeyRing.networkName : null) || obj.privateKey.networkName; }; @@ -391,12 +393,17 @@ Identity.prototype.createWallet = function(opts, cb) { opts.version = opts.version || this.version; this.storage.setPassphrase(opts.passphrase); + + var w = this._getWallet(opts); - var self = this; - w.store(function(err) { + this.profile.addWallet(w.id, function(err) { if (err) return cb(err); - self.storage.setLastOpened(w.id, function(err) { - return cb(err, w); + var self = this; + w.store(function(err) { + if (err) return cb(err); + self.storage.setLastOpened(w.id, function(err) { + return cb(err, w); + }); }); }); }; diff --git a/js/models/Profile.js b/js/models/Profile.js index a5831e5fc..c29a3595a 100644 --- a/js/models/Profile.js +++ b/js/models/Profile.js @@ -12,6 +12,7 @@ function Profile(info, password, storage) { this.email = info.email; this.extra = info.extra; + this.walletIds = {}; this.hash = Profile.hash(this.email, password); this.storage = storage; }; @@ -44,6 +45,17 @@ Profile.open = function(storage, cb) { }); }; + +Profile.prototype.addWallet = function(walletId, cb) { + if (this.walletIds[walletId]) + return cb(new Error('WEXIST: Wallet already on profile')); + + this.walletIds[walletId] = Date.now(); + this.store({ + overwrite: true + }, cb); +}; + Profile.prototype.store = function(opts, cb) { var self = this; var val = self.toObj(); diff --git a/test/test.Identity.js b/test/test.Identity.js index a1d96055c..65e266db7 100644 --- a/test/test.Identity.js +++ b/test/test.Identity.js @@ -46,11 +46,16 @@ describe('Identity model', function() { profile = sinon.stub(); profile.test = sinon.stub(); - profile.store = sinon.stub(); - profile.store.yields(null); + profile.store = sinon.stub().yields(null);; Identity._newProfile = sinon.stub().returns(profile); iden = new Identity(email, password, config); + + var w = sinon.stub(); + w.store = sinon.stub().yields(null); + iden._getWallet = sinon.stub().returns(w); + + }); @@ -187,6 +192,40 @@ describe('Identity model', function() { }); }); + + describe('#createWallet', function() { + it('should create wallet', function(done) { + iden.createWallet(null, function(err, w) { + should.exist(w); + should.not.exist(err); + done(); + }); + }); + + it('should be able to create wallets with given pk', function(done) { + var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m'; + iden.createWallet({ + privateKeyHex: priv, + }, function(err, w) { + iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.equal(priv); + should.not.exist(err); + done(); + }); + }); + + it('should be able to create wallets with random pk', function(done) { + iden.createWallet(null, function(err, w1) { + iden.createWallet(null, function(err, w2) { + iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.not.equal( + iden._getWallet.getCall(1).args[0].privateKey.toObj().extendedPrivateKeyString + ); + done(); + }); + }); + }); + }); + + // TODO this is a WALLET TEST! not Wallet Factory. Move it. describe.skip('#fromObj / #toObj', function() { it('round trip', function() { @@ -492,38 +531,6 @@ describe('Identity model', function() { }); }); - describe('#createWallet', function() { - it('should create wallet', function(done) { - iden.createWallet(null, function(err, w) { - should.exist(w); - should.not.exist(err); - done(); - }); - }); - - it('should be able to create wallets with given pk', function(done) { - var priv = 'tprv8ZgxMBicQKsPdEqHcA7RjJTayxA3gSSqeRTttS1JjVbgmNDZdSk9EHZK5pc52GY5xFmwcakmUeKWUDzGoMLGAhrfr5b3MovMUZUTPqisL2m'; - iden.createWallet({ - privateKeyHex: priv, - }, function(err, w) { - iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.equal(priv); - should.not.exist(err); - done(); - }); - }); - - it('should be able to create wallets with random pk', function(done) { - iden.createWallet(null, function(err, w1) { - iden.createWallet(null, function(err, w2) { - iden._getWallet.getCall(0).args[0].privateKey.toObj().extendedPrivateKeyString.should.not.equal( - iden._getWallet.getCall(1).args[0].privateKey.toObj().extendedPrivateKeyString - ); - done(); - }); - }); - }); - }); - describe('#joinWallet', function() { var opts = { secret: '8WtTuiFTkhP5ao7AF2QErSwV39Cbur6pdMebKzQXFqL59RscXM', diff --git a/test/test.Profile.js b/test/test.Profile.js index 4baafcea6..7eb4adfad 100644 --- a/test/test.Profile.js +++ b/test/test.Profile.js @@ -44,6 +44,27 @@ describe('Profile model', function() { p2.should.deep.equal(p); }); + describe.only('#addWallet', function() { + it('should add a wallet id', function(done) { + var p = new Profile(opts, password, storage); + p.addWallet('123', function(err) { + p.walletIds['123'].should.be.above(123456789); + storage.set.getCall(0).args[1].should.deep.equal(p.toObj()); + done(); + }) + }); + it('should keep old value', function(done) { + var p = new Profile(opts, password, storage); + p.walletIds['123']=1; + p.addWallet('123', function(err) { + p.walletIds['123'].should.equal(1); + should.not.exist(storage.set.getCall(0)); + done(); + }) + }); + + }); + describe('#store', function() { it('should call storage set', function(done) { var p = new Profile(opts, password, storage); @@ -62,11 +83,13 @@ describe('Profile model', function() { done(); }) }); - + it('should use overwrite param', function(done) { storage.get = sinon.stub().yields(123); var p = new Profile(opts, password, storage); - p.store({overwrite:true}, function(err) { + p.store({ + overwrite: true + }, function(err) { storage.set.getCall(0).args[1].should.deep.equal(p.toObj()); should.not.exist(err); done();