Skip to content

Commit

Permalink
Merge branch 'jeongseup/add-consensus-key-to-tendermint-key' (#2516)
Browse files Browse the repository at this point in the history
* jeongseup/add-consensus-key-to-tendermint-key:
  changelog: add #2516
  make fmt
  Add a new cli 'wallet convert' for coverting tendermnint key json file with consesus key in wallet.toml
  • Loading branch information
tzemanovic committed Feb 15, 2024
2 parents 7da8d80 + 510fc85 commit 1535ac4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Added wallet command to "convert" a consensus key
into Tendermint private validator key JSON format.
([\#2516](https://github.com/anoma/namada/pull/2516))
41 changes: 41 additions & 0 deletions crates/apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ pub mod cmds {
KeyAddrFind(WalletFindKeysAddresses),
/// Key export
KeyExport(WalletExportKey),
/// Key convert
KeyConvert(WalletConvertKey),
/// Key import
KeyImport(WalletImportKey),
/// Key / address add
Expand All @@ -525,6 +527,7 @@ pub mod cmds {
.subcommand(WalletListKeysAddresses::def())
.subcommand(WalletFindKeysAddresses::def())
.subcommand(WalletExportKey::def())
.subcommand(WalletConvertKey::def())
.subcommand(WalletImportKey::def())
.subcommand(WalletAddKeyAddress::def())
.subcommand(WalletRemoveKeyAddress::def())
Expand All @@ -537,6 +540,7 @@ pub mod cmds {
let key_addr_list = SubCmd::parse(matches).map(Self::KeyAddrList);
let key_addr_find = SubCmd::parse(matches).map(Self::KeyAddrFind);
let export = SubCmd::parse(matches).map(Self::KeyExport);
let convert = SubCmd::parse(matches).map(Self::KeyConvert);
let import = SubCmd::parse(matches).map(Self::KeyImport);
let key_addr_add = SubCmd::parse(matches).map(Self::KeyAddrAdd);
let key_addr_remove =
Expand All @@ -546,6 +550,7 @@ pub mod cmds {
.or(key_addr_list)
.or(key_addr_find)
.or(export)
.or(convert)
.or(import)
.or(key_addr_add)
.or(key_addr_remove)
Expand Down Expand Up @@ -712,6 +717,29 @@ pub mod cmds {
}
}

/// Export key to a file
#[derive(Clone, Debug)]
pub struct WalletConvertKey(pub args::KeyConvert);

impl SubCmd for WalletConvertKey {
const CMD: &'static str = "convert";

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

fn def() -> App {
App::new(Self::CMD)
.about(
"Convert to tendermint priv_validator_key.json with your \
consensus key alias",
)
.add_args::<args::KeyConvert>()
}
}

/// Import key from a file
#[derive(Clone, Debug)]
pub struct WalletImportKey(pub args::KeyImport);
Expand Down Expand Up @@ -6696,6 +6724,19 @@ pub mod args {
}
}

impl Args for KeyConvert {
fn parse(matches: &ArgMatches) -> Self {
let alias = ALIAS.parse(matches);
Self { alias }
}

fn def(app: App) -> App {
app.arg(
ALIAS.def().help("The alias of the key you wish to export."),
)
}
}

impl Args for KeyImport {
fn parse(matches: &ArgMatches) -> Self {
let file_path = FILE_PATH.parse(matches);
Expand Down
23 changes: 23 additions & 0 deletions crates/apps/src/lib/cli/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::cli::api::CliApi;
use crate::cli::args::CliToSdk;
use crate::cli::{args, cmds, Context};
use crate::client::utils::PRE_GENESIS_DIR;
use crate::node::ledger::tendermint_node::validator_key_to_json;
use crate::wallet::{
self, read_and_confirm_encryption_password, CliWalletUtils,
};
Expand All @@ -54,6 +55,9 @@ impl CliApi {
cmds::NamadaWallet::KeyExport(cmds::WalletExportKey(args)) => {
key_export(ctx, io, args)
}
cmds::NamadaWallet::KeyConvert(cmds::WalletConvertKey(args)) => {
key_convert(ctx, io, args)
}
cmds::NamadaWallet::KeyImport(cmds::WalletImportKey(args)) => {
key_import(ctx, io, args)
}
Expand Down Expand Up @@ -1245,6 +1249,25 @@ fn key_export(
})
}

/// Convert a consensus key to tendermint validator key in json format
fn key_convert(
ctx: Context,
io: &impl Io,
args::KeyConvert { alias }: args::KeyConvert,
) {
let alias = alias.to_lowercase();
let mut wallet = load_wallet(ctx);
let sk = wallet.find_secret_key(&alias, None);
let key: serde_json::Value = validator_key_to_json(&sk.unwrap()).unwrap();
let file_name = format!("priv_validator_key.json_{}", alias);
let file = File::create(&file_name).unwrap();
serde_json::to_writer_pretty(file, &key).unwrap_or_else(|err| {
edisplay_line!(io, "{}", err);
cli::safe_exit(1)
});
display_line!(io, "Converted to file {}", file_name);
}

/// Import a transparent keypair / MASP spending key from a file.
fn key_import(
ctx: Context,
Expand Down
4 changes: 2 additions & 2 deletions crates/apps/src/lib/node/ledger/tendermint_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ pub fn rollback(tendermint_dir: impl AsRef<Path>) -> Result<BlockHeight> {

/// Convert a common signing scheme validator key into JSON for
/// Tendermint
fn validator_key_to_json(
pub fn validator_key_to_json(
sk: &common::SecretKey,
) -> std::result::Result<serde_json::Value, ParseSecretKeyError> {
let raw_hash = tm_consensus_key_raw_hash(&sk.ref_to());
Expand Down Expand Up @@ -316,7 +316,7 @@ pub fn write_validator_state(home_dir: impl AsRef<Path>) -> Result<()> {
}

/// Abstract over the initialization of validator data for Tendermint
fn write_validator(
pub fn write_validator(
path: PathBuf,
err_dir: &'static str,
err_file: &'static str,
Expand Down
7 changes: 7 additions & 0 deletions crates/sdk/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,13 @@ pub struct KeyExport {
pub alias: String,
}

/// Wallet key export arguments
#[derive(Clone, Debug)]
pub struct KeyConvert {
/// Key alias
pub alias: String,
}

/// Wallet key import arguments
#[derive(Clone, Debug)]
pub struct KeyImport {
Expand Down

0 comments on commit 1535ac4

Please sign in to comment.