staking-deposit-cli/eth2deposit/utils/validation.py

72 lines
2.4 KiB
Python
Raw Normal View History

2020-11-03 18:39:24 +00:00
import click
import json
2020-05-07 06:18:29 +00:00
from eth_typing import (
BLSPubkey,
BLSSignature,
)
from typing import Any, Dict
from py_ecc.bls import G2ProofOfPossession as bls
from eth2deposit.exceptions import ValidationError
from eth2deposit.utils.ssz import (
compute_deposit_domain,
compute_signing_root,
DepositData,
DepositMessage,
)
from eth2deposit.utils.constants import (
MAX_DEPOSIT_AMOUNT,
MIN_DEPOSIT_AMOUNT,
)
def verify_deposit_data_json(filefolder: str) -> bool:
"""
Validate every deposit found in the deposit-data JSON file folder.
"""
with open(filefolder, 'r') as f:
deposit_json = json.load(f)
2020-11-03 18:39:24 +00:00
with click.progressbar(deposit_json, label='Verifying your deposits:\t',
show_percent=False, show_pos=True) as deposits:
return all([validate_deposit(deposit) for deposit in deposits])
return False
2020-05-21 12:43:02 +00:00
def validate_deposit(deposit_data_dict: Dict[str, Any]) -> bool:
'''
Checks whether a deposit is valid based on the eth2 rules.
https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/beacon-chain.md#deposits
'''
2020-05-07 06:18:29 +00:00
pubkey = BLSPubkey(bytes.fromhex(deposit_data_dict['pubkey']))
withdrawal_credentials = bytes.fromhex(deposit_data_dict['withdrawal_credentials'])
amount = deposit_data_dict['amount']
2020-05-07 06:18:29 +00:00
signature = BLSSignature(bytes.fromhex(deposit_data_dict['signature']))
deposit_message_root = bytes.fromhex(deposit_data_dict['deposit_data_root'])
fork_version = bytes.fromhex(deposit_data_dict['fork_version'])
# Verify deposit amount
if not MIN_DEPOSIT_AMOUNT < amount <= MAX_DEPOSIT_AMOUNT:
return False
# Verify deposit signature && pubkey
deposit_message = DepositMessage(pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount)
domain = compute_deposit_domain(fork_version)
signing_root = compute_signing_root(deposit_message, domain)
if not bls.Verify(pubkey, signing_root, signature):
return False
# Verify Deposit Root
signed_deposit = DepositData(
2020-05-22 14:09:20 +00:00
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
amount=amount,
signature=signature,
)
return signed_deposit.hash_tree_root == deposit_message_root
def validate_password_strength(password: str) -> None:
if len(password) < 8:
2021-02-12 16:55:39 +00:00
raise ValidationError(f"The password length should be at least 8. Got {len(password)}. Please retype")