From cd1fd8b8fc83d2d242d547b88d50f0853b1c8e61 Mon Sep 17 00:00:00 2001 From: Andy Nogueira Date: Thu, 19 Nov 2020 17:23:29 -0500 Subject: [PATCH] Added 'keys list' command to show key added on a chain (#363) --- relayer-cli/README.md | 14 ++++--- relayer-cli/src/commands/keys.rs | 7 +++- relayer-cli/src/commands/keys/add.rs | 6 +-- relayer-cli/src/commands/keys/list.rs | 55 +++++++++++++++++++++++++++ relayer/src/keys.rs | 1 + relayer/src/keys/list.rs | 32 ++++++++++++++++ 6 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 relayer-cli/src/commands/keys/list.rs create mode 100644 relayer/src/keys/list.rs diff --git a/relayer-cli/README.md b/relayer-cli/README.md index 270619ad9b..ea50e8dc1e 100644 --- a/relayer-cli/README.md +++ b/relayer-cli/README.md @@ -27,7 +27,7 @@ Note: This is work in progress, more commands will be implemented and tested wit * Start two chains using the `/scripts/two-chainz` script from the [cosmos/relayer](https://github.com/cosmos/relayer) (make sure to checkout the latest release, currently 0.6.1) -* After you run the script, the Go relayer will create a `data` folder for the chains. Add the key seed file (shown below) `[GO RELAYER DATA FOLDER]/data/ibc1/key_seed.json` for chain `ibc-1` to the relayer config folder ($HOME/.rrly) using the `keys add` command below +* After you run the script, the Go relayer will create a `data` folder for the chains. Add the key seed file (shown below) `[GO RELAYER DATA FOLDER]/data/ibc-1/key_seed.json` for chain `ibc-1` to the relayer config folder ($HOME/.rrly) using the `keys add` command below { "name":"user", @@ -37,15 +37,17 @@ Note: This is work in progress, more commands will be implemented and tested wit "mnemonic":"[MNEMONIC WORDS"} } - Add the key using the `keys add` command. The key file will be added exactly the above (json format and unencrypted). Later when we have a proper keyring the key file will be safely stored but for now just ensure you're using this on non-production systems. +* Add the key using the `relayer-clie keys add` command. The key file will be added exactly the above (json format and unencrypted). Later when we have a proper keyring the key file will be safely stored but for now just ensure you're using this on non-production systems. `$ cargo run --bin relayer -- -c ./relayer-cli/tests/fixtures/two_chains.toml keys add ibc-1 [GO RELAYER DATA FOLDER]/data/ibc-1/key_seed.json` - After you add the key, you can check if they were properly added using the command `tree $HOME/.rrly` +* After you add the key, you can check if they were properly added using the command: + + `$ cargo run --bin relayer -- -c ./relayer-cli/tests/fixtures/two_chains.toml keys list ibc-1` -* Run the transaction command. In this example, it will try to initialize an `ibczeroconn2` connection on chain `ibc1` +* Run the transaction command. In this example, it will try to initialize an `ibczeroconn2` connection on chain `ibc-1` - `$ cargo run --bin relayer -- -c ./relayer-cli/tests/fixtures/two_chains.toml tx raw conn-init ibc1 ibc0 ibczeroclient ibconeclient ibczeroconn2 -d ibconeconn` + `$ cargo run --bin relayer -- -c ./relayer-cli/tests/fixtures/two_chains.toml tx raw conn-init ibc-1 ibc-0 ibczeroclient ibconeclient ibczeroconn2 -d ibconeconn` If you get an empty response it means the tx worked @@ -53,7 +55,7 @@ Note: This is work in progress, more commands will be implemented and tested wit * Check if the connection was created on `ibc-1`: - `$ cargo run --bin relayer -- -c ./relayer-cli/tests/fixtures/two_chains.toml query connection end ibc1 ibczeroconn2 | jq .` + `$ cargo run --bin relayer -- -c ./relayer-cli/tests/fixtures/two_chains.toml query connection end ibc-1 ibczeroconn2 | jq .` If you see an entry in the JSON file that points to the `ibczeroconn2` connection with state `STATE_INIT` it confirms that the transaction worked: diff --git a/relayer-cli/src/commands/keys.rs b/relayer-cli/src/commands/keys.rs index 6ec307f645..56c81174bb 100644 --- a/relayer-cli/src/commands/keys.rs +++ b/relayer-cli/src/commands/keys.rs @@ -2,6 +2,7 @@ use abscissa_core::{Command, Help, Options, Runnable}; mod add; +mod list; mod restore; /// `keys` subcommand @@ -13,7 +14,11 @@ pub enum KeysCmd { /// The `keys add` subcommand #[options(help = "adds a key to a configured chain")] - Add(add::KeyAddCmd), + Add(add::KeysAddCmd), + + /// The `keys list` subcommand + #[options(help = "list keys configured on a chain")] + List(list::KeysListCmd), ///// The `keys restore` subcommand // #[options(help = "restore a key to a configured chain using a mnemonic")] // Restore(restore::KeyRestoreCmd), diff --git a/relayer-cli/src/commands/keys/add.rs b/relayer-cli/src/commands/keys/add.rs index 4f3aaeb678..f70b612a00 100644 --- a/relayer-cli/src/commands/keys/add.rs +++ b/relayer-cli/src/commands/keys/add.rs @@ -7,7 +7,7 @@ use crate::prelude::*; use relayer::keys::add::{add_key, KeysAddOptions}; #[derive(Clone, Command, Debug, Options)] -pub struct KeyAddCmd { +pub struct KeysAddCmd { #[options(free, help = "identifier of the chain")] chain_id: Option, @@ -15,7 +15,7 @@ pub struct KeyAddCmd { file: Option, } -impl KeyAddCmd { +impl KeysAddCmd { fn validate_options(&self, config: &Config) -> Result { let chain_id = self .chain_id @@ -43,7 +43,7 @@ impl KeyAddCmd { } } -impl Runnable for KeyAddCmd { +impl Runnable for KeysAddCmd { fn run(&self) { let config = app_config(); diff --git a/relayer-cli/src/commands/keys/list.rs b/relayer-cli/src/commands/keys/list.rs new file mode 100644 index 0000000000..aa282d8c4f --- /dev/null +++ b/relayer-cli/src/commands/keys/list.rs @@ -0,0 +1,55 @@ +use crate::application::app_config; +use abscissa_core::{Command, Options, Runnable}; +use relayer::config::Config; + +use crate::error::{Error, Kind}; +use crate::prelude::*; +use relayer::keys::list::{list_keys, KeysListOptions}; + +#[derive(Clone, Command, Debug, Options)] +pub struct KeysListCmd { + #[options(free, help = "identifier of the chain")] + chain_id: Option, +} + +impl KeysListCmd { + fn validate_options(&self, config: &Config) -> Result { + let chain_id = self + .chain_id + .clone() + .ok_or_else(|| "missing chain identifier".to_string())?; + + let chain_config = config + .chains + .iter() + .find(|c| c.id == chain_id.parse().unwrap()) + .ok_or_else(|| { + "Invalid chain identifier. Cannot retrieve the chain configuration".to_string() + })?; + + Ok(KeysListOptions { + chain_config: chain_config.clone(), + }) + } +} + +impl Runnable for KeysListCmd { + fn run(&self) { + let config = app_config(); + + let opts = match self.validate_options(&config) { + Err(err) => { + status_err!("invalid options: {}", err); + return; + } + Ok(result) => result, + }; + + let res: Result = list_keys(opts).map_err(|e| Kind::Keys.context(e).into()); + + match res { + Ok(r) => status_info!("keys list result: ", "{:?}", r), + Err(e) => status_info!("keys list failed: ", "{}", e), + } + } +} diff --git a/relayer/src/keys.rs b/relayer/src/keys.rs index 96cd1fcab3..628017c3fe 100644 --- a/relayer/src/keys.rs +++ b/relayer/src/keys.rs @@ -1,2 +1,3 @@ pub mod add; +pub mod list; pub mod restore; diff --git a/relayer/src/keys/list.rs b/relayer/src/keys/list.rs new file mode 100644 index 0000000000..594d587c35 --- /dev/null +++ b/relayer/src/keys/list.rs @@ -0,0 +1,32 @@ +use crate::chain::{Chain, CosmosSDKChain}; +use crate::config::ChainConfig; +use crate::error; +use crate::error::{Error, Kind}; +use crate::keyring::store::{KeyRing, KeyRingOperations}; +use futures::AsyncReadExt; +use std::fs; +use std::fs::File; +use std::io::Write; +use std::path::{Path, PathBuf}; + +#[derive(Clone, Debug)] +pub struct KeysListOptions { + pub chain_config: ChainConfig, +} + +pub fn list_keys(opts: KeysListOptions) -> Result { + // Get the destination chain + let chain = CosmosSDKChain::from_config(opts.chain_config)?; + + let key_entry = chain.keybase().get_key(); + + match key_entry { + Ok(k) => Ok(format!( + "chain: {} -> {} ({})", + chain.config().id.clone(), + chain.config().key_name.clone(), + k.account.as_str(), + )), + Err(e) => Err(Kind::KeyBase.context(e).into()), + } +}