diff --git a/src/commands/cosmos_to_eth.rs b/src/commands/cosmos_to_eth.rs index bb4f1658..fefe85d8 100644 --- a/src/commands/cosmos_to_eth.rs +++ b/src/commands/cosmos_to_eth.rs @@ -64,7 +64,9 @@ impl Runnable for CosmosToEthCmd { let is_cosmos_originated = !denom.starts_with("gravity"); let amount: Uint256 = self.amount.parse().expect("cannot parse amount"); - let cosmos_key = config.load_gravity_deep_space_key(self.cosmos_key.to_string()); + let cosmos_key = config + .load_gravity_deep_space_key(self.cosmos_key.to_string()) + .expect("cannot load cosmos key"); let cosmos_prefix = config.cosmos.prefix.trim(); let cosmos_address = cosmos_key.to_address(cosmos_prefix).unwrap(); diff --git a/src/commands/deploy/erc20.rs b/src/commands/deploy/erc20.rs index 03bb76af..4098008b 100644 --- a/src/commands/deploy/erc20.rs +++ b/src/commands/deploy/erc20.rs @@ -44,7 +44,9 @@ impl Erc20 { async fn deploy(&self) { let config = APP.config(); - let ethereum_wallet = config.load_ethers_wallet(self.ethereum_key.clone()); + let ethereum_wallet = config + .load_ethers_wallet(self.ethereum_key.clone()) + .expect("failed to load wallet"); let contract_address = config .gravity .contract diff --git a/src/commands/eth_to_cosmos.rs b/src/commands/eth_to_cosmos.rs index c406da43..9a5ea510 100644 --- a/src/commands/eth_to_cosmos.rs +++ b/src/commands/eth_to_cosmos.rs @@ -46,7 +46,9 @@ impl Runnable for EthToCosmosCmd { .parse() .expect("Invalid ERC20 contract address!"); - let ethereum_wallet = config.load_ethers_wallet(self.ethereum_key.clone()); + let ethereum_wallet = config + .load_ethers_wallet(self.ethereum_key.clone()) + .expect("failed to load wallet"); let gravity_address: EthAddress = self .gravity_address diff --git a/src/commands/keys/cosmos/add.rs b/src/commands/keys/cosmos/add.rs index a21d7ef6..3887a2a3 100644 --- a/src/commands/keys/cosmos/add.rs +++ b/src/commands/keys/cosmos/add.rs @@ -25,7 +25,7 @@ impl Runnable for AddCosmosKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); if let Ok(_info) = keystore.info(&name) { if !self.overwrite { diff --git a/src/commands/keys/cosmos/delete.rs b/src/commands/keys/cosmos/delete.rs index d2fa0a59..61a92364 100644 --- a/src/commands/keys/cosmos/delete.rs +++ b/src/commands/keys/cosmos/delete.rs @@ -17,7 +17,7 @@ impl Runnable for DeleteCosmosKeyCmd { fn run(&self) { let config = APP.config(); let keystore = Path::new(&config.keystore); - let keystore = signatory::FsKeyStore::create_or_open(keystore).unwrap(); + let keystore = signatory::FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); FsKeyStore::delete(&keystore, &name).unwrap(); } diff --git a/src/commands/keys/cosmos/recover.rs b/src/commands/keys/cosmos/recover.rs index ef4bb8d7..17d81fb5 100644 --- a/src/commands/keys/cosmos/recover.rs +++ b/src/commands/keys/cosmos/recover.rs @@ -28,7 +28,7 @@ impl Runnable for RecoverCosmosKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); if let Ok(_info) = keystore.info(&name) { diff --git a/src/commands/keys/cosmos/rename.rs b/src/commands/keys/cosmos/rename.rs index b75251cc..69429794 100644 --- a/src/commands/keys/cosmos/rename.rs +++ b/src/commands/keys/cosmos/rename.rs @@ -23,7 +23,7 @@ impl Runnable for RenameCosmosKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = signatory::FsKeyStore::create_or_open(keystore).unwrap(); + let keystore = signatory::FsKeyStore::open(keystore).unwrap(); let name = self.name.parse().expect("Could not parse name"); let new_name = self.new_name.parse().expect("Could not parse new_name"); diff --git a/src/commands/keys/cosmos/show.rs b/src/commands/keys/cosmos/show.rs index 5394bde4..09568069 100644 --- a/src/commands/keys/cosmos/show.rs +++ b/src/commands/keys/cosmos/show.rs @@ -15,8 +15,9 @@ impl Runnable for ShowCosmosKeyCmd { fn run(&self) { let config = APP.config(); let name = self.name.clone(); - let key = config.load_deep_space_key(name.clone()); - + let key = config + .load_deep_space_key(name.clone()) + .expect("Could not load key"); let address = key .to_address(config.cosmos.prefix.trim()) .expect("Could not generate public key"); diff --git a/src/commands/keys/eth/add.rs b/src/commands/keys/eth/add.rs index 60c01f9d..69c1da2c 100644 --- a/src/commands/keys/eth/add.rs +++ b/src/commands/keys/eth/add.rs @@ -24,7 +24,7 @@ impl Runnable for AddKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); if let Ok(_info) = keystore.info(&name) { diff --git a/src/commands/keys/eth/delete.rs b/src/commands/keys/eth/delete.rs index 3fb0fcad..da57d084 100644 --- a/src/commands/keys/eth/delete.rs +++ b/src/commands/keys/eth/delete.rs @@ -16,7 +16,7 @@ impl Runnable for DeleteKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); keystore.delete(&name).expect("Could not delete key"); diff --git a/src/commands/keys/eth/import.rs b/src/commands/keys/eth/import.rs index 5d660e16..10781241 100644 --- a/src/commands/keys/eth/import.rs +++ b/src/commands/keys/eth/import.rs @@ -28,7 +28,7 @@ impl Runnable for ImportEthKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); if let Ok(_info) = keystore.info(&name) { diff --git a/src/commands/keys/eth/rename.rs b/src/commands/keys/eth/rename.rs index fb202bc6..f3a8a338 100644 --- a/src/commands/keys/eth/rename.rs +++ b/src/commands/keys/eth/rename.rs @@ -23,7 +23,7 @@ impl Runnable for RenameKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); let new_name = self.new_name.parse().expect("Could not parse new_name"); diff --git a/src/commands/keys/eth/show.rs b/src/commands/keys/eth/show.rs index f4a74d95..0f77808e 100644 --- a/src/commands/keys/eth/show.rs +++ b/src/commands/keys/eth/show.rs @@ -18,7 +18,7 @@ impl Runnable for ShowKeyCmd { fn run(&self) { let config = APP.config(); let keystore = path::Path::new(&config.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = FsKeyStore::open(keystore).expect("Could not open keystore"); let name = self.name.parse().expect("Could not parse name"); let key = keystore.load(&name).expect("Could not load key"); diff --git a/src/commands/orchestrator/start.rs b/src/commands/orchestrator/start.rs index 18234d6f..7017b37f 100644 --- a/src/commands/orchestrator/start.rs +++ b/src/commands/orchestrator/start.rs @@ -42,10 +42,14 @@ impl Runnable for StartCommand { let config = APP.config(); let cosmos_prefix = config.cosmos.prefix.clone(); - let cosmos_key = config.load_gravity_deep_space_key(self.cosmos_key.clone()); + let cosmos_key = config + .load_gravity_deep_space_key(self.cosmos_key.clone()) + .expect("failed to load key"); let cosmos_address = cosmos_key.to_address(&cosmos_prefix).unwrap(); - let ethereum_wallet = config.load_ethers_wallet(self.ethereum_key.clone()); + let ethereum_wallet = config + .load_ethers_wallet(self.ethereum_key.clone()) + .expect("failed to load wallet"); let ethereum_address = ethereum_wallet.address(); let contract_address: EthAddress = config diff --git a/src/commands/sign_delegate_keys.rs b/src/commands/sign_delegate_keys.rs index 93737011..bb3e53be 100644 --- a/src/commands/sign_delegate_keys.rs +++ b/src/commands/sign_delegate_keys.rs @@ -20,7 +20,9 @@ impl Runnable for SignDelegateKeysCmd { fn run(&self) { let config = APP.config(); abscissa_tokio::run_with_actix(&APP, async { - let key = config.load_clarity_key(self.ethereum_key.clone()); + let key = config + .load_clarity_key(self.ethereum_key.clone()) + .expect("failed to load key"); let address = self.val_address.parse().expect("Could not parse address"); let nonce: u64 = match self.nonce { diff --git a/src/config.rs b/src/config.rs index 89a0c4ed..19172b35 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ //! See instructions in `commands.rs` to specify the path to your //! application's configuration file and/or command-line options //! for specifying it. -use crate::{prelude::APP, somm_send::MAX_GAS_PER_BLOCK}; +use crate::{error::ErrorKind, prelude::APP, somm_send::MAX_GAS_PER_BLOCK}; use abscissa_core::Application; use deep_space::{Address as CosmosAddress, PrivateKey as CosmosPrivateKey}; use ethers::signers::LocalWallet as EthWallet; @@ -17,7 +17,9 @@ lazy_static! { static ref DELEGATE_KEY: CosmosPrivateKey = { let config = APP.config(); let name = &config.keys.delegate_key; - config.load_deep_space_key(name.clone()) + config + .load_deep_space_key(name.clone()) + .expect("failed to load delegate key") }; static ref DELEGATE_ADDRESS: CosmosAddress = { let config = APP.config(); @@ -54,33 +56,76 @@ pub struct StewardConfig { } impl StewardConfig { - fn load_secret_key(&self, name: String) -> k256::elliptic_curve::SecretKey { + fn load_secret_key( + &self, + name: String, + ) -> Result, crate::error::Error> { let keystore = Path::new(&self.keystore); - let keystore = FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); + let keystore = match FsKeyStore::open(keystore) { + Ok(keystore) => keystore, + Err(signatory::Error::NotADirectory) => { + return Err(ErrorKind::Config + .context("keystore path is not a directory") + .into()); + } + Err(signatory::Error::Permissions) => { + return Err(ErrorKind::Config + .context("insufficient permissions for keystore path") + .into()); + } + Err(e) => { + return Err(ErrorKind::Config.context(e).into()); + } + }; let name = name.parse().expect("Could not parse name"); let key = keystore.load(&name).expect("Could not load key"); - key.to_pem().parse().expect("Could not parse pem") + key.to_pem().parse().map_err(|err| { + ErrorKind::Config + .context(format!("failed to parse key {:?}", err)) + .into() + }) } - pub fn load_clarity_key(&self, name: String) -> clarity::PrivateKey { - let key = self.load_secret_key(name).to_bytes(); - clarity::PrivateKey::from_slice(key.as_slice()).expect("Could not convert key") + pub fn load_clarity_key( + &self, + name: String, + ) -> Result { + let key = self.load_secret_key(name)?.to_bytes(); + clarity::PrivateKey::from_slice(key.as_slice()).map_err(|err| { + ErrorKind::Config + .context(format!("failed to convert key {:?}", err)) + .into() + }) } - pub fn load_deep_space_key(&self, name: String) -> CosmosPrivateKey { - let key = self.load_secret_key(name).to_bytes(); + pub fn load_deep_space_key( + &self, + name: String, + ) -> Result { + let key = self.load_secret_key(name)?.to_bytes(); let key = deep_space::utils::bytes_to_hex_str(key.as_slice()); - key.parse().expect("Could not parse private key") + key.parse().map_err(|err| { + ErrorKind::Config + .context(format!("failed to parse key {:?}", err)) + .into() + }) } - pub fn load_gravity_deep_space_key(&self, name: String) -> cosmos_gravity::crypto::PrivateKey { - let key = self.load_secret_key(name).to_bytes(); + pub fn load_gravity_deep_space_key( + &self, + name: String, + ) -> Result { + let key = self.load_secret_key(name)?.to_bytes(); let key = deep_space::utils::bytes_to_hex_str(key.as_slice()); - key.parse().expect("Could not parse private key") + key.parse().map_err(|err| { + ErrorKind::Config + .context(format!("failed to parse key {:?}", err)) + .into() + }) } - pub fn load_ethers_wallet(&self, name: String) -> EthWallet { - EthWallet::from(self.load_secret_key(name)) + pub fn load_ethers_wallet(&self, name: String) -> Result { + Ok(EthWallet::from(self.load_secret_key(name)?)) } }