nimbus-eth2/beacon_chain/validator_keygen.nim

80 lines
2.5 KiB
Nim
Raw Normal View History

import
2019-07-31 09:39:46 +00:00
os, ospaths, strutils, strformat, json,
2019-07-12 14:24:11 +00:00
chronos, blscurve, nimcrypto, json_serialization, confutils, web3, stint,
2019-02-14 22:03:45 +00:00
spec/[datatypes, digest, crypto], conf, time, ssz,
../tests/testutil
2019-07-12 14:24:11 +00:00
contract(DepositContract):
proc deposit(pubkey: Bytes48, withdrawalCredentials: Bytes32, signature: Bytes96)
proc writeTextFile(filename: string, contents: string) =
writeFile(filename, contents)
echo "Wrote ", filename
proc writeFile(filename: string, value: auto) =
Json.saveFile(filename, value, pretty = true)
echo "Wrote ", filename
2019-07-12 14:24:11 +00:00
proc ethToWei(eth: UInt256): UInt256 =
eth * 1000000000000000000.u256
proc main(totalValidators: int, outputDir: string, generateFakeKeys: bool, depositWeb3Url, depositContractAddress: string) {.async.} =
var web3: Web3
var contractAddress: Address
var eth1Addresses: seq[Address]
if depositWeb3Url.len > 0:
2019-07-31 09:39:46 +00:00
web3 = await newWeb3(depositWeb3Url)
2019-07-12 14:24:11 +00:00
contractAddress = Address.fromHex(depositContractAddress)
eth1Addresses = await web3.provider.eth_accounts()
for i in 0 ..< totalValidators:
let
v = validatorFileBaseName(i)
depositFn = outputDir / v & ".deposit.json"
privKeyFn = outputDir / v & ".privkey"
if existsFile(depositFn) and existsFile(privKeyFn):
continue
let
privKey = if generateFakeKeys: makeFakeValidatorPrivKey(i)
else: ValidatorPrivKey.random
pubKey = privKey.pubKey()
let
withdrawalCredentials = makeFakeHash(i)
domain = 3'u64
var
2019-07-12 14:24:11 +00:00
dp = Deposit(
data: DepositData(
amount: MAX_EFFECTIVE_BALANCE,
pubkey: pubKey,
withdrawal_credentials: withdrawalCredentials))
2019-07-12 14:24:11 +00:00
dp.data.signature =
bls_sign(privkey, signing_root(dp.data).data,
domain)
writeTextFile(privKeyFn, $privKey)
2019-07-12 14:24:11 +00:00
writeFile(depositFn, dp)
if not web3.isNil:
2019-08-06 08:57:45 +00:00
web3.defaultAccount = eth1Addresses[i]
let depositContract = web3.contractSender(DepositContract, contractAddress)
let tx = await depositContract.deposit(Bytes48(pubKey.getBytes()), Bytes32(withdrawalCredentials.data), Bytes96(dp.data.signature.getBytes())).send(value = 32.u256.ethToWei)
if generateFakeKeys:
echo "Keys generated by this tool are only for testing!"
2019-07-12 14:24:11 +00:00
cli do (totalValidators: int = 125000,
outputDir: string = "validators",
generateFakeKeys = false,
depositWeb3Url: string = "",
depositContractAddress: string = ""):
waitFor main(totalValidators, outputDir, generateFakeKeys, depositWeb3Url, depositContractAddress)