2014-04-13 13:28:42 -07:00
|
|
|
var coinUtil = require('../util');
|
|
|
|
var timeUtil = require('../util/time');
|
2014-03-19 13:52:34 -07:00
|
|
|
var Key = require('./Key');
|
2014-03-05 11:11:16 -08:00
|
|
|
var PrivateKey = require('./PrivateKey');
|
|
|
|
var Address = require('./Address');
|
2013-08-08 15:44:46 -07:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
function WalletKey(cfg) {
|
|
|
|
if (!cfg) cfg = {};
|
|
|
|
if (!cfg.network) throw new Error('network parameter is required');
|
|
|
|
this.network = cfg.network; // required
|
|
|
|
this.created = cfg.created;
|
|
|
|
this.privKey = cfg.privKey;
|
|
|
|
};
|
2013-08-08 15:44:46 -07:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
WalletKey.prototype.generate = function() {
|
2014-03-13 10:31:02 -07:00
|
|
|
this.privKey = Key.generateSync();
|
2014-03-05 11:11:16 -08:00
|
|
|
this.created = timeUtil.curtime();
|
|
|
|
};
|
2013-08-08 15:44:46 -07:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
WalletKey.prototype.storeObj = function() {
|
|
|
|
var pubKey = this.privKey.public.toString('hex');
|
|
|
|
var pubKeyHash = coinUtil.sha256ripe160(this.privKey.public);
|
2014-04-07 14:30:49 -07:00
|
|
|
var addr = new Address(this.network.addressVersion, pubKeyHash);
|
|
|
|
var priv = new PrivateKey(this.network.privKeyVersion, this.privKey.private, this.privKey.compressed);
|
2014-03-05 11:11:16 -08:00
|
|
|
var obj = {
|
|
|
|
created: this.created,
|
|
|
|
priv: priv.toString(),
|
|
|
|
pub: pubKey,
|
|
|
|
addr: addr.toString(),
|
2014-01-16 01:21:35 -08:00
|
|
|
};
|
2013-08-08 15:44:46 -07:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
return obj;
|
|
|
|
};
|
2013-10-31 06:40:50 -07:00
|
|
|
|
2014-03-05 11:11:16 -08:00
|
|
|
WalletKey.prototype.fromObj = function(obj) {
|
|
|
|
this.created = obj.created;
|
2014-03-13 10:31:02 -07:00
|
|
|
this.privKey = new Key();
|
2014-03-19 13:52:34 -07:00
|
|
|
if (obj.priv.length == 64) {
|
|
|
|
this.privKey.private = new Buffer(obj.priv, 'hex');
|
2014-06-23 10:57:02 -07:00
|
|
|
this.privKey.compressed = typeof obj.compressed === 'undefined' ? true : obj.compressed;
|
2014-03-19 13:52:34 -07:00
|
|
|
} else {
|
2014-03-05 11:11:16 -08:00
|
|
|
var priv = new PrivateKey(obj.priv);
|
2014-03-19 15:00:45 -07:00
|
|
|
priv.validate();
|
2014-03-05 11:11:16 -08:00
|
|
|
this.privKey.private = new Buffer(priv.payload());
|
|
|
|
this.privKey.compressed = priv.compressed();
|
|
|
|
}
|
|
|
|
this.privKey.regenerateSync();
|
2013-08-08 15:44:46 -07:00
|
|
|
};
|
2014-03-05 11:11:16 -08:00
|
|
|
|
2014-07-10 12:08:42 -07:00
|
|
|
module.exports = (WalletKey);
|