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
143
144
145
146
147
148
use std::fmt;
use std::str::FromStr;
use std::ops::Deref;
use base58::{ToBase58, FromBase58};
use crypto::checksum;
use network::Network;
use {DisplayLayout, Error, AddressHash};
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Type {
P2PKH,
P2SH,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Address {
pub kind: Type,
pub network: Network,
pub hash: AddressHash,
}
pub struct AddressDisplayLayout([u8; 25]);
impl Deref for AddressDisplayLayout {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DisplayLayout for Address {
type Target = AddressDisplayLayout;
fn layout(&self) -> Self::Target {
let mut result = [0u8; 25];
result[0] = match (self.network, self.kind) {
(Network::Mainnet, Type::P2PKH) => 0,
(Network::Mainnet, Type::P2SH) => 5,
(Network::Testnet, Type::P2PKH) => 111,
(Network::Testnet, Type::P2SH) => 196,
};
result[1..21].copy_from_slice(&*self.hash);
let cs = checksum(&result[0..21]);
result[21..25].copy_from_slice(&*cs);
AddressDisplayLayout(result)
}
fn from_layout(data: &[u8]) -> Result<Self, Error> where Self: Sized {
if data.len() != 25 {
return Err(Error::InvalidAddress);
}
let cs = checksum(&data[0..21]);
if &data[21..] != &*cs {
return Err(Error::InvalidChecksum);
}
let (network, kind) = match data[0] {
0 => (Network::Mainnet, Type::P2PKH),
5 => (Network::Mainnet, Type::P2SH),
111 => (Network::Testnet, Type::P2PKH),
196 => (Network::Testnet, Type::P2SH),
_ => return Err(Error::InvalidAddress),
};
let mut hash = AddressHash::default();
hash.copy_from_slice(&data[1..21]);
let address = Address {
kind: kind,
network: network,
hash: hash,
};
Ok(address)
}
}
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.layout().to_base58().fmt(f)
}
}
impl FromStr for Address {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> where Self: Sized {
let hex = try!(s.from_base58().map_err(|_| Error::InvalidAddress));
Address::from_layout(&hex)
}
}
impl From<&'static str> for Address {
fn from(s: &'static str) -> Self {
s.parse().unwrap()
}
}
#[cfg(test)]
mod tests {
use network::Network;
use super::{Address, Type};
#[test]
fn test_address_to_string() {
let address = Address {
kind: Type::P2PKH,
network: Network::Mainnet,
hash: "3f4aa1fedf1f54eeb03b759deadb36676b184911".into(),
};
assert_eq!("16meyfSoQV6twkAAxPe51RtMVz7PGRmWna".to_owned(), address.to_string());
}
#[test]
fn test_address_from_str() {
let address = Address {
kind: Type::P2PKH,
network: Network::Mainnet,
hash: "3f4aa1fedf1f54eeb03b759deadb36676b184911".into(),
};
assert_eq!(address, "16meyfSoQV6twkAAxPe51RtMVz7PGRmWna".into());
}
}