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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use crate::rlp::RlpToken;
use crate::utils::bytes_to_hex_str;
use crate::utils::display_uint256_as_address;
use crate::utils::hex_str_to_bytes;
use crate::Error;
use num256::Uint256;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use sha3::{Digest, Keccak256};
use std::str;
use std::str::FromStr;
use std::{
    convert::TryFrom,
    fmt::{self, Display},
};

/// Representation of an Ethereum address.
///
/// Address is usually derived from a `PrivateKey`, or converted from its
/// textual representation.
#[derive(PartialEq, Clone, Copy, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Address([u8; 20]);

impl Address {
    /// Get raw bytes of the address.
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Creates an Address from a slice.
    ///
    /// This requires a slice to be exactly 20 bytes in length,
    pub fn from_slice(data: &[u8]) -> Result<Address, Error> {
        if data.len() != 20 {
            return Err(Error::InvalidAddressLength {
                got: data.len(),
                expected: 20,
            });
        }

        let mut result: [u8; 20] = Default::default();
        result.copy_from_slice(data);
        Ok(Address(result))
    }

    /// Attempts to decode an address from RLP data, with special case
    /// handling for the zero address case
    pub fn from_rlp_data(data: RlpToken) -> Result<Address, Error> {
        let address_data = &data.get_byte_content()?;
        match Address::from_slice(address_data) {
            Ok(v) => Ok(v),
            Err(e) => {
                // an empty address field means the zero address
                // anything in between 0 bytes and 20 bytes is an error
                if address_data.is_empty() {
                    Ok(Address::default())
                } else {
                    Err(e)
                }
            }
        }
    }

    // Parses and validates the address according to the EIP-55 standard
    pub fn parse_and_validate(input: &str) -> Result<Address, Error> {
        let address: Address = input.parse()?;
        let eip_55_encoded = address.to_string();
        if eip_55_encoded == input {
            Ok(address)
        } else {
            Err(Error::InvalidEip55)
        }
    }
}

impl Serialize for Address {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for Address {
    fn deserialize<D>(deserializer: D) -> Result<Address, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let s = match s.strip_prefix("0x") {
            Some(s) => s,
            None => &s,
        };

        hex_str_to_bytes(s)
            .and_then(move |bytes| Address::from_slice(&bytes))
            .map_err(serde::de::Error::custom)
    }
}

impl From<[u8; 20]> for Address {
    fn from(val: [u8; 20]) -> Address {
        Address(val)
    }
}

impl From<[u8; 32]> for Address {
    fn from(val: [u8; 32]) -> Address {
        let mut data: [u8; 20] = Default::default();
        data.copy_from_slice(&val[12..]);
        Address(data)
    }
}

#[allow(clippy::from_over_into)]
impl Into<[u8; 20]> for Address {
    fn into(self) -> [u8; 20] {
        self.0
    }
}

#[allow(clippy::from_over_into)]
impl Into<[u8; 32]> for Address {
    fn into(self) -> [u8; 32] {
        let mut data: [u8; 32] = Default::default();
        data[12..].copy_from_slice(self.as_bytes());
        data
    }
}

impl fmt::LowerHex for Address {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if f.alternate() {
            let res = write!(f, "0x");
            res?;
        }

        for hex_char in self.0.iter() {
            let res = write!(f, "{hex_char:x}");
            res?;
        }
        Ok(())
    }
}

impl fmt::UpperHex for Address {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if f.alternate() {
            let res = write!(f, "0x");
            res?;
        }

        for hex_char in self.0.iter() {
            let res = write!(f, "{hex_char:X}");
            res?;
        }
        Ok(())
    }
}

impl FromStr for Address {
    type Err = Error;

