Skip to content

Commit 232b8f5

Browse files
committed
feat(cast): display public_key on wallet creation with "new" and "new-mnemonic" subcommands
The method used to obtain public_key with the wallet is taken from "public-key" subcommand. Update tests to match the new outputs: - match pubkey - update redactions for pubkey and to match various spacings - use raw data for assertion in "wallet_mnemonic_from_entropy" to avoid conflict with redactions
1 parent 2d25470 commit 232b8f5

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

crates/cast/src/cmd/wallet/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,27 @@ impl WalletSubcommands {
302302
)?;
303303
let identifier = account_name_ref.as_deref().unwrap_or(&uuid);
304304

305+
let public_key = hex::encode(
306+
&SecretKey::from_slice(&wallet.credential().to_bytes())
307+
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
308+
.public_key()
309+
.to_encoded_point(false)
310+
.as_bytes()[1..],
311+
);
312+
305313
if let Some(json) = json_values.as_mut() {
306314
json.push(json!({
307315
"address": wallet.address().to_checksum(None),
316+
"public_key": format!("0x{}", public_key),
308317
"path": format!("{}", path.join(identifier).display()),
309318
}));
310319
} else {
311320
sh_println!(
312321
"Created new encrypted keystore file: {}",
313322
path.join(identifier).display()
314323
)?;
315-
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
324+
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
325+
sh_println!("Public key: 0x{}", public_key)?;
316326
}
317327
}
318328

