mv #read to wallet

This commit is contained in:
Matias Alejo Garcia 2014-09-29 16:55:45 -03:00
commit 4c582384b0
4 changed files with 201 additions and 146 deletions

View file

@ -188,7 +188,9 @@ Identity.prototype.store = function(opts, cb) {
* @param {string[]} skipFields - fields to ignore when importing
* @return {Wallet}
*/
Identity.prototype.importWallet = function(base64, passphrase, skipFields) {
Identity.prototype.importWallet = function(base64, passphrase, skipFields, cb) {
preconditions.checkArgument(cb);
this.storage.setPassphrase(passphrase);
var obj = this.storage.decrypt(base64);
@ -197,7 +199,7 @@ Identity.prototype.importWallet = function(base64, passphrase, skipFields) {
var networkName = Wallet.obtainNetworkName(obj);
var w = Identity._walletFromObj(obj, this.storage, this.networks[networkName], this.blockchains[networkName]);
this._checkVersion(w.version);
this.addWallet(w.id, function(err) {
this.addWallet(w, function(err) {
if (err) return cb(err);
w.store(cb);
});
@ -226,42 +228,6 @@ Identity.prototype.migrateWallet = function(walletId, passphrase, cb) {
};
/**
* @desc Retrieve a wallet from storage
* @param {string} walletId - the wallet id
* @param {string[]} skipFields - parameters to ignore when importing
* @param {function} callback - {err, Wallet}
*/
Identity.prototype._readWallet = function(walletId, skipFields, cb) {
var self = this,
err;
var obj = {};
this.storage.getFirst('wallet::' + walletId, function(err, ret) {
if (err) return cb(err);
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
if (!_.any(_.values(obj)))
return cb(new Error('Wallet not found'));
var w, err;
obj.id = walletId;
try {
w = self.fromObj(obj, skipFields);
} catch (e) {
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('Could not read: ' + walletId);
} else {
err = e;
}
w = null;
}
return cb(err, w);
});
};
Identity.prototype.read_Old = function(walletId, skipFields, cb) {
var self = this,
@ -376,16 +342,15 @@ Identity.prototype.createWallet = function(opts, cb) {
Identity.prototype.addWallet = function(wallet, cb) {
preconditions.checkArgument(wallet);
preconditions.checkArgument(wallet.id);
preconditions.checkArgument(wallet.getId);
preconditions.checkArgument(cb);
var self = this;
self.profile.addWallet(wallet.id, function(err) {
if (err) return cb(err);
self.wallets.push(w);
w.store(function(err) {
self.wallets.push(wallet);
wallet.store(function(err) {
return cb(err);
});
});

View file

@ -199,6 +199,51 @@ Wallet.delete = function(walletId, storage, cb) {
});
};
/**
* @desc Retrieve a wallet from storage
*
* @param {string} walletId - the wallet id
* @param storage
* @param network
* @param blockchain
* @param {string[]} skipFields - parameters to ignore when importing
* @param {function} callback - {err, Wallet}
* @return {undefined}
*/
Wallet.read = function(walletId, storage, network, blockchain, skipFields, cb) {
preconditions.checkArgument(cb);
var self = this,
err;
var obj = {};
storage.getFirst('wallet::' + walletId, function(err, ret) {
if (err) return cb(err);
if (!ret)
return cb(new Error('WNOTFOUND: Wallet not found'));
_.each(Wallet.PERSISTED_PROPERTIES, function(p) {
obj[p] = ret[p];
});
var w, err;
obj.id = walletId;
try {
w = self.fromObj(obj, storage, network, blockchain, skipFields);
} catch (e) {
if (e && e.message && e.message.indexOf('MISSOPTS')) {
err = new Error('WERROR: Could not read: ' + walletId);
} else {
err = e;
}
w = null;
}
return cb(err, w);
});
};
/**
* @desc obtain network name from serialized wallet
@ -209,7 +254,7 @@ Wallet.obtainNetworkName = function(obj) {
return obj.networkName ||
(obj.opts ? obj.opts.networkName : null) ||
(obj.publicKeyRing ? obj.publicKeyRing.networkName : null) ||
(obj.publicKeyRing ? obj.privateKey.networkName : null);
(obj.privateKey ? obj.privateKey.networkName : null);
};
@ -919,6 +964,11 @@ Wallet.prototype.store = function(cb) {
});
};
Wallet.prototype.getId = function() {
return this.id;
};
/**
* @desc Serialize the wallet into a plain object.
* @return {Object}