Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
data[0] = 0x00;
this.privateKey.copy(data, 1);
data.writeUInt32BE(index, 33);
// Normal child
}
else {
// data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index)
this.publicKey.copy(data, 0);
data.writeUInt32BE(index, 33);
}
const I = crypto.hmacSHA512(this.chainCode, data);
const IL = I.slice(0, 32);
const IR = I.slice(32);
// if parse256(IL) >= n, proceed with the next value for i
if (!ecc.isPrivate(IL))
return this.derive(index + 1);
// Private parent key -> private child key
let hd;
if (!this.isNeutered()) {
// ki = parse256(IL) + kpar (mod n)
const ki = ecc.privateAdd(this.privateKey, IL);
// In case ki == 0, proceed with the next value for i
if (ki == null)
return this.derive(index + 1);
hd = fromPrivateKeyLocal(ki, IR, this.network, this.depth + 1, index, this.fingerprint.readUInt32BE(0));
// Public parent key -> public child key
}
else {
// Ki = point(parse256(IL)) + Kpar
// = G*IL + Kpar
const Ki = ecc.pointAddScalar(this.publicKey, IL, true);
chainCode: Buffer,
network?: Network,
depth?: number,
index?: number,
parentFingerprint?: number,
): BIP32Interface {
typeforce(
{
privateKey: UINT256_TYPE,
chainCode: UINT256_TYPE,
},
{ privateKey, chainCode },
);
network = network || BITCOIN;
if (!ecc.isPrivate(privateKey))
throw new TypeError('Private key not in range [1, n)');
return new BIP32(
privateKey,
undefined,
chainCode,
network,
depth,
index,
parentFingerprint,
);
}
function makeRandom(options) {
typeforce(isOptions, options);
if (options === undefined) options = {};
const rng = options.rng || randomBytes;
let d;
do {
d = rng(32);
typeforce(types.Buffer256bit, d);
} while (!ecc.isPrivate(d));
return fromPrivateKey(d, options);
}
exports.makeRandom = makeRandom;
function fromPrivateKey(buffer: Buffer, options?: ECPairOptions): ECPair {
typeforce(types.Buffer256bit, buffer);
if (!ecc.isPrivate(buffer))
throw new TypeError('Private key not in range [1, n)');
typeforce(isOptions, options);
return new ECPair(buffer, undefined, options);
}
function fromPrivateKeyLocal(privateKey, chainCode, network, depth, index, parentFingerprint) {
typeforce({
privateKey: UINT256_TYPE,
chainCode: UINT256_TYPE,
}, { privateKey, chainCode });
network = network || BITCOIN;
if (!ecc.isPrivate(privateKey))
throw new TypeError('Private key not in range [1, n)');
return new BIP32(privateKey, undefined, chainCode, network, depth, index, parentFingerprint);
}
function fromPublicKey(publicKey, chainCode, network) {
function fromPrivateKey(buffer, options) {
typeforce(types.Buffer256bit, buffer);
if (!ecc.isPrivate(buffer))
throw new TypeError('Private key not in range [1, n)');
typeforce(isOptions, options);
return new ECPair(buffer, undefined, options);
}
exports.fromPrivateKey = fromPrivateKey;