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

feat(cast): add json flag in cast wallet new-mnemonic #9139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
45 changes: 37 additions & 8 deletions crates/cast/bin/cmd/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub enum WalletSubcommands {
/// Entropy to use for the mnemonic
#[arg(long, short, conflicts_with = "words")]
entropy: Option<String>,

/// Output generated mnemonic phrase as JSON.
#[arg(long, short, default_value = "false")]
json: bool,
},

/// Generate a vanity address.
Expand Down Expand Up @@ -290,16 +294,21 @@ impl WalletSubcommands {
}
}
}
Self::NewMnemonic { words, accounts, entropy } => {
Self::NewMnemonic { words, accounts, entropy, json } => {
let mut json_values = if json { Some(vec![]) } else { None };

let phrase = if let Some(entropy) = entropy {
let entropy = Entropy::from_slice(hex::decode(entropy)?)?;
println!("{}", "Generating mnemonic from provided entropy...".yellow());
Mnemonic::<English>::new_from_entropy(entropy).to_phrase()
} else {
let mut rng = thread_rng();
Mnemonic::<English>::new_with_count(&mut rng, words)?.to_phrase()
};

if !json {
println!("{}", "Generating mnemonic from provided entropy...".yellow());
}

let builder = MnemonicBuilder::<English>::default().phrase(phrase.as_str());
let derivation_path = "m/44'/60'/0'/0/";
let wallets = (0..accounts)
Expand All @@ -308,13 +317,33 @@ impl WalletSubcommands {
let wallets =
wallets.into_iter().map(|b| b.build()).collect::<Result<Vec<_>, _>>()?;

println!("{}", "Successfully generated a new mnemonic.".green());
println!("Phrase:\n{phrase}");
println!("\nAccounts:");
if !json {
println!("{}", "Successfully generated a new mnemonic.".green());
println!("Phrase:\n{phrase}");
println!("\nAccounts:");
}

let mut accounts = json!([]);
for (i, wallet) in wallets.iter().enumerate() {
println!("- Account {i}:");
println!("Address: {}", wallet.address());
println!("Private key: 0x{}\n", hex::encode(wallet.credential().to_bytes()));
let private_key = hex::encode(wallet.credential().to_bytes());
if json {
accounts.as_array_mut().unwrap().push(json!({
"address": wallet.address(),
"private_key": format!("0x{}", hex::encode(wallet.credential().to_bytes())),
}));
} else {
println!("- Account {i}:");
println!("Address: {}", wallet.address());
println!("Private key: 0x{private_key}\n");
}
}

if let Some(json) = json_values.as_mut() {
json.push(json!({
"mnemonic": phrase,
"accounts": accounts,
}));
println!("{}", serde_json::to_string_pretty(json)?);
}
}
Self::Vanity(cmd) => {
Expand Down
36 changes: 36 additions & 0 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,42 @@ Address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Private key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80


"#]]);
});

// tests that `cast wallet new-mnemonic --json` outputs the expected mnemonic
casttest!(wallet_mnemonic_json, |_prj, cmd| {
cmd.args([
"wallet",
"new-mnemonic",
"--json",
"--accounts",
"3",
"--entropy",
"0xdf9bf37e6fcdf9bf37e6fcdf9bf37e3c",
])
.assert_success()
.stdout_eq(str![[r#"
[
{
"mnemonic": "test test test test test test test test test test test junk",
"accounts": [
{
"address": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"private_key": "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
},
{
"address": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
"private_key": "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
},
{
"address": "0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc",
"private_key": "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a"
}
]
}
]

"#]]);
});

Expand Down
Loading