HDNode: rename priv/pub to privKey/pubKey

This commit is contained in:
Daniel Cousens 2014-06-03 17:30:05 +10:00
parent dca193be4e
commit 6a73bc02f5
4 changed files with 29 additions and 29 deletions

View file

@ -40,10 +40,10 @@ function HDNode(K, chainCode, network) {
this.network = network
if (K instanceof BigInteger) {
this.priv = new ECKey(K, true)
this.pub = this.priv.pub
this.privKey = new ECKey(K, true)
this.pubKey = this.privKey.pub
} 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() {
return crypto.hash160(this.pub.toBuffer())
return crypto.hash160(this.pubKey.toBuffer())
}
HDNode.prototype.getFingerprint = function() {
@ -145,7 +145,7 @@ HDNode.prototype.getFingerprint = function() {
}
HDNode.prototype.getAddress = function() {
return this.pub.getAddress(this.network.pubKeyHash)
return this.pubKey.getAddress(this.network.pubKeyHash)
}
HDNode.prototype.toBase58 = function(isPrivate) {
@ -159,7 +159,7 @@ HDNode.prototype.toBase58 = function(isPrivate) {
}
HDNode.prototype.toBuffer = function(isPrivate) {
if (isPrivate == undefined) isPrivate = !!this.priv
if (isPrivate == undefined) isPrivate = !!this.privKey
// Version
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
if (isPrivate) {
assert(this.priv, 'Missing private key')
assert(this.privKey, 'Missing private key')
// 0x00 + k for private keys
buffer.writeUInt8(0, 45)
this.priv.D.toBuffer(32).copy(buffer, 46)
this.privKey.D.toBuffer(32).copy(buffer, 46)
} else {
// X9.62 encoding for public keys
this.pub.toBuffer().copy(buffer, 45)
this.pubKey.toBuffer().copy(buffer, 45)
}
return buffer
@ -213,11 +213,11 @@ HDNode.prototype.derive = function(index) {
// Hardened child
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 = Buffer.concat([
this.priv.D.toBuffer(33),
this.privKey.D.toBuffer(33),
indexBuffer
])
@ -226,7 +226,7 @@ HDNode.prototype.derive = function(index) {
// data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index)
data = Buffer.concat([
this.pub.toBuffer(),
this.pubKey.toBuffer(),
indexBuffer
])
}
@ -243,9 +243,9 @@ HDNode.prototype.derive = function(index) {
}
// Private parent key -> private child key
if (this.priv) {
if (this.privKey) {
// 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
if (ki.signum() === 0) {
@ -258,7 +258,7 @@ HDNode.prototype.derive = function(index) {
} else {
// Ki = point(parse256(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
if (Ki.isInfinity()) {

View file

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

View file

@ -18,15 +18,15 @@ describe('HDNode', function() {
it('calculates the publicKey from a BigInteger', function() {
var hd = new HDNode(D, chainCode)
assert(hd.pub.Q.equals(Q))
assert(hd.pubKey.Q.equals(Q))
})
it('only uses compressed points', function() {
var hd = new HDNode(Q, chainCode)
var hdP = new HDNode(D, chainCode)
assert.strictEqual(hd.pub.compressed, true)
assert.strictEqual(hdP.pub.compressed, true)
assert.strictEqual(hd.pubKey.compressed, true)
assert.strictEqual(hdP.pubKey.compressed, true)
})
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() {
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)
})
})
@ -209,8 +209,8 @@ describe('HDNode', function() {
describe('derive', function() {
function verifyVector(hd, v, depth) {
assert.equal(hd.priv.toWIF(), v.wif)
assert.equal(hd.pub.toHex(), v.pubKey)
assert.equal(hd.privKey.toWIF(), v.wif)
assert.equal(hd.pubKey.toHex(), v.pubKey)
assert.equal(hd.chainCode.toString('hex'), v.chainCode)
assert.equal(hd.depth, depth || 0)

View file

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