Skip to content

Commit

Permalink
Added 'keys list' command to show key added on a chain (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
andynog committed Nov 19, 2020
1 parent ce1aa53 commit cd1fd8b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 10 deletions.
14 changes: 8 additions & 6 deletions relayer-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -37,23 +37,25 @@ 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

`conn init, result: []`

* 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:

Expand Down
7 changes: 6 additions & 1 deletion relayer-cli/src/commands/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use abscissa_core::{Command, Help, Options, Runnable};

mod add;
mod list;
mod restore;

/// `keys` subcommand
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions relayer-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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<String>,

#[options(free, help = "the key path and filename")]
file: Option<String>,
}

impl KeyAddCmd {
impl KeysAddCmd {
fn validate_options(&self, config: &Config) -> Result<KeysAddOptions, String> {
let chain_id = self
.chain_id
Expand Down Expand Up @@ -43,7 +43,7 @@ impl KeyAddCmd {
}
}

impl Runnable for KeyAddCmd {
impl Runnable for KeysAddCmd {
fn run(&self) {
let config = app_config();

Expand Down
55 changes: 55 additions & 0 deletions relayer-cli/src/commands/keys/list.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

impl KeysListCmd {
fn validate_options(&self, config: &Config) -> Result<KeysListOptions, String> {
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<String, Error> = 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),
}
}
}
1 change: 1 addition & 0 deletions relayer/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod add;
pub mod list;
pub mod restore;
32 changes: 32 additions & 0 deletions relayer/src/keys/list.rs
Original file line number Diff line number Diff line change
@@ -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<String, Error> {
// 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()),
}
}

0 comments on commit cd1fd8b

Please sign in to comment.