diff --git a/src/hdnode.js b/src/hdnode.js index 1fd50be..4511224 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -43,7 +43,11 @@ function HDNode(K, chainCode, network) { if (K instanceof BigInteger) { this.privKey = new ECKey(K, true) this.pubKey = this.privKey.pub + } else if (K instanceof ECKey) { + assert(K.pub.compressed, 'ECKey must be compressed') + this.privKey = K } else if (K instanceof ECPubKey) { + assert(K.compressed, 'ECPubKey must be compressed') this.pubKey = K } else { this.pubKey = new ECPubKey(K, true) diff --git a/test/hdnode.js b/test/hdnode.js index a1cb97a..b2055ad 100644 --- a/test/hdnode.js +++ b/test/hdnode.js @@ -2,6 +2,8 @@ var assert = require('assert') var networks = require('../src/networks') var BigInteger = require('bigi') +var ECKey = require('../src/eckey') +var ECPubKey = require('../src/ecpubkey') var HDNode = require('../src/hdnode') var ecurve = require('ecurve') @@ -22,6 +24,36 @@ describe('HDNode', function() { assert(hd.pubKey.Q.equals(Q)) }) + it('allows initialization directly from an ECKey', function() { + var ek = new ECKey(d) + var hd = new HDNode(ek, chainCode) + + assert.equal(hd.privKey, ek) + }) + + it('allows initialization directly from an ECPubKey', function() { + var ek = new ECPubKey(Q) + var hd = new HDNode(ek, chainCode) + + assert.equal(hd.pubKey, ek) + }) + + it('throws if ECKey is not compressed', function() { + var ek = new ECKey(d, false) + + assert.throws(function() { + new HDNode(ek, chainCode) + }, /ECKey must be compressed/) + }) + + it('throws if ECPubKey is not compressed', function() { + var ek = new ECPubKey(Q, false) + + assert.throws(function() { + new HDNode(ek, chainCode) + }, /ECPubKey must be compressed/) + }) + it('only uses compressed points', function() { var hd = new HDNode(Q, chainCode) var hdP = new HDNode(d, chainCode)