aboutsummaryrefslogtreecommitdiff
path: root/src/transaction/rlp.rs
blob: beb07477efa3ad67f501a05918486696416e1087 (plain)
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
//! Tiny (and inefficient) RLP encoding implementation.

use ethnum::U256;

/// RLP encode a list.
pub fn list(items: &[&[u8]]) -> Vec<u8> {
    let total_len = items.iter().map(|item| item.len()).sum();
    let mut buf = len(total_len, 0xc0);
    for item in items {
        buf.extend_from_slice(item);
    }
    buf
}

/// RLP encodes an iterator as a list.
pub fn iter<U, I>(items: I) -> Vec<u8>
where
    U: AsRef<[u8]>,
    I: IntoIterator<Item = U>,
{
    let collected = items.into_iter().collect::<Vec<_>>();
    let items = collected.iter().map(U::as_ref).collect::<Vec<_>>();
    list(&items)
}

/// RLP encode some bytes.
pub fn bytes(bytes: &[u8]) -> Vec<u8> {
    match bytes {
        [x] if *x < 0x80 => vec![*x],
        _ => {
            let mut buf = len(bytes.len(), 0x80);
            buf.extend_from_slice(bytes);
            buf
        }
    }
}

/// RLP encode a length.
pub fn len(len: usize, offset: u8) -> Vec<u8> {
    if len < 56 {
        vec![len as u8 + offset]
    } else {
        let bl_buf = len.to_be_bytes();
        let bl = {
            let start = len.leading_zeros() / 8;
            &bl_buf[start as usize..]
        };
        let mut buf = vec![bl.len() as u8 + offset + 55];
        buf.extend_from_slice(bl);
        buf
    }
}

/// RLP encode a unsigned integer. This ensures that it is shortned to its
/// shortest little endian byte representation.
pub fn uint(value: U256) -> Vec<u8> {
    let start = value.leading_zeros() / 8;
    bytes(&value.to_be_bytes()[start as usize..])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn length_encoding() {
        assert_eq!(len(1024, 0x80), [0xb9, 0x04, 0x00]);
    }

    #[test]
    fn examples() {
        // RLP encoding examples taken from the Ethereum wiki
        // <https://eth.wiki/en/fundamentals/rlp>
        assert_eq!(bytes(b"dog"), b"\x83dog");
        assert_eq!(
            list(&[&bytes(b"cat"), &bytes(b"dog")]),
            b"\xc8\x83cat\x83dog"
        );
        assert_eq!(bytes(b""), [0x80]);
        assert_eq!(list(&[]), [0xc0]);
        assert_eq!(uint(U256::ZERO), [0x80]);
        assert_eq!(bytes(b"\0"), [0x00]);
        assert_eq!(uint(U256::new(15)), [0x0f]);
        assert_eq!(uint(U256::new(1024)), [0x82, 0x04, 0x00]);
        assert_eq!(
            list(&[
                &list(&[]),
                &list(&[&list(&[])]),
                &list(&[&list(&[]), &list(&[&list(&[])]),]),
            ]),
            [0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0],
        );
        assert_eq!(
            bytes(b"Lorem ipsum dolor sit amet, consectetur adipisicing elit"),
            b"\xb8\x38Lorem ipsum dolor sit amet, consectetur adipisicing elit"
        );
    }
}