more hd optimization.
This commit is contained in:
parent
ebee3965f7
commit
3804c2ea74
@ -662,25 +662,31 @@ HDPrivateKey.prototype._unbuild = function _unbuild(xkey) {
|
|||||||
else
|
else
|
||||||
this.network = 'testnet';
|
this.network = 'testnet';
|
||||||
|
|
||||||
|
this.xprivkey = xkey;
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
HDPrivateKey.prototype._build = function _build(data) {
|
HDPrivateKey.prototype._build = function _build(data) {
|
||||||
var sequence = new Buffer(82);
|
|
||||||
var off = 0;
|
var off = 0;
|
||||||
var checksum;
|
var sequence, checksum;
|
||||||
|
|
||||||
off += utils.copy(data.version, sequence, off);
|
if (!this.xprivkey) {
|
||||||
off += utils.copy(data.depth, sequence, off);
|
sequence = new Buffer(82);
|
||||||
off += utils.copy(data.parentFingerPrint, sequence, off);
|
off += utils.copy(data.version, sequence, off);
|
||||||
off += utils.copy(data.childIndex, sequence, off);
|
off += utils.copy(data.depth, sequence, off);
|
||||||
off += utils.copy(data.chainCode, sequence, off);
|
off += utils.copy(data.parentFingerPrint, sequence, off);
|
||||||
off += utils.writeU8(sequence, 0, off);
|
off += utils.copy(data.childIndex, sequence, off);
|
||||||
off += utils.copy(data.privateKey, sequence, off);
|
off += utils.copy(data.chainCode, sequence, off);
|
||||||
assert(off === 78, off);
|
off += utils.writeU8(sequence, 0, off);
|
||||||
checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4);
|
off += utils.copy(data.privateKey, sequence, off);
|
||||||
off += utils.copy(checksum, sequence, off);
|
assert(off === 78, off);
|
||||||
assert(off === 82, off);
|
checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4);
|
||||||
|
off += utils.copy(checksum, sequence, off);
|
||||||
|
assert(off === 82, off);
|
||||||
|
|
||||||
|
this.xprivkey = utils.toBase58(sequence);
|
||||||
|
}
|
||||||
|
|
||||||
this.version = data.version;
|
this.version = data.version;
|
||||||
this.depth = data.depth;
|
this.depth = data.depth;
|
||||||
@ -694,7 +700,6 @@ HDPrivateKey.prototype._build = function _build(data) {
|
|||||||
this.fingerPrint = null;
|
this.fingerPrint = null;
|
||||||
|
|
||||||
this.hdPrivateKey = this;
|
this.hdPrivateKey = this;
|
||||||
this.xprivkey = utils.toBase58(sequence);
|
|
||||||
|
|
||||||
this.hdPublicKey = new HDPublicKey({
|
this.hdPublicKey = new HDPublicKey({
|
||||||
network: this.network,
|
network: this.network,
|
||||||
@ -1026,29 +1031,35 @@ HDPublicKey.prototype._unbuild = function _unbuild(xkey) {
|
|||||||
else
|
else
|
||||||
this.network = 'testnet';
|
this.network = 'testnet';
|
||||||
|
|
||||||
|
this.xpubkey = xkey;
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
HDPublicKey.prototype._build = function _build(data) {
|
HDPublicKey.prototype._build = function _build(data) {
|
||||||
var sequence = new Buffer(82);
|
|
||||||
var off = 0;
|
var off = 0;
|
||||||
var checksum;
|
var sequence, checksum;
|
||||||
|
|
||||||
off += utils.copy(data.version, sequence, off);
|
if (!this.xpubkey) {
|
||||||
off += utils.copy(data.depth, sequence, off);
|
sequence = new Buffer(82);
|
||||||
off += utils.copy(data.parentFingerPrint, sequence, off);
|
off += utils.copy(data.version, sequence, off);
|
||||||
off += utils.copy(data.childIndex, sequence, off);
|
off += utils.copy(data.depth, sequence, off);
|
||||||
off += utils.copy(data.chainCode, sequence, off);
|
off += utils.copy(data.parentFingerPrint, sequence, off);
|
||||||
off += utils.copy(data.publicKey, sequence, off);
|
off += utils.copy(data.childIndex, sequence, off);
|
||||||
assert(off === 78, off);
|
off += utils.copy(data.chainCode, sequence, off);
|
||||||
checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4);
|
off += utils.copy(data.publicKey, sequence, off);
|
||||||
off += utils.copy(checksum, sequence, off);
|
assert(off === 78, off);
|
||||||
assert(off === 82, off);
|
checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4);
|
||||||
|
off += utils.copy(checksum, sequence, off);
|
||||||
|
assert(off === 82, off);
|
||||||
|
|
||||||
if (!data.checksum || !data.checksum.length)
|
if (!data.checksum || !data.checksum.length)
|
||||||
data.checksum = checksum;
|
data.checksum = checksum;
|
||||||
else if (utils.toHex(checksum) !== utils.toHex(data.checksum))
|
else if (utils.toHex(checksum) !== utils.toHex(data.checksum))
|
||||||
throw new Error('checksum mismatch');
|
throw new Error('checksum mismatch');
|
||||||
|
|
||||||
|
this.xpubkey = utils.toBase58(sequence);
|
||||||
|
}
|
||||||
|
|
||||||
this.version = data.version;
|
this.version = data.version;
|
||||||
this.depth = data.depth;
|
this.depth = data.depth;
|
||||||
@ -1062,7 +1073,6 @@ HDPublicKey.prototype._build = function _build(data) {
|
|||||||
this.fingerPrint = null;
|
this.fingerPrint = null;
|
||||||
|
|
||||||
this.hdPublicKey = this;
|
this.hdPublicKey = this;
|
||||||
this.xpubkey = utils.toBase58(sequence);
|
|
||||||
|
|
||||||
this.hdPrivateKey = null;
|
this.hdPrivateKey = null;
|
||||||
this.xprivkey = null;
|
this.xprivkey = null;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user