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

Cosmos raw address fix #7

Merged
merged 2 commits into from
Jun 13, 2024
Merged
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
36 changes: 26 additions & 10 deletions src/crypto/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ use crate::crypto::hashing::{ripemd160, sha256};
use crate::crypto::secp256k1::to_compressed_key;
use cosmwasm_std::StdResult;

pub type RawCosmosAddress = [u8; 20];

pub fn cosmos_raw_address_from_pubkey_secp256k1(pubkey: &[u8]) -> StdResult<RawCosmosAddress> {
pub fn cosmos_raw_address_from_pubkey_secp256k1(pubkey: &[u8]) -> StdResult<Vec<u8>> {
let compressed_pubkey = to_compressed_key(pubkey)?;

let hash = ripemd160(&sha256(&compressed_pubkey));

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

Ok(addr)
Ok(hash)
}

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

Expand All @@ -32,7 +27,7 @@ pub fn cosmos_address_from_pubkey_secp256k1(pubkey: &[u8], prefix: &str) -> StdR
#[cfg(test)]
mod tests {
use super::*;
use crate::crypto::encoding::parse_bech32;
use crate::crypto::encoding::{parse_bech32, parse_bech32_with_prefix};

#[test]
fn test_pubkey_to_address() {
Expand All @@ -43,10 +38,31 @@ mod tests {
let address = Addr::unchecked("fetch1967p3vkp0yngdfturv4ypq2p4g760ml705wcxy".to_string());

// Get pubkey in bytes
let pubkey_bytes = parse_bech32(&pubkey_str, "pub").unwrap();
let pubkey_bytes = parse_bech32_with_prefix(&pubkey_str, "pub").unwrap();
// Convert pubkey bytes to address
let recovered_addr = cosmos_address_from_pubkey_secp256k1(&pubkey_bytes, "fetch").unwrap();

assert_eq!(recovered_addr, address);
}

#[test]
fn test_canonical() {
let addr_asi = "asi1rhrlzsx9z865dqen8t4v47r99dw6y4vaw76rd9";
let addr_fet = "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x";

let (asi_prefix, raw_addr) = parse_bech32(addr_asi).unwrap();
let recovered_addr = encode_bech32("fetch", &raw_addr).unwrap();

assert_eq!(asi_prefix, "asi");
assert_eq!(addr_fet, recovered_addr);

let contract_addr_fet = "fetch1mxz8kn3l5ksaftx8a9pj9a6prpzk2uhxnqdkwuqvuh37tw80xu6qges77l";
let contract_addr_asi = "asi1mxz8kn3l5ksaftx8a9pj9a6prpzk2uhxnqdkwuqvuh37tw80xu6qepjszq";

let (asi_prefix, raw_addr) = parse_bech32(contract_addr_asi).unwrap();
let recovered_addr = encode_bech32("fetch", &raw_addr).unwrap();

assert_eq!(asi_prefix, "asi");
assert_eq!(contract_addr_fet, recovered_addr);
}
}
18 changes: 12 additions & 6 deletions src/crypto/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use bech32::{FromBase32, ToBase32};
use cosmwasm_std::StdError;

pub fn parse_bech32(data: &str, expected_prefix: &str) -> Result<Vec<u8>, StdError> {
let (prefix, parsed_data, _variant) = match bech32::decode(data) {
Ok(parsed_data) => Ok(parsed_data),
Err(err) => Err(base32_parsing_error(&err)),
}?;
pub fn parse_bech32_with_prefix(data: &str, expected_prefix: &str) -> Result<Vec<u8>, StdError> {
let (prefix, parsed_data) = parse_bech32(data)?;

if prefix != expected_prefix {
return Err(prefix_error(expected_prefix, &prefix));
}

Ok(parsed_data)
}

pub fn parse_bech32(data: &str) -> Result<(String, Vec<u8>), StdError> {
let (prefix, parsed_data, _variant) = match bech32::decode(data) {
Ok(parsed_data) => Ok(parsed_data),
Err(err) => Err(base32_parsing_error(&err)),
}?;

match Vec::<u8>::from_base32(&parsed_data) {
Ok(res) => Ok(res),
Ok(res) => Ok((prefix, res)),
Err(err) => Err(base32_parsing_error(&err)),
}
}
Expand Down
Loading