Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding function to decide if HDNode is private #536

Merged
merged 3 commits into from
Feb 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/hdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ HDNode.prototype.toBase58 = function (__isPrivate) {

// Version
var network = this.keyPair.network
var version = this.keyPair.d ? network.bip32.private : network.bip32.public
var version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public
var buffer = new Buffer(78)

// 4 bytes: version bytes
Expand All @@ -190,7 +190,7 @@ HDNode.prototype.toBase58 = function (__isPrivate) {
this.chainCode.copy(buffer, 13)

// 33 bytes: the public key or private key data
if (this.keyPair.d) {
if (!this.isNeutered()) {
// 0x00 + k for private keys
buffer.writeUInt8(0, 45)
this.keyPair.d.toBuffer(32).copy(buffer, 46)
Expand All @@ -211,7 +211,7 @@ HDNode.prototype.derive = function (index) {

// Hardened child
if (isHardened) {
if (!this.keyPair.d) throw new TypeError('Could not derive hardened child key')
if (this.isNeutered()) throw new TypeError('Could not derive hardened child key')

// data = 0x00 || ser256(kpar) || ser32(index)
data[0] = 0x00
Expand Down Expand Up @@ -239,7 +239,7 @@ HDNode.prototype.derive = function (index) {

// Private parent key -> private child key
var derivedKeyPair
if (this.keyPair.d) {
if (!this.isNeutered()) {
// ki = parse256(IL) + kpar (mod n)
var ki = pIL.add(this.keyPair.d).mod(curve.n)

Expand Down Expand Up @@ -281,6 +281,12 @@ HDNode.prototype.deriveHardened = function (index) {
return this.derive(index + HDNode.HIGHEST_BIT)
}

// Private === not neutered
// Public === neutered
HDNode.prototype.isNeutered = function () {
return !(this.keyPair.d)
}

HDNode.prototype.toString = HDNode.prototype.toBase58

module.exports = HDNode
6 changes: 5 additions & 1 deletion test/hdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('HDNode', function () {

assert.strictEqual(hd.toBase58(), f.master.base58)
assert.strictEqual(hd.keyPair.network, network)
assert.strictEqual(hd.isNeutered(), true)
})
})

Expand All @@ -202,6 +203,7 @@ describe('HDNode', function () {

assert.strictEqual(hd.toBase58(), f.master.base58Priv)
assert.strictEqual(hd.keyPair.network, network)
assert.strictEqual(hd.isNeutered(), false)
})
})

Expand Down Expand Up @@ -240,14 +242,16 @@ describe('HDNode', function () {
var f = fixtures.valid[0]

it('strips all private information', function () {
var hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
var hd = HDNode.fromBase58(f.master.base58Priv, NETWORKS_LIST)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha. Good catch.

var hdn = hd.neutered()

assert.strictEqual(hdn.keyPair.d, undefined)
assert.strictEqual(hdn.keyPair.Q, hd.keyPair.Q)
assert.strictEqual(hdn.chainCode, hd.chainCode)
assert.strictEqual(hdn.depth, hd.depth)
assert.strictEqual(hdn.index, hd.index)
assert.strictEqual(hdn.isNeutered(), true)
assert.strictEqual(hd.isNeutered(), false)
})
})

Expand Down