Skip to content

Commit

Permalink
key add command for memory store working (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
andynog committed Nov 11, 2020
1 parent 3c89481 commit 1bc1046
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
15 changes: 2 additions & 13 deletions relayer-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use relayer::config::Config;
use crate::error::{Error, Kind};
use crate::prelude::*;
use relayer::keys::add::{add_key, KeysAddOptions};
use std::fs;
use std::path::Path;

#[derive(Clone, Command, Debug, Options)]
pub struct KeyAddCmd {
Expand Down Expand Up @@ -40,23 +38,14 @@ impl KeyAddCmd {
.clone()
.ok_or_else(|| "missing key name".to_string())?;

// Get content of key seed file
let key_filename = self
.file
.clone()
.ok_or_else(|| "missing signer key file".to_string())?;

let key_file = Path::new(&key_filename).exists();
if !key_file {
return Err("cannot find key file specified".to_string());
}

let key_file_contents = fs::read_to_string(key_filename)
.expect("Something went wrong reading the key seed file");

Ok(KeysAddOptions {
name: key_name,
file: key_file_contents,
file: key_filename,
chain_config: chain_config.clone(),
})
}
Expand All @@ -74,7 +63,7 @@ impl Runnable for KeyAddCmd {
Ok(result) => result,
};

let res: Result<Vec<u8>, Error> = add_key(opts).map_err(|e| Kind::Keys.context(e).into());
let res: Result<String, Error> = add_key(opts).map_err(|e| Kind::Keys.context(e).into());

match res {
Ok(r) => status_info!("key add result: ", "{:?}", r),
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/keyring/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub type Error = anomaly::Error<Kind>;

#[derive(Clone, Debug, Error)]
pub enum Kind {
#[error("cannot retrieve key for address")]
#[error("invalid key")]
InvalidKey,

#[error("key already exists")]
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/keyring/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl KeyRingOperations for KeyRing {
match self {
KeyRing::MemoryKeyStore { store: s } => {
match s.get(&key_id) {
Some(s) => return Err(Kind::ExistingKey.context("Key already exists".to_string()))?,
Some(s) => return Err(Kind::ExistingKey.context("key already exists".to_string()))?,
None => {
s.insert(key_id, key.clone());
Ok(key)
Expand Down
22 changes: 12 additions & 10 deletions relayer/src/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::error::{Error, Kind};
use crate::keyring::store::{KeyRing, KeyRingOperations};
use std::fs;
use std::path::Path;
use futures::AsyncReadExt;

#[derive(Clone, Debug)]
pub struct KeysAddOptions {
Expand All @@ -13,30 +14,31 @@ pub struct KeysAddOptions {
pub chain_config: ChainConfig,
}

pub fn add_key(opts: KeysAddOptions) -> Result<Vec<u8>, Error> {
pub fn add_key(opts: KeysAddOptions) -> Result<String, Error> {
// TODO - Implement the logic to persist the key in the filesystem
// Get the destination chain
let mut chain = CosmosSDKChain::from_config(opts.clone().chain_config)?;

// Check if the key file exists
if fs::metadata(Path::new(&opts.file)).is_err() {
return Err(Kind::KeyBase.context("error reading the key file, file does not exist").into());
};
let file = Path::new(&opts.file);

let key_file_contents = fs::read_to_string(&opts.name).map_err(|e| Kind::KeyBase.context("error reading the key file"))?;
if !file.exists() {
return Err(Kind::Store.context("cannot find key file specified".to_string()))?;
}

let key_file_contents = fs::read_to_string(&file).map_err(|e| Kind::KeyBase.context("error reading the key file"))?;

let key_entry = chain.keybase.key_from_seed_file(&opts.file);
let key_entry = chain.keybase.key_from_seed_file(&key_file_contents);

match key_entry {
Ok(k) => {
chain
.keybase
.add(opts.name, k.clone())
.add(opts.name.clone(), k.clone())
.map_err(|e| error::Kind::KeyBase.context(e))?;
Ok(k.address)
Ok(format!("Added {} key - Account: {}", opts.name.as_str(), k.account.as_str()))
}
Err(e) => {
return Err(Kind::KeyBase.context("error reading the key file").into());
return Err(Kind::KeyBase.context(e).into());
}
}
}

0 comments on commit 1bc1046

Please sign in to comment.