From c3a178061c882e90d83805a92f0e8ce9d3b5e66a Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 23 Dec 2014 10:56:16 -0300 Subject: [PATCH] Adding G7 methods to HDPrivateKEy --- lib/hdprivatekey.js | 4 ++++ test/hdprivatekey.js | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 9d88f85fc..ad53e1c6a 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -214,6 +214,10 @@ HDPrivateKey._validateNetwork = function(data, networkArg) { return null; }; +HDPrivateKey.fromJSON = HDPrivateKey.fromObject = HDPrivateKey.fromString = function(arg) { + return new HDPrivateKey(arg); +}; + HDPrivateKey.prototype._buildFromJSON = function(arg) { return this._buildFromObject(JSON.parse(arg)); }; diff --git a/test/hdprivatekey.js b/test/hdprivatekey.js index c81b37b78..6ba3b4d67 100644 --- a/test/hdprivatekey.js +++ b/test/hdprivatekey.js @@ -8,7 +8,7 @@ var bitcore = require('..'); var errors = bitcore.errors; var hdErrors = errors.HDPrivateKey; var buffer = require('buffer'); -var bufferUtil = bitcore.util.buffer; +var BufferUtil = bitcore.util.buffer; var HDPrivateKey = bitcore.HDPrivateKey; var Base58Check = bitcore.encoding.Base58Check; @@ -168,7 +168,7 @@ describe('HDPrivate key interface', function() { var error = null; try { var buffers = privKey._buffers; - buffers.checksum = bufferUtil.integerAsBuffer(0); + buffers.checksum = BufferUtil.integerAsBuffer(0); var privateKey = new HDPrivateKey(buffers); } catch (e) { error = e; @@ -189,5 +189,39 @@ describe('HDPrivate key interface', function() { var derivedByNumber = privateKey.derive(0, true).derive(1).derive(2, true); derivedByNumber.xprivkey.should.equal(derivedByString.xprivkey); }); + + describe('conversion to plain object/json', function() { + var plainObject = { + 'network':'livenet', + 'depth':0, + 'fingerPrint':876747070, + 'parentFingerPrint':0, + 'childIndex':0, + 'chainCode':'873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508', + 'privateKey':'e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35', + 'checksum':-411132559, + 'xprivkey':'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi' + }; + it('toObject leaves no Buffer instances', function() { + var privKey = new HDPrivateKey(xprivkey); + var object = privKey.toObject(); + _.each(_.values(object), function(value) { + expect(BufferUtil.isBuffer(value)).to.equal(false); + }); + }); + it('roundtrips toJSON', function() { + expect(HDPrivateKey.fromJSON(new HDPrivateKey(xprivkey).toJSON()).xprivkey).to.equal(xprivkey); + }); + it('roundtrips to JSON and to Object', function() { + var privkey = new HDPrivateKey(xprivkey); + expect(HDPrivateKey.fromJSON(privkey.toJSON()).xprivkey).to.equal(xprivkey); + }); + it('recovers state from JSON', function() { + new HDPrivateKey(JSON.stringify(plainObject)).xprivkey.should.equal(xprivkey); + }); + it('recovers state from Object', function() { + new HDPrivateKey(plainObject).xprivkey.should.equal(xprivkey); + }); + }); });