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

Commit d941759

Browse files
committed
Add possibility to select compression of WIF for Private Key
1 parent 026ddb4 commit d941759

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

lib/privatekey.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,27 +300,49 @@ PrivateKey.prototype.toString = function() {
300300
};
301301

302302
/**
303-
* Will output the PrivateKey to a WIF string
303+
* Will output the PrivateKey to a WIF string leading to compressed Public Key form
304304
*
305305
* @returns {string} A WIP representation of the private key
306306
*/
307-
PrivateKey.prototype.toWIF = function() {
307+
PrivateKey.prototype.toCompressedWIF = function() {
308308
var network = this.network;
309309
var compressed = this.compressed;
310310

311-
var buf;
312-
if (compressed) {
313-
buf = Buffer.concat([Buffer.from([network.privatekey]),
314-
this.bn.toBuffer({size: 32}),
315-
Buffer.from([0x01])]);
316-
} else {
317-
buf = Buffer.concat([Buffer.from([network.privatekey]),
318-
this.bn.toBuffer({size: 32})]);
319-
}
311+
var buf = Buffer.concat([new Buffer([network.privatekey]),
312+
this.bn.toBuffer({size: 32}),
313+
new Buffer([0x01])]);
320314

321315
return Base58Check.encode(buf);
322316
};
323317

318+
/**
319+
* Will output the PrivateKey to a WIF string leading to uncompressed Public Key form
320+
*
321+
* @returns {string} A WIP representation of the private key
322+
*/
323+
PrivateKey.prototype.toUncompressedWIF = function() {
324+
var network = this.network;
325+
var compressed = this.compressed;
326+
327+
var buf = Buffer.concat([Buffer.from([network.privatekey]),
328+
this.bn.toBuffer({size: 32})]);
329+
330+
return Base58Check.encode(buf);
331+
};
332+
333+
/**
334+
* Will output the PrivateKey to a WIF string
335+
*
336+
* @returns {string} A WIP representation of the private key
337+
*/
338+
PrivateKey.prototype.toWIF = function() {
339+
if (this.compressed) {
340+
return this.toCompressedWIF();
341+
} else {
342+
return this.toUncompressedWIF();
343+
}
344+
};
345+
324346
/**
325347
* Will return the private key as a BN instance
326348
*

0 commit comments

Comments
 (0)