|
16 | 16 |
|
17 | 17 | use std::cmp; |
18 | 18 | use std::fmt; |
| 19 | +use std::hash::{Hash, Hasher}; |
| 20 | +use std::ops::{Deref, DerefMut}; |
19 | 21 | use std::str::FromStr; |
20 | 22 |
|
21 | 23 | use bech32::Bech32; |
| 24 | +use heapsize::HeapSizeOf; |
22 | 25 | use primitives::H160; |
| 26 | +use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp}; |
| 27 | +use rustc_hex::FromHexError; |
23 | 28 | use serde::de::{Error as SerdeError, Visitor}; |
24 | 29 | use serde::{Deserialize, Deserializer, Serialize, Serializer}; |
25 | 30 |
|
26 | | -use super::{Address, Error, Network}; |
| 31 | +use super::{Error, Network}; |
| 32 | + |
| 33 | +#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)] |
| 34 | +pub struct Address(H160); |
| 35 | + |
| 36 | +impl Address { |
| 37 | + pub fn zero() -> Self { |
| 38 | + Address(H160::zero()) |
| 39 | + } |
| 40 | + |
| 41 | + pub fn new() -> Self { |
| 42 | + Address(H160::new()) |
| 43 | + } |
| 44 | + |
| 45 | + pub fn random() -> Self { |
| 46 | + Address(H160::random()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl Deref for Address { |
| 51 | + type Target = H160; |
| 52 | + |
| 53 | + fn deref(&self) -> &Self::Target { |
| 54 | + &self.0 |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl DerefMut for Address { |
| 59 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 60 | + &mut self.0 |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl fmt::Display for Address { |
| 65 | + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 66 | + write!(f, "{}", self.0) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl PartialOrd for Address { |
| 71 | + fn partial_cmp(&self, m: &Address) -> Option<cmp::Ordering> { |
| 72 | + self.0.partial_cmp(&m.0) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl Ord for Address { |
| 77 | + fn cmp(&self, m: &Address) -> cmp::Ordering { |
| 78 | + self.0.cmp(&m.0) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl Hash for Address { |
| 83 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 84 | + self.0.hash(state); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl Default for Address { |
| 89 | + fn default() -> Self { |
| 90 | + Address(Default::default()) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Encodable for Address { |
| 95 | + fn rlp_append(&self, s: &mut RlpStream) { |
| 96 | + let data: H160 = self.0.into(); |
| 97 | + data.rlp_append(s); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl Decodable for Address { |
| 102 | + fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> { |
| 103 | + let data = H160::decode(rlp)?; |
| 104 | + Ok(Address(data)) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl FromStr for Address { |
| 109 | + type Err = FromHexError; |
| 110 | + |
| 111 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 112 | + Ok(Address(H160::from_str(s)?)) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl From<H160> for Address { |
| 117 | + fn from(s: H160) -> Self { |
| 118 | + Address(s) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl From<u64> for Address { |
| 123 | + fn from(s: u64) -> Self { |
| 124 | + Address(H160::from(s)) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl From<[u8; 20]> for Address { |
| 129 | + fn from(s: [u8; 20]) -> Self { |
| 130 | + Address(H160::from(s)) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl From<&'static str> for Address { |
| 135 | + fn from(s: &'static str) -> Self { |
| 136 | + Address(H160::from(s)) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl Into<[u8; 20]> for Address { |
| 141 | + fn into(self) -> [u8; 20] { |
| 142 | + self.0.into() |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl AsRef<[u8]> for Address { |
| 147 | + fn as_ref(&self) -> &[u8] { |
| 148 | + &self.0.as_ref() |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl HeapSizeOf for Address { |
| 153 | + fn heap_size_of_children(&self) -> usize { |
| 154 | + self.0.heap_size_of_children() |
| 155 | + } |
| 156 | +} |
27 | 157 |
|
28 | 158 | #[derive(Debug, PartialEq, Eq, Clone)] |
29 | 159 | pub struct FullAddress { |
@@ -135,7 +265,7 @@ impl FromStr for FullAddress { |
135 | 265 | for i in 0..20 { |
136 | 266 | arr[i] = data[1 + i]; |
137 | 267 | } |
138 | | - H160(arr) |
| 268 | + Address(H160(arr)) |
139 | 269 | }, |
140 | 270 | }) |
141 | 271 | } |
|
0 commit comments