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

Disallow PubKey as PrivKey for cmd_set_default_account command - display error if user accidentally pass wrong key. #1645

Merged
merged 2 commits into from
Nov 29, 2023

Conversation

Sajjon
Copy link
Contributor

@Sajjon Sajjon commented Nov 10, 2023

Summary

Validate input to cmd_set_default_account, ensuring we don't accidentally pass in PublicKey instead of PrivateKey.

Details

In main it is possible to accidentally pass in the PublicKey to the PrivateKey value for command cmd_set_default_account. This results in confusing state and error when trying to e.g. perform a transfer.

Screenshot 2023-11-10 at 15 55 19

N.B: we don't get any information on what went wrong, that is because of the program panics at unwrap at line 62, so we never reach the next line, with more info: Error::NoDefaultPrivateKey. This should also be fix (I can do another PR), but this PR fixes so that should never happen...

I got into this state since I accidentally copied the PublicKey instead of the PrivateKey, resulting in this bad state.
Screenshot 2023-11-10 at 15 55 33

Testing

Screenshot 2023-11-10 at 16 03 31

Now it is not possible to pass in the PublicKey instead of PrivateKey anymore, and we display a very informative error GotPublicKeyExpectedPrivateKey.

Added a unit test.

…rivate key when using cmd_set_default_account, throwing specific error if you accidentally pass in the public key instead of private key.
@@ -27,6 +27,16 @@ impl Secp256k1PrivateKey {
self.0.secret_bytes().to_vec()
}

pub fn to_hex(&self) -> String {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We convert between key and hex in a few places, makes sense for the type type to have these methods IMO.

@@ -19,12 +19,47 @@ pub struct SetDefaultAccount {
impl SetDefaultAccount {
pub fn run<O: std::io::Write>(&self, out: &mut O) -> Result<(), Error> {
let mut configs = get_configs()?;
let private_key = parse_private_key_from_str(&self.private_key).map_err(|e| {
if Secp256k1PublicKey::from_str(&self.private_key).is_ok() {
Error::GotPublicKeyExpectedPrivateKey
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we fail to create a PrivateKey (32 bytes/64 hex char input) from the passed in string, we try to parse it as a PublicKey to see if the user made the very reasonable error of accidentally copy pasting the public key - like I did!

@iamyulong iamyulong merged commit 3dd79e8 into radixdlt:main Nov 29, 2023
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants