2018-12-19 12:58:53 +00:00
|
|
|
import
|
2019-09-01 15:02:49 +00:00
|
|
|
os, ospaths, strutils, strformat,
|
2019-09-02 13:09:55 +00:00
|
|
|
chronicles, chronos, blscurve, nimcrypto, json_serialization, serialization,
|
|
|
|
web3, stint,
|
2019-09-01 15:02:49 +00:00
|
|
|
spec/[datatypes, digest, crypto], conf, time, ssz, interop
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-07-12 14:24:11 +00:00
|
|
|
contract(DepositContract):
|
|
|
|
proc deposit(pubkey: Bytes48, withdrawalCredentials: Bytes32, signature: Bytes96)
|
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
proc writeTextFile(filename: string, contents: string) =
|
|
|
|
writeFile(filename, contents)
|
|
|
|
echo "Wrote ", filename
|
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
proc writeFile(filename: string, value: auto) =
|
|
|
|
Json.saveFile(filename, value, pretty = true)
|
2018-12-28 16:51:40 +00:00
|
|
|
echo "Wrote ", filename
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-07-12 14:24:11 +00:00
|
|
|
proc ethToWei(eth: UInt256): UInt256 =
|
|
|
|
eth * 1000000000000000000.u256
|
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
proc generateDeposits*(
|
|
|
|
totalValidators: int, outputDir: string, randomKeys: bool): seq[Deposit] =
|
|
|
|
info "Generating deposits", totalValidators, outputDir, randomKeys
|
2019-03-27 12:06:06 +00:00
|
|
|
for i in 0 ..< totalValidators:
|
2019-03-07 13:59:28 +00:00
|
|
|
let
|
2019-03-18 03:54:08 +00:00
|
|
|
v = validatorFileBaseName(i)
|
|
|
|
depositFn = outputDir / v & ".deposit.json"
|
|
|
|
privKeyFn = outputDir / v & ".privkey"
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-07 13:59:28 +00:00
|
|
|
if existsFile(depositFn) and existsFile(privKeyFn):
|
2019-09-01 15:02:49 +00:00
|
|
|
try:
|
|
|
|
result.add Json.loadFile(depositFn, Deposit)
|
|
|
|
continue
|
2019-09-02 13:09:55 +00:00
|
|
|
except SerializationError as err:
|
|
|
|
debug "Rewriting unreadable deposit", err = err.formatMsg(depositFn)
|
2019-09-01 15:02:49 +00:00
|
|
|
discard
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-07 13:59:28 +00:00
|
|
|
let
|
2019-09-01 15:02:49 +00:00
|
|
|
privKey = if randomKeys: ValidatorPrivKey.random
|
|
|
|
else: makeInteropPrivKey(i)
|
2019-03-07 13:59:28 +00:00
|
|
|
pubKey = privKey.pubKey()
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
let dp = makeDeposit(pubKey, privKey)
|
2019-03-07 13:59:28 +00:00
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
writeTextFile(privKeyFn, $privKey)
|
2019-07-12 14:24:11 +00:00
|
|
|
writeFile(depositFn, dp)
|
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
result.add(dp)
|
2019-07-12 14:24:11 +00:00
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
proc sendDeposits*(
|
|
|
|
deposits: seq[Deposit],
|
|
|
|
depositWeb3Url, depositContractAddress: string) {.async.} =
|
|
|
|
let
|
|
|
|
web3 = await newWeb3(depositWeb3Url)
|
|
|
|
contractAddress = Address.fromHex(depositContractAddress)
|
|
|
|
eth1Addresses = await web3.provider.eth_accounts()
|
2019-07-12 14:24:11 +00:00
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
for i, dp in deposits:
|
|
|
|
web3.defaultAccount = eth1Addresses[i]
|
|
|
|
let depositContract = web3.contractSender(DepositContract, contractAddress)
|
|
|
|
let tx = await depositContract.deposit(
|
|
|
|
Bytes48(dp.data.pubKey.getBytes()),
|
|
|
|
Bytes32(dp.data.withdrawal_credentials.data),
|
|
|
|
Bytes96(dp.data.signature.getBytes())).send(value = 32.u256.ethToWei)
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
import confutils
|
|
|
|
|
|
|
|
cli do (totalValidators: int = 125000,
|
|
|
|
outputDir: string = "validators",
|
|
|
|
randomKeys: bool = false,
|
|
|
|
depositWeb3Url: string = "",
|
|
|
|
depositContractAddress: string = ""):
|
|
|
|
let deposits = generateDeposits(totalValidators, outputDir, randomKeys)
|
|
|
|
|
|
|
|
if depositWeb3Url.len() > 0 and depositContractAddress.len() > 0:
|
|
|
|
waitFor sendDeposits(deposits, depositWeb3Url, depositContractAddress)
|