1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::fmt;
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[repr(C)]
pub struct Address(pub [u8; 20]);
impl Address {
pub unsafe fn from_raw(bytes: *const u8) -> Self {
let mut addr = Self::default();
addr.0
.copy_from_slice(std::slice::from_raw_parts(bytes, 20));
addr
}
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
pub const fn size() -> usize {
std::mem::size_of::<Self>()
}
pub fn path_repr(&self) -> std::path::PathBuf {
std::path::PathBuf::from(hex::encode(self))
}
pub fn zero() -> Self {
Self::default()
}
}
impl AsRef<[u8]> for Address {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl std::str::FromStr for Address {
type Err = hex::FromHexError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes: Vec<u8> = hex::decode(s)?;
if bytes.len() != Address::size() {
return Err(hex::FromHexError::InvalidStringLength);
}
let mut addr = Self::default();
addr.0.copy_from_slice(&bytes);
Ok(addr)
}
}
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{}", hex::encode(self.0))
}
}
impl fmt::LowerHex for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&hex::encode(self.0))
}
}
#[cfg(feature = "serde")]
const _IMPL_SERDE_FOR_ADDRESS: () = {
impl borsh::BorshSerialize for Address {
fn serialize<W: std::io::Write>(&self, writer: &mut W) -> Result<(), std::io::Error> {
writer.write_all(&self.0)
}
}
impl borsh::BorshDeserialize for Address {
fn deserialize<R: std::io::Read>(reader: &mut R) -> Result<Self, std::io::Error> {
let mut addr = Address::default();
reader.read_exact(&mut addr.0)?;
Ok(addr)
}
}
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn convert_str() {
use std::str::FromStr;
let addr = Address([
96, 255, 103, 244, 45, 95, 214, 205, 158, 83, 176, 57, 114, 69, 94, 82, 182, 223, 75,
28,
]);
let addr_str = "60ff67f42d5fd6cd9e53b03972455e52b6df4b1c";
assert_eq!(&addr.path_repr(), std::path::Path::new(addr_str));
assert_eq!(&format!("{:x}", addr), addr_str);
assert_eq!(format!("{}", addr), format!("0x{}", addr_str));
assert_eq!(Address::from_str(addr_str).unwrap(), addr);
assert!(Address::from_str(&addr_str[1..]).is_err());
assert!(Address::from_str(&format!("{}ab", addr_str)).is_err());
assert!(Address::from_str("zz").is_err());
}
#[test]
fn convert_raw() {
let addr = Address([
96, 255, 103, 244, 45, 95, 214, 205, 158, 83, 176, 57, 114, 69, 94, 82, 182, 223, 75,
28,
]);
assert_eq!(unsafe { Address::from_raw(addr.as_ptr()) }, addr);
}
}
#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use super::*;
use borsh::{BorshDeserialize as _, BorshSerialize as _};
#[test]
fn roundtrip_serialize_address() {
let bytes = [1u8; 20];
let addr = Address::try_from_slice(&Address(bytes).try_to_vec().unwrap()).unwrap();
assert_eq!(addr.0, bytes);
}
#[test]
#[should_panic]
fn fail_deserialize_address_short() {
let bytes = [1u8; 19];
Address::try_from_slice(&bytes.try_to_vec().unwrap()).unwrap();
}
#[test]
#[should_panic]
fn fail_deserialize_address_long() {
let bytes = [1u8; 21];
Address::try_from_slice(&bytes.as_ref().try_to_vec().unwrap()).unwrap();
}
}