more hd cleanup.

This commit is contained in:
Christopher Jeffrey 2016-02-23 19:58:11 -08:00
parent 3804c2ea74
commit b6e32ecaf8

View File

@ -160,7 +160,7 @@ function HDPrivateKey(options) {
this._build(data);
if (new bn(data.parentFingerPrint).cmpn(0) === 0)
if (utils.readU32BE(data.parentFingerPrint) === 0)
this.isMaster = true;
else
this.isMaster = false;
@ -203,7 +203,7 @@ HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback)
: root.derive(accountIndex, true);
if (isAccount)
accountIndex = new bn(self.childIndex).toNumber() - constants.hd.hardened;
accountIndex = utils.readU32BE(self.childIndex) - constants.hd.hardened;
// 2. derive the external chain node of this account
var chain = account.derive(chainConstant);
@ -462,15 +462,15 @@ HDPrivateKey.prototype.deriveCosignerAddress = function deriveCosignerAddress(co
};
HDPrivateKey.prototype.isPurpose45 = function isPurpose45(options) {
if (new bn(this.depth).toNumber() !== 1)
if (utils.readU8(this.depth) !== 1)
return false;
return new bn(this.childIndex).toNumber() === constants.hd.hardened + 45;
return utils.readU32BE(this.childIndex) === constants.hd.hardened + 45;
};
HDPrivateKey.prototype.isAccount44 = function isAccount44(options) {
if (new bn(this.childIndex).toNumber() < constants.hd.hardened)
if (utils.readU32BE(this.childIndex) < constants.hd.hardened)
return false;
return new bn(this.depth).toNumber() === 3;
return utils.readU8(this.depth) === 3;
};
HDPrivateKey.getPath = function getPath(options) {
@ -516,57 +516,31 @@ HDPrivateKey.prototype._normalize = function _normalize(data) {
}
// version = uint_32be
if (typeof data.version === 'string')
data.version = new Buffer(data.version, 'hex');
else if (typeof data.version === 'number')
if (typeof data.version === 'number')
data.version = array32(data.version);
// depth = unsigned char
if (typeof data.depth === 'string')
data.depth = new Buffer(data.depth, 'hex');
else if (typeof data.depth === 'number')
if (typeof data.depth === 'number')
data.depth = new Buffer([data.depth]);
if (new bn(data.depth).toNumber() > 0xff)
if (utils.readU8(data.depth) > 0xff)
throw new Error('Depth is too high');
// parent finger print = uint_32be
if (typeof data.parentFingerPrint === 'string')
data.parentFingerPrint = new Buffer(data.parentFingerPrint, 'hex');
else if (typeof data.parentFingerPrint === 'number')
if (typeof data.parentFingerPrint === 'number')
data.parentFingerPrint = array32(data.parentFingerPrint);
// child index = uint_32be
if (typeof data.childIndex === 'string')
data.childIndex = new Buffer(data.childIndex, 'hex');
else if (typeof data.childIndex === 'number')
if (typeof data.childIndex === 'number')
data.childIndex = array32(data.childIndex);
// chain code = 32 bytes
if (typeof data.chainCode === 'string')
data.chainCode = new Buffer(data.chainCode, 'hex');
// private key = 32 bytes
if (data.privateKey) {
if (data.privateKey.getPrivateKey)
data.privateKey = data.privateKey.getPrivateKey();
else if (typeof data.privateKey === 'string')
data.privateKey = utils.ensureBuffer(data.privateKey);
}
// public key = 33 bytes
if (data.publicKey) {
if (data.publicKey.getPublicKey)
data.publicKey = data.privateKey.getPublicKey();
else if (typeof data.publicKey === 'string')
data.publicKey = utils.ensureBuffer(data.publicKey);
}
// checksum = 4 bytes
if (typeof data.checksum === 'string')
data.checksum = new Buffer(data.checksum, 'hex');
else if (typeof data.checksum === 'number')
data.checksum = array32(data.checksum);
return data;
};
@ -589,9 +563,9 @@ HDPrivateKey.prototype._seed = function _seed(seed) {
return {
version: network[this.network].prefixes.xprivkey,
depth: 0,
parentFingerPrint: 0,
childIndex: 0,
depth: new Buffer([0]),
parentFingerPrint: new Buffer([0, 0, 0, 0]),
childIndex: new Buffer([0, 0, 0, 0]),
chainCode: hash.slice(32, 64),
privateKey: hash.slice(0, 32),
checksum: null
@ -612,9 +586,9 @@ HDPrivateKey._generate = function _generate(privateKey, entropy) {
return {
version: null,
depth: 0,
parentFingerPrint: 0,
childIndex: 0,
depth: new Buffer([0]),
parentFingerPrint: new Buffer([0, 0, 0, 0]),
childIndex: new Buffer([0, 0, 0, 0]),
chainCode: entropy,
privateKey: privateKey,
checksum: null
@ -761,7 +735,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
network: this.network,
data: {
version: this.version,
depth: new bn(this.depth).toNumber() + 1,
depth: utils.readU8(this.depth) + 1,
parentFingerPrint: this.fingerPrint,
childIndex: index,
chainCode: chainCode,
@ -964,7 +938,7 @@ function HDPublicKey(options) {
this._build(data);
if (new bn(data.parentFingerPrint).cmpn(0) === 0)
if (utils.readU32BE(data.parentFingerPrint) === 0)
this.isMaster = true;
else
this.isMaster = false;
@ -1118,7 +1092,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
network: this.network,
data: {
version: this.version,
depth: new bn(this.depth).toNumber() + 1,
depth: utils.readU8(this.depth) + 1,
parentFingerPrint: this.fingerPrint,
childIndex: index,
chainCode: chainCode,