From 47cb4bd3da3f8409b66a7d4a7e7ec07086835190 Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Mon, 28 Apr 2014 12:02:43 -0300 Subject: [PATCH] added a wrapper for the method fromObj of Wallet --- js/models/core/Wallet.js | 14 ++++++++------ js/models/core/WalletFactory.js | 11 ++--------- test/test.Wallet.js | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index ba30c175f..ee10c65cf 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -225,14 +225,16 @@ Wallet.prototype.toObj = function() { return walletObj; }; -Wallet.fromObj = function(wallet) { - var opts = wallet.opts; - opts['publicKeyRing'] = this.publicKeyring.fromObj(wallet.publicKeyRing); - opts['txProposals'] = this.txProposal.fromObj(wallet.txProposals); - opts['privateKey'] = this.privateKey.fromObj(wallet.privateKey); +Wallet.fromObj = function(o, storage, network, blockchain) { + var opts = JSON.parse(JSON.stringify(o.opts)); + opts.publicKeyRing = copay.PublicKeyRing.fromObj(o.publicKeyRing); + opts.txProposals = copay.TxProposals.fromObj(o.txProposals); + opts.privateKey = copay.PrivateKey.fromObj(o.privateKey); + opts.storage = storage; + opts.network = network; + opts.blockchain = blockchain; var w = new Wallet(opts); - return w; }; diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index 5460907df..4f865cca9 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -52,15 +52,8 @@ WalletFactory.prototype._checkRead = function(walletId) { }; WalletFactory.prototype.fromObj = function(obj) { - var opts = obj.opts; - opts.publicKeyRing = new PublicKeyRing.fromObj(obj.publicKeyRing); - opts.txProposals = new TxProposals.fromObj(obj.txProposals); - opts.privateKey = new PrivateKey.fromObj(obj.privateKey); - opts.storage = this.storage; - opts.network = this.network; - opts.blockchain = this.blockchain; - opts.verbose = this.verbose; - var w = new Wallet(opts); + var w = Wallet.fromObj(obj, this.storage, this.network, this.blockchain); + w.verbose = this.verbose; // JIC: Add our key try { diff --git a/test/test.Wallet.js b/test/test.Wallet.js index 9fdaaac4e..bc34c0690 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -170,4 +170,22 @@ describe('Wallet model', function() { } }); + it('#fromObj #toObj round trip', function () { + + var w = createW2(); + + var o = w.toObj(); + o = JSON.parse(JSON.stringify(o)); + + var w2 = Wallet.fromObj(o, + new Storage(config.storage), + new Network(config.network), + new Blockchain(config.blockchain)); + should.exist(w2); + w2.publicKeyRing.requiredCopayers.should.equal(w.publicKeyRing.requiredCopayers); + should.exist(w2.publicKeyRing.getCopayerId); + should.exist(w2.txProposals.toObj); + should.exist(w2.privateKey.toObj); + }); + });