    /// Parses a string into a valid Ethereum address.
    ///
    /// # Supported formats
    ///
    /// * `0x` prefixed address
    /// * Raw bytes of an address represented by a bytes as an hexadecimal.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use std::str::FromStr;
    /// use clarity::Address;
    /// // Method 1
    /// Address::from_str("0x0102030405060708090a0b0c0d0e0f1011121314").unwrap();
    /// // Method 1 (without 0x prefix)
    /// Address::from_str("0102030405060708090a0b0c0d0e0f1011121314").unwrap();
    /// // Method 2
    /// let _address : Address = "14131211100f0e0d0c0b0a090807060504030201".parse().unwrap();
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            return Ok(Address::default());
        }
        let s = match s.strip_prefix("0x") {
            Some(s) => s,
            None => s,
        };

        if s.len() == 40 {
            let r = hex_str_to_bytes(s)?;
            Ok(Address::from_slice(&r)?)
        } else {
            Err(Error::InvalidAddressLength {
                got: s.len(),
                expected: 40,
            })
        }
    }
}

/// Gets the EIP-55 encoded version of an address passed in as bytes
fn eip_55_string(address_bytes: [u8; 20]) -> String {
    let hex_str = bytes_to_hex_str(&address_bytes);
    let hash = Keccak256::digest(hex_str.as_bytes());
    let mut capitalized_hex_str: Vec<char> = Vec::new();
    for (counter, character) in hex_str.chars().enumerate() {
        match character {
            'a'..='f' => {
                // this is the real doozy here we're indexing
                // into the array to mask against the correct byte
                let index = (4 * counter) / 8;
                let bit = if counter > 0 {
                    // the 4 * i th bit indexed from the right which is why
                    // we need the -1 yes this is a bit backwards to think about
                    ((4 * counter) - 1) % 8
                } else {
                    // represent the zeroth bit of the hash string which
                    // is seven indexed from the other direction
                    7
                };
                if hash[index] & 1 << bit != 0 {
                    capitalized_hex_str.push(character.to_ascii_uppercase());
                } else {
                    capitalized_hex_str.push(character);
                }
            }
            '0'..='9' => capitalized_hex_str.push(character),
            // impossible output from bytes to hex str
            _ => panic!(),
        }
    }
    capitalized_hex_str.iter().collect()
}

impl Display for Address {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "0x{}", eip_55_string(self.0))
    }
}

impl fmt::Debug for Address {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "0x{}", eip_55_string(self.0))
    }
}

impl TryFrom<Uint256> for Address {
    type Error = Error;

    fn try_from(value: Uint256) -> Result<Self, Self::Error> {
        let string = display_uint256_as_address(value);
        string.parse()
    }
}

#[test]
#[should_panic]
fn decode_invalid_length() {
    "123".parse::<Address>().unwrap();
}

#[test]
#[should_panic]
fn decode_invalid_character() {
    "\u{012345}123456789012345678901234567890123456"
        .parse::<Address>()
        .unwrap();
}

#[test]
fn decode() {
    let address: Address = "1234567890123456789012345678901234567890"
        .parse::<Address>()
        .unwrap();

    assert_eq!(
        address,
        Address::from([
            0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78,
            0x90, 0x12, 0x34, 0x56, 0x78, 0x90
        ])
    );
}