@@ -323,14 +333,24 @@ impl WalletSubcommands {
323333
for _ in 0..number {
324334
let wallet = PrivateKeySigner::random_with(&mut rng);
325335

336+
let public_key = hex::encode(
337+
&SecretKey::from_slice(&wallet.credential().to_bytes())
338+
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
339+
.public_key()
340+
.to_encoded_point(false)
341+
.as_bytes()[1..],
342+
);
343+
326344
if let Some(json) = json_values.as_mut() {
327345
json.push(json!({
328346
"address": wallet.address().to_checksum(None),
347+
"public_key": format!("0x{}", public_key),
329348
"private_key": format!("0x{}", hex::encode(wallet.credential().to_bytes())),
330349
}))
331350
} else {
332351
sh_println!("Successfully created new keypair.")?;
333352
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
353+
sh_println!("Public key: 0x{}", public_key)?;
334354
sh_println!(
335355
"Private key: 0x{}",
336356
hex::encode(wallet.credential().to_bytes())
@@ -374,15 +394,24 @@ impl WalletSubcommands {
374394

375395
let mut accounts = json!([]);
376396
for (i, wallet) in wallets.iter().enumerate() {
397+
let public_key = hex::encode(
398+
&SecretKey::from_slice(&wallet.credential().to_bytes())
399+
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
400+
.public_key()
401+
.to_encoded_point(false)
402+
.as_bytes()[1..],
403+
);
377404
let private_key = hex::encode(wallet.credential().to_bytes());
378405
if format_json {
379406
accounts.as_array_mut().unwrap().push(json!({
380407
"address": format!("{}", wallet.address()),
408+
"public_key": format!("0x{}", public_key),
381409
"private_key": format!("0x{}", private_key),
382410
}));
383411
} else {
384412
sh_println!("- Account {i}:")?;
385413
sh_println!("Address: {}", wallet.address())?;
414+
sh_println!("Public key: 0x{}", public_key)?;
386415
sh_println!("Private key: 0x{private_key}\n")?;
387416
}
388417
}

crates/cast/tests/cli/main.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use foundry_test_utils::{
1111
next_etherscan_api_key, next_http_archive_rpc_url, next_http_rpc_endpoint,
1212
next_rpc_endpoint, next_ws_rpc_endpoint,
1313
},
14+
snapbox::IntoData,
1415
str,
1516
util::OutputExt,
1617
};
@@ -163,6 +164,7 @@ casttest!(new_wallet_keystore_with_password, |_prj, cmd| {
163164
.stdout_eq(str![[r#"
164165
Created new encrypted keystore file: [..]
165166
[ADDRESS]
167+
[PUBLIC_KEY]
166168
167169
"#]]);
168170
});
@@ -362,24 +364,34 @@ casttest!(wallet_list_local_accounts, |prj, cmd| {
362364
.stdout_eq(str![[r#"
363365
Created new encrypted keystore file: [..]
364366
[ADDRESS]
367+
[PUBLIC_KEY]
365368
Created new encrypted keystore file: [..]
366369
[ADDRESS]
370+
[PUBLIC_KEY]
367371
Created new encrypted keystore file: [..]
368372
[ADDRESS]
373+
[PUBLIC_KEY]
369374
Created new encrypted keystore file: [..]
370375
[ADDRESS]
376+
[PUBLIC_KEY]
371377
Created new encrypted keystore file: [..]
372378
[ADDRESS]
379+
[PUBLIC_KEY]
373380
Created new encrypted keystore file: [..]
374381
[ADDRESS]
382+
[PUBLIC_KEY]
375383
Created new encrypted keystore file: [..]
376384
[ADDRESS]
385+
[PUBLIC_KEY]
377386
Created new encrypted keystore file: [..]
378387
[ADDRESS]
388+
[PUBLIC_KEY]
379389
Created new encrypted keystore file: [..]
380390
[ADDRESS]
391+
[PUBLIC_KEY]
381392
Created new encrypted keystore file: [..]
382393
[ADDRESS]
394+
[PUBLIC_KEY]
383395
384396
"#]]);
385397

@@ -421,18 +433,21 @@ test test test test test test test test test test test junk
421433
Accounts:
422434
- Account 0:
423435
Address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
436+
Public key: 0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5
424437
Private key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
425438
426439
- Account 1:
427440
Address: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
441+
Public key: 0xba5734d8f7091719471e7f7ed6b9df170dc70cc661ca05e688601ad984f068b0d67351e5f06073092499336ab0839ef8a521afd334e53807205fa2f08eec74f4
428442
Private key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
429443
430444
- Account 2:
431445
Address: 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
446+
Public key: 0x9d9031e97dd78ff8c15aa86939de9b1e791066a0224e331bc962a2099a7b1f0464b8bbafe1535f2301c72c2cb3535b172da30b02686ab0393d348614f157fbdb
432447
Private key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
433448
434449
435-
"#]]);
450+
"#]].raw());
436451
});
437452

438453
// tests that `cast wallet new-mnemonic --json` outputs the expected mnemonic
@@ -453,14 +468,17 @@ casttest!(wallet_mnemonic_from_entropy_json, |_prj, cmd| {
453468
"accounts": [
454469
{
455470
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
471+
"public_key": "0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5",
456472
"private_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
457473
},
458474
{
459475
"address": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
476+
"public_key": "0xba5734d8f7091719471e7f7ed6b9df170dc70cc661ca05e688601ad984f068b0d67351e5f06073092499336ab0839ef8a521afd334e53807205fa2f08eec74f4",
460477
"private_key": "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
461478
},
462479
{
463480
"address": "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",
481+
"public_key": "0x9d9031e97dd78ff8c15aa86939de9b1e791066a0224e331bc962a2099a7b1f0464b8bbafe1535f2301c72c2cb3535b172da30b02686ab0393d348614f157fbdb",
464482
"private_key": "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a"
465483
}
466484
]

crates/test-utils/src/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,8 @@ fn test_redactions() -> snapbox::Redactions {
10151015
("[FILE]", r"Location(.|\n)*\.rs(.|\n)*Backtrace"),
10161016
("[COMPILING_FILES]", r"Compiling \d+ files?"),
10171017
("[TX_HASH]", r"Transaction hash: 0x[0-9A-Fa-f]{64}"),
1018-
("[ADDRESS]", r"Address: 0x[0-9A-Fa-f]{40}"),
1018+
("[ADDRESS]", r"Address: +0x[0-9A-Fa-f]{40}"),
1019+
("[PUBLIC_KEY]", r"Public key: +0x[0-9A-Fa-f]{128}"),
10191020
("[UPDATING_DEPENDENCIES]", r"Updating dependencies in .*"),
10201021
("[SAVED_TRANSACTIONS]", r"Transactions saved to: .*\.json"),
10211022
("[SAVED_SENSITIVE_VALUES]", r"Sensitive values saved to: .*\.json"),

0 commit comments

Comments
 (0)