Browse Source

HDNode: rename priv/pub to privKey/pubKey

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
6a73bc02f5
  1. 30
      src/hdnode.js
  2. 4
      src/wallet.js
  3. 12
      test/hdnode.js
  4. 12
      test/wallet.js

30
src/hdnode.js

@ -40,10 +40,10 @@ function HDNode(K, chainCode, network) {
this.network = network this.network = network
if (K instanceof BigInteger) { if (K instanceof BigInteger) {
this.priv = new ECKey(K, true) this.privKey = new ECKey(K, true)
this.pub = this.priv.pub this.pubKey = this.privKey.pub
} else { } else {
this.pub = new ECPubKey(K, true) this.pubKey = new ECPubKey(K, true)
} }
} }
@ -137,7 +137,7 @@ HDNode.fromHex = function(hex, isPrivate) {
} }
HDNode.prototype.getIdentifier = function() { HDNode.prototype.getIdentifier = function() {
return crypto.hash160(this.pub.toBuffer()) return crypto.hash160(this.pubKey.toBuffer())
} }
HDNode.prototype.getFingerprint = function() { HDNode.prototype.getFingerprint = function() {
@ -145,7 +145,7 @@ HDNode.prototype.getFingerprint = function() {
} }
HDNode.prototype.getAddress = function() { HDNode.prototype.getAddress = function() {
return this.pub.getAddress(this.network.pubKeyHash) return this.pubKey.getAddress(this.network.pubKeyHash)
} }
HDNode.prototype.toBase58 = function(isPrivate) { HDNode.prototype.toBase58 = function(isPrivate) {
@ -159,7 +159,7 @@ HDNode.prototype.toBase58 = function(isPrivate) {
} }
HDNode.prototype.toBuffer = function(isPrivate) { HDNode.prototype.toBuffer = function(isPrivate) {
if (isPrivate == undefined) isPrivate = !!this.priv if (isPrivate == undefined) isPrivate = !!this.privKey
// Version // Version
var version = isPrivate ? this.network.bip32.private : this.network.bip32.public var version = isPrivate ? this.network.bip32.private : this.network.bip32.public
@ -185,15 +185,15 @@ HDNode.prototype.toBuffer = function(isPrivate) {
// 33 bytes: the public key or private key data // 33 bytes: the public key or private key data
if (isPrivate) { if (isPrivate) {
assert(this.priv, 'Missing private key') assert(this.privKey, 'Missing private key')
// 0x00 + k for private keys // 0x00 + k for private keys
buffer.writeUInt8(0, 45) buffer.writeUInt8(0, 45)
this.priv.D.toBuffer(32).copy(buffer, 46) this.privKey.D.toBuffer(32).copy(buffer, 46)
} else { } else {
// X9.62 encoding for public keys // X9.62 encoding for public keys
this.pub.toBuffer().copy(buffer, 45) this.pubKey.toBuffer().copy(buffer, 45)
} }
return buffer return buffer
@ -213,11 +213,11 @@ HDNode.prototype.derive = function(index) {
// Hardened child // Hardened child
if (isHardened) { if (isHardened) {
assert(this.priv, 'Could not derive hardened child key') assert(this.privKey, 'Could not derive hardened child key')
// data = 0x00 || ser256(kpar) || ser32(index) // data = 0x00 || ser256(kpar) || ser32(index)
data = Buffer.concat([ data = Buffer.concat([
this.priv.D.toBuffer(33), this.privKey.D.toBuffer(33),
indexBuffer indexBuffer
]) ])
@ -226,7 +226,7 @@ HDNode.prototype.derive = function(index) {
// data = serP(point(kpar)) || ser32(index) // data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index) // = serP(Kpar) || ser32(index)
data = Buffer.concat([ data = Buffer.concat([
this.pub.toBuffer(), this.pubKey.toBuffer(),
indexBuffer indexBuffer
]) ])
} }
@ -243,9 +243,9 @@ HDNode.prototype.derive = function(index) {
} }
// Private parent key -> private child key // Private parent key -> private child key
if (this.priv) { if (this.privKey) {
// ki = parse256(IL) + kpar (mod n) // ki = parse256(IL) + kpar (mod n)
var ki = pIL.add(this.priv.D).mod(ecparams.getN()) var ki = pIL.add(this.privKey.D).mod(ecparams.getN())
// In case ki == 0, proceed with the next value for i // In case ki == 0, proceed with the next value for i
if (ki.signum() === 0) { if (ki.signum() === 0) {
@ -258,7 +258,7 @@ HDNode.prototype.derive = function(index) {
} else { } else {
// Ki = point(parse256(IL)) + Kpar // Ki = point(parse256(IL)) + Kpar
// = G*IL + Kpar // = G*IL + Kpar
var Ki = ecparams.getG().multiply(pIL).add(this.pub.Q) var Ki = ecparams.getG().multiply(pIL).add(this.pubKey.Q)
// In case Ki is the point at infinity, proceed with the next value for i // In case Ki is the point at infinity, proceed with the next value for i
if (Ki.isInfinity()) { if (Ki.isInfinity()) {

4
src/wallet.js

@ -246,11 +246,11 @@ function Wallet(seed, network) {
this.getExternalAccount = function() { return externalAccount } this.getExternalAccount = function() { return externalAccount }
this.getPrivateKey = function(index) { this.getPrivateKey = function(index) {
return externalAccount.derive(index).priv return externalAccount.derive(index).privKey
} }
this.getInternalPrivateKey = function(index) { this.getInternalPrivateKey = function(index) {
return internalAccount.derive(index).priv return internalAccount.derive(index).privKey
} }
this.getPrivateKeyForAddress = function(address) { this.getPrivateKeyForAddress = function(address) {

12
test/hdnode.js

@ -18,15 +18,15 @@ describe('HDNode', function() {
it('calculates the publicKey from a BigInteger', function() { it('calculates the publicKey from a BigInteger', function() {
var hd = new HDNode(D, chainCode) var hd = new HDNode(D, chainCode)
assert(hd.pub.Q.equals(Q)) assert(hd.pubKey.Q.equals(Q))
}) })
it('only uses compressed points', function() { it('only uses compressed points', function() {
var hd = new HDNode(Q, chainCode) var hd = new HDNode(Q, chainCode)
var hdP = new HDNode(D, chainCode) var hdP = new HDNode(D, chainCode)
assert.strictEqual(hd.pub.compressed, true) assert.strictEqual(hd.pubKey.compressed, true)
assert.strictEqual(hdP.pub.compressed, true) assert.strictEqual(hdP.pubKey.compressed, true)
}) })
it('has a default depth/index of 0', function() { it('has a default depth/index of 0', function() {
@ -60,7 +60,7 @@ describe('HDNode', function() {
it('calculates privKey and chainCode for ' + f.master.fingerprint, function() { it('calculates privKey and chainCode for ' + f.master.fingerprint, function() {
var hd = HDNode.fromSeedHex(f.master.seed) var hd = HDNode.fromSeedHex(f.master.seed)
assert.equal(hd.priv.toWIF(), f.master.wif) assert.equal(hd.privKey.toWIF(), f.master.wif)
assert.equal(hd.chainCode.toString('hex'), f.master.chainCode) assert.equal(hd.chainCode.toString('hex'), f.master.chainCode)
}) })
}) })
@ -209,8 +209,8 @@ describe('HDNode', function() {
describe('derive', function() { describe('derive', function() {
function verifyVector(hd, v, depth) { function verifyVector(hd, v, depth) {
assert.equal(hd.priv.toWIF(), v.wif) assert.equal(hd.privKey.toWIF(), v.wif)
assert.equal(hd.pub.toHex(), v.pubKey) assert.equal(hd.pubKey.toHex(), v.pubKey)
assert.equal(hd.chainCode.toString('hex'), v.chainCode) assert.equal(hd.chainCode.toString('hex'), v.chainCode)
assert.equal(hd.depth, depth || 0) assert.equal(hd.depth, depth || 0)

12
test/wallet.js

@ -121,8 +121,8 @@ describe('Wallet', function() {
it('returns the private key at the given index of external account', function(){ it('returns the private key at the given index of external account', function(){
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).priv) assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).privKey)
assertEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).priv) assertEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).privKey)
}) })
}) })
@ -130,8 +130,8 @@ describe('Wallet', function() {
it('returns the private key at the given index of internal account', function(){ it('returns the private key at the given index of internal account', function(){
var wallet = new Wallet(seed, networks.testnet) var wallet = new Wallet(seed, networks.testnet)
assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).priv) assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).privKey)
assertEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).priv) assertEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).privKey)
}) })
}) })
@ -144,11 +144,11 @@ describe('Wallet', function() {
assertEqual( assertEqual(
wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"), wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
wallet.getExternalAccount().derive(1).priv wallet.getExternalAccount().derive(1).privKey
) )
assertEqual( assertEqual(
wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"), wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
wallet.getInternalAccount().derive(0).priv wallet.getInternalAccount().derive(0).privKey
) )
}) })

Loading…
Cancel
Save