-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'keys list' command to show key added on a chain (#363)
- Loading branch information
Showing
6 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod add; | ||
pub mod list; | ||
pub mod restore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |