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

feat: support loading private key #314

Merged
merged 2 commits into from
Jun 15, 2024

Conversation

malm0d
Copy link
Contributor

@malm0d malm0d commented Jun 14, 2024

To load using private key, we can now use the --private-key option. And if we want to load with private key using flashbots send bundle mode, we can pass another option --flashbots-private-key. An addition option is required for using flashbots mode while loading with private key, since the private key only maps to a single address without hierarchical derivation.

Sample command to load using private key:
cargo run --release -- node --eth-client-address http://127.0.0.1:8545 --private-key 4c5e5d30… --beneficiary 0xf39Fd6e5… --entry-points 0x5FF137D4… --http --ws

Sample command to load using private key with flashbots mode:
cargo run --release -- node --eth-client-address http://127.0.0.1:8545 --private-key 4c5e5d30… --flashbots-private-key df218be... --send-bundle-mode flashbots --beneficiary 0xf39Fd6e5… --entry-points 0x5FF137D4… --http --ws

  • The --mnemonic-file and --private-key options are mutually exclusive, one and only one can be used.
  • Similarly, --flashbots-private-key cannot be used in the same command as --mnemonic-file, and it cannot be used alone without the --private-key option; but it can be optional when --private-key is used.
  • If --send-bundle-mode is not flashbots and --flashbots-private-key is provided, --flashbots-private-key will be ignored.

Additional considerations:
I added a value-parser for the --private-key and --flashbot-private-key arguments, and the current implementation only checks if there are 64 characters and if each char is an ASCII hexadecimal digit. Am wondering if this is sufficient or if theres another better way to do this:

pub fn validate_private_key(hex_string: &str) -> Result<String, String> {
    let chars = hex_string.chars();

    if chars.clone().count() != 64 {
        return Err(format!("{hex_string} is not a valid private key"));
    }

    for c in chars {
        if !c.is_ascii_hexdigit() {
            return Err(format!("{hex_string} is not a valid hexadecimal string"));
        }
    }

    Ok(String::from(hex_string))
}

@malm0d malm0d marked this pull request as ready for review June 14, 2024 17:48
@malm0d malm0d requested review from Vid201 and zsluedem as code owners June 14, 2024 17:48
@Vid201 Vid201 linked an issue Jun 14, 2024 that may be closed by this pull request
Copy link
Member

@Vid201 Vid201 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks really good and clean, thanks!

I would just remove the check validate_private_key because the program would terminate either way if there is a wrong format/key - when converting to wallet in wallet.rs.

Copy link
Member

@Vid201 Vid201 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging this now, thanks for contributing!!

@Vid201 Vid201 merged commit e55ac50 into silius-rs:main Jun 15, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support loading private key
2 participants