From 28a1eae0be4b8045655abf63e14a6703fb15e682 Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 27 Jan 2015 11:59:20 -0300 Subject: [PATCH] HDKeys: add toBuffer/fromBuffer to HDPrivateKey --- lib/hdprivatekey.js | 8 ++++++++ test/hdprivatekey.js | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 8e40411..27b9e8f 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -484,6 +484,14 @@ HDPrivateKey.prototype.toJSON = function toJSON() { return JSON.stringify(this.toObject()); }; +HDPrivateKey.fromBuffer = function(arg) { + return new HDPrivateKey(arg); +}; + +HDPrivateKey.prototype.toBuffer = function() { + return this._buffers.xprivkey; +}; + HDPrivateKey.DefaultDepth = 0; HDPrivateKey.DefaultFingerprint = 0; HDPrivateKey.DefaultChildIndex = 0; diff --git a/test/hdprivatekey.js b/test/hdprivatekey.js index e6c57e8..140351b 100644 --- a/test/hdprivatekey.js +++ b/test/hdprivatekey.js @@ -262,6 +262,17 @@ describe('HDPrivate key interface', function() { }); }); + describe.only('conversion to/from buffer', function() { + var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'; + it('should roundtrip to/from a buffer', function() { + var priv = new HDPrivateKey(str); + var toBuffer = priv.toBuffer(); + var fromBuffer = HDPrivateKey.fromBuffer(toBuffer); + var roundTrip = new HDPrivateKey(fromBuffer.toBuffer()); + roundTrip.xprivkey.should.equal(str); + }); + }); + describe('conversion to plain object/json', function() { var plainObject = { 'network':'livenet',