require('classtool');

function ClassSpec(b) {
	var coinUtil = require('./util/util');
	var timeUtil = require('./util/time');
	var KeyModule = require('./Key');
	var Address = require('./Address').class();

	function WalletKey(cfg) {
		this.network = cfg.network;	// required
		this.created = cfg.created;
		this.privKey = cfg.privKey;
	};

	WalletKey.prototype.generate = function() {
		this.privKey = KeyModule.Key.generateSync();
		this.created = timeUtil.curtime();
	};

	WalletKey.prototype.storeObj = function() {
		var pubKey = this.privKey.public.toString('hex');
		var pubKeyHash = coinUtil.sha256ripe160(this.privKey.public);
		var addr = new Address(this.network.addressPubkey, pubKeyHash);
		var obj = {
			created: this.created,
			priv: this.privKey.private.toString('hex'),
			pub: pubKey,
			addr: addr.toString(),
		};

		return obj;
	};

	WalletKey.prototype.fromObj = function(obj) {
		this.created = obj.created;
		this.privKey = new KeyModule.Key();
		this.privKey.private = new Buffer(obj.priv, 'hex');
	};

	return WalletKey;
};
module.defineClass(ClassSpec);


@@ -151,24 +151,75 @@
 e('./Key');%0A
+%09var PrivateKey = require('./PrivateKey').class();%0A
 %09var Address
@@ -747,16 +747,116 @@
 yHash);%0A
+%09%09var priv = new PrivateKey(this.network.keySecret, this.privKey.private, this.privKey.compressed);%0A
 %09%09var ob
@@ -896,36 +896,20 @@
 %09%09priv: 
-this.privKey.private
+priv
 .toStrin
@@ -910,21 +910,16 @@
 oString(
-'hex'
 ),%0A%09%09%09pu
@@ -1091,24 +1091,54 @@
 dule.Key();%0A
+%09%09if (obj.priv.length==64) %7B%0A%09
 %09%09this.privK
@@ -1174,17 +1174,16 @@
 riv,
- 
 'hex');%0A
 %09%7D;%0A
@@ -1178,16 +1178,243 @@
 'hex');%0A
+%09%09%09this.privKey.compressed = true;%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09var priv = new PrivateKey(obj.priv);%0A%09%09%09this.privKey.private = new Buffer(priv.payload());%0A%09%09%09this.privKey.compressed = priv.compressed();%0A%09%09%7D%0A%09%09this.privKey.regenerateSync();%0A
 %09%7D;%0A%0A%09re
@@ -1463,9 +1463,8 @@
 sSpec);%0A
-%0A

save private keys in base58 format