Skip to content

Commit

Permalink
Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 committed Apr 10, 2024
1 parent 652c12e commit 1da9dc2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
16 changes: 13 additions & 3 deletions src/crypto/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ use cosmwasm_std::Addr;
use crate::crypto::encoding::encode_bech32;
use crate::crypto::hashing::{ripemd160, sha256};

pub fn pubkey_to_cosmos_address(pubkey: &[u8], prefix: &str) -> Addr {
pub type RawCosmosAddress = [u8; 20];

pub fn cosmos_raw_address(pubkey: &[u8]) -> RawCosmosAddress {
let hash = ripemd160(&sha256(pubkey));

let mut addr = [0u8; 20];
addr.copy_from_slice(&hash[..]);

Addr::unchecked(encode_bech32(prefix, &addr).unwrap())
addr
}

pub fn cosmos_address(raw_address: &RawCosmosAddress, prefix: &str) -> Addr {
Addr::unchecked(encode_bech32(prefix, raw_address).unwrap())
}

pub fn cosmos_address_from_pubkey(pubkey: &[u8], prefix: &str) -> Addr {
cosmos_address(&cosmos_raw_address(pubkey), prefix)
}

#[cfg(test)]
Expand All @@ -28,7 +38,7 @@ mod tests {
// Get pubkey in bytes
let pubkey_bytes = parse_bech32(&pubkey_str, "pub").unwrap();
// Convert pubkey bytes to address
let recovered_addr = pubkey_to_cosmos_address(&pubkey_bytes, "fetch");
let recovered_addr = cosmos_address_from_pubkey(&pubkey_bytes, "fetch");

assert_eq!(recovered_addr, address);
}
Expand Down
22 changes: 13 additions & 9 deletions src/crypto/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ pub type EthAddress = [u8; 20];
///
/// * `msg` - The message to wrap
///
fn build_eth_signed_msg_wrapper(msg: &str) -> String {
format!("\x19Ethereum Signed Message:\n{}{}", msg.len(), msg)
fn build_eth_msg_for_signing(canonical_msg: &str) -> String {
format!(
"\x19Ethereum Signed Message:\n{}{}",
canonical_msg.len(),
canonical_msg
)
}

/// Computes the message and digest that should be signed by the user
pub fn compute_eth_digest(msg: &str) -> KeccakDigest {
let wrapped = build_eth_signed_msg_wrapper(msg);
pub fn compute_eth_msg_digest(canonical_msg: &str) -> KeccakDigest {
let wrapped = build_eth_msg_for_signing(canonical_msg);
keccak(wrapped.as_bytes())
}

Expand Down Expand Up @@ -113,7 +117,7 @@ pub fn check_registration(
// compute the expected message and then the digest for it

let msg = format!("Associate {} with {}", destination_address, eth_address);
let msg_hash = compute_eth_digest(&msg);
let msg_hash = compute_eth_msg_digest(&msg);

let recovered_public_key = recover_pubkey(api, &msg_hash, signature)?;
let recovered_address = pubkey_to_eth_address(&recovered_public_key)?;
Expand Down Expand Up @@ -164,7 +168,7 @@ pub fn addresses_error<T: std::fmt::Display>(err: &T) -> StdError {
#[cfg(test)]
mod tests {
use super::*;
use crate::crypto::cosmos::pubkey_to_cosmos_address;
use crate::crypto::cosmos::cosmos_address_from_pubkey;
use crate::crypto::hashing::compress_key;
use cosmwasm_std::testing::mock_dependencies;

Expand All @@ -179,7 +183,7 @@ mod tests {

#[test]
fn it_wraps_eth_sign_messages() {
let wrapped = build_eth_signed_msg_wrapper("why hello there");
let wrapped = build_eth_msg_for_signing("why hello there");
assert_eq!(&wrapped, "\x19Ethereum Signed Message:\n15why hello there")
}

Expand All @@ -188,7 +192,7 @@ mod tests {
let native = Addr::unchecked("native-address");

let msg = format!("Associate {} with {}", &native, "eth-address");
let result = compute_eth_digest(&msg);
let result = compute_eth_msg_digest(&msg);

assert_eq!(
hex::encode(result),
Expand Down Expand Up @@ -284,7 +288,7 @@ mod tests {

let compressed_pubkey = compress_key(&eth_pubkey).unwrap();

let fetch_address = pubkey_to_cosmos_address(&compressed_pubkey, "fetch");
let fetch_address = cosmos_address_from_pubkey(&compressed_pubkey, "fetch");
assert_eq!(fetch_address, expected_fetch_address);
}
}

0 comments on commit 1da9dc2

Please sign in to comment.