Skip to content

Commit

Permalink
make cli::Context chain optional and allow to use wallet --pre-genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Jan 12, 2023
1 parent 983ad23 commit 182b275
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 290 deletions.
24 changes: 14 additions & 10 deletions apps/src/bin/namada-node/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ use namada_apps::node::ledger;
pub fn main() -> Result<()> {
let (cmd, mut ctx) = cli::namada_node_cli()?;
if let Some(mode) = ctx.global_args.mode.clone() {
ctx.config.ledger.tendermint.tendermint_mode = mode;
if let Some(chain) = ctx.chain.as_mut() {
chain.config.ledger.tendermint.tendermint_mode = mode;
}
}
match cmd {
cmds::NamadaNode::Ledger(sub) => match sub {
cmds::Ledger::Run(cmds::LedgerRun(args)) => {
let wasm_dir = ctx.wasm_dir();
let chain_ctx = ctx.take_chain_or_exit();
let wasm_dir = chain_ctx.wasm_dir();

// Sleep until start time if needed
if let Some(time) = args.0 {
Expand All @@ -31,10 +34,11 @@ pub fn main() -> Result<()> {
}
}
}
ledger::run(ctx.config.ledger, wasm_dir);
ledger::run(chain_ctx.config.ledger, wasm_dir);
}
cmds::Ledger::Reset(_) => {
ledger::reset(ctx.config.ledger)
let chain_ctx = ctx.take_chain_or_exit();
ledger::reset(chain_ctx.config.ledger)
.wrap_err("Failed to reset Namada node")?;
}
},
Expand All @@ -44,18 +48,18 @@ pub fn main() -> Result<()> {
// In here, we just need to overwrite the default chain ID, in
// case it's been already set to a different value
if let Some(chain_id) = ctx.global_args.chain_id.as_ref() {
ctx.global_config.default_chain_id = chain_id.clone();
ctx.global_config.default_chain_id = Some(chain_id.clone());
ctx.global_config
.write(&ctx.global_args.base_dir)
.unwrap_or_else(|err| {
eprintln!("Error writing global config: {}", err);
eprintln!("Error writing global config: {err}");
cli::safe_exit(1)
});
tracing::debug!(
"Generated config and set default chain ID to \
{chain_id}"
);
}
tracing::debug!(
"Generated config and set default chain ID to {}",
&ctx.global_config.default_chain_id
);
}
},
}
Expand Down
140 changes: 93 additions & 47 deletions apps/src/bin/namada-wallet/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use namada::types::masp::{MaspValue, PaymentAddress};
use namada_apps::cli;
use namada_apps::cli::{args, cmds, Context};
use namada_apps::client::tx::find_valid_diversifier;
use namada_apps::wallet::{DecryptionError, FindKeyError};
use namada_apps::client::utils::PRE_GENESIS_DIR;
use namada_apps::wallet::{DecryptionError, FindKeyError, Wallet};
use rand_core::OsRng;

