Skip to content

feat(cast wallet) display pubkey when creating new keypair or converting private key to an address #9748

@coffee-converter

Description

@coffee-converter

Component

Cast

Describe the feature you would like

It would be super useful if cast wallet new and cast wallet address <PK> would display the pubkey along with the generated address.

Additional context

No response

Activity

added
first issueA good way to start contributing
C-castCommand: cast
and removed
T-needs-triageType: this issue needs to be labelled
on Jan 23, 2025
wengDavo

wengDavo commented on Jan 29, 2025

@wengDavo

May I pick this up?

mablr

mablr commented on Apr 18, 2025

@mablr
Contributor

@wengDavo Are you still working on it, or can I take over?

Yasersarhadikarbasak

Yasersarhadikarbasak commented on Apr 18, 2025

@Yasersarhadikarbasak
wengDavo

wengDavo commented on Apr 19, 2025

@wengDavo

@mablr Yes you can take over I've got a bunch on my plate right now and probably won't get back to it anytime soon. Appreciate you jumping in!

mablr

mablr commented on Apr 19, 2025

@mablr
Contributor

@wengDavo All right, thanks for letting me know! I’ll it take over.

All the best!

mablr

mablr commented on Apr 20, 2025

@mablr
Contributor

TL;DR: "All that for this?"

I've started implementation work (in cast wallet), but I believe it's important to clarify a few design questions before proceeding:

  1. Verbosity Control
    For the wallet address sub-command, the public key could be displayed alongside the address when the verbosity level (sh_verbosity()) is greater than 0, similar to how the address is displayed with the private key in the wallet private-key sub-command.
    Is this the intended behavior, or should the public key always be shown regardless of verbosity?

  2. JSON Output Handling
    In the wallet new sub-command, when shell::is_json() is false, we could format the public key display similarly to wallet address.
    However, when shell::is_json() is true, adding the public key would alter the structure of the JSON output. How should we handle this?

    • Always include the public key in the JSON output, regardless of verbosity
    • Include it only when verbosity > 0 (but this would lead to inconsistent JSON structure)
    • Never include it in the JSON output
  3. Code Organization
    Extracting the public key string from a WalletSigner object involves several processing steps, as seen in the wallet public-key sub-command.
    To avoid code duplication, we'd likely need to introduce a helper method in the WalletSubcommands implementation if we want to display the public key in other sub-commands.

Given all this, I’m starting to wonder: is it really necessary to display the public key in wallet new and wallet address, when users can simply run wallet public-key instead?

0x8a8a

0x8a8a commented on May 9, 2025

@0x8a8a
  1. Verbosity Control
    For the wallet address sub-command, the public key could be displayed alongside the address when the verbosity level (sh_verbosity()) is greater than 0, similar to how the address is displayed with the private key in the wallet private-key sub-command.
    Is this the intended behavior, or should the public key always be shown regardless of verbosity?

Given we already have the public-key subcommand, modifying the address subcommand to also output the public key is redundant. If one already has a private key, they should directly use the public-key subcommand. Maybe restrict the scope to only modify the output of cast wallet new?

  1. JSON Output Handling
    In the wallet new sub-command, when shell::is_json() is false, we could format the public key display similarly to wallet address.
    However, when shell::is_json() is true, adding the public key would alter the structure of the JSON output. How should we handle this?

    • Always include the public key in the JSON output, regardless of verbosity
    • Include it only when verbosity > 0 (but this would lead to inconsistent JSON structure)
    • Never include it in the JSON output

IMO, if cast wallet new will print the public key to stdout, then the JSON should reflect it as well. So, moving forward always include it in stdout and in the JSON.

  1. Code Organization
    Extracting the public key string from a WalletSigner object involves several processing steps, as seen in the wallet public-key sub-command.
    To avoid code duplication, we'd likely need to introduce a helper method in the WalletSubcommands implementation if we want to display the public key in other sub-commands.

Agreed. Extracting the logic from the public-key subcommand seems simple enough.

let secret_key = SecretKey::from_slice(&private_key_bytes)
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?;
// Get the public key from the private key
let public_key = secret_key.public_key();
// Serialize it as uncompressed (65 bytes: 0x04 || X (32 bytes) || Y (32 bytes))
let pubkey_bytes = public_key.to_encoded_point(false);
// Strip the 1-byte prefix (0x04) to get 64 bytes for Ethereum use
let ethereum_pubkey = &pubkey_bytes.as_bytes()[1..];

The above portion seems a sufficient candidate, and should therefore make testing the new function simple.

Given all this, I’m starting to wonder: is it really necessary to display the public key in wallet new and wallet address, when users can simply run wallet public-key instead?

I'd recommend restricting the scope, and simply displaying the public key in the cast wallet new subcommand (taking into consideration the JSON output as well).

What do you think @coffee-converter ?

mablr

mablr commented on May 11, 2025

@mablr
Contributor

Thanks @0x8a8a for your feedback. I think we are at the same page about design, but I'm still sceptical about the real value of this feature even with the reduced scope. 🤔

I would also be interested to hear your opinion @coffee-converter.

6 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-castCommand: castT-featureType: featurefirst issueA good way to start contributing

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

    Participants

    @mattsse@mablr@coffee-converter@zerosnacks@wengDavo

    Issue actions

      feat(`cast wallet`) display pubkey when creating new keypair or converting private key to an address · Issue #9748 · foundry-rs/foundry