diff --git a/lib/hdkey.js b/lib/hdkey.js index 670386c..eba25e6 100644 --- a/lib/hdkey.js +++ b/lib/hdkey.js @@ -57,7 +57,8 @@ Object.defineProperty(HDKey.prototype, 'publicKey', { Object.defineProperty(HDKey.prototype, 'privateExtendedKey', { get: function () { - return cs.encode(serialize(this, this.versions.private, Buffer.concat([new Buffer([0]), this.privateKey]))) + if (this._privateKey) return cs.encode(serialize(this, this.versions.private, Buffer.concat([new Buffer([0]), this.privateKey]))) + else return null } }) diff --git a/test/hdkey.test.js b/test/hdkey.test.js index 6d05e3c..3d09053 100644 --- a/test/hdkey.test.js +++ b/test/hdkey.test.js @@ -139,4 +139,20 @@ describe('hdkey', function () { assert(HDKey.HARDENED_OFFSET) }) }) + + describe('> when private key is null', function () { + it('privateExtendedKey should return null and not throw', function () { + var seed = '000102030405060708090a0b0c0d0e0f' + var masterKey = HDKey.fromMasterSeed(new Buffer(seed, 'hex')) + + assert.ok(masterKey.privateExtendedKey, 'xpriv is truthy') + masterKey._privateKey = null + + assert.doesNotThrow(function () { + masterKey.privateExtendedKey + }) + + assert.ok(!masterKey.privateExtendedKey, 'xpriv is falsy') + }) + }) })