Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add amount parameter #326

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ You can use `new-mnemonic --help` to see all arguments. Note that if there are m
| -------- | -------- | -------- |
| `--num_validators` | Non-negative integer | The number of signing keys you want to generate. Note that the child key(s) are generated via the same master key. |
| `--mnemonic_language` | String. Options: `简体中文`, `繁體中文`, `český jazyk`, `English`, `Italiano`, `한국어`, `Português`, `Español`. Default to `English` | The mnemonic language |
| `--amount` | Non-negative integer between 1-32. 32 by default. | The ether amount to sign deposits per validator. |
| `--folder` | String. Pointing to `./validator_keys` by default | The folder path for the keystore(s) and deposit(s) |
| `--chain` | String. `mainnet` by default | The chain setting for the signing domain. |
| `--eth1_withdrawal_address` | String. Eth1 address in hexadecimal encoded form | If this field is set and valid, the given Eth1 address will be used to create the withdrawal credentials. Otherwise, it will generate withdrawal credentials with the mnemonic-derived withdrawal public key in [EIP-2334 format](https://eips.ethereum.org/EIPS/eip-2334#eth2-specific-parameters). |
Expand All @@ -149,6 +150,7 @@ You can use `existing-mnemonic --help` to see all arguments. Note that if there
| -------- | -------- | -------- |
| `--validator_start_index` | Non-negative integer | The index of the first validator's keys you wish to generate. If this is your first time generating keys with this mnemonic, use 0. If you have generated keys using this mnemonic before, use the next index from which you want to start generating keys from (eg, if you've generated 4 keys before (keys #0, #1, #2, #3), then enter 4 here.|
| `--num_validators` | Non-negative integer | The number of new signing keys you want to generate. Note that the child key(s) are generated via the same master key. |
| `--amount` | Non-negative integer between 1-32. 32 by default. | The ether amount to sign deposits per validator. |
| `--folder` | String. Pointing to `./validator_keys` by default | The folder path for the keystore(s) and deposit(s) |
| `--chain` | String. `mainnet` by default | The chain setting for the signing domain. |
| `--eth1_withdrawal_address` | String. Eth1 address in hexadecimal encoded form | If this field is set and valid, the given Eth1 address will be used to create the withdrawal credentials. Otherwise, it will generate withdrawal credentials with the mnemonic-derived withdrawal public key in [EIP-2334 format](https://eips.ethereum.org/EIPS/eip-2334#eth2-specific-parameters). |
Expand Down Expand Up @@ -210,11 +212,11 @@ or
You can also run the tool with optional arguments:

```sh
./deposit.sh new-mnemonic --num_validators=<NUM_VALIDATORS> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
./deposit.sh new-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

```sh
./deposit.sh existing-mnemonic --num_validators=<NUM_VALIDATORS> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
./deposit.sh existing-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

###### Language Argument
Expand Down Expand Up @@ -276,11 +278,11 @@ python3 ./staking_deposit/deposit.py existing-mnemonic
You can also run the tool with optional arguments:

```sh
python3 ./staking_deposit/deposit.py new-mnemonic --num_validators=<NUM_VALIDATORS> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
python3 ./staking_deposit/deposit.py new-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

```sh
python3 ./staking_deposit/deposit.py existing-mnemonic --num_validators=<NUM_VALIDATORS> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
python3 ./staking_deposit/deposit.py existing-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

###### Language Argument
Expand Down Expand Up @@ -359,11 +361,11 @@ deposit.exe existing-mnemonic
You can also run the tool with optional arguments:

```sh
deposit.exe new-mnemonic --num_validators=<NUM_VALIDATORS> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
deposit.exe new-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

```sh
deposit.exe existing-mnemonic --num_validators=<NUM_VALIDATORS> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
deposit.exe existing-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

###### Language Argument
Expand Down Expand Up @@ -421,11 +423,11 @@ or
You can also run the tool with optional arguments:

```sh
./deposit.sh new-mnemonic --num_validators=<NUM_VALIDATORS> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
./deposit.sh new-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

```sh
./deposit.sh existing-mnemonic --num_validators=<NUM_VALIDATORS> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
./deposit.sh existing-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

###### Language Argument
Expand Down Expand Up @@ -485,11 +487,11 @@ python .\staking_deposit\deposit.py existing-mnemonic
You can also run the tool with optional arguments:

```cmd
python .\staking_deposit\deposit.py new-mnemonic --num_validators=<NUM_VALIDATORS> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
python .\staking_deposit\deposit.py new-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --mnemonic_language=english --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

```cmd
python .\staking_deposit\deposit.pyexisting-mnemonic --num_validators=<NUM_VALIDATORS> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
python .\staking_deposit\deposit.pyexisting-mnemonic --num_validators=<NUM_VALIDATORS> --amount=<AMOUNT> --validator_start_index=<START_INDEX> --chain=<CHAIN_NAME> --folder=<YOUR_FOLDER_PATH>
```

###### Language Argument
Expand Down
15 changes: 13 additions & 2 deletions staking_deposit/cli/generate_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
verify_deposit_data_json,
validate_int_range,
validate_password_strength,
validate_ether_amount_range
)
from staking_deposit.utils.constants import (
ETH2GWEI,
MAX_DEPOSIT_AMOUNT,
DEFAULT_VALIDATOR_KEYS_FOLDER_NAME,
)
Expand Down Expand Up @@ -69,6 +71,15 @@ def generate_keys_arguments_decorator(function: Callable[..., Any]) -> Callable[
param_decls="--num_validators",
prompt=lambda: load_text(['num_validators', 'prompt'], func='generate_keys_arguments_decorator'),
),
jit_option(
callback=captive_prompt_callback(
lambda num: validate_ether_amount_range(num),
lambda: load_text(['num_validators', 'prompt'], func='generate_keys_arguments_decorator')
),
default=MAX_DEPOSIT_AMOUNT // ETH2GWEI,
help=lambda: load_text(['amount', 'help'], func='generate_keys_arguments_decorator'),
param_decls="--amount",
),
jit_option(
default=os.getcwd(),
help=lambda: load_text(['folder', 'help'], func='generate_keys_arguments_decorator'),
Expand Down Expand Up @@ -120,11 +131,11 @@ def generate_keys_arguments_decorator(function: Callable[..., Any]) -> Callable[
@click.command()
@click.pass_context
def generate_keys(ctx: click.Context, validator_start_index: int,
num_validators: int, folder: str, chain: str, keystore_password: str,
num_validators: int, amount: int, folder: str, chain: str, keystore_password: str,
eth1_withdrawal_address: HexAddress, **kwargs: Any) -> None:
mnemonic = ctx.obj['mnemonic']
mnemonic_password = ctx.obj['mnemonic_password']
amounts = [MAX_DEPOSIT_AMOUNT] * num_validators
amounts = [amount * ETH2GWEI] * num_validators
folder = os.path.join(folder, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
chain_setting = get_chain_setting(chain)
if not os.path.exists(folder):
Expand Down
63 changes: 33 additions & 30 deletions staking_deposit/intl/en/cli/generate_keys.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
{
"validate_eth1_withdrawal_address": {
"err_invalid_ECDSA_hex_addr": "The given Eth1 address is not in hexadecimal encoded form.",
"msg_ECDSA_addr_withdrawal": "**[Warning] you are setting an Eth1 address as your withdrawal address. Please ensure that you have control over this address.**"
"validate_eth1_withdrawal_address": {
"err_invalid_ECDSA_hex_addr": "The given Eth1 address is not in hexadecimal encoded form.",
"msg_ECDSA_addr_withdrawal": "**[Warning] you are setting an Eth1 address as your withdrawal address. Please ensure that you have control over this address.**"
},
"generate_keys_arguments_decorator": {
"num_validators": {
"help": "The number of new validator keys you want to generate (you can always generate more later)",
"prompt": "Please choose how many new validators you wish to run"
},
"generate_keys_arguments_decorator": {
"num_validators": {
"help": "The number of new validator keys you want to generate (you can always generate more later)",
"prompt": "Please choose how many new validators you wish to run"
},
"folder": {
"help": "The folder path for the keystore(s) and deposit(s). Pointing to `./validator_keys` by default."
},
"chain": {
"help": "The name of Ethereum PoS chain you are targeting. Use \"mainnet\" if you are depositing ETH",
"prompt": "Please choose the (mainnet or testnet) network/chain name"
},
"keystore_password": {
"help": "The password that will secure your keystores. You will need to re-enter this to decrypt them when you setup your Ethereum validators. (It is recommended not to use this argument, and wait for the CLI to ask you for your mnemonic as otherwise it will appear in your shell history.)",
"prompt": "Create a password that secures your validator keystore(s). You will need to re-enter this to decrypt them when you setup your Ethereum validators.",
"confirm": "Repeat your keystore password for confirmation",
"mismatch": "Error: the two entered values do not match. Please type again."
},
"eth1_withdrawal_address": {
"help": "If this field is set and valid, the given Eth1 address will be used to create the withdrawal credentials. Otherwise, it will generate withdrawal credentials with the mnemonic-derived withdrawal public key."
}
"amount": {
"help": "Ether amount you want to sign between 1-32"
},
"generate_keys": {
"msg_key_creation": "Creating your keys.",
"msg_creation_success": "\nSuccess!\nYour keys can be found at: ",
"msg_pause": "\n\nPress any key.",
"err_verify_keystores": "Failed to verify the keystores.",
"err_verify_deposit": "Failed to verify the deposit data JSON files."
"folder": {
"help": "The folder path for the keystore(s) and deposit(s). Pointing to `./validator_keys` by default."
},
"chain": {
"help": "The name of Ethereum PoS chain you are targeting. Use \"mainnet\" if you are depositing ETH",
"prompt": "Please choose the (mainnet or testnet) network/chain name"
},
"keystore_password": {
"help": "The password that will secure your keystores. You will need to re-enter this to decrypt them when you setup your Ethereum validators. (It is recommended not to use this argument, and wait for the CLI to ask you for your mnemonic as otherwise it will appear in your shell history.)",
"prompt": "Create a password that secures your validator keystore(s). You will need to re-enter this to decrypt them when you setup your Ethereum validators.",
"confirm": "Repeat your keystore password for confirmation",
"mismatch": "Error: the two entered values do not match. Please type again."
},
"eth1_withdrawal_address": {
"help": "If this field is set and valid, the given Eth1 address will be used to create the withdrawal credentials. Otherwise, it will generate withdrawal credentials with the mnemonic-derived withdrawal public key."
}
},
"generate_keys": {
"msg_key_creation": "Creating your keys.",
"msg_creation_success": "\nSuccess!\nYour keys can be found at: ",
"msg_pause": "\n\nPress any key.",
"err_verify_keystores": "Failed to verify the keystores.",
"err_verify_deposit": "Failed to verify the deposit data JSON files."
}
}
29 changes: 16 additions & 13 deletions staking_deposit/intl/en/utils/validation.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"verify_deposit_data_json": {
"msg_deposit_verification": "Verifying your deposits:\t"
},
"validate_password_strength": {
"msg_password_length": "The password length should be at least 8. Please retype"
},
"validate_int_range": {
"err_not_positive_integer": "That is not a positive integer. Please retype."
},
"validate_choice": {
"err_invalid_choice": "That is not one of the valid choices. Please retype your choice."
}
}
"verify_deposit_data_json": {
"msg_deposit_verification": "Verifying your deposits:\t"
},
"validate_password_strength": {
"msg_password_length": "The password length should be at least 8. Please retype"
},
"validate_int_range": {
"err_not_positive_integer": "That is not a positive integer. Please retype."
},
"validate_ether_amount_range": {
"err_not_within_amount_limits": "That is not a positive integer between 1-32 Please retype."
},
"validate_choice": {
"err_invalid_choice": "That is not one of the valid choices. Please retype your choice."
}
}
13 changes: 13 additions & 0 deletions staking_deposit/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,16 @@ def validate_int_range(num: Any, low: int, high: int) -> int:
return num_int
except (ValueError, AssertionError):
raise ValidationError(load_text(['err_not_positive_integer']))


def validate_ether_amount_range(num: Any) -> int:
'''
Verifies that `num` is an `int` and 1 <= num <= 32
'''
try:
num_int = int(num) # Try cast to int
assert num_int == float(num) # Check num is not float
assert 1 <= num_int <= 32 # Check num in range
return num_int
except (ValueError, AssertionError):
raise ValidationError(load_text(['err_not_within_amount_limits']))
2 changes: 2 additions & 0 deletions tests/test_cli/test_existing_menmonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async def test_script() -> None:
'--non_interactive',
'existing-mnemonic',
'--num_validators', '1',
'--amount', '32',
'--mnemonic="abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"',
'--mnemonic-password', 'TREZOR',
'--validator_start_index', '1',
Expand Down Expand Up @@ -177,6 +178,7 @@ async def test_script_abbreviated_mnemonic() -> None:
'--non_interactive',
'existing-mnemonic',
'--num_validators', '1',
'--amount', '32',
'--mnemonic="aban aban aban aban aban aban aban aban aban aban aban abou"',
'--mnemonic-password', 'TREZOR',
'--validator_start_index', '1',
Expand Down
2 changes: 2 additions & 0 deletions tests/test_cli/test_new_mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ async def test_script() -> None:
'--non_interactive',
'new-mnemonic',
'--num_validators', '5',
'--amount', '32',
'--mnemonic_language', 'english',
'--chain', 'mainnet',
'--keystore_password', 'MyPassword',
Expand Down Expand Up @@ -206,6 +207,7 @@ async def test_script_abbreviated_mnemonic() -> None:
'--non_interactive',
'new-mnemonic',
'--num_validators', '5',
'--amount', '32',
'--mnemonic_language', 'english',
'--chain', 'mainnet',
'--keystore_password', 'MyPassword',
Expand Down
22 changes: 22 additions & 0 deletions tests/test_utils/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from staking_deposit.utils.validation import (
validate_int_range,
validate_password_strength,
validate_ether_amount_range
)


Expand Down Expand Up @@ -43,3 +44,24 @@ def test_validate_int_range(num: Any, low: int, high: int, valid: bool) -> None:
else:
with pytest.raises(ValidationError):
validate_int_range(num, low, high)


@pytest.mark.parametrize(
'num, valid',
[
(2, True),
(0, False),
(-1, False),
(32, True),
(1, True),
(32.1, False),
('0', False),
('a', False),
]
)
def test_validate_ether_amount_range(num: Any, valid: bool) -> None:
if valid:
validate_ether_amount_range(num)
else:
with pytest.raises(ValidationError):
validate_ether_amount_range(num)