Skip to content

Commit

Permalink
feat: complete --secret-key option for keys command
Browse files Browse the repository at this point in the history
  • Loading branch information
liyukun committed Sep 12, 2023
1 parent 0f4b2bc commit d5a3521
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 27 deletions.
37 changes: 31 additions & 6 deletions crates/relayer-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use eyre::eyre;
use hdpath::StandardHDPath;
use ibc_relayer::{
chain::ChainType,
config::{ChainConfig, Config},
config::{AddressType, ChainConfig, Config},
keyring::{
AnySigningKeyPair, KeyRing, Secp256k1KeyPair, SigningKeyPair, SigningKeyPairSized, Store,
},
Expand Down Expand Up @@ -291,12 +291,37 @@ pub fn restore_key(
}

pub fn parse_key_from_secret(
_secret_file: &Path,
_key_name: &str,
_config: &ChainConfig,
_overwrite: bool,
secret_file: &Path,
key_name: &str,
config: &ChainConfig,
overwrite: bool,
) -> eyre::Result<AnySigningKeyPair> {
unimplemented!()
let secret_key =
fs::read_to_string(secret_file).map_err(|_| eyre!("error reading the secret file"))?;

let (account_prefix, address_type) = match config.r#type() {
ChainType::CosmosSdk => (config.cosmos().account_prefix.as_str(), AddressType::Cosmos),
ChainType::Eth => (
"eth",
AddressType::Ethermint {
pk_type: Default::default(),
},
),
ChainType::Axon => ("axon", AddressType::Axon),
ChainType::Ckb => ("ckb", AddressType::Ckb),
ChainType::Ckb4Ibc => ("ckb4ibc", AddressType::Ckb),
};
let key_pair = {
let mut keyring = KeyRing::new_secp256k1(Store::Test, account_prefix, config.id())?;

check_key_exists(&keyring, key_name, overwrite);

let key_pair = Secp256k1KeyPair::from_secret_key(secret_key.trim(), &address_type)?;

keyring.add_key(key_name, key_pair.clone())?;
key_pair.into()
};
Ok(key_pair)
}

/// Check if the key with the given key name already exists.
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/chain/ckb/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn test_create_eth_multi_client(case_id: usize) {
let network = chain.network().unwrap();
let is_mainnet = network == NetworkType::Mainnet;
let account_prefix = if is_mainnet { "ckb" } else { "ckt" };
let address_type = AddressType::Ckb { is_mainnet };
let address_type = AddressType::Ckb;
Secp256k1KeyPair::from_mnemonic(mnemonic, &hd_path, &address_type, account_prefix)
.unwrap()
};
Expand Down
4 changes: 2 additions & 2 deletions crates/relayer/src/chain/cosmos/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ fn encode_signer_info(
) -> Result<SignerInfo, Error> {
let pk_type = match address_type {
AddressType::Cosmos => "/cosmos.crypto.secp256k1.PubKey".to_string(),
AddressType::Ethermint { pk_type } | AddressType::Axon { pk_type } => pk_type.clone(),
AddressType::Ckb { .. } => todo!(),
AddressType::Ethermint { pk_type } => pk_type.clone(),
AddressType::Ckb | AddressType::Axon => todo!(),
};
// Create a MsgSend proto Any message
let pk_any = Any {
Expand Down
12 changes: 4 additions & 8 deletions crates/relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,21 +670,17 @@ pub enum AddressType {
Ethermint {
pk_type: String,
},
Ckb {
is_mainnet: bool,
},
Axon {
pk_type: String,
},
Ckb,
Axon,
}

impl Display for AddressType {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
AddressType::Cosmos => write!(f, "cosmos"),
AddressType::Ethermint { .. } => write!(f, "ethermint"),
AddressType::Ckb { .. } => write!(f, "ckb"),
AddressType::Axon { .. } => write!(f, "axon"),
AddressType::Ckb => write!(f, "ckb"),
AddressType::Axon => write!(f, "axon"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/keyring/ed25519_key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Ed25519KeyPair {
impl SigningKeyPair for Ed25519KeyPair {
const KEY_TYPE: KeyType = KeyType::Ed25519;

fn from_secret_key(_secret_key: &str) -> Result<Self, Error> {
fn from_secret_key(_secret_key: &str, _address_type: &AddressType) -> Result<Self, Error> {
unimplemented!()
}

Expand Down
27 changes: 23 additions & 4 deletions crates/relayer/src/keyring/secp256k1_key_pair.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::any::Any;
use std::str::FromStr;

use bip39::{Language, Mnemonic, Seed};
use bitcoin::{
Expand Down Expand Up @@ -96,8 +97,8 @@ impl TryFrom<&AddressType> for Secp256k1AddressType {
Ok(Self::Ethermint)
}
AddressType::Cosmos | AddressType::Ethermint { pk_type: _ } => Ok(Self::Cosmos),
AddressType::Ckb { .. } => Ok(Self::Ckb),
AddressType::Axon { .. } => Ok(Self::Axon),
AddressType::Ckb => Ok(Self::Ckb),
AddressType::Axon => Ok(Self::Axon),
}
}
}
Expand Down Expand Up @@ -284,8 +285,26 @@ impl Secp256k1KeyPair {
impl SigningKeyPair for Secp256k1KeyPair {
const KEY_TYPE: KeyType = KeyType::Secp256k1;

fn from_secret_key(_secret_key: &str) -> Result<Self, Error> {
unimplemented!()
fn from_secret_key(secret_key: &str, address_type: &AddressType) -> Result<Self, Error> {
let private_key =
SecretKey::from_str(secret_key).map_err(|e| Error::secp256k1(e.to_string()))?;
let public_key = private_key.public_key(&Secp256k1::signing_only());
let address_type: Secp256k1AddressType = address_type.try_into()?;
let address = get_address(&public_key, address_type);
let account = if address_type == Secp256k1AddressType::Cosmos {
return Err(Error::secp256k1(
"--secret-key not support Cosmos chain".to_owned(),
));
} else {
hex::encode(address)
};
Ok(Self {
private_key,
public_key,
address,
address_type,
account,
})
}

fn from_key_file(key_file: KeyFile, hd_path: &StandardHDPath) -> Result<Self, Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer/src/keyring/signing_key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::config::AddressType;
pub trait SigningKeyPair {
const KEY_TYPE: KeyType;

fn from_secret_key(secret_key: &str) -> Result<Self, Error>
fn from_secret_key(secret_key: &str, address_type: &AddressType) -> Result<Self, Error>
where
Self: Sized;

Expand Down
6 changes: 2 additions & 4 deletions tools/test-framework/src/chain/chain_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ impl ChainType {
Self::Evmos => AddressType::Ethermint {
pk_type: "/ethermint.crypto.v1.ethsecp256k1.PubKey".to_string(),
},
Self::Ckb => AddressType::Ckb { is_mainnet: false },
Self::Axon => AddressType::Axon {
pk_type: "/axon.crypto.v1.ethsecp256k1.PubKey".to_string(),
},
Self::Ckb => AddressType::Ckb,
Self::Axon => AddressType::Axon,
}
}
}
Expand Down

0 comments on commit d5a3521

Please sign in to comment.