-
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
18c4b05
commit 812ebaf
Showing
5 changed files
with
74 additions
and
37 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ pub mod cosmos; | |
pub mod encoding; | ||
pub mod ethereum; | ||
pub mod hashing; | ||
pub mod secp256k1; | ||
pub mod uagents; |
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,41 @@ | ||
use cosmwasm_std::{StdError, StdResult}; | ||
|
||
pub type CompressedPubkey = [u8; 33]; | ||
|
||
fn compress_pubkey(uncompressed_pubkey_bytes: &[u8]) -> CompressedPubkey { | ||
// The first byte is the prefix, followed by 32 bytes for X and 32 bytes for Y | ||
let x_bytes = &uncompressed_pubkey_bytes[0..32]; | ||
let y_bytes = &uncompressed_pubkey_bytes[32..64]; | ||
|
||
// Determine if Y is even or odd for the prefix | ||
// Y's last byte's least significant bit determines its evenness or oddness | ||
let prefix_byte = if (y_bytes[31] & 1) == 0 { 0x02 } else { 0x03 }; | ||
|
||
// Create the compressed public key array | ||
let mut compressed_pubkey = [0u8; 33]; | ||
compressed_pubkey[0] = prefix_byte; | ||
compressed_pubkey[1..].copy_from_slice(x_bytes); | ||
|
||
compressed_pubkey | ||
} | ||
|
||
pub fn to_compressed_key(pubkey: &[u8]) -> StdResult<CompressedPubkey> { | ||
match pubkey.len() { | ||
// Compressed pubkey | ||
33 => pubkey | ||
.try_into() | ||
.map_err(|_| pubkey_error(&"Conversion error")), | ||
|
||
// Uncompressed without checksum | ||
64 => Ok(compress_pubkey(pubkey)), | ||
|
||
// Uncompressed with checksum | ||
65 => Ok(compress_pubkey(&pubkey[1..])), | ||
_ => Err(pubkey_error(&"Wrong len")), | ||
} | ||
} | ||
|
||
// Error | ||
pub fn pubkey_error<T: std::fmt::Display>(err: &T) -> StdError { | ||
StdError::generic_err(format!("Secp256k1 pubkey error {}", err)) | ||
} |