Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix keystore creation bug #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/commands/cosmos_to_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/commands/deploy/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/commands/eth_to_cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/cosmos/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/cosmos/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/cosmos/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify the existence of the keystore before opening it.

The change to use FsKeyStore::open(keystore) ensures that only existing keystores are accessed. However, this might lead to an error if the keystore does not exist. Consider adding a check to verify the existence of the keystore before attempting to open it.

if !keystore.exists() {
    eprintln!("Keystore does not exist, exiting.");
    return;
}
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/cosmos/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions src/commands/keys/cosmos/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/eth/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/eth/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/eth/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify the existence of the keystore before opening it.

The change to use FsKeyStore::open(keystore) ensures that only existing keystores are accessed. However, this might lead to an error if the keystore does not exist. Consider adding a check to verify the existence of the keystore before attempting to open it.

if !keystore.exists() {
    eprintln!("Keystore does not exist, exiting.");
    return;
}
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/eth/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/keys/eth/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 6 additions & 2 deletions src/commands/orchestrator/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/commands/sign_delegate_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
77 changes: 61 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
cbrit marked this conversation as resolved.
Show resolved Hide resolved
};
static ref DELEGATE_ADDRESS: CosmosAddress = {
let config = APP.config();
Expand Down Expand Up @@ -54,33 +56,76 @@ pub struct StewardConfig {
}

impl StewardConfig {
fn load_secret_key(&self, name: String) -> k256::elliptic_curve::SecretKey<k256::Secp256k1> {
fn load_secret_key(
&self,
name: String,
) -> Result<k256::elliptic_curve::SecretKey<k256::Secp256k1>, 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<clarity::PrivateKey, crate::error::Error> {
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<CosmosPrivateKey, crate::error::Error> {
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<cosmos_gravity::crypto::PrivateKey, crate::error::Error> {
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<EthWallet, crate::error::Error> {
Ok(EthWallet::from(self.load_secret_key(name)?))
}
}

Expand Down
Loading