add #addWallet to profile

This commit is contained in:
Matias Alejo Garcia 2014-09-28 21:22:53 -03:00
commit 7abdc77611
4 changed files with 91 additions and 42 deletions

View file

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

View file

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