Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fromSeed(seed) {
assert(Buffer.isBuffer(seed));
if (seed.length * 8 < common.MIN_ENTROPY
|| seed.length * 8 > common.MAX_ENTROPY) {
throw new Error('Entropy not in range.');
}
const hash = sha512.mac(seed, SEED_SALT);
const left = hash.slice(0, 32);
const right = hash.slice(32, 64);
// Only a 1 in 2^127 chance of happening.
if (!secp256k1.privateKeyVerify(left))
throw new Error('Master private key is invalid.');
this.depth = 0;
this.parentFingerPrint = 0;
this.childIndex = 0;
this.chainCode = right;
this.privateKey = left;
this.publicKey = secp256k1.publicKeyCreate(left, true);
return this;
}
fromSeed(seed) {
assert(Buffer.isBuffer(seed));
if (seed.length * 8 < common.MIN_ENTROPY
|| seed.length * 8 > common.MAX_ENTROPY) {
throw new Error('Entropy not in range.');
}
const hash = sha512.mac(seed, SEED_SALT);
const left = hash.slice(0, 32);
const right = hash.slice(32, 64);
// Only a 1 in 2^127 chance of happening.
if (!secp256k1.privateKeyVerify(left))
throw new Error('Master private key is invalid.');
this.depth = 0;
this.parentFingerPrint = 0;
this.childIndex = 0;
this.chainCode = right;
this.privateKey = left;
this.publicKey = secp256k1.publicKeyCreate(left, true);
return this;
}
return cache;
const bw = bio.pool(37);
if (index & common.HARDENED) {
bw.writeU8(0);
bw.writeBytes(this.privateKey);
bw.writeU32BE(index);
} else {
bw.writeBytes(this.publicKey);
bw.writeU32BE(index);
}
const data = bw.render();
const hash = sha512.mac(data, this.chainCode);
const left = hash.slice(0, 32);
const right = hash.slice(32, 64);
let key;
try {
key = secp256k1.privateKeyTweakAdd(this.privateKey, left);
} catch (e) {
return this.derive(index + 1);
}
if (this.fingerPrint === -1) {
const fp = hash160.digest(this.publicKey);
this.fingerPrint = fp.readUInt32BE(0, true);
}
const child = new this.constructor();
throw new Error('Depth too high.');
const id = this.getID(index);
const cache = common.cache.get(id);
if (cache)
return cache;
const bw = bio.pool(37);
bw.writeBytes(this.publicKey);
bw.writeU32BE(index);
const data = bw.render();
const hash = sha512.mac(data, this.chainCode);
const left = hash.slice(0, 32);
const right = hash.slice(32, 64);
let key;
try {
key = secp256k1.publicKeyTweakAdd(this.publicKey, left, true);
} catch (e) {
return this.derive(index + 1);
}
if (this.fingerPrint === -1) {
const fp = hash160.digest(this.publicKey);
this.fingerPrint = fp.readUInt32BE(0, true);
}
const child = new this.constructor();
throw new Error('Depth too high.');
const id = this.getID(index);
const cache = common.cache.get(id);
if (cache)
return cache;
const bw = bio.pool(37);
bw.writeBytes(this.publicKey);
bw.writeU32BE(index);
const data = bw.render();
const hash = sha512.mac(data, this.chainCode);
const left = hash.slice(0, 32);
const right = hash.slice(32, 64);
let key;
try {
key = secp256k1.publicKeyTweakAdd(this.publicKey, left, true);
} catch (e) {
return this.derive(index + 1);
}
if (this.fingerPrint === -1) {
const fp = hash160.digest(this.publicKey);
this.fingerPrint = fp.readUInt32BE(0, true);
}
const child = new this.constructor();
return cache;
const bw = bio.pool(37);
if (index & common.HARDENED) {
bw.writeU8(0);
bw.writeBytes(this.privateKey);
bw.writeU32BE(index);
} else {
bw.writeBytes(this.publicKey);
bw.writeU32BE(index);
}
const data = bw.render();
const hash = sha512.mac(data, this.chainCode);
const left = hash.slice(0, 32);
const right = hash.slice(32, 64);
let key;
try {
key = secp256k1.privateKeyTweakAdd(this.privateKey, left);
} catch (e) {
return this.derive(index + 1);
}
if (this.fingerPrint === -1) {
const fp = hash160.digest(this.publicKey);
this.fingerPrint = fp.readUInt32BE(0, true);
}
const child = new this.constructor();