Skip to content

Commit d958999

Browse files
author
Jong-Shian Wu
committed
Initial commit
0 parents  commit d958999

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BSD 2-Clause License
2+
3+
Copyright (c) 2018, chelpis
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pure Python implementation of RLP encoding utilities for CPython 3.6+

rlp.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
def RLP(x):
2+
assert _is_RLP_serializable(x)
3+
if type(x) is int and x >= 0:
4+
return RLP(BE(x))
5+
elif type(x) is bytes:
6+
return RLP_B(x)
7+
else:
8+
return RLP_L(x)
9+
10+
11+
def _is_RLP_serializable(value):
12+
return (
13+
(type(value) is int and value >= 0)
14+
or type(value) is bytes
15+
or (
16+
type(value) is tuple
17+
and all(_is_RLP_serializable(v) for v in value)
18+
)
19+
)
20+
21+
22+
def BE(n):
23+
assert type(n) is int and n >= 0
24+
byte_length = (n.bit_length() - 1) // 8 + 1
25+
return n.to_bytes(byte_length, "big")
26+
27+
28+
def RLP_B(x):
29+
assert type(x) is bytes
30+
if len(x) == 1 and x[0] < 128:
31+
return x
32+
# 0 <= head <= 127
33+
elif len(x) < 56:
34+
return bytes([128 + len(x)]) + x
35+
# 0 <= len(x) <= 55
36+
# 128 <= head <= 183
37+
else:
38+
# 56 <= len(x) <= ...
39+
# 1 <= len(BE(len(x))) <= 8
40+
# 184 <= head <= 191
41+
return bytes([183 + len(BE(len(x)))]) + BE(len(x)) + x
42+
43+
44+
def RLP_L(l):
45+
assert type(l) is tuple
46+
s = b"".join(RLP(e) for e in l)
47+
if len(s) < 56:
48+
# 0 <= len(s) <= 55
49+
# 192 <= head <= 247
50+
return bytes([192 + len(s)]) + s
51+
else:
52+
# 56 <= len(s) <= ...
53+
# 1 <= len(BE(len(s))) <= 9
54+
# 248 <= head <= 255
55+
return bytes([247 + len(BE(len(s)))]) + BE(len(s)) + s

0 commit comments

Comments
 (0)