Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .changelog/unreleased/bug-fixes/3971-fix-config-from-env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Fix the prefix separator for config key-vals set from env var to a
single underscore between the "NAMADA" prefix and the rest of the path.
([\#3971](https://github.com/anoma/namada/pull/3971))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add new useful client utilities.
([\#3968](https://github.com/anoma/namada/pull/3968))
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ clap_complete_nushell = "4.5"
clru = {git = "https://github.com/marmeladema/clru-rs.git", rev = "71ca566"}
color-eyre = "0.6.2"
concat-idents = "1.1.2"
config = { git = "https://github.com/mehcode/config-rs.git", rev = "e3c1d0b452639478662a44f15ef6d5b6d969bf9b" }
config = "0.14.1"
data-encoding = "2.3.2"
derivation-path = "0.2.0"
derivative = "2.2.0"
Expand Down
93 changes: 93 additions & 0 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,8 @@ pub mod cmds {
ValidateGenesisTemplates(ValidateGenesisTemplates),
SignGenesisTxs(SignGenesisTxs),
ParseMigrationJson(MigrationJson),
DeriveIbcToken(DeriveIbcToken),
PubKeyToAddr(PubKeyToAddr),
}

impl SubCmd for ClientUtils {
Expand Down Expand Up @@ -2527,6 +2529,10 @@ pub mod cmds {
SubCmd::parse(matches).map(Self::SignGenesisTxs);
let parse_migrations_json =
SubCmd::parse(matches).map(Self::ParseMigrationJson);
let derive_ibc_token =
SubCmd::parse(matches).map(Self::DeriveIbcToken);
let pubkey_to_addr =
SubCmd::parse(matches).map(Self::PubKeyToAddr);
join_network
.or(validate_wasm)
.or(init_network)
Expand All @@ -2541,6 +2547,8 @@ pub mod cmds {
.or(genesis_tx)
.or(parse_migrations_json)
.or(sign_offline)
.or(derive_ibc_token)
.or(pubkey_to_addr)
})
}

Expand All @@ -2561,6 +2569,8 @@ pub mod cmds {
.subcommand(ValidateGenesisTemplates::def())
.subcommand(SignGenesisTxs::def())
.subcommand(MigrationJson::def())
.subcommand(DeriveIbcToken::def())
.subcommand(PubKeyToAddr::def())
.subcommand_required(true)
.arg_required_else_help(true)
}
Expand Down Expand Up @@ -2607,6 +2617,51 @@ pub mod cmds {
}
}

#[derive(Clone, Debug)]
pub struct DeriveIbcToken(pub args::DeriveIbcToken);

impl SubCmd for DeriveIbcToken {
const CMD: &'static str = "derive-token-address";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches
.subcommand_matches(Self::CMD)
.map(|matches| Self(args::DeriveIbcToken::parse(matches)))
}

fn def() -> App {
App::new(Self::CMD)
.about(wrap!(
"Derive the IBC token address on Namada from the denom \
string, typically in the form of \
'transfer/channel-<num>/<name>'."
))
.add_args::<args::DeriveIbcToken>()
}
}

#[derive(Clone, Debug)]
pub struct PubKeyToAddr(pub args::PubKeyToAddr);

impl SubCmd for PubKeyToAddr {
const CMD: &'static str = "pubkey-to-address";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches
.subcommand_matches(Self::CMD)
.map(|matches| Self(args::PubKeyToAddr::parse(matches)))
}

fn def() -> App {
App::new(Self::CMD)
.about(wrap!(
"Derive the implicit address (tnam) associated with a \
given public key (tpknam)."
))
.add_args::<args::PubKeyToAddr>()
}
}

#[derive(Clone, Debug)]
pub struct InitNetwork(pub args::InitNetwork);

Expand Down Expand Up @@ -3441,6 +3496,7 @@ pub mod args {
pub const HISTORIC: ArgFlag = flag("historic");
pub const IBC_SHIELDING_DATA_PATH: ArgOpt<PathBuf> =
arg_opt("ibc-shielding-data");
pub const IBC_DENOM: Arg<String> = arg("ibc-denom");
pub const IBC_MEMO: ArgOpt<String> = arg_opt("ibc-memo");
pub const INPUT_OPT: ArgOpt<PathBuf> = arg_opt("input");
pub const LEDGER_ADDRESS_ABOUT: &str = textwrap_macros::fill!(
Expand Down Expand Up @@ -8274,6 +8330,43 @@ pub mod args {
}
}

#[derive(Clone, Debug)]
pub struct DeriveIbcToken {
pub ibc_denom: String,
}

impl Args for DeriveIbcToken {
fn parse(matches: &ArgMatches) -> Self {
let ibc_denom: String = IBC_DENOM.parse(matches);
Self { ibc_denom }
}

fn def(app: App) -> App {
app.arg(IBC_DENOM.def().help(wrap!(
"The IBC token string, in the format of \
'transfer/channel-<num>/<name>"
)))
}
}

#[derive(Clone, Debug)]
pub struct PubKeyToAddr {
pub public_key: common::PublicKey,
}

impl Args for PubKeyToAddr {
fn parse(matches: &ArgMatches) -> Self {
let public_key: common::PublicKey = RAW_PUBLIC_KEY.parse(matches);
Self { public_key }
}

fn def(app: App) -> App {
app.arg(RAW_PUBLIC_KEY.def().help(wrap!(
"The raw public key to be converted into an implicit address"
)))
}
}

#[derive(Clone, Debug)]
pub struct InitNetwork {
pub templates_path: PathBuf,
Expand Down
6 changes: 6 additions & 0 deletions crates/apps_lib/src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,12 @@ impl CliApi {
)
}
}
ClientUtils::DeriveIbcToken(DeriveIbcToken(args)) => {
utils::derive_ibc_token_address(args);
}
ClientUtils::PubKeyToAddr(PubKeyToAddr(args)) => {
utils::pubkey_to_address(args);
}
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion crates/apps_lib/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use flate2::write::GzEncoder;
use flate2::Compression;
use itertools::Either;
use namada_sdk::account::AccountPublicKeysMap;
use namada_sdk::address::Address;
use namada_sdk::address::{Address, ImplicitAddress};
use namada_sdk::args::DeviceTransport;
use namada_sdk::chain::ChainId;
use namada_sdk::dec::Dec;
use namada_sdk::ibc::trace::ibc_token;
use namada_sdk::key::*;
use namada_sdk::string_encoding::StringEncoded;
use namada_sdk::token;
Expand Down Expand Up @@ -1164,3 +1165,20 @@ fn safe_exit(code: i32) -> ! {
fn safe_exit(code: i32) -> ! {
panic!("Process exited unsuccessfully with error code: {}", code);
}

/// Derive the IBC token address on namada from the denom string
pub fn derive_ibc_token_address(
args::DeriveIbcToken { ibc_denom }: args::DeriveIbcToken,
) {
let token_address = ibc_token(&ibc_denom);
println!("{token_address}");
}

/// Derive the implicit address from a raw public key
pub fn pubkey_to_address(
args::PubKeyToAddr { public_key }: args::PubKeyToAddr,
) {
let pkh = PublicKeyHash::from(&public_key);
let addr = Address::Implicit(ImplicitAddress(pkh.clone()));
println!("{addr}");
}
33 changes: 31 additions & 2 deletions crates/apps_lib/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ impl Config {
.add_source(defaults)
.add_source(config::File::with_name(file_name))
.add_source(
config::Environment::with_prefix("NAMADA").separator("__"),
config::Environment::with_prefix("NAMADA")
.prefix_separator("_")
.separator("__"),
);

let config = builder.build().map_err(Error::ReadError)?;
Expand Down Expand Up @@ -854,11 +856,38 @@ namespace = "cometbft"

#[cfg(test)]
mod tests {
use super::DEFAULT_COMETBFT_CONFIG;
use std::env;

use namada_sdk::chain::ChainId;
use tempfile::tempdir;

use super::{Config, DEFAULT_COMETBFT_CONFIG};
use crate::config::TendermintMode;
use crate::tendermint_config::TendermintConfig;

#[test]
fn test_default_cometbft_config() {
assert!(TendermintConfig::parse_toml(DEFAULT_COMETBFT_CONFIG).is_ok());
}

/// Check that a key-val set from an env var gets applied to a loaded config
#[test]
fn test_config_env_var() {
let base_dir = tempdir().unwrap();
let chain_id = ChainId("ChainyMcChainFace".to_owned());

Config::generate(
base_dir.path(),
&chain_id,
TendermintMode::Full,
true,
)
.unwrap();

let moniker = "moniker_from_env";
env::set_var("NAMADA_LEDGER__COMETBFT__MONIKER", moniker);
let config = Config::load(&base_dir, &chain_id, None);

assert_eq!(config.ledger.cometbft.moniker.as_ref(), moniker);
}
}
Loading