-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b246b7a
commit 65be5d3
Showing
13 changed files
with
523 additions
and
99 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use cosmwasm_std::Addr; | ||
|
||
use crate::crypto::encoding::encode_bech32; | ||
use crate::crypto::hashing::{ripemd160, sha256}; | ||
|
||
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 | ||
} | ||
|
||
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)] | ||
mod tests { | ||
use super::*; | ||
use crate::crypto::encoding::parse_bech32; | ||
|
||
#[test] | ||
fn test_pubkey_to_address() { | ||
// Test if encoded pubkey can be decoded and converted to address | ||
|
||
let pubkey_str = | ||
"pub1qv6wrktsr7hng9rmmjqa2yfqj0cg7w43n0qkq3xuqmgxu6ewnyyjykzgyam".to_string(); | ||
let address = Addr::unchecked("fetch1967p3vkp0yngdfturv4ypq2p4g760ml705wcxy".to_string()); | ||
|
||
// Get pubkey in bytes | ||
let pubkey_bytes = parse_bech32(&pubkey_str, "pub").unwrap(); | ||
// Convert pubkey bytes to address | ||
let recovered_addr = cosmos_address_from_pubkey(&pubkey_bytes, "fetch"); | ||
|
||
assert_eq!(recovered_addr, address); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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)), | ||
}?; | ||
|
||
if prefix != expected_prefix { | ||
return Err(prefix_error(expected_prefix, &prefix)); | ||
} | ||
|
||
match Vec::<u8>::from_base32(&parsed_data) { | ||
Ok(res) => Ok(res), | ||
Err(err) => Err(base32_parsing_error(&err)), | ||
} | ||
} | ||
|
||
pub fn encode_bech32(prefix: &str, data: &[u8]) -> Result<String, StdError> { | ||
let encoded = match bech32::encode(prefix, data.to_base32(), bech32::Variant::Bech32) { | ||
Ok(encoded) => Ok(encoded), | ||
Err(err) => Err(base32_parsing_error(&err)), | ||
}?; | ||
|
||
Ok(encoded) | ||
} | ||
|
||
// Errors | ||
pub fn prefix_error(expected: &str, actual: &str) -> StdError { | ||
StdError::generic_err(format!( | ||
"Wrong prefix. Expected {}, got {}.", | ||
expected, actual | ||
)) | ||
} | ||
|
||
pub fn base32_parsing_error<T: std::fmt::Display>(err: &T) -> StdError { | ||
StdError::generic_err(format!("Base32 parsing failed: {}", err)) | ||
} |
Oops, something went wrong.