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

Add possibility to select compression of WIF for Private Key #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions lib/privatekey.js
Original file line number Diff line number Diff line change
@@ -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
*