Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Add possibility to select compression of WIF for Private Key
Browse files Browse the repository at this point in the history
  • Loading branch information
k06a committed Mar 12, 2018
1 parent 026ddb4 commit d941759
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions lib/privatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,27 +300,49 @@ PrivateKey.prototype.toString = function() {
};

/**
* Will output the PrivateKey to a WIF string
* Will output the PrivateKey to a WIF string leading to compressed Public Key form
*
* @returns {string} A WIP representation of the private key
*/
PrivateKey.prototype.toWIF = function() {
PrivateKey.prototype.toCompressedWIF = function() {
var network = this.network;
var compressed = this.compressed;

var buf;
if (compressed) {
buf = Buffer.concat([Buffer.from([network.privatekey]),
this.bn.toBuffer({size: 32}),
Buffer.from([0x01])]);
} else {
buf = Buffer.concat([Buffer.from([network.privatekey]),
this.bn.toBuffer({size: 32})]);
}
var buf = Buffer.concat([new Buffer([network.privatekey]),
this.bn.toBuffer({size: 32}),
new Buffer([0x01])]);

return Base58Check.encode(buf);
};

/**
* Will output the PrivateKey to a WIF string leading to uncompressed Public Key form
*
* @returns {string} A WIP representation of the private key
*/
PrivateKey.prototype.toUncompressedWIF = function() {
var network = this.network;
var compressed = this.compressed;

var buf = Buffer.concat([Buffer.from([network.privatekey]),
this.bn.toBuffer({size: 32})]);

return Base58Check.encode(buf);
};

/**
* Will output the PrivateKey to a WIF string
*
* @returns {string} A WIP representation of the private key
*/
PrivateKey.prototype.toWIF = function() {
if (this.compressed) {
return this.toCompressedWIF();
} else {
return this.toUncompressedWIF();
}
};

/**
* Will return the private key as a BN instance
*
Expand Down

0 comments on commit d941759

Please sign in to comment.