From f8397a617ef4b64a7aead3df269135bbf3ecc41d Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 9 Dec 2014 11:27:54 -0300 Subject: [PATCH] Add option to create private key from hex --- lib/privatekey.js | 7 ++++++- test/privatekey.js | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 337216c21..0a75d1c9a 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -7,6 +7,7 @@ var networks = require('./networks'); var base58check = require('./encoding/base58check'); var Address = require('./address'); var PublicKey = require('./publickey'); +var jsUtil = require('./util/js'); /** * @@ -51,7 +52,11 @@ var PrivateKey = function PrivateKey(data, network, compressed) { } else if (data instanceof Buffer || data instanceof Uint8Array) { info = PrivateKey._transformBuffer(data, network, compressed); } else if (typeof(data) === 'string'){ - info = PrivateKey._transformWIF(data, network, compressed); + if (jsUtil.isHexa(data)) { + info.bn = BN(new Buffer(data, 'hex')); + } else { + info = PrivateKey._transformWIF(data, network, compressed); + } } else { throw new TypeError('First argument is an unrecognized data type.'); } diff --git a/test/privatekey.js b/test/privatekey.js index c1d878a9d..77c38f074 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -72,6 +72,13 @@ describe('PrivateKey', function() { }).should.throw('Invalid network'); }); + it('can be instantiated from a hex string', function() { + var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; + var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; + var privkey = new PrivateKey(privhex); + privkey.publicKey.toString().should.equal(pubhex); + }); + it('should not be able to instantiate because compressed is non-boolean', function() { (function() { var a = new PrivateKey(null, 'testnet', 'compressed');