Browse Source

save private keys in base58 format

...to allow them to be imported into other wallets, preserving the "compressed"
or "uncompressed" state of the public key in the private key representation.
patch-2
Ryan X. Charles 11 years ago
parent
commit
f67b32c233
  1. 16
      WalletKey.js

16
WalletKey.js

@ -4,6 +4,7 @@ function ClassSpec(b) {
var coinUtil = require('./util/util'); var coinUtil = require('./util/util');
var timeUtil = require('./util/time'); var timeUtil = require('./util/time');
var KeyModule = require('./Key'); var KeyModule = require('./Key');
var PrivateKey = require('./PrivateKey').class();
var Address = require('./Address').class(); var Address = require('./Address').class();
function WalletKey(cfg) { function WalletKey(cfg) {
@ -21,9 +22,10 @@ function ClassSpec(b) {
var pubKey = this.privKey.public.toString('hex'); var pubKey = this.privKey.public.toString('hex');
var pubKeyHash = coinUtil.sha256ripe160(this.privKey.public); var pubKeyHash = coinUtil.sha256ripe160(this.privKey.public);
var addr = new Address(this.network.addressPubkey, pubKeyHash); var addr = new Address(this.network.addressPubkey, pubKeyHash);
var priv = new PrivateKey(this.network.keySecret, this.privKey.private, this.privKey.compressed);
var obj = { var obj = {
created: this.created, created: this.created,
priv: this.privKey.private.toString('hex'), priv: priv.toString(),
pub: pubKey, pub: pubKey,
addr: addr.toString(), addr: addr.toString(),
}; };
@ -34,10 +36,18 @@ function ClassSpec(b) {
WalletKey.prototype.fromObj = function(obj) { WalletKey.prototype.fromObj = function(obj) {
this.created = obj.created; this.created = obj.created;
this.privKey = new KeyModule.Key(); this.privKey = new KeyModule.Key();
this.privKey.private = new Buffer(obj.priv, 'hex'); if (obj.priv.length==64) {
this.privKey.private = new Buffer(obj.priv,'hex');
this.privKey.compressed = true;
}
else {
var priv = new PrivateKey(obj.priv);
this.privKey.private = new Buffer(priv.payload());
this.privKey.compressed = priv.compressed();
}
this.privKey.regenerateSync();
}; };
return WalletKey; return WalletKey;
}; };
module.defineClass(ClassSpec); module.defineClass(ClassSpec);

Loading…
Cancel
Save