From d941759b921e59568d5d44e4d8d0f5edfe8a2372 Mon Sep 17 00:00:00 2001 From: Anton Bukov Date: Mon, 12 Mar 2018 00:33:33 +0300 Subject: [PATCH] Add possibility to select compression of WIF for Private Key --- lib/privatekey.js | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 95a3bc505..b8af8a169 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -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 *