2018-12-19 12:58:53 +00:00
|
|
|
import
|
2020-06-01 19:48:20 +00:00
|
|
|
os, strutils, terminal,
|
2019-12-10 00:18:47 +00:00
|
|
|
chronicles, chronos, blscurve, nimcrypto, json_serialization, serialization,
|
2020-06-01 19:48:20 +00:00
|
|
|
web3, stint, eth/keys, confutils,
|
|
|
|
spec/[datatypes, digest, crypto, keystore], conf, ssz/merkleization, merkle_minimal
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-07-12 14:24:11 +00:00
|
|
|
contract(DepositContract):
|
2019-09-09 15:59:02 +00:00
|
|
|
proc deposit(pubkey: Bytes48, withdrawalCredentials: Bytes32, signature: Bytes96, deposit_data_root: FixedBytes[32])
|
2019-07-12 14:24:11 +00:00
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
const
|
|
|
|
keystoreFileName* = "keystore.json"
|
|
|
|
depositFileName* = "deposit.json"
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
type
|
|
|
|
DelayGenerator* = proc(): chronos.Duration {.closure, gcsafe.}
|
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
{.push raises: [Defect].}
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-07-12 14:24:11 +00:00
|
|
|
proc ethToWei(eth: UInt256): UInt256 =
|
|
|
|
eth * 1000000000000000000.u256
|
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
proc loadKeyStore(conf: BeaconNodeConf|ValidatorClientConf,
|
|
|
|
validatorsDir, keyName: string): Option[ValidatorPrivKey] =
|
|
|
|
let
|
|
|
|
keystorePath = validatorsDir / keyName / keystoreFileName
|
|
|
|
keystoreContents = KeyStoreContent:
|
|
|
|
try: readFile(keystorePath)
|
|
|
|
except IOError as err:
|
|
|
|
error "Failed to read keystore", err = err.msg, path = keystorePath
|
|
|
|
return
|
|
|
|
|
|
|
|
if conf.secretsDir.isSome:
|
|
|
|
let passphrasePath = conf.secretsDir.get / keyName
|
|
|
|
if fileExists(passphrasePath):
|
|
|
|
let
|
|
|
|
passphrase = KeyStorePass:
|
|
|
|
try: readFile(passphrasePath)
|
|
|
|
except IOError as err:
|
|
|
|
error "Failed to read passphrase file", err = err.msg, path = passphrasePath
|
|
|
|
return
|
|
|
|
|
|
|
|
let res = decryptKeystore(keystoreContents, passphrase)
|
|
|
|
if res.isOk:
|
|
|
|
return res.get.some
|
|
|
|
else:
|
|
|
|
error "Failed to decrypt keystore", keystorePath, passphrasePath
|
|
|
|
return
|
|
|
|
|
|
|
|
if conf.nonInteractive:
|
|
|
|
error "Unable to load validator key store. Please ensure matching passphrase exists in the secrets dir",
|
|
|
|
keyName, validatorsDir, secretsDir = conf.secretsDir
|
|
|
|
return
|
|
|
|
|
|
|
|
var remainingAttempts = 3
|
2020-06-02 19:59:51 +00:00
|
|
|
var prompt = "Please enter passphrase for key \"" & validatorsDir/keyName & "\"\n"
|
2020-06-01 19:48:20 +00:00
|
|
|
while remainingAttempts > 0:
|
|
|
|
let passphrase = KeyStorePass:
|
|
|
|
try: readPasswordFromStdin(prompt)
|
|
|
|
except IOError:
|
|
|
|
error "STDIN not readable. Cannot obtain KeyStore password"
|
|
|
|
return
|
|
|
|
|
|
|
|
let decrypted = decryptKeystore(keystoreContents, passphrase)
|
|
|
|
if decrypted.isOk:
|
|
|
|
return decrypted.get.some
|
|
|
|
else:
|
|
|
|
prompt = "Keystore decryption failed. Please try again"
|
|
|
|
dec remainingAttempts
|
|
|
|
|
|
|
|
iterator validatorKeys*(conf: BeaconNodeConf|ValidatorClientConf): ValidatorPrivKey =
|
|
|
|
for validatorKeyFile in conf.validators:
|
|
|
|
try:
|
|
|
|
yield validatorKeyFile.load
|
|
|
|
except CatchableError as err:
|
|
|
|
error "Failed to load validator private key",
|
|
|
|
file = validatorKeyFile.string, err = err.msg
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
let validatorsDir = conf.localValidatorsDir
|
|
|
|
try:
|
|
|
|
for kind, file in walkDir(validatorsDir):
|
|
|
|
if kind == pcDir:
|
|
|
|
let keyName = splitFile(file).name
|
|
|
|
let key = loadKeyStore(conf, validatorsDir, keyName)
|
|
|
|
if key.isSome:
|
|
|
|
yield key.get
|
|
|
|
else:
|
|
|
|
quit 1
|
|
|
|
except OSError as err:
|
|
|
|
error "Validator keystores directory not accessible",
|
|
|
|
path = validatorsDir, err = err.msg
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
type
|
|
|
|
GenerateDepositsError = enum
|
|
|
|
RandomSourceDepleted,
|
|
|
|
FailedToCreateValidatoDir
|
|
|
|
FailedToCreateSecretFile
|
|
|
|
FailedToCreateKeystoreFile
|
|
|
|
FailedToCreateDepositFile
|
|
|
|
|
2019-10-29 02:43:23 +00:00
|
|
|
proc generateDeposits*(totalValidators: int,
|
2020-06-01 19:48:20 +00:00
|
|
|
validatorsDir: string,
|
|
|
|
secretsDir: string): Result[seq[Deposit], GenerateDepositsError] =
|
|
|
|
var deposits: seq[Deposit]
|
|
|
|
|
|
|
|
info "Generating deposits", totalValidators, validatorsDir, secretsDir
|
2019-03-27 12:06:06 +00:00
|
|
|
for i in 0 ..< totalValidators:
|
2020-06-01 19:48:20 +00:00
|
|
|
let password = KeyStorePass getRandomBytesOrPanic(32).toHex
|
|
|
|
let credentials = generateCredentials(password = password)
|
|
|
|
|
2019-03-07 13:59:28 +00:00
|
|
|
let
|
2020-06-01 19:48:20 +00:00
|
|
|
keyName = $(credentials.signingKey.toPubKey)
|
|
|
|
validatorDir = validatorsDir / keyName
|
|
|
|
passphraseFile = secretsDir / keyName
|
|
|
|
depositFile = validatorDir / depositFileName
|
|
|
|
keystoreFile = validatorDir / keystoreFileName
|
|
|
|
|
|
|
|
if existsDir(validatorDir) and existsFile(depositFile):
|
|
|
|
continue
|
|
|
|
|
|
|
|
try: createDir validatorDir
|
|
|
|
except OSError, IOError: return err FailedToCreateValidatoDir
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
try: writeFile(secretsDir / keyName, password.string)
|
|
|
|
except IOError: return err FailedToCreateSecretFile
|
2019-03-07 13:59:28 +00:00
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
try: writeFile(keystoreFile, credentials.keyStore.string)
|
|
|
|
except IOError: return err FailedToCreateKeystoreFile
|
|
|
|
|
|
|
|
deposits.add credentials.prepareDeposit()
|
2019-07-12 14:24:11 +00:00
|
|
|
|
2020-04-15 07:59:47 +00:00
|
|
|
# Does quadratic additional work, but fast enough, and otherwise more
|
|
|
|
# cleanly allows free intermixing of pre-existing and newly generated
|
|
|
|
# deposit and private key files. TODO: only generate new Merkle proof
|
|
|
|
# for the most recent deposit if this becomes bottleneck.
|
2020-06-01 19:48:20 +00:00
|
|
|
attachMerkleProofs(deposits)
|
|
|
|
try: Json.saveFile(depositFile, deposits[^1], pretty = true)
|
|
|
|
except: return err FailedToCreateDepositFile
|
2020-04-15 07:59:47 +00:00
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
ok deposits
|
2020-04-15 07:59:47 +00:00
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
{.pop.}
|
2019-12-03 12:10:47 +00:00
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
proc sendDeposits*(deposits: seq[Deposit],
|
|
|
|
web3Url, depositContractAddress, privateKey: string,
|
|
|
|
delayGenerator: DelayGenerator = nil) {.async.} =
|
2020-03-24 11:13:07 +00:00
|
|
|
var web3 = await newWeb3(web3Url)
|
2019-11-05 18:16:10 +00:00
|
|
|
if privateKey.len != 0:
|
2020-06-01 19:48:20 +00:00
|
|
|
web3.privateKey = PrivateKey.fromHex(privateKey).tryGet
|
2019-12-03 12:10:47 +00:00
|
|
|
else:
|
|
|
|
let accounts = await web3.provider.eth_accounts()
|
|
|
|
if accounts.len == 0:
|
2020-03-24 11:13:07 +00:00
|
|
|
error "No account offered by the web3 provider", web3Url
|
2019-12-03 12:10:47 +00:00
|
|
|
return
|
|
|
|
web3.defaultAccount = accounts[0]
|
2019-12-02 23:27:59 +00:00
|
|
|
|
|
|
|
let contractAddress = Address.fromHex(depositContractAddress)
|
2020-06-01 19:48:20 +00:00
|
|
|
let depositContract = web3.contractSender(DepositContract, contractAddress)
|
2019-12-02 23:27:59 +00:00
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
for i, dp in deposits:
|
2019-11-18 12:48:41 +00:00
|
|
|
discard await depositContract.deposit(
|
2020-04-11 08:51:07 +00:00
|
|
|
Bytes48(dp.data.pubKey.toRaw()),
|
2019-09-01 15:02:49 +00:00
|
|
|
Bytes32(dp.data.withdrawal_credentials.data),
|
2020-04-11 08:51:07 +00:00
|
|
|
Bytes96(dp.data.signature.toRaw()),
|
2019-11-05 18:16:10 +00:00
|
|
|
FixedBytes[32](hash_tree_root(dp.data).data)).send(value = 32.u256.ethToWei, gasPrice = 1)
|
2019-09-01 15:02:49 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
if delayGenerator != nil:
|
|
|
|
await sleepAsync(delayGenerator())
|
|
|
|
|