nimbus-eth2/beacon_chain/validator_keygen.nim

71 lines
2.2 KiB
Nim
Raw Normal View History

import
os, ospaths, strutils, strformat,
asyncdispatch2, milagro_crypto, nimcrypto, json_serialization, confutils,
spec/[datatypes, digest, crypto], conf, randao, time, ssz,
../tests/testutil
proc writeFile(filename: string, value: auto) =
Json.saveFile(filename, value, pretty = true)
echo "Wrote ", filename
proc genSingleValidator(path: string): (ValidatorPubKey,
ValidatorPrivKey,
Eth2Digest) =
var v: PrivateValidatorData
v.privKey = newSigKey()
if randomBytes(v.randao.seed.data) != sizeof(v.randao.seed.data):
2018-12-08 14:17:47 +00:00
raise newException(Exception, "Could not generate randao seed")
writeFile(path, v)
return (v.privKey.pubKey(), v.privKey, v.randao.initialCommitment)
2018-12-08 14:17:47 +00:00
# TODO: Make these more comprehensive and find them a new home
type
Ether* = distinct int64
GWei* = distinct int64
template eth*(x: SomeInteger): Ether = Ether(x)
template gwei*(x: Ether): Gwei = Gwei(int(x) * 1000000000)
2018-12-08 14:17:47 +00:00
cli do (validators: int,
outputDir: string,
startupDelay = 0):
2018-12-08 14:17:47 +00:00
if validators < 64:
echo "The number of validators must be higher than ", EPOCH_LENGTH, " (EPOCH_LENGTH)"
echo "There must be at least one validator assigned per slot."
quit 1
var startupData: ChainStartupData
for i in 1 .. validators:
let (pubKey, privKey, randaoCommitment) =
genSingleValidator(outputDir / &"validator-{i:02}.json")
let
withdrawalCredentials = makeFakeHash(i)
proofOfPossessionData = DepositInput(
pubkey: pubKey,
withdrawal_credentials: withdrawalCredentials,
randao_commitment: randaoCommitment)
proofOfPossession = signMessage(
privkey, hash_tree_root_final(proofOfPossessionData).data)
startupData.validatorDeposits.add Deposit(
deposit_data: DepositData(
amount: MAX_DEPOSIT_AMOUNT,
timestamp: now(),
deposit_input: DepositInput(
pubkey: pubKey,
proof_of_possession: proofOfPossession,
withdrawal_credentials: withdrawalCredentials,
randao_commitment: randaoCommitment)))
startupData.genesisTime = uint64(int(now() div 1000) + startupDelay)
2018-12-08 14:17:47 +00:00
writeFile(outputDir / "startup.json", startupData)
2018-12-08 14:17:47 +00:00