Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!key) {
try {
const data = fs.readFileSync(filename, 'utf8');
key = Buffer.from(data.trim(), 'hex');
} catch (e) {
if (e.code !== 'ENOENT')
throw e;
key = this.genKey();
fresh = true;
}
} else {
fresh = true;
}
if (key.length !== 32 || !secp256k1.privateKeyVerify(key))
throw new Error('Invalid identity key.');
if (fresh) {
// XXX Shouldn't be doing this.
fs.mkdirpSync(this.config.prefix);
fs.writeFileSync(filename, key.toString('hex') + '\n');
}
return key;
}
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;
}
let joinPrivKey;
if (options.joinPrivKey != null) {
assert(Buffer.isBuffer(options.joinPrivKey),
'joinPrivKey must be a buffer.');
assert(secp256k1.privateKeyVerify(options.joinPrivKey),
'joinPrivKey is not a private key.');
joinPrivKey = options.joinPrivKey;
} else {
joinPrivKey = secp256k1.privateKeyGenerate();
}
let authPrivKey;
if (options.authPrivKey != null) {
assert(Buffer.isBuffer(options.authPrivKey),
'authPrivKey must be a buffer.');
assert(secp256k1.privateKeyVerify(options.authPrivKey),
'authPrivKey is not a private key.');
authPrivKey = options.authPrivKey;
} else {
authPrivKey = secp256k1.privateKeyGenerate();
}
this.joinPrivKey = joinPrivKey;
this.joinPubKey = secp256k1.publicKeyCreate(this.joinPrivKey, true);
this.authPrivKey = authPrivKey;
this.authPubKey = secp256k1.publicKeyCreate(this.authPrivKey, true);
this.master = master;
this.fingerPrint = getFingerprint(master);
this.accountPrivKey = this.master.deriveAccount(44, this.purpose, 0);
this.network = Network.get(options.network);
let master;
if (options.master != null) {
assert(hd.PrivateKey.isHDPrivateKey(options.master),
'Bad master key.');
master = options.master;
} else {
master = hd.generate();
}
let joinPrivKey;
if (options.joinPrivKey != null) {
assert(Buffer.isBuffer(options.joinPrivKey),
'joinPrivKey must be a buffer.');
assert(secp256k1.privateKeyVerify(options.joinPrivKey),
'joinPrivKey is not a private key.');
joinPrivKey = options.joinPrivKey;
} else {
joinPrivKey = secp256k1.privateKeyGenerate();
}
let authPrivKey;
if (options.authPrivKey != null) {
assert(Buffer.isBuffer(options.authPrivKey),
'authPrivKey must be a buffer.');
assert(secp256k1.privateKeyVerify(options.authPrivKey),
'authPrivKey is not a private key.');
authPrivKey = options.authPrivKey;
} else {
authPrivKey = secp256k1.privateKeyGenerate();
if (options.publicHost != null) {
assert(typeof options.publicHost === 'string');
this.address.setHost(options.publicHost);
}
if (options.publicPort != null) {
assert(typeof options.publicPort === 'number');
assert(options.publicPort > 0 && options.publicPort <= 0xffff);
this.address.setPort(options.publicPort);
}
if (options.identityKey) {
assert(Buffer.isBuffer(options.identityKey),
'Identity key must be a buffer.');
assert(secp256k1.privateKeyVerify(options.identityKey),
'Invalid identity key.');
this.address.setKey(secp256k1.publicKeyCreate(options.identityKey));
}
if (options.services != null) {
assert(typeof options.services === 'number');
this.services = options.services;
}
if (options.onion != null) {
assert(typeof options.onion === 'boolean');
this.onion = options.onion;
}
if (options.maxBuckets != null) {
assert(typeof options.maxBuckets === 'number');
if (options.version) {
assert(typeof options.version === 'number');
this.version = options.version;
}
if (options.agent) {
assert(typeof options.agent === 'string');
assert(options.agent.length <= 255);
this.agent = options.agent;
}
if (options.identityKey) {
assert(Buffer.isBuffer(options.identityKey),
'Identity key must be a buffer.');
assert(secp256k1.privateKeyVerify(options.identityKey),
'Invalid identity key.');
this.identityKey = options.identityKey;
}
if (options.banScore != null) {
assert(typeof this.options.banScore === 'number');
this.banScore = this.options.banScore;
}
if (options.banTime != null) {
assert(typeof this.options.banTime === 'number');
this.banTime = this.options.banTime;
}
if (options.feeRate != null) {
assert(typeof this.options.feeRate === 'number');