Wallet/js/models/Profile.js

149 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-09-27 15:14:32 -03:00
'use strict';
var preconditions = require('preconditions').singleton();
var _ = require('underscore');
var log = require('../log');
var bitcore = require('bitcore');
2014-09-29 19:58:00 -03:00
function Profile(info, storage) {
2014-09-28 18:38:06 -03:00
preconditions.checkArgument(info.email);
2014-09-29 19:58:00 -03:00
preconditions.checkArgument(info.hash);
2014-09-27 15:14:32 -03:00
preconditions.checkArgument(storage);
2014-09-30 21:16:46 -03:00
preconditions.checkArgument(storage.setPassword, 'bad storage');
2014-09-27 15:14:32 -03:00
2014-09-29 19:58:00 -03:00
this.hash = info.hash;
2014-09-28 20:50:37 -03:00
this.email = info.email;
this.extra = info.extra || {};
2014-10-01 08:35:17 -03:00
this.walletInfos = info.walletInfos || {};
2014-09-29 19:58:00 -03:00
this.key = Profile.key(this.hash);
2014-09-27 18:00:27 -03:00
this.storage = storage;
2014-09-27 15:14:32 -03:00
};
2014-09-28 20:50:37 -03:00
Profile.hash = function(email, password) {
return bitcore.util.sha256ripe160(email + password).toString('hex');
};
2014-09-29 19:58:00 -03:00
Profile.key = function(hash) {
2014-09-30 15:28:02 -03:00
return 'profile::' + hash;
};
Profile.create = function(email, password, storage, cb) {
preconditions.checkArgument(cb);
2014-09-30 21:16:46 -03:00
preconditions.checkArgument(storage.setPassword);
2014-09-30 20:12:02 -03:00
preconditions.checkState(storage.hasPassphrase());
2014-09-30 15:28:02 -03:00
var p = new Profile({
email: email,
2014-10-01 08:35:17 -03:00
hash: Profile.hash(email, password),
2014-09-30 15:28:02 -03:00
}, storage);
2014-09-30 20:12:02 -03:00
p.store({}, function(err) {
2014-10-01 08:35:17 -03:00
return cb(err, p);
2014-09-30 20:12:02 -03:00
});
2014-09-27 15:14:32 -03:00
};
2014-09-29 19:58:00 -03:00
Profile.open = function(email, password, storage, cb) {
preconditions.checkArgument(cb);
2014-10-01 08:35:17 -03:00
preconditions.checkState(storage.hasPassphrase());
2014-09-28 20:50:37 -03:00
2014-09-29 19:58:00 -03:00
var key = Profile.key(Profile.hash(email, password));
2014-10-01 08:35:17 -03:00
storage.get(key, function(err, val) {
if (err || !val)
2014-09-29 19:58:00 -03:00
return cb(new Error('PNOTFOUND: Profile not found'));
2014-09-27 15:14:32 -03:00
2014-10-01 08:35:17 -03:00
if (!val.email)
return cb(new Error('PERROR: Could not open profile'));
return cb(null, new Profile(val, storage));
2014-09-28 20:50:37 -03:00
});
};
2014-09-27 15:14:32 -03:00
2014-09-29 19:58:00 -03:00
Profile.prototype.toObj = function() {
2014-10-01 08:35:17 -03:00
return _.clone(_.pick(this, 'hash', 'email', 'extra', 'walletInfos'));
2014-09-29 19:58:00 -03:00
};
Profile.prototype.getWallet = function(walletId, cb) {
2014-09-29 06:31:04 -03:00
return this.walletInfos[walletId];
};
Profile.prototype.listWallets = function(opts, cb) {
return _.sortBy(this.walletInfos, function(winfo) {
return -winfo.lastOpenedTs || -winfo.createdTs;
2014-09-29 06:31:04 -03:00
});
};
Profile.prototype.deleteWallet = function(walletId, cb) {
if (!this.walletInfos[walletId])
2014-10-01 08:35:17 -03:00
return cb(new Error('WNOEXIST: Wallet not on profile '));
2014-09-29 06:31:04 -03:00
delete this.walletInfos[walletId];
this.store({
overwrite: true
}, cb);
};
Profile.prototype.addToWallet = function(walletId, info, cb) {
if (!this.walletInfos[walletId])
2014-10-01 08:35:17 -03:00
return cb(new Error('WNOEXIST: Wallet not on profile '));
2014-09-29 06:31:04 -03:00
this.walletInfos[walletId] = _.extend(this.walletInfos[walletId], info);
2014-09-28 21:22:53 -03:00
2014-09-29 06:31:04 -03:00
this.store({
overwrite: true
}, cb);
};
Profile.prototype.addWallet = function(walletId, info, cb) {
2014-09-30 21:16:46 -03:00
preconditions.checkArgument(cb);
if (this.walletInfos[walletId])
2014-10-01 08:35:17 -03:00
return cb(new Error('WEXIST: Wallet already on profile '));
2014-09-28 21:22:53 -03:00
this.walletInfos[walletId] = _.extend(info, {
createdTs: Date.now(),
id: walletId
});
2014-09-29 06:31:04 -03:00
2014-09-28 21:22:53 -03:00
this.store({
overwrite: true
}, cb);
};
2014-09-30 21:16:46 -03:00
Profile.prototype.setLastOpenedTs = function(walletId, cb) {
return this.addToWallet(walletId, {
lastOpenedTs: Date.now()
}, cb);
};
2014-09-28 20:50:37 -03:00
Profile.prototype.store = function(opts, cb) {
var self = this;
var val = self.toObj();
2014-09-29 19:58:00 -03:00
var key = self.key;
2014-09-27 18:00:27 -03:00
2014-09-28 20:50:37 -03:00
self.storage.get(key, function(val2) {
2014-09-30 16:04:17 -03:00
2014-09-28 20:50:37 -03:00
if (val2 && !opts.overwrite) {
if (cb)
2014-10-01 08:35:17 -03:00
return cb(new Error('PEXISTS: Profile already exist '))
2014-09-28 20:50:37 -03:00
} else {
self.storage.set(key, val, function(err) {
2014-09-30 21:16:46 -03:00
log.debug('Profile stored');
2014-09-28 20:50:37 -03:00
if (cb)
cb(err);
});
}
2014-09-27 18:00:27 -03:00
});
2014-09-27 15:14:32 -03:00
};
Profile.prototype.getName = function() {
return this.extra.nickname || this.email;
};
2014-09-27 15:14:32 -03:00
module.exports = Profile;