Package: borsh-python
Python Borsh LibraryThis is an unofficial Python library for serializing and deserializing data in theBorshbinary serialization format.Borsh stands forBinary Object Representation Serializer for Hashing.It was originally implemented in Rust for use with theNear protocolwith a focus on "consistency, safety, and speed." It is used for serializing data in a number of applications, including theSolanablockchain.InstallationThis library can be installed using pip:pip install borsh-pythonUsageAll Borsh byte streams have aschemathat 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 foundon this page.In this library, schemas may be declared as follows:importborshfromborshimporttypes# create an example dict that we will serializeexample_dict={'w':123,'x':30000,'y':'hello','z':[1,2,3,4]}# define the schema for the dictexample_dict_schema=borsh.schema({'w':types.u8,'x':types.i16,'y':types.string,'z':types.dynamic_array(types.i8)})We can serializeexample_dictby calling theserializemethod 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 thedeserializemethod 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 thebase64library. For example:importbase64importborshfromborshimporttypesbase64_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]}StructsThe Borsh format supports astructtype which is similar in function to its counterparts in C-family languages. Consider that we are trying to wrap ourexample_dictinto astruct. We may declare our data and schema as follows:importborshfromborshimporttypes# 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 structexample_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 ourstructlike 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'])# helloType MappingThis library supports the following Borsh types, each of which is mapped to a respective Python type during deserialization.Borsh TypePython Typedynamic_arrayList[type]fixed_arrayList[type]f32floatf64floathashmapdict{k_t: v_t}hashsetSet[type]i8inti16inti32inti64inti128intoption(type)Noneortypestringstrstruct_struct[dict]u8intu16intu32intu64intu128intunitNoneUnimplemented TypesThe following Borsh types are not yet implemented in this library:Borsh Typeenumfieldsnamed_fieldsunnamed_fields