#[test]
fn serialize_null_address() {
    let address = Address::default();
    let s = serde_json::to_string(&address).unwrap();
    assert_eq!(s, r#""0x0000000000000000000000000000000000000000""#);
    let recovered_addr: Address = serde_json::from_str(&s).unwrap();
    assert_eq!(address, recovered_addr);
}

#[test]
fn serialize_padded_address() {
    let raw_address = "00000000000000000000000000000000000000C0";
    let address: Address = raw_address.parse().unwrap();
    assert_eq!(
        serde_json::to_string(&address).unwrap(),
        format!(r#""0x{raw_address}""#)
    );
}

#[test]
#[should_panic]
fn address_less_than_20_filler() {
    // Data found in AddressLessThan20Filler.json
    let _address: Address = "0b9331677e6ebf".parse().unwrap();
}

#[test]
fn handle_prefixed() {
    let address: Address = "0x000000000000000000000000000b9331677e6ebf"
        .parse()
        .unwrap();
    assert_eq!(
        address,
        Address::from([
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0b, 0x93, 0x31, 0x67, 0x7e, 0x6e, 0xbf
        ])
    );
}

#[test]
fn hashed() {
    // One of the use cases for Address could be a key in a HashMap to store some
    // additional values per address.
    use std::collections::HashMap;
    let a = Address::from_str("0x000000000000000000000000000b9331677e6ebf").unwrap();
    let b = Address::from_str("0x00000000000000000000000000000000deadbeef").unwrap();
    let mut map = HashMap::new();
    map.insert(a, "Foo");
    map.insert(b, "Bar");

    assert_eq!(&map[&a], &"Foo");
    assert_eq!(&map[&b], &"Bar");
}

#[test]
fn ordered() {
    let a = Address::from_str("0x000000000000000000000000000000000000000a").unwrap();
    let b = Address::from_str("0x000000000000000000000000000000000000000b").unwrap();
    let c = Address::from_str("0x000000000000000000000000000000000000000c").unwrap();
    assert!(c > b);
    assert!(b > a);
    assert!(b < c);
    assert!(a < c);
    assert_ne!(a, b);
    assert_ne!(b, c);
    assert_ne!(a, c);
}

#[test]
fn to_hex() {
    let address: Address = "1234567890123456789ABCDEF678901234567890"
        .parse::<Address>()
        .unwrap();

    assert_eq!(
        format!("{address:x}"),
        "1234567890123456789abcdef678901234567890",
    );
    assert_eq!(
        format!("{address:#x}"),
        "0x1234567890123456789abcdef678901234567890",
    );
    assert_eq!(
        format!("{address:#X}"),
        "0x1234567890123456789ABCDEF678901234567890",
    );
}

#[test]
fn eip_55_validate() {
    // testcases taken from here https://eips.ethereum.org/EIPS/eip-55
    let eip_55_testcases = [
        "0x52908400098527886E0F7030069857D2E4169EE7",
        "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
        "0xde709f2102306220921060314715629080e2fb77",
        "0x27b1fdb04752bbc536007a920d24acb045561c26",
        "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
        "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
        "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
        "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
    ];
    let eip_55_invalid = [
        "0x52908400098527886E0F7030069857D2E4169eE7",
        "0x8617E340b3D01FA5F11F306F4090FD50E238070D",
        "0xde709f2102306220921060314715629080e2fB77",
        "0x27b1fDb04752bbc536007a920d24acb045561c26",
        "0x5aaeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
        "0xFB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
        "0xdbF03B407c01e7cD3CBea99509d93f8DDDC8C6FB",
        "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDB",
    ];
    for starting_address in eip_55_testcases.iter() {
        let unvalidated: Address = starting_address.parse().unwrap();
        let failure_message =
            format!("Failed to validate address theirs: {starting_address} ours: {unvalidated} !");
        let _address: Address =
            Address::parse_and_validate(starting_address).expect(&failure_message);
    }
    for starting_address in eip_55_invalid.iter() {
        assert!(Address::parse_and_validate(starting_address).is_err())
    }
}

#[test]
fn eip_55_display() {
    // testcases taken from here https://eips.ethereum.org/EIPS/eip-55
    let eip_55_testcases = [
        "0x52908400098527886E0F7030069857D2E4169EE7",
        "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
        "0xde709f2102306220921060314715629080e2fb77",
        "0x27b1fdb04752bbc536007a920d24acb045561c26",
        "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
        "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
        "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
        "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
    ];
    for starting_address in eip_55_testcases.iter() {
        // this also checks that parse still functions properly with
        // eip invalid but otherwise correct addresses
        let unvalidated: Address = starting_address.parse().unwrap();
        assert_eq!(format!("{unvalidated}"), **starting_address)
    }
}