Skip to content

feat(cast): display public_key on wallet creation with "new" and "new-mnemonic" sub-commands #10600

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

Merged
merged 6 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 30 additions & 1 deletion crates/cast/src/cmd/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,27 @@ impl WalletSubcommands {
)?;
let identifier = account_name_ref.as_deref().unwrap_or(&uuid);

let public_key = hex::encode(
&SecretKey::from_slice(&wallet.credential().to_bytes())
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
.public_key()
.to_encoded_point(false)
.as_bytes()[1..],
);

if let Some(json) = json_values.as_mut() {
json.push(json!({
"address": wallet.address().to_checksum(None),
"public_key": format!("0x{}", public_key),
"path": format!("{}", path.join(identifier).display()),
}));
} else {
sh_println!(
"Created new encrypted keystore file: {}",
path.join(identifier).display()
)?;
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
sh_println!("Public key: 0x{}", public_key)?;
}
}

Expand All @@ -326,14 +336,24 @@ impl WalletSubcommands {
for _ in 0..number {
let wallet = PrivateKeySigner::random_with(&mut rng);

let public_key = hex::encode(
&SecretKey::from_slice(&wallet.credential().to_bytes())
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
.public_key()
.to_encoded_point(false)
.as_bytes()[1..],
);

if let Some(json) = json_values.as_mut() {
json.push(json!({
"address": wallet.address().to_checksum(None),
"public_key": format!("0x{}", public_key),
"private_key": format!("0x{}", hex::encode(wallet.credential().to_bytes())),
}))
} else {
sh_println!("Successfully created new keypair.")?;
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
sh_println!("Public key: 0x{}", public_key)?;
sh_println!(
"Private key: 0x{}",
hex::encode(wallet.credential().to_bytes())
Expand Down Expand Up @@ -377,15 +397,24 @@ impl WalletSubcommands {

let mut accounts = json!([]);
for (i, wallet) in wallets.iter().enumerate() {
let public_key = hex::encode(
&SecretKey::from_slice(&wallet.credential().to_bytes())
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
.public_key()
.to_encoded_point(false)
.as_bytes()[1..],
);
let private_key = hex::encode(wallet.credential().to_bytes());
if format_json {
accounts.as_array_mut().unwrap().push(json!({
"address": format!("{}", wallet.address()),
"public_key": format!("0x{}", public_key),
"private_key": format!("0x{}", private_key),
}));
} else {
sh_println!("- Account {i}:")?;
sh_println!("Address: {}", wallet.address())?;
sh_println!("Public key: 0x{}", public_key)?;
sh_println!("Private key: 0x{private_key}\n")?;
}
}
Expand Down
20 changes: 19 additions & 1 deletion crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use foundry_test_utils::{
next_etherscan_api_key, next_http_archive_rpc_url, next_http_rpc_endpoint,
next_rpc_endpoint, next_ws_rpc_endpoint,
},
snapbox::IntoData,
str,
util::OutputExt,
};
Expand Down Expand Up @@ -164,6 +165,7 @@ casttest!(new_wallet_keystore_with_password, |_prj, cmd| {
.stdout_eq(str![[r#"
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]

"#]]);
});
Expand Down Expand Up @@ -423,24 +425,34 @@ casttest!(wallet_list_local_accounts, |prj, cmd| {
.stdout_eq(str![[r#"
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]
Created new encrypted keystore file: [..]
[ADDRESS]
[PUBLIC_KEY]

"#]]);

Expand Down Expand Up @@ -482,18 +494,21 @@ test test test test test test test test test test test junk
Accounts:
- Account 0:
Address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Public key: 0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5
Private key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

- Account 1:
Address: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
Public key: 0xba5734d8f7091719471e7f7ed6b9df170dc70cc661ca05e688601ad984f068b0d67351e5f06073092499336ab0839ef8a521afd334e53807205fa2f08eec74f4
Private key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d

- Account 2:
Address: 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
Public key: 0x9d9031e97dd78ff8c15aa86939de9b1e791066a0224e331bc962a2099a7b1f0464b8bbafe1535f2301c72c2cb3535b172da30b02686ab0393d348614f157fbdb
Private key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a


"#]]);
"#]].raw());
});

// tests that `cast wallet new-mnemonic --json` outputs the expected mnemonic
Expand All @@ -514,14 +529,17 @@ casttest!(wallet_mnemonic_from_entropy_json, |_prj, cmd| {
"accounts": [
{
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"public_key": "0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5",
"private_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
},
{
"address": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
"public_key": "0xba5734d8f7091719471e7f7ed6b9df170dc70cc661ca05e688601ad984f068b0d67351e5f06073092499336ab0839ef8a521afd334e53807205fa2f08eec74f4",
"private_key": "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
},
{
"address": "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",
"public_key": "0x9d9031e97dd78ff8c15aa86939de9b1e791066a0224e331bc962a2099a7b1f0464b8bbafe1535f2301c72c2cb3535b172da30b02686ab0393d348614f157fbdb",
"private_key": "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a"
}
]
Expand Down
3 changes: 2 additions & 1 deletion crates/test-utils/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,8 @@ fn test_redactions() -> snapbox::Redactions {
("[FILE]", r"Location(.|\n)*\.rs(.|\n)*Backtrace"),
("[COMPILING_FILES]", r"Compiling \d+ files?"),
("[TX_HASH]", r"Transaction hash: 0x[0-9A-Fa-f]{64}"),
("[ADDRESS]", r"Address: 0x[0-9A-Fa-f]{40}"),
("[ADDRESS]", r"Address: +0x[0-9A-Fa-f]{40}"),
("[PUBLIC_KEY]", r"Public key: +0x[0-9A-Fa-f]{128}"),
("[UPDATING_DEPENDENCIES]", r"Updating dependencies in .*"),
("[SAVED_TRANSACTIONS]", r"Transactions saved to: .*\.json"),
("[SAVED_SENSITIVE_VALUES]", r"Sensitive values saved to: .*\.json"),
Expand Down