From 2006b3aae22f2bf938235f0a9db2cdf522719802 Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 23 Dec 2014 12:23:35 -0300 Subject: [PATCH] Add G7 methods to HDPublicKey --- lib/hdpublickey.js | 4 ++++ test/hdpublickey.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 63439ca..158aa05 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -344,6 +344,10 @@ HDPublicKey._validateBufferArguments = function (arg) { } }; +HDPublicKey.fromString = HDPublicKey.fromObject = HDPublicKey.fromJSON = function(arg) { + return new HDPublicKey(arg); +}; + /** * Returns the base58 checked representation of the public key * @return {string} a string starting with "xpub..." in livenet diff --git a/test/hdpublickey.js b/test/hdpublickey.js index 67c31e0..721fca8 100644 --- a/test/hdpublickey.js +++ b/test/hdpublickey.js @@ -153,6 +153,30 @@ describe('HDPublicKey interface', function() { pubKey.inspect().should.equal(''); }); + describe('conversion to different formats', function() { + var plainObject = { + 'network':'livenet', + 'depth':0, + 'fingerPrint':876747070, + 'parentFingerPrint':0, + 'childIndex':0, + 'chainCode':'873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508', + 'publicKey':'0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2', + 'checksum':-1421395167, + 'xpubkey':'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8' + }; + it('roundtrips to JSON and to Object', function() { + var pubkey = new HDPublicKey(xpubkey); + expect(HDPublicKey.fromJSON(pubkey.toJSON()).xpubkey).to.equal(xpubkey); + }); + it('recovers state from JSON', function() { + new HDPublicKey(JSON.stringify(plainObject)).xpubkey.should.equal(xpubkey); + }); + it('recovers state from Object', function() { + new HDPublicKey(plainObject).xpubkey.should.equal(xpubkey); + }); + }); + describe('derivation', function() { it('derivation is the same whether deriving with number or string', function() { var pubkey = new HDPublicKey(xpubkey);