A Borsh library for Python 3.
Project description
Python Borsh Library
This is an unofficial Python library for serializing and deserializing data in the Borsh binary serialization format.
Borsh stands for Binary Object Representation Serializer for Hashing. It was originally implemented in Rust for use with the Near protocol with a focus on "consistency, safety, and speed." It is used for serializing data in a number of applications, including the Solana blockchain.
Installation
This library can be installed using pip:
pip install borsh-python
Usage
All Borsh byte streams have a schema that specifies name and type pairs for each of the values in the stream. The names of the available types are based on Rust and may be found on this page.
In this library, schemas may be declared as follows:
import borsh
from borsh import types
# create an example dict that we will serialize
example_dict = {
'w': 123,
'x': 30000,
'y': 'hello',
'z': [1, 2, 3, 4]
}
# define the schema for the dict
example_dict_schema = borsh.schema({
'w': types.u8,
'x': types.i16,
'y': types.string,
'z': types.dynamic_array(types.i8)
})
We can serialize example_dict
by calling the serialize
method and providing the schema:
serialized_bytes = borsh.serialize(example_dict_schema, example_dict)
print(serialized_bytes)
# b'{0u\x05\x00\x00\x00hello\x04\x00\x00\x00\x01\x02\x03\x04'
This byte string can be deserialized by calling the deserialize
method and providing the same schema:
deserialized_data = borsh.deserialize(example_dict_schema, serialized_bytes)
print(deserialized_data)
# {'w': 123, 'x': 30000, 'y': 'hello', 'z': [1, 2, 3, 4]}
Borsh data streams are often base64 encoded. This library can handle these streams when used in conjunction with the base64
library. For example:
import base64
import borsh
from borsh import types
base64_borsh_data = base64.b64decode('ezB1BQAAAGhlbGxvBAAAAAECAwQ=')
example_dict_schema = borsh.schema({
'w': types.u8,
'x': types.i16,
'y': types.string,
'z': types.dynamic_array(types.i8)
})
print(borsh.deserialize(example_dict_schema, base64_borsh_data))
# {'w': 123, 'x': 30000, 'y': 'hello', 'z': [1, 2, 3, 4]}
Structs
The Borsh format supports a struct
type which is similar in function to its counterparts in C-family languages. Consider that we are trying to wrap our example_dict
into a struct
. We may declare our data and schema as follows:
import borsh
from borsh import types
# define the data dict with our data wrapped in a struct called 'example'
example_struct_dict = {
'example': types.struct({
'w': 123,
'x': 30000,
'y': 'hello',
'z': [1, 2, 3, 4]
})
}
# define a schema for our new struct
example_struct_schema = {
'example': types.struct({
'w': types.u8,
'x': types.i16,
'y': types.string,
'z': types.dynamic_array(types.i8)
})
}
We may serialize and deserialize our struct
like this:
serialized_bytes = borsh.serialize(example_struct_schema, example_struct_dict)
example_struct = borsh.deserialize(example_struct_schema, serialized_bytes)['example']
print(example_struct['y'])
# hello
Type Mapping
This library supports the following Borsh types, each of which is mapped to a respective Python type during deserialization.
Borsh Type | Python Type |
---|---|
dynamic_array |
List[type] |
fixed_array |
List[type] |
f32 |
float |
f64 |
float |
hashmap |
dict{k_t: v_t} |
hashset |
Set[type] |
i8 |
int |
i16 |
int |
i32 |
int |
i64 |
int |
i128 |
int |
option(type) |
None or type |
string |
str |
struct |
_struct[dict] |
u8 |
int |
u16 |
int |
u32 |
int |
u64 |
int |
u128 |
int |
unit |
None |
Unimplemented Types
The following Borsh types are not yet implemented in this library:
Borsh Type |
---|
enum |
fields |
named_fields |
unnamed_fields |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file borsh-python-0.1.5.tar.gz
.
File metadata
- Download URL: borsh-python-0.1.5.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88e0d6e481518c362c81069a8ce784cd9b9f2b15b16afb5b201fc52b746ea15e |
|
MD5 | 00b890708b9f85db86543f5beaf79403 |
|
BLAKE2b-256 | bc974be99fea2fd0887a6d9d5bed027801b7531de89a2563c3ec6acadb548751 |