Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* istanbul ignore next */
throw new InvalidFormatError('Invalid NEP2 format when decoded from encryptedKey.');
}
const salt = decoded.slice(3, 7);
const derived = await getNEP2Derived({ password, salt });
const derived1 = derived.slice(0, 32);
const derived2 = derived.slice(32, 64);
const decipher = createDecipheriv(NEP2_CIPHER, derived2, Buffer.alloc(0, 0));
decipher.setAutoPadding(false);
decipher.end(decoded.slice(7, 7 + 32));
const plainText = decipher.read() as Buffer;
const privateKey = common.asPrivateKey(xor(plainText, derived1));
const addressSalt = getNEP2Salt({ addressVersion, privateKey });
if (!salt.equals(addressSalt)) {
/* istanbul ignore next */
throw new Error('Wrong passphrase.');
}
return common.bufferToPrivateKey(privateKey);
};
getDomain(state.fork, currentEpoch, Domain.PROPOSAL),
);
assert(blockSignatureVerified);
// RANDAO
const proposer = state.validatorRegistry[getBeaconProposerIndex(state, state.slot)];
const randaoRevealVerified = blsVerify(
proposer.pubkey,
intToBytes(currentEpoch, 32),
block.randaoReveal,
getDomain(state.fork, currentEpoch, Domain.PROPOSAL),
);
assert(randaoRevealVerified);
state.latestRandaoMixes[currentEpoch.modn(LATEST_RANDAO_MIXES_LENGTH)] =
xor(getRandaoMix(state, currentEpoch), hash(block.randaoReveal));
// Eth1 data
let eth1DataVote: Eth1DataVote = state.eth1DataVotes.find((vote) =>
vote.eth1Data.blockHash.equals(block.eth1Data.blockHash) &&
vote.eth1Data.depositRoot.equals(block.eth1Data.depositRoot));
if (eth1DataVote) {
eth1DataVote.voteCount = eth1DataVote.voteCount.addn(1);
} else {
state.eth1DataVotes.push({
eth1Data: block.eth1Data,
voteCount: new BN(1),
});
}
// Transactions
config: IBeaconConfig,
state: BeaconState,
body: BeaconBlockBody
): void {
const currentEpoch = getCurrentEpoch(config, state);
const proposer = state.validators[getBeaconProposerIndex(config, state)];
// Verify RANDAO reveal
assert(bls.verify(
proposer.pubkey,
hashTreeRoot(currentEpoch, config.types.Epoch),
body.randaoReveal,
getDomain(config, state, DomainType.RANDAO),
));
// Mix it in
state.randaoMixes[currentEpoch % config.params.EPOCHS_PER_HISTORICAL_VECTOR] =
xor(getRandaoMix(config, state, currentEpoch), hash(body.randaoReveal));
}
privateKey,
}: {
readonly addressVersion: number;
readonly password: string;
readonly privateKey: PrivateKey;
}): Promise => {
const salt = getNEP2Salt({ addressVersion, privateKey });
const derived = await getNEP2Derived({ password, salt });
const derived1 = derived.slice(0, 32);
const derived2 = derived.slice(32, 64);
const cipher = createCipheriv(NEP2_CIPHER, derived2, Buffer.alloc(0, 0));
cipher.setAutoPadding(false);
cipher.end(xor(privateKey, derived1));
const ciphertext = cipher.read() as Buffer;
const result = Buffer.alloc(7 + 32, 0);
result.writeUInt8(NEP2_ZERO, 0);
result.writeUInt8(NEP2_ONE, 1);
result.writeUInt8(NEP2_TWO, 2);
salt.copy(result, 3);
ciphertext.copy(result, 7);
return base58CheckEncode(result);
};