aboutsummaryrefslogtreecommitdiff
path: root/src/account
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2020-11-30 19:11:36 +0100
committerNicholas Rodrigues Lordello <[email protected]>2020-11-30 19:11:36 +0100
commita699b6b4fe6f0e2567f7298fd8174e779f1bd43d (patch)
treee79f3927acc133cd05e06e22b1c95c776effe991 /src/account
parent678d45e7ae09510796d27a80ecd894f6a1ad8279 (diff)
hd wallet impl
Diffstat (limited to 'src/account')
-rw-r--r--src/account/address.rs115
-rw-r--r--src/account/signature.rs26
2 files changed, 141 insertions, 0 deletions
diff --git a/src/account/address.rs b/src/account/address.rs
new file mode 100644
index 0000000..ea62cc0
--- /dev/null
+++ b/src/account/address.rs
@@ -0,0 +1,115 @@
+use crate::hash;
+use hex::FromHexError;
+use std::{
+ fmt::{self, Debug, Display, Formatter},
+ ops::{Deref, DerefMut},
+ str::{self, FromStr},
+};
+
+/// An Ethereum public address.
+#[derive(Copy, Clone, Default, Eq, PartialEq)]
+pub struct Address(pub [u8; 20]);
+
+impl Address {
+ /// Creates an address from a slice.
+ ///
+ /// # Panics
+ ///
+ /// This method panics if the length of the slice is not 20 bytes.
+ pub fn from_slice(slice: &[u8]) -> Self {
+ let mut address = Self::default();
+ address.as_mut().copy_from_slice(slice);
+ address
+ }
+}
+
+impl Debug for Address {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ struct DisplayAsDebug<T>(T);
+
+ impl<T> Debug for DisplayAsDebug<T>
+ where
+ T: Display,
+ {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ self.0.fmt(f)
+ }
+ }
+
+ f.debug_tuple("Address")
+ .field(&DisplayAsDebug(self))
+ .finish()
+ }
+}
+
+impl Display for Address {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ let mut buf = *b"0x0000000000000000000000000000000000000000";
+ let addr = &mut buf[2..];
+ hex::encode_to_slice(self, addr).expect("error decoding hex");
+
+ let digest = hash::keccak256(&addr);
+ for i in 0..addr.len() {
+ let byte = digest[i / 2];
+ let nibble = 0xf & if i % 2 == 0 { byte >> 4 } else { byte };
+ if nibble >= 8 {
+ addr[i] = addr[i].to_ascii_uppercase();
+ }
+ }
+
+ f.write_str(unsafe { str::from_utf8_unchecked(&buf) })
+ }
+}
+
+impl AsRef<[u8]> for Address {
+ fn as_ref(&self) -> &[u8] {
+ &self.0
+ }
+}
+
+impl AsMut<[u8]> for Address {
+ fn as_mut(&mut self) -> &mut [u8] {
+ &mut self.0
+ }
+}
+
+impl Deref for Address {
+ type Target = [u8; 20];
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl DerefMut for Address {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.0
+ }
+}
+
+impl FromStr for Address {
+ type Err = FromHexError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let mut address = Self::default();
+ let s = s.strip_prefix("0x").unwrap_or(s);
+ hex::decode_to_slice(s, address.as_mut())?;
+ Ok(address)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn checksum_address() {
+ for s in &[
+ "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1",
+ "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
+ ] {
+ let address = s.parse::<Address>().unwrap();
+ assert_eq!(address.to_string(), *s);
+ }
+ }
+}
diff --git a/src/account/signature.rs b/src/account/signature.rs
new file mode 100644
index 0000000..eef37da
--- /dev/null
+++ b/src/account/signature.rs
@@ -0,0 +1,26 @@
+//! Module containing signature data model.
+
+use std::fmt::{self, Display, Formatter};
+
+/// A secp256k1 signature.
+#[derive(Clone, Debug, Default, Eq, PartialEq)]
+pub struct Signature {
+ /// Signature V value in Electrum notation.
+ pub v: u8,
+ /// Signature R value.
+ pub r: [u8; 32],
+ /// Signature S value.
+ pub s: [u8; 32],
+}
+
+impl Display for Signature {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(
+ f,
+ "0x{}{}{:02x}",
+ hex::encode(&self.r),
+ hex::encode(&self.s),
+ self.v
+ )
+ }
+}