From 0bae82fdf41b72d7cb065c037f3f6b76296e0b9a Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 27 Jan 2015 13:35:10 -0300 Subject: [PATCH] Add fromBuffer and toBuffer to PrivateKey --- lib/privatekey.js | 42 ++++++++++++++++++++++++++++++++++++------ test/privatekey.js | 11 +++++++++-- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 9dd6d0d..acd548c 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -152,12 +152,8 @@ PrivateKey._transformBuffer = function(buf, network) { var info = {}; - if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) { - info.compressed = true; - } else if (buf.length === 1 + 32) { - info.compressed = false; - } else { - throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)'); + if (buf.length === 32) { + return PrivateKey._transformBNBuffer(buf, network); } info.network = Networks.get(buf[0], 'privatekey'); @@ -173,10 +169,33 @@ PrivateKey._transformBuffer = function(buf, network) { throw new TypeError('Private key network mismatch'); } + if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) { + info.compressed = true; + } else if (buf.length === 1 + 32) { + info.compressed = false; + } else { + throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)'); + } + info.bn = BN.fromBuffer(buf.slice(1, 32 + 1)); return info; +}; +/** + * Internal function to transform a BN buffer into a private key + * + * @param {Buffer} buf + * @param {Network|string} [network] - a {@link Network} object, or a string with the network name + * @returns {object} an Object with keys: bn, network, and compressed + * @private + */ +PrivateKey._transformBNBuffer = function(buf, network) { + var info = {}; + info.network = Networks.get(network) || Networks.defaultNetwork; + info.bn = BN.fromBuffer(buf); + info.compressed = false; + return info; }; /** @@ -204,9 +223,20 @@ PrivateKey.fromJSON = function(json) { return new PrivateKey(json); }; +/** + * Instantiate a PrivateKey from a Buffer with the DER or WIF representation + * + * @param {Buffer} arg + * @param {Network} network + * @return {PrivateKey} + */ +PrivateKey.fromBuffer = function(arg, network) { + return new PrivateKey(arg, network); +}; /** * Internal function to transform a JSON string on plain object into a private key + * return this. * * @param {String} json - A JSON string or plain object * @returns {Object} An object with keys: bn, network and compressed diff --git a/test/privatekey.js b/test/privatekey.js index 1b7f05a..02c78d1 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -268,11 +268,18 @@ describe('PrivateKey', function() { }); - describe('#toBuffer', function() { - it('should output known buffer', function() { + describe('buffer serialization', function() { + it('returns an expected value when creating a PrivateKey from a buffer', function() { var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet'); privkey.toString().should.equal(buf.toString('hex')); }); + + it('roundtrips correctly when using toBuffer/fromBuffer', function() { + var privkey = new PrivateKey(BN.fromBuffer(buf)); + var toBuffer = new PrivateKey(privkey.toBuffer()); + var fromBuffer = PrivateKey.fromBuffer(toBuffer.toBuffer()); + fromBuffer.toString().should.equal(privkey.toString()); + }); }); describe('#toBigNumber', function() {