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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::convert::From;
use std::result::Result;
use std::error::Error;
use std::{fmt, str};
use std::str::FromStr;
use rust_base58::{ToBase58, FromBase58};
use rust_base58::base58::FromBase58Error;
#[cfg(feature = "json-types")]
use rustc_serialize::json;
pub const WALLET_ADDRESS_LEN: usize = 7;
#[derive(Eq, PartialEq, PartialOrd, Ord, Debug, Clone, Copy, RustcEncodable, RustcDecodable)]
pub struct WalletAddress {
address: [u8; WALLET_ADDRESS_LEN],
}
impl WalletAddress {
pub fn from_data(addr: [u8; WALLET_ADDRESS_LEN]) -> WalletAddress {
assert_eq!(addr[0],
0x00,
"the provided address is not a correct Fractal Global wallet address, its \
first byt should be 0x00");
WalletAddress { address: addr }
}
pub fn get_raw(&self) -> &[u8] {
&self.address
}
}
impl From<[u8; WALLET_ADDRESS_LEN]> for WalletAddress {
fn from(other: [u8; WALLET_ADDRESS_LEN]) -> WalletAddress {
WalletAddress { address: other }
}
}
impl FromStr for WalletAddress {
type Err = WalletAddressParseError;
fn from_str(s: &str) -> Result<WalletAddress, WalletAddressParseError> {
if &s[0..2] != "fr" {
return Err(WalletAddressParseError::new(s,
"the address does not start with \"fr\"",
None));
}
let bytes = match s[2..].from_base58() {
Ok(b) => b,
Err(FromBase58Error::InvalidBase58Byte(c, i)) => {
let new_error = FromBase58Error::InvalidBase58Byte(c, i + 2);
return Err(WalletAddressParseError::new(s,
&format!("the address is not a valid \
base-58 encoded string: {}",
new_error),
Some(new_error)));
}
};
if bytes[0] != 0x00 {
return Err(WalletAddressParseError::new(s,
"the first byte of the address is not 0x00",
None));
}
let mut checksum = [0u8; 2];
for byte in &bytes[..WALLET_ADDRESS_LEN] {
checksum[0] ^= *byte;
checksum[1] ^= checksum[0];
}
if checksum[0] != bytes[WALLET_ADDRESS_LEN] ||
checksum[1] != bytes[WALLET_ADDRESS_LEN + 1] {
Err(WalletAddressParseError::new(s, "checksum fail", None))
} else {
let mut address = [0u8; WALLET_ADDRESS_LEN];
address.clone_from_slice(&bytes[..WALLET_ADDRESS_LEN]);
Ok(WalletAddress::from_data(address))
}
}
}
#[cfg(feature = "json-types")]
impl json::ToJson for WalletAddress {
fn to_json(&self) -> json::Json {
json::Json::String(format!("{}", self))
}
}
impl fmt::Display for WalletAddress {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut arr = [0u8; WALLET_ADDRESS_LEN + 2];
arr[0..WALLET_ADDRESS_LEN].clone_from_slice(&self.address);
for byte in &self.address {
arr[WALLET_ADDRESS_LEN] ^= *byte;
arr[WALLET_ADDRESS_LEN + 1] ^= arr[WALLET_ADDRESS_LEN];
}
write!(f, "fr{}", arr.to_base58())
}
}
#[derive(Debug)]
pub struct WalletAddressParseError {
description: String,
cause: Option<FromBase58Error>,
}
impl WalletAddressParseError {
fn new<S: AsRef<str>>(wallet_address: S,
error: S,
cause: Option<FromBase58Error>)
-> WalletAddressParseError {
WalletAddressParseError {
description: format!("the wallet address {:?} is not a valid Fractal Global wallet \
address, {}",
wallet_address.as_ref(),
error.as_ref()),
cause: cause,
}
}
}
impl fmt::Display for WalletAddressParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description)
}
}
impl Error for WalletAddressParseError {
fn description(&self) -> &str {
&self.description
}
fn cause(&self) -> Option<&Error> {
None
}
}