simple_rlp/
encode.rs

1//! Simple RLP encoding library
2
3use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
4use near_sdk::serde::{Deserialize, Serialize};
5
6/// RLP Encoding Struct
7#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Default, Deserialize, Serialize)]
8#[serde(crate = "near_sdk::serde")]
9pub struct RlpEncode;
10
11impl RlpEncode {
12    /// Starting point for short u8
13    pub const LIST_SHORT_START: u8 = 0xc0;
14    /// Starting point for long u8
15    pub const LIST_LONG_START: u8 = 0xf7;
16
17    const MAX_UINT8: u8 = u8::MAX;
18    const MAX_UINT16: u16 = u16::MAX;
19    const MAX_UINT32: u32 = u32::MAX;
20    const MAX_UINT64: u64 = u64::MAX;
21    const MAX_UINT128: u128 = u128::MAX;
22
23    fn get_rlp_encoded(&self) -> Vec<u8> {
24        Self::try_to_vec(self).expect("Failed to serialize RlpEncode")
25    }
26
27    /// RLP-encode a byte string.
28    /// Returns the RLP encoded string in bytes.
29    pub fn encode_bytes(bytes: Self) -> Vec<u8> {
30        let encoded: Vec<u8>;
31        let rlp = bytes.get_rlp_encoded();
32        if rlp.len() == 1 && rlp[0] <= 128 {
33            encoded = rlp.clone();
34        } else {
35            encoded = Self::concat(&Self::encode_length(rlp.len(), 128), &rlp);
36        }
37        encoded
38    }
39
40    /// RLP-encode a list of RLP-encoded byte strings.
41    /// Returns the RLP encoded list of items in bytes.
42    pub fn encode_list(bytes: Vec<Self>) -> Vec<u8> {
43        let mut rlp_list: Vec<Vec<u8>> = vec![];
44        for entry in bytes {
45            let rlp = entry.get_rlp_encoded();
46            rlp_list.push(rlp);
47        }
48        let list = Self::flatten(rlp_list);
49        Self::concat(&Self::encode_length(list.len(), 192), &list)
50    }
51
52    /// RLP-encode a uint.
53    /// Returns the RLP encoded uint in bytes.
54    pub fn encode_uint(item: Self) -> Vec<u8> {
55        let rlp_encode = item.get_rlp_encoded();
56        let n_bytes = Self::bit_length(rlp_encode.len()) / 8 + 1;
57        let uint_bytes = Self::encode_uint_by_length(rlp_encode.len());
58        if n_bytes - uint_bytes.len() > 0 {}
59        Self::encode_bytes(item)
60    }
61
62    pub fn encode_int() -> Vec<u8> {
63        todo!()
64    }
65
66    pub fn encode_bool() -> Vec<u8> {
67        todo!()
68    }
69
70    fn encode_length(len: usize, offset: usize) -> Vec<u8> {
71        todo!()
72    }
73
74    fn to_binary(x: usize) -> Vec<u8> {
75        todo!()
76    }
77
78    fn memcpy(dest: usize, src: usize, len: usize) {
79        todo!()
80    }
81
82    fn flatten(list: Vec<Vec<u8>>) -> Vec<u8> {
83        todo!()
84    }
85
86    fn concat(before: &[u8], after: &[u8]) -> Vec<u8> {
87        todo!()
88    }
89
90    fn add_length(length: usize, is_long_list: bool) -> Vec<u8> {
91        todo!()
92    }
93
94    fn encode_uint_by_length(length: usize) -> Vec<u8> {
95        todo!()
96    }
97
98    fn bit_length(n: usize) -> usize {
99        todo!()
100    }
101}