Skip to content

Simple RLP (Recursive Length Prefix) - Encode the and decode data structures simple and fast

License

Notifications You must be signed in to change notification settings

SamuelHaidu/simple-rlp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple RLP (Recursive Length Prefix)

Efficiently encode and decode data structures with minimal dependencies

This module provides an alternative to the official Ethereum pyrlp library.

While pyrlp requires 5 dependencies, this alternative is written in pure Python and has no dependencies. It is recommended for projects that do not require the full suite of Ethereum tools. If your project already uses Ethereum tools, consider using pyrlp instead.

Features:

  • Straightforward encoding and decoding of lists containing various data types
  • Fast encoding process
  • Automatic serialization of Python objects (refer to supported types below)
  • Templates for converting bytes into decoded objects
  • Dependency-free implementation

Installation:

pip install simple-rlp 

Usage:

Encoding:
>>> import rlp
>>> my_list = ['python', 'rlp', 255]
>>> rlp.encode(my_list)
b'\xcd\x86python\x83rlp\x81\xff'
Decoding:
>>> import rlp
>>> my_list_encoded = b'\xcd\x86python\x83rlp\x81\xff'
>>> rlp.decode(my_list_encoded)
[b'python', b'rlp', b'\xff']

Use templates to decode and convert bytes to Python objects

Supported Types:
  • Signed integer
  • Unsigned integer
  • Boolean
  • Float
  • String
rlp.converters.UInteger # Unsigned integer
rlp.converters.SInteger # Signed integer
rlp.converters.Bool # Boolean
rlp.converters.Float # Float
rlp.converters.String # String

Both signed and unsigned integers use big-endian byte order by default. To use little-endian, modify the static attribute:

LittleEndianUInt = rlp.converters.UInteger
LittleEndianUInt.byteorder =  'little'

String uses UTF-8 encoding by default. To use a different encoding, modify the static attribute:

ASCIIString = rlp.converters.String
ASCIIString.encoding =  'ascii'
Template Usage:
>>> from rlp.converters import *
>>> import rlp

>>> my_list = ['rlp', 1024, 3.14159, True, b'\x08']
>>> my_list_template = [String, UInteger, Float, Bool, None]
>>> my_list_encoded = rlp.encode(my_list)
>>> rlp.decode(my_list_encoded, template=my_list_template)
['rlp', 1024, 3.141590118408203, True, b'\x08']

Note: Use a None object in the template to leave the corresponding element unchanged.

About

Simple RLP (Recursive Length Prefix) - Encode the and decode data structures simple and fast

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages