Wallet/js/models/Profile.js

43 lines
1.1 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-27 18:00:27 -03:00
function Profile(opts, password, storage) {
2014-09-27 15:14:32 -03:00
preconditions.checkArgument(opts.email);
2014-09-27 18:00:27 -03:00
preconditions.checkArgument(password);
2014-09-27 15:14:32 -03:00
preconditions.checkArgument(storage);
preconditions.checkArgument(storage.getItem);
this.email = opts.email;
2014-09-27 18:00:27 -03:00
this.hash = bitcore.util.sha256ripe160(this.email + this.password).toString('hex');
this.storage = storage;
2014-09-27 15:14:32 -03:00
this.extra = opts.extra;
};
2014-09-27 18:00:27 -03:00
Profile.fromObj = function(obj, password, storage) {
var o = _.clone(obj);
return new Profile(obj, password, storage);
2014-09-27 15:14:32 -03:00
};
Profile.prototype.toObj = function() {
2014-09-27 18:00:27 -03:00
var obj = _.clone(this);
delete obj['hash'];
return JSON.parse(JSON.stringify(obj));
2014-09-27 15:14:32 -03:00
};
Profile.prototype.store = function(cb) {
2014-09-27 18:00:27 -03:00
var val = this.toObj();
var key = 'identity::' + this.hash + '_' + this.email;
this.storage.setFromObj(key, val, function(err) {
log.debug('Identity stored');
if (cb)
cb(err);
});
2014-09-27 15:14:32 -03:00
};
module.exports = Profile;