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

refactor: space indentation #5

Merged
merged 1 commit into from
Sep 22, 2023
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
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
hard_tabs = true
hard_tabs = false
tab_spaces = 2
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ description = "A (WIP) Rust library for easily create, manage, use and protect E
edition = "2021"
repository = "https://github.com/mikesposito/walleth/"
keywords = [
"language",
"cli",
"http-client",
"api-client",
"json-selector",
"ethereum",
"wallet",
"library",
"crypto",
"signing",
]

[dependencies]
Expand Down
78 changes: 39 additions & 39 deletions src/account/keychain/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ use sha3::{Digest, Keccak256};

#[derive(Clone, Debug)]
pub struct Account {
pub address: String,
pub public_key: XPub,
pub address: String,
pub public_key: XPub,
}

impl Account {
/// Create a new `Account` from an extended public key
pub fn from_extended_public_key(extended_public_key: &XPub) -> Result<Self, String> {
let address = extended_public_key_to_address(extended_public_key)?;
/// Create a new `Account` from an extended public key
pub fn from_extended_public_key(extended_public_key: &XPub) -> Result<Self, String> {
let address = extended_public_key_to_address(extended_public_key)?;

assert_is_valid_hex_address(address.as_str())?;
assert_is_valid_hex_address(address.as_str())?;

Ok(Account {
address: add0x(address),
public_key: extended_public_key.to_owned(),
})
}
Ok(Account {
address: add0x(address),
public_key: extended_public_key.to_owned(),
})
}
}

/// Assert that a string is a valid hex address
Expand All @@ -34,18 +34,18 @@ impl Account {
/// assert_eq!(assert_is_valid_hex_address("0x00C08c440DbDC3A2a9C9D99b30077a53Ba7eDEAD").is_ok(), true);
/// ```
pub fn assert_is_valid_hex_address(value: &str) -> Result<(), String> {
let unprefixed = remove0x(value);
let unprefixed = remove0x(value);

assert_is_hex(&unprefixed)?;
assert_is_hex(&unprefixed)?;

if unprefixed.len() != 40 {
return Err(format!(
"String passed into assert_is_valid_hex_address is {} hex chars long instead of 40.",
value.len()
));
}
if unprefixed.len() != 40 {
return Err(format!(
"String passed into assert_is_valid_hex_address is {} hex chars long instead of 40.",
value.len()
));
}

Ok(())
Ok(())
}

/// Assert that a string is a valid hex
Expand All @@ -60,10 +60,10 @@ pub fn assert_is_valid_hex_address(value: &str) -> Result<(), String> {
/// assert!(assertion.is_ok());
/// ```
pub fn assert_is_hex(value: &str) -> Result<(), String> {
match decode(value) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
match decode(value) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
}

/// Remove the 0x prefix from a string
Expand All @@ -80,10 +80,10 @@ pub fn assert_is_hex(value: &str) -> Result<(), String> {
/// );
/// ```
pub fn remove0x(value: &str) -> &str {
match value.starts_with("0x") {
true => &value[2..],
_ => value,
}
match value.starts_with("0x") {
true => &value[2..],
_ => value,
}
}

/// Add the 0x prefix to a string
Expand All @@ -99,25 +99,25 @@ pub fn remove0x(value: &str) -> &str {
/// );
/// ```
pub fn add0x(value: String) -> String {
match value.starts_with("0x") {
true => value,
_ => format!("0x{}", value),
}
match value.starts_with("0x") {
true => value,
_ => format!("0x{}", value),
}
}

/// Convert an extended public key to an ethereum address
pub fn extended_public_key_to_address(extended_public_key: &XPub) -> Result<String, String> {
let address = keccak_hash(&extended_public_key.to_bytes());
Ok(address[(address.len() - 40)..].to_string())
let address = keccak_hash(&extended_public_key.to_bytes());
Ok(address[(address.len() - 40)..].to_string())
}

/// Hash data using the keccak256 algorithm
fn keccak_hash<T>(data: &T) -> String
where
T: ?Sized + AsRef<[u8]>,
T: ?Sized + AsRef<[u8]>,
{
let mut hasher = Keccak256::new();
hasher.update(data);
let result = hasher.finalize();
hex::encode(result)
let mut hasher = Keccak256::new();
hasher.update(data);
let result = hasher.finalize();
hex::encode(result)
}
Loading