From 0976b44d31942a5c87e77204f27512b6cacd19c6 Mon Sep 17 00:00:00 2001 From: Jiri Date: Tue, 11 Jun 2024 14:30:28 +0200 Subject: [PATCH] Raw addr fix --- src/crypto/cosmos.rs | 36 ++++++++++++++++++++++++++---------- src/crypto/encoding.rs | 18 ++++++++++++------ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/crypto/cosmos.rs b/src/crypto/cosmos.rs index 7d18436..fd0ba11 100644 --- a/src/crypto/cosmos.rs +++ b/src/crypto/cosmos.rs @@ -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 { +pub fn cosmos_raw_address_from_pubkey_secp256k1(pubkey: &[u8]) -> StdResult> { 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()) } @@ -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() { @@ -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); + } } diff --git a/src/crypto/encoding.rs b/src/crypto/encoding.rs index c950ba4..df91d6f 100644 --- a/src/crypto/encoding.rs +++ b/src/crypto/encoding.rs @@ -1,18 +1,24 @@ use bech32::{FromBase32, ToBase32}; use cosmwasm_std::StdError; -pub fn parse_bech32(data: &str, expected_prefix: &str) -> Result, 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, 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), 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::::from_base32(&parsed_data) { - Ok(res) => Ok(res), + Ok(res) => Ok((prefix, res)), Err(err) => Err(base32_parsing_error(&err)), } }