pub fn main() -> Result<()> {
Expand All @@ -35,7 +36,9 @@ pub fn main() -> Result<()> {
cmds::WalletAddress::Find(cmds::AddressOrAliasFind(args)) => {
address_or_alias_find(ctx, args)
}
cmds::WalletAddress::List(cmds::AddressList) => address_list(ctx),
cmds::WalletAddress::List(cmds::AddressList(args)) => {
address_list(ctx, args)
}
cmds::WalletAddress::Add(cmds::AddressAdd(args)) => {
address_add(ctx, args)
}
Expand All @@ -50,8 +53,8 @@ pub fn main() -> Result<()> {
cmds::WalletMasp::AddAddrKey(cmds::MaspAddAddrKey(args)) => {
address_key_add(ctx, args)
}
cmds::WalletMasp::ListPayAddrs(cmds::MaspListPayAddrs) => {
payment_addresses_list(ctx)
cmds::WalletMasp::ListPayAddrs(cmds::MaspListPayAddrs(args)) => {
payment_addresses_list(ctx, args)
}
cmds::WalletMasp::ListKeys(cmds::MaspListKeys(args)) => {
spending_keys_list(ctx, args)
Expand All @@ -70,9 +73,10 @@ fn address_key_find(
args::AddrKeyFind {
alias,
unsafe_show_secret,
is_pre_genesis,
}: args::AddrKeyFind,
) {
let mut wallet = ctx.wallet;
let mut wallet = load_wallet(ctx, is_pre_genesis);
let alias = alias.to_lowercase();
if let Ok(viewing_key) = wallet.find_viewing_key(&alias) {
// Check if alias is a viewing key
Expand Down Expand Up @@ -105,9 +109,10 @@ fn spending_keys_list(
args::MaspKeysList {
decrypt,
unsafe_show_secret,
is_pre_genesis,
}: args::MaspKeysList,
) {
let wallet = ctx.wallet;
let wallet = load_wallet(ctx, is_pre_genesis);
let known_view_keys = wallet.get_viewing_keys();
let known_spend_keys = wallet.get_spending_keys();
if known_view_keys.is_empty() {
Expand Down Expand Up @@ -171,8 +176,11 @@ fn spending_keys_list(
}

/// List payment addresses.
fn payment_addresses_list(ctx: Context) {
let wallet = ctx.wallet;
fn payment_addresses_list(
ctx: Context,
args::MaspListPayAddrs { is_pre_genesis }: args::MaspListPayAddrs,
) {
let wallet = load_wallet(ctx, is_pre_genesis);
let known_addresses = wallet.get_payment_addrs();
if known_addresses.is_empty() {
println!(
Expand All @@ -194,10 +202,11 @@ fn spending_key_gen(
ctx: Context,
args::MaspSpendKeyGen {
alias,
is_pre_genesis,
unsafe_dont_encrypt,
}: args::MaspSpendKeyGen,
) {
let mut wallet = ctx.wallet;
let mut wallet = load_wallet(ctx, is_pre_genesis);
let alias = alias.to_lowercase();
let (alias, _key) = wallet.gen_spending_key(alias, unsafe_dont_encrypt);
wallet.save().unwrap_or_else(|err| eprintln!("{}", err));
Expand All @@ -209,23 +218,28 @@ fn spending_key_gen(

/// Generate a shielded payment address from the given key.
fn payment_address_gen(
mut ctx: Context,
ctx: Context,
args::MaspPayAddrGen {
alias,
viewing_key,
pin,
is_pre_genesis,
}: args::MaspPayAddrGen,
) {
let mut wallet = load_wallet(ctx, is_pre_genesis);
let alias = alias.to_lowercase();
let viewing_key =
ExtendedFullViewingKey::from(ctx.get_cached(&viewing_key))
.fvk
.vk;
let viewing_key = wallet
.find_viewing_key(&viewing_key.raw)
.map(Clone::clone)
.unwrap_or_else(|_| {
eprintln!("Unknown viewing key {}", viewing_key.raw);
cli::safe_exit(1)
});
let viewing_key = ExtendedFullViewingKey::from(viewing_key).fvk.vk;
let (div, _g_d) = find_valid_diversifier(&mut OsRng);
let payment_addr = viewing_key
.to_payment_address(div)
.expect("a PaymentAddress");
let mut wallet = ctx.wallet;
let alias = wallet
.insert_payment_addr(
alias,
Expand All @@ -244,18 +258,19 @@ fn payment_address_gen(

/// Add a viewing key, spending key, or payment address to wallet.
fn address_key_add(
mut ctx: Context,
ctx: Context,
args::MaspAddrKeyAdd {
alias,
value,
is_pre_genesis,
unsafe_dont_encrypt,
}: args::MaspAddrKeyAdd,
) {
let alias = alias.to_lowercase();
let mut wallet = load_wallet(ctx, is_pre_genesis);
let (alias, typ) = match value {
MaspValue::FullViewingKey(viewing_key) => {
let alias = ctx
.wallet
let alias = wallet
.insert_viewing_key(alias, viewing_key)
.unwrap_or_else(|| {
eprintln!("Viewing key not added");
Expand All @@ -264,8 +279,7 @@ fn address_key_add(
(alias, "viewing key")
}
MaspValue::ExtendedSpendingKey(spending_key) => {
let alias = ctx
.wallet
let alias = wallet
.encrypt_insert_spending_key(
alias,
spending_key,
Expand All @@ -278,8 +292,7 @@ fn address_key_add(
(alias, "spending key")
}
MaspValue::PaymentAddress(payment_addr) => {
let alias = ctx
.wallet
let alias = wallet
.insert_payment_addr(alias, payment_addr)
.unwrap_or_else(|| {
eprintln!("Payment address not added");
Expand All @@ -288,7 +301,7 @@ fn address_key_add(
(alias, "payment address")
}
};
ctx.wallet.save().unwrap_or_else(|err| eprintln!("{}", err));
wallet.save().unwrap_or_else(|err| eprintln!("{}", err));
println!(
"Successfully added a {} with the following alias to wallet: {}",
typ, alias,
Expand All @@ -302,10 +315,11 @@ fn key_and_address_gen(
args::KeyAndAddressGen {
scheme,
alias,
is_pre_genesis,
unsafe_dont_encrypt,
}: args::KeyAndAddressGen,
) {
let mut wallet = ctx.wallet;
let mut wallet = load_wallet(ctx, is_pre_genesis);
let (alias, _key) = wallet.gen_key(scheme, alias, unsafe_dont_encrypt);
wallet.save().unwrap_or_else(|err| eprintln!("{}", err));
println!(
Expand All @@ -321,10 +335,11 @@ fn key_find(
public_key,
alias,
value,
is_pre_genesis,
unsafe_show_secret,
}: args::KeyFind,
) {
let mut wallet = ctx.wallet;
let mut wallet = load_wallet(ctx, is_pre_genesis);
let found_keypair = match public_key {
Some(pk) => wallet.find_key_by_pk(&pk),
None => {
Expand Down Expand Up @@ -361,10 +376,11 @@ fn key_list(
ctx: Context,
args::KeyList {
decrypt,
is_pre_genesis,
unsafe_show_secret,
}: args::KeyList,
) {
let wallet = ctx.wallet;
let wallet = load_wallet(ctx, is_pre_genesis);
let known_keys = wallet.get_keys();
if known_keys.is_empty() {
println!(
Expand Down Expand Up @@ -406,8 +422,14 @@ fn key_list(
}

/// Export a keypair to a file.
fn key_export(ctx: Context, args::KeyExport { alias }: args::KeyExport) {
let mut wallet = ctx.wallet;
fn key_export(
ctx: Context,
args::KeyExport {
alias,
is_pre_genesis,
}: args::KeyExport,
) {
let mut wallet = load_wallet(ctx, is_pre_genesis);
wallet
.find_key(alias.to_lowercase())
.map(|keypair| {
Expand All @@ -427,8 +449,11 @@ fn key_export(ctx: Context, args::KeyExport { alias }: args::KeyExport) {
}

/// List all known addresses.
fn address_list(ctx: Context) {
let wallet = ctx.wallet;
fn address_list(
ctx: Context,
args::AddressList { is_pre_genesis }: args::AddressList,
) {
let wallet = load_wallet(ctx, is_pre_genesis);
let known_addresses = wallet.get_addresses();
if known_addresses.is_empty() {
println!(
Expand All @@ -447,50 +472,71 @@ fn address_list(ctx: Context) {
}

/// Find address (alias) by its alias (address).
fn address_or_alias_find(ctx: Context, args: args::AddressOrAliasFind) {
let wallet = ctx.wallet;
if args.address.is_some() && args.alias.is_some() {
fn address_or_alias_find(
ctx: Context,
args::AddressOrAliasFind {
alias,
address,
is_pre_genesis,
}: args::AddressOrAliasFind,
) {
let wallet = load_wallet(ctx, is_pre_genesis);
if address.is_some() && alias.is_some() {
panic!(
"This should not be happening: clap should emit its own error \
message."
);
} else if args.alias.is_some() {
if let Some(address) = wallet.find_address(args.alias.as_ref().unwrap())
{
} else if alias.is_some() {
if let Some(address) = wallet.find_address(alias.as_ref().unwrap()) {
println!("Found address {}", address.to_pretty_string());
} else {
println!(
"No address with alias {} found. Use the command `address \
list` to see all the known addresses.",
args.alias.unwrap().to_lowercase()
alias.unwrap().to_lowercase()
);
}
} else if args.address.is_some() {
if let Some(alias) = wallet.find_alias(args.address.as_ref().unwrap()) {
} else if address.is_some() {
if let Some(alias) = wallet.find_alias(address.as_ref().unwrap()) {
println!("Found alias {}", alias);
} else {
println!(
"No alias with address {} found. Use the command `address \
list` to see all the known addresses.",
args.address.unwrap()
address.unwrap()
);
}
}
}

/// Add an address to the wallet.
fn address_add(ctx: Context, args: args::AddressAdd) {
let mut wallet = ctx.wallet;
if wallet
.add_address(args.alias.clone().to_lowercase(), args.address)
.is_none()
{
fn address_add(
ctx: Context,
args::AddressAdd {
alias,
address,
is_pre_genesis,
}: args::AddressAdd,
) {
let mut wallet = load_wallet(ctx, is_pre_genesis);
if wallet.add_address(alias.to_lowercase(), address).is_none() {
eprintln!("Address not added");
cli::safe_exit(1);
}
wallet.save().unwrap_or_else(|err| eprintln!("{}", err));
println!(
"Successfully added a key and an address with alias: \"{}\"",
args.alias.to_lowercase()
alias.to_lowercase()
);
}

/// Load wallet for chain when `ctx.chain.is_some()` or pre-genesis wallet when
/// `is_pre_genesis || ctx.chain.is_none()`.
fn load_wallet(ctx: Context, is_pre_genesis: bool) -> Wallet {
if is_pre_genesis || ctx.chain.is_none() {
let wallet_path = ctx.global_args.base_dir.join(PRE_GENESIS_DIR);
Wallet::load_or_new(&wallet_path)
} else {
ctx.take_chain_or_exit().wallet
}
}
Loading

0 comments on commit 182b275

Please sign in to comment.