Skip to content
New issue

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

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

Already on GitHub? # to your account

Add private key cli arg #35

Merged
merged 2 commits into from
Jul 15, 2021

Conversation

njgheorghita
Copy link
Collaborator

Add a cli arg for custom private keys.

let enr_key = CombinedKey::generate_secp256k1();
let enr_key = match config.private_key {
Some(val) => {
CombinedKey::secp256k1_from_bytes(val.clone().as_mut_slice()).unwrap()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This pr took way longer than it should have, but most of that time was wasted b/c I was using b'0' * 32 as my test private key, but for whatever reason this library rejects the 0 bytestring as an invalid private key. Is this just a security thing? Is this desired behavior? b'0' * 31 + b'1' is accepted, along with all other private keys i've used

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup, I don't remember the details of why, but 0 is an invalid key. Rejecting that key is the correct behavior AIUI.

@njgheorghita njgheorghita force-pushed the private-key-cli-flag branch from c9dcdcd to d0ba276 Compare June 30, 2021 19:15
@njgheorghita njgheorghita marked this pull request as ready for review June 30, 2021 19:16
@njgheorghita njgheorghita force-pushed the private-key-cli-flag branch from d0ba276 to 5162d8f Compare June 30, 2021 19:19
@njgheorghita njgheorghita requested a review from carver June 30, 2021 19:26
src/cli.rs Outdated
Comment on lines 86 to 91
.arg(
Arg::with_name("private_key")
.long("private-key")
.help("Hex encoded 32 byte private key")
.takes_value(true),
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Private keys should not be taken as command line arguments, since they end up in terminal history and in the /proc file system.

Probably should pass a path, and read from there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup, this makes sense and I def agree. Any objections to me just creating an issue to track the update for now? And consider the update "critical" before any kind of initial release. This pr is mostly to help with simplicity of spinning up a testnet and all the various nodes - which becomes more complex if we require keyfiles. All of the private keys I'm using are not even real (00.01, 00.02, etc).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe rename the argument to unsafe_private_key?

In any case, my review is nonbinding, I'm only a contributor 🤣

Copy link
Collaborator Author

@njgheorghita njgheorghita Jul 1, 2021

Choose a reason for hiding this comment

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

Hahaha no way, all contributors matter. I updated the long param to unsafe-private-key. left the name private_key as is, since that's internal to the code. Tracking the update here: https://github.com/carver/trin/issues/36

@njgheorghita njgheorghita force-pushed the private-key-cli-flag branch from 440fed2 to 752ee61 Compare July 1, 2021 00:40
README.md Outdated
--discovery-port <discovery_port> The UDP port to listen on. [default: 9000]
--external-address <external_addr> The public IP address and port under which this node is accessible
--pool-size <pool_size> max size of threadpool [default: 2]
--private-key <private_key> Hex encoded 32 byte private key
Copy link
Collaborator

Choose a reason for hiding this comment

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

New name now?

src/cli.rs Outdated
.arg(
Arg::with_name("private_key")
.long("unsafe-private-key")
.help("Hex encoded 32 byte private key (unsafe)")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably explain the "unsafe" bit in the description for the arg.

let hex_private_key = value_t!(matches.value_of("private_key"), String)?;
match hex_private_key.len() {
64 => (),
val => panic!(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe could return error instead of panic?

Copy link
Collaborator Author

@njgheorghita njgheorghita Jul 15, 2021

Choose a reason for hiding this comment

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

From what I read - panic! is for unrecoverable errors and Err is for recoverable errors. My assumption was that if a user enters an invalid key - we want to terminate and report. Similarly, we panic with other invalid cli args.

If I understand your suggestion correctly, you're suggesting that we want to use a recoverable error? Is this just for code cleanliness/understanding? Seems like we don't want trin to continue if the key is invalid, since trin'll generate a random key if the user-provided key is invalid, and then the node has a different identity from what the user is expecting.

In terms of ux, imo it seems like the panic! output is slightly less muddled than Err

panic!

thread 'main' panicked at 'Invalid private key length: 6, expected 32 byte hexstring', src/cli.rs:110:24
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Err

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "Invalid private key length: 6, expected 32 byte hexstring"', src/cli.rs:121:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Merging for now, but i'll circle back to this in a follow up pr if we want to improve the cli-arg validation strategy

@njgheorghita njgheorghita merged commit ea546e5 into ethereum:master Jul 15, 2021
@njgheorghita njgheorghita deleted the private-key-cli-flag branch July 15, 2021 22:51
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants