-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce 42 characters for address on inboxId create (#1202)
* enforce 42 characters for address * add the error * update all of the tests * try and get closer to a solution * simple fix * one more error fix * fix up all the tests * fmt * fix the lint * fix another lint issue * update the test * cargo fmt * fix up the test * fix up another test * more test fixing
- Loading branch information
1 parent
c1066eb
commit 71b47a2
Showing
20 changed files
with
136 additions
and
82 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
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
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 |
---|---|---|
@@ -1,12 +1,31 @@ | ||
use sha2::{Digest, Sha256}; | ||
|
||
use super::AssociationError; | ||
|
||
/// Helper function to generate a SHA256 hash as a hex string. | ||
fn sha256_string(input: String) -> String { | ||
let mut hasher = Sha256::new(); | ||
hasher.update(input.as_bytes()); | ||
let result = hasher.finalize(); | ||
format!("{:x}", result) | ||
} | ||
|
||
pub fn generate_inbox_id(account_address: &str, nonce: &u64) -> String { | ||
sha256_string(format!("{}{}", account_address.to_lowercase(), nonce)) | ||
/// Validates that the account address is exactly 42 characters, starts with "0x", | ||
/// and contains only valid hex digits. | ||
fn is_valid_address(account_address: &str) -> bool { | ||
account_address.len() == 42 | ||
&& account_address.starts_with("0x") | ||
&& account_address[2..].chars().all(|c| c.is_ascii_hexdigit()) | ||
} | ||
|
||
/// Generates an inbox ID if the account address is valid. | ||
pub fn generate_inbox_id(account_address: &str, nonce: &u64) -> Result<String, AssociationError> { | ||
if !is_valid_address(account_address) { | ||
return Err(AssociationError::InvalidAccountAddress); | ||
} | ||
Ok(sha256_string(format!( | ||
"{}{}", | ||
account_address.to_lowercase(), | ||
nonce | ||
))) | ||
} |
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
Oops, something went wrong.