revert: "feat: Generate cosmos account addresses for Injective" (#4615)
Reverts hyperlane-xyz/hyperlane-monorepo#4576 context: https://discord.com/channels/935678348330434570/1291354470730436680/1291357041624875010 > The problem is that Injective supports both "normal cosmos" signing with the bitcoin style address, and also ethereum style addresses. They disambiguate depending on the type of the pubkey that's signed in the tx > The relayer signs things using a bitcoin style address on Injective bc this was easier than making code changes to have it do ethereum-style (which isn't supported easily out of the box with the cosmos libs we use) > So we want the relayer account balance metric to look at the bitcoin-style address relating to our relayer key, not the Ethereum-style one at least this is the theory behind what happenedpull/4619/head
parent
22871425d6
commit
3c819c777e
@ -1,70 +0,0 @@ |
||||
use cosmrs::crypto::PublicKey; |
||||
use cosmwasm_std::HexBinary; |
||||
|
||||
use crypto::decompress_public_key; |
||||
|
||||
use crate::libs::account::CosmosAccountId; |
||||
|
||||
const COMPRESSED_PUBLIC_KEY: &str = |
||||
"02962d010010b6eec66846322704181570d89e28236796579c535d2e44d20931f4"; |
||||
const INJECTIVE_ADDRESS: &str = "inj1m6ada382hfuxvuke4h9p4uswhn2qcca7mlg0dr"; |
||||
const NEUTRON_ADDRESS: &str = "neutron1mydju5alsmhnfsawy0j4lyns70l7qukgdgy45w"; |
||||
|
||||
#[test] |
||||
fn test_account_id() { |
||||
// given
|
||||
let pub_key = compressed_public_key(); |
||||
|
||||
// when
|
||||
let neutron_account_id = CosmosAccountId::account_id_from_pubkey(pub_key, "neutron").unwrap(); |
||||
let injective_account_id = CosmosAccountId::account_id_from_pubkey(pub_key, "inj").unwrap(); |
||||
|
||||
// then
|
||||
assert_eq!(neutron_account_id.as_ref(), NEUTRON_ADDRESS); |
||||
assert_eq!(injective_account_id.as_ref(), INJECTIVE_ADDRESS); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_bitcoin_style() { |
||||
// given
|
||||
let compressed = compressed_public_key(); |
||||
let decompressed = decompressed_public_key(); |
||||
|
||||
// when
|
||||
let from_compressed = CosmosAccountId::bitcoin_style(compressed, "neutron").unwrap(); |
||||
let from_decompressed = CosmosAccountId::bitcoin_style(decompressed, "neutron").unwrap(); |
||||
|
||||
// then
|
||||
assert_eq!(from_compressed.as_ref(), NEUTRON_ADDRESS); |
||||
assert_eq!(from_decompressed.as_ref(), NEUTRON_ADDRESS); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_ethereum_style() { |
||||
// given
|
||||
let compressed = compressed_public_key(); |
||||
let decompressed = decompressed_public_key(); |
||||
|
||||
// when
|
||||
let from_compressed = CosmosAccountId::ethereum_style(compressed, "inj").unwrap(); |
||||
let from_decompressed = CosmosAccountId::ethereum_style(decompressed, "inj").unwrap(); |
||||
|
||||
// then
|
||||
assert_eq!(from_compressed.as_ref(), INJECTIVE_ADDRESS); |
||||
assert_eq!(from_decompressed.as_ref(), INJECTIVE_ADDRESS); |
||||
} |
||||
|
||||
fn compressed_public_key() -> PublicKey { |
||||
let hex = hex::decode(COMPRESSED_PUBLIC_KEY).unwrap(); |
||||
let tendermint = tendermint::PublicKey::from_raw_secp256k1(&hex).unwrap(); |
||||
let pub_key = PublicKey::from(tendermint); |
||||
pub_key |
||||
} |
||||
|
||||
fn decompressed_public_key() -> PublicKey { |
||||
let hex = hex::decode(COMPRESSED_PUBLIC_KEY).unwrap(); |
||||
let decompressed = decompress_public_key(&hex).unwrap(); |
||||
let tendermint = tendermint::PublicKey::from_raw_secp256k1(&decompressed).unwrap(); |
||||
let pub_key = PublicKey::from(tendermint); |
||||
pub_key |
||||
} |
@ -1,14 +0,0 @@ |
||||
[package] |
||||
name = "crypto" |
||||
documentation.workspace = true |
||||
edition.workspace = true |
||||
homepage.workspace = true |
||||
license-file.workspace = true |
||||
publish.workspace = true |
||||
version.workspace = true |
||||
|
||||
[dependencies] |
||||
elliptic-curve = { workspace = true, features = ["sec1"] } |
||||
hex = { workspace = true } |
||||
k256 = { workspace = true } |
||||
thiserror = { workspace = true } |
@ -1,51 +0,0 @@ |
||||
use std::fmt::{Display, Formatter}; |
||||
|
||||
use elliptic_curve::sec1::ToEncodedPoint; |
||||
|
||||
#[derive(Debug, thiserror::Error)] |
||||
pub enum PublicKeyError { |
||||
Decode(String), |
||||
} |
||||
|
||||
impl Display for PublicKeyError { |
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
||||
write!(f, "{:?}", self) |
||||
} |
||||
} |
||||
|
||||
/// Decompresses public key of secp256k1 if it was compressed
|
||||
///
|
||||
/// Public key can be expressed in compressed or decompressed forms.
|
||||
/// Compressed form contains one byte as prefix and x component of the public key.
|
||||
/// Decompressed form contains one byte as prefix, x and y components of the public key.
|
||||
pub fn decompress_public_key(public_key: &[u8]) -> Result<Vec<u8>, PublicKeyError> { |
||||
let elliptic: elliptic_curve::PublicKey<k256::Secp256k1> = |
||||
elliptic_curve::PublicKey::from_sec1_bytes(public_key) |
||||
.map_err(|e| PublicKeyError::Decode(e.to_string()))?; |
||||
|
||||
// if public key was compressed, encoding into the point will decompress it.
|
||||
let point = elliptic.to_encoded_point(false); |
||||
let decompressed = point.to_bytes().to_vec(); |
||||
Ok(decompressed) |
||||
} |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use crate::key::decompress_public_key; |
||||
|
||||
#[test] |
||||
fn test_decompress_public_key() { |
||||
// given
|
||||
let compressed = "02962d010010b6eec66846322704181570d89e28236796579c535d2e44d20931f4"; |
||||
let hex = hex::decode(compressed).unwrap(); |
||||
|
||||
// when
|
||||
let decompressed = hex::encode(decompress_public_key(&hex).unwrap()); |
||||
|
||||
// then
|
||||
assert_eq!( |
||||
"04962d010010b6eec66846322704181570d89e28236796579c535d2e44d20931f40cb1152fb9e61ec7493a0d9a35d2e8a57198e109613854abdd3be5603d504008", |
||||
decompressed |
||||
); |
||||
} |
||||
} |
@ -1,4 +0,0 @@ |
||||
pub use key::decompress_public_key; |
||||
pub use key::PublicKeyError; |
||||
|
||||
mod key; |
Loading…
Reference in new issue