added a wrapper for the method fromObj of Wallet

This commit is contained in:
Mario Colque 2014-04-28 12:02:43 -03:00
parent 1168bf82b8
commit 47cb4bd3da
3 changed files with 28 additions and 15 deletions

View File

@ -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;
};

View File

@ -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 {

View File

@ -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);
});
});