Python SCALE Codec Library for Polymesh
Project description
This project is forked from https://github.com/polkascan/py-scale-codec
Python Polymath SCALE Codec
Python Polymath SCALE Codec Library
Description
Most of the data that the Substrate RPCs output is encoded with the SCALE Codec. This codec is used by the Substrate nodes' internal runtime. In order to get to meaningful data this data will need to be decoded. The Python SCALE Codec Library will specialize in this task.
Documentation
https://polkascan.github.io/py-scale-codec/
Installation
pip install polymath-scalecodec
Examples
Decode a SCALE-encoded Compact<Balance>
RuntimeConfiguration().update_type_registry(load_type_registry_preset("default"))
RuntimeConfiguration().update_type_registry(load_type_registry_preset("kusama"))
obj = ScaleDecoder.get_decoder_class('Compact<Balance>', ScaleBytes("0x130080cd103d71bc22"))
obj.decode()
print(obj.value)
Encode to Compact<Balance>
RuntimeConfiguration().update_type_registry(load_type_registry_preset("default"))
obj = ScaleDecoder.get_decoder_class('Compact<Balance>')
scale_data = obj.encode(2503000000000000000)
print(scale_data)
Encode to Vec<Bytes>
RuntimeConfiguration().update_type_registry(load_type_registry_preset("default"))
value = ['test', 'vec']
obj = ScaleDecoder.get_decoder_class('Vec<Bytes>')
scale_data = obj.encode(value)
print(scale_data)
Add custom types to type registry
RuntimeConfiguration().update_type_registry(load_type_registry_preset("default"))
custom_types = {
"types": {
"MyCustomType": "u32",
"CustomNextAuthority": {
"type": "struct",
"type_mapping": [
["AuthorityId", "AuthorityId"],
["weight", "AuthorityWeight"]
]
}
}
}
RuntimeConfiguration().update_type_registry(custom_types)
Or from a custom JSON file
RuntimeConfiguration().update_type_registry(load_type_registry_preset("default"))
RuntimeConfiguration().update_type_registry(load_type_registry_file("/path/to/type_registry.json"))
Using the type registry updater in your application
To ensure the type registries are in sync with the current runtime of the blockchain, you can use the updater function in your application:
from scalecodec.updater import update_type_registries
# Update type registries with latest version from Github
try:
update_type_registries()
except Exception:
pass
License
https://github.com/PolymathNetwork/py-scale-codec/blob/master/LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for polymath-scalecodec-3.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e78c5587cdc78b85d47fe57504818314d6c8d1d45e77a63583322cf6a28983da |
|
MD5 | 0a3ad0e84dac0a39adfd67867ce30ef1 |
|
BLAKE2b-256 | 14027a7989828898e252ed8bc926cd5c07fab0f23332da04da4f0200237547a7 |
Hashes for polymath_scalecodec-3.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b0c718640b9dfe3a835d3d1335715f39eae33a64212af06405b1757a27caa63 |
|
MD5 | 9f17080206b04f722cf9838219d2cee0 |
|
BLAKE2b-256 | 3b82711bf68150507a6c24dbfe8b89569082918a88b2cce13a6c542c1a7c570c |