Skip to content

Commit ffeefa2

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 b5d99b6 commit ffeefa2

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
@@ -305,17 +305,27 @@ impl WalletSubcommands {
305305
)?;
306306
let identifier = account_name_ref.as_deref().unwrap_or(&uuid);
307307

308+
let public_key = hex::encode(
309+
&SecretKey::from_slice(&wallet.credential().to_bytes())
310+
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
311+
.public_key()
312+
.to_encoded_point(false)
313+
.as_bytes()[1..],
314+
);
315+
308316
if let Some(json) = json_values.as_mut() {
309317
json.push(json!({
310318
"address": wallet.address().to_checksum(None),
319+
"public_key": format!("0x{}", public_key),
311320
"path": format!("{}", path.join(identifier).display()),
312321
}));
313322
} else {
314323
sh_println!(
315324
"Created new encrypted keystore file: {}",
316325
path.join(identifier).display()
317326
)?;
318-
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
327+
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
328+
sh_println!("Public key: 0x{}", public_key)?;
319329
}
320330
}
321331

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

339+
let public_key = hex::encode(
340+
&SecretKey::from_slice(&wallet.credential().to_bytes())
341+
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
342+
.public_key()
343+
.to_encoded_point(false)
344+
.as_bytes()[1..],
345+
);
346+
329347
if let Some(json) = json_values.as_mut() {
330348
json.push(json!({
331349
"address": wallet.address().to_checksum(None),
350+
"public_key": format!("0x{}", public_key),
332351
"private_key": format!("0x{}", hex::encode(wallet.credential().to_bytes())),
333352
}))
334353
} else {
335354
sh_println!("Successfully created new keypair.")?;
336355
sh_println!("Address: {}", wallet.address().to_checksum(None))?;
356+
sh_println!("Public key: 0x{}", public_key)?;
337357
sh_println!(
338358
"Private key: 0x{}",
339359
hex::encode(wallet.credential().to_bytes())
@@ -377,15 +397,24 @@ impl WalletSubcommands {
377397

378398
let mut accounts = json!([]);
379399
for (i, wallet) in wallets.iter().enumerate() {
400+
let public_key = hex::encode(
401+
&SecretKey::from_slice(&wallet.credential().to_bytes())
402+
.map_err(|e| eyre::eyre!("Invalid private key: {}", e))?
403+
.public_key()
404+
.to_encoded_point(false)
405+
.as_bytes()[1..],
406+
);
380407
let private_key = hex::encode(wallet.credential().to_bytes());
381408
if format_json {
382409
accounts.as_array_mut().unwrap().push(json!({
383410
"address": format!("{}", wallet.address()),
411+
"public_key": format!("0x{}", public_key),
384412
"private_key": format!("0x{}", private_key),
385413
}));
386414
} else {
387415
sh_println!("- Account {i}:")?;
388416
sh_println!("Address: {}", wallet.address())?;
417+
sh_println!("Public key: 0x{}", public_key)?;
389418
sh_println!("Private key: 0x{private_key}\n")?;
390419
}
391420
}

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
});
@@ -422,24 +424,34 @@ casttest!(wallet_list_local_accounts, |prj, cmd| {
422424
.stdout_eq(str![[r#"
423425
Created new encrypted keystore file: [..]
424426
[ADDRESS]
427+
[PUBLIC_KEY]
425428
Created new encrypted keystore file: [..]
426429
[ADDRESS]
430+
[PUBLIC_KEY]
427431
Created new encrypted keystore file: [..]
428432
[ADDRESS]
433+
[PUBLIC_KEY]
429434
Created new encrypted keystore file: [..]
430435
[ADDRESS]
436+
[PUBLIC_KEY]
431437
Created new encrypted keystore file: [..]
432438
[ADDRESS]
439+
[PUBLIC_KEY]
433440
Created new encrypted keystore file: [..]
434441
[ADDRESS]
442+
[PUBLIC_KEY]
435443
Created new encrypted keystore file: [..]
436444
[ADDRESS]
445+
[PUBLIC_KEY]
437446
Created new encrypted keystore file: [..]
438447
[ADDRESS]
448+
[PUBLIC_KEY]
439449
Created new encrypted keystore file: [..]
440450
[ADDRESS]
451+
[PUBLIC_KEY]
441452
Created new encrypted keystore file: [..]
442453
[ADDRESS]
454+
[PUBLIC_KEY]
443455
444456
"#]]);
445457

@@ -481,18 +493,21 @@ test test test test test test test test test test test junk
481493
Accounts:
482494
- Account 0:
483495
Address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
496+
Public key: 0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5
484497
Private key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
485498
486499
- Account 1:
487500
Address: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
501+
Public key: 0xba5734d8f7091719471e7f7ed6b9df170dc70cc661ca05e688601ad984f068b0d67351e5f06073092499336ab0839ef8a521afd334e53807205fa2f08eec74f4
488502
Private key: 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
489503
490504
- Account 2:
491505
Address: 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
506+
Public key: 0x9d9031e97dd78ff8c15aa86939de9b1e791066a0224e331bc962a2099a7b1f0464b8bbafe1535f2301c72c2cb3535b172da30b02686ab0393d348614f157fbdb
492507
Private key: 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
493508
494509
495-
"#]]);
510+
"#]].raw());
496511
});
497512

498513
// tests that `cast wallet new-mnemonic --json` outputs the expected mnemonic
@@ -513,14 +528,17 @@ casttest!(wallet_mnemonic_from_entropy_json, |_prj, cmd| {
513528
"accounts": [
514529
{
515530
"address": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
531+
"public_key": "0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5",
516532
"private_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
517533
},
518534
{
519535
"address": "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
536+
"public_key": "0xba5734d8f7091719471e7f7ed6b9df170dc70cc661ca05e688601ad984f068b0d67351e5f06073092499336ab0839ef8a521afd334e53807205fa2f08eec74f4",
520537
"private_key": "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
521538
},
522539
{
523540
"address": "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",
541+
"public_key": "0x9d9031e97dd78ff8c15aa86939de9b1e791066a0224e331bc962a2099a7b1f0464b8bbafe1535f2301c72c2cb3535b172da30b02686ab0393d348614f157fbdb",
524542
"private_key": "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a"
525543
}
526544
]

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)