#[cfg(any(test, feature = "testing", feature = "benches"))]
pub use dev::{
addresses, albert_address, albert_keypair, bertha_address, bertha_keypair,
christel_address, christel_keypair, daewon_address, daewon_keypair,
derive_template_dir, ester_address, ester_keypair, frank_keypair,
get_unencrypted_keypair, is_use_device, keys, tokens,
validator_account_keypair, validator_address, validator_keypair,
validator_keys,
};
#[cfg(any(test, feature = "testing", feature = "benches"))]
mod dev {
use std::path::{Path, PathBuf};
use lazy_static::lazy_static;
use namada_sdk::address::Address;
use namada_sdk::address::testing::{
apfel, btc, dot, eth, kartoffel, nam, schnitzel,
};
use namada_sdk::collections::HashMap;
use namada_sdk::governance::pgf;
use namada_sdk::key::*;
use namada_sdk::wallet::Wallet;
use namada_sdk::wallet::alias::Alias;
use namada_sdk::wallet::pre_genesis::ValidatorWallet;
use namada_sdk::{governance, proof_of_stake};
use crate::wallet::CliWalletUtils;
pub fn validator_keys() -> (common::SecretKey, common::SecretKey) {
let protocol_key = VALIDATOR_WALLET
.store
.validator_keys
.protocol_keypair
.clone();
let eth_bridge_key = VALIDATOR_WALLET.eth_hot_key.clone();
(protocol_key, eth_bridge_key)
}
pub fn keys() -> HashMap<Alias, common::SecretKey> {
vec![
("albert".into(), albert_keypair()),
("bertha".into(), bertha_keypair()),
("christel".into(), christel_keypair()),
("frank".into(), frank_keypair()),
("daewon".into(), daewon_keypair()),
("ester".into(), ester_keypair()),
("validator".into(), validator_keypair()),
]
.into_iter()
.collect()
}
pub fn tokens() -> HashMap<Address, &'static str> {
vec![
(nam(), "NAM"),
(btc(), "BTC"),
(eth(), "ETH"),
(dot(), "DOT"),
(schnitzel(), "Schnitzel"),
(apfel(), "Apfel"),
(kartoffel(), "Kartoffel"),
]
.into_iter()
.collect()
}
pub fn addresses() -> HashMap<Alias, Address> {
let mut addresses: HashMap<Alias, Address> = vec![
("pos".into(), proof_of_stake::ADDRESS),
("pos_slash_pool".into(), proof_of_stake::SLASH_POOL_ADDRESS),
("governance".into(), governance::ADDRESS),
("governance".into(), pgf::ADDRESS),
("validator".into(), validator_address()),
("albert".into(), albert_address()),
("bertha".into(), bertha_address()),
("christel".into(), christel_address()),
("daewon".into(), daewon_address()),
("ester".into(), ester_address()),
("masp".into(), namada_sdk::address::MASP),
]
.into_iter()
.collect();
let token_addresses = tokens()
.into_iter()
.map(|(addr, alias)| (alias.into(), addr));
addresses.extend(token_addresses);
addresses
}
pub fn albert_address() -> Address {
PREGENESIS_WALLET
.find_address("albert")
.expect("Albert's address should be in the pre-genesis wallet")
.into_owned()
}
pub fn bertha_address() -> Address {
PREGENESIS_WALLET
.find_address("bertha")
.expect("Bertha's address should be in the pre-genesis wallet")
.into_owned()
}
pub fn christel_address() -> Address {
PREGENESIS_WALLET
.find_address("christel")
.expect("Christel's address should be in the pre-genesis wallet")
.into_owned()
}
pub fn daewon_address() -> Address {
(&daewon_keypair().ref_to()).into()
}
pub fn ester_address() -> Address {
(&ester_keypair().ref_to()).into()
}
pub fn validator_address() -> Address {
PREGENESIS_WALLET
.find_address("validator-0")
.expect(
"The zeroth validator's address should be in the pre-genesis \
wallet",
)
.into_owned()
}
pub fn get_unencrypted_keypair(name: &str) -> common::SecretKey {
let sk = match PREGENESIS_WALLET.get_secret_keys().get(name).unwrap().0
{
namada_sdk::wallet::StoredKeypair::Encrypted(_) => {
panic!("{name}'s keypair should not be encrypted")
}
namada_sdk::wallet::StoredKeypair::Raw(sk) => sk,
};
sk.clone()
}
pub fn albert_keypair() -> common::SecretKey {
get_unencrypted_keypair("albert-key")
}
pub fn bertha_keypair() -> common::SecretKey {
get_unencrypted_keypair("bertha-key")
}
pub fn christel_keypair() -> common::SecretKey {
get_unencrypted_keypair("christel-key")
}
pub fn daewon_keypair() -> common::SecretKey {
get_unencrypted_keypair("daewon")
}
pub fn ester_keypair() -> common::SecretKey {
get_unencrypted_keypair("ester")
}
pub fn frank_keypair() -> common::SecretKey {
get_unencrypted_keypair("frank-key")
}
pub fn validator_keypair() -> common::SecretKey {
VALIDATOR_WALLET.consensus_key.clone()
}
pub fn validator_account_keypair() -> common::SecretKey {
get_unencrypted_keypair("validator-0-account-key")
}
pub const ENV_VAR_USE_DEVICE: &str = "NAMADA_E2E_USE_DEVICE";
pub fn is_use_device() -> bool {
match std::env::var(ENV_VAR_USE_DEVICE) {
Ok(val) => !val.eq_ignore_ascii_case("false"),
_ => false,
}
}
pub fn derive_template_dir(working_dir: &Path) -> PathBuf {
if is_use_device() {
working_dir.join("genesis").join("hardware")
} else {
working_dir.join("genesis").join("localnet")
}
}
const PROJECT_ROOT_UNIQUE_FILE: &str = "rust-toolchain.toml";
const VALIDATOR_0_PREGENESIS_DIR: &str = "src/pre-genesis/validator-0";
lazy_static! {
static ref PREGENESIS_WALLET: Wallet<CliWalletUtils> = {
let mut root_dir = std::env::current_dir()
.expect("Current directory should exist")
.canonicalize()
.expect("Current directory should exist");
while !root_dir.join(PROJECT_ROOT_UNIQUE_FILE).exists() {
root_dir.pop();
}
let path = derive_template_dir(&root_dir).join("src/pre-genesis");
crate::wallet::load(&path).unwrap()
};
static ref VALIDATOR_WALLET: ValidatorWallet = {
let mut root_dir = std::env::current_dir()
.expect("Current directory should exist")
.canonicalize()
.expect("Current directory should exist");
while !root_dir.join(PROJECT_ROOT_UNIQUE_FILE).exists() {
root_dir.pop();
}
let path =
derive_template_dir(&root_dir).join(VALIDATOR_0_PREGENESIS_DIR);
crate::wallet::pre_genesis::load(&path).unwrap()
};
}
}