From 7a73f582b4cdbded73405e667e1534b88d655abe Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 27 Jan 2015 12:24:00 -0300 Subject: [PATCH] HDKeys: add toBuffer/fromBuffer to HDPublicKey --- lib/hdpublickey.js | 19 +++++++++++++++++++ test/hdprivatekey.js | 2 +- test/hdpublickey.js | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 721b18a..7126d50 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -417,6 +417,25 @@ HDPublicKey.prototype.toJSON = function toJSON() { return JSON.stringify(this.toObject()); }; +/** + * Create a HDPublicKey from a buffer argument + * + * @param {Buffer} arg + * @return {HDPublicKey} + */ +HDPublicKey.fromBuffer = function(arg) { + return new HDPublicKey(arg); +}; + +/** + * Return a buffer representation of the xpubkey + * + * @return {Buffer} + */ +HDPublicKey.prototype.toBuffer = function() { + return this._buffers.xpubkey; +}; + HDPublicKey.Hardened = 0x80000000; HDPublicKey.RootElementAlias = ['m', 'M']; diff --git a/test/hdprivatekey.js b/test/hdprivatekey.js index 140351b..1c2788c 100644 --- a/test/hdprivatekey.js +++ b/test/hdprivatekey.js @@ -262,7 +262,7 @@ describe('HDPrivate key interface', function() { }); }); - describe.only('conversion to/from buffer', function() { + describe('conversion to/from buffer', function() { var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'; it('should roundtrip to/from a buffer', function() { var priv = new HDPrivateKey(str); diff --git a/test/hdpublickey.js b/test/hdpublickey.js index f67b1e4..4d48f88 100644 --- a/test/hdpublickey.js +++ b/test/hdpublickey.js @@ -153,6 +153,17 @@ describe('HDPublicKey interface', function() { pubKey.inspect().should.equal(''); }); + describe('conversion to/from buffer', function() { + + it('should roundtrip to an equivalent object', function() { + var pubKey = new HDPublicKey(xpubkey); + var toBuffer = pubKey.toBuffer(); + var fromBuffer = HDPublicKey.fromBuffer(toBuffer); + var roundTrip = new HDPublicKey(fromBuffer.toBuffer()); + roundTrip.xpubkey.should.equal(xpubkey); + }); + }); + describe('conversion to different formats', function() { var plainObject = { 'network':'livenet',