Skip to main content

Python SCALE Codec Library

Project description

Python SCALE Codec

Build Status Latest Version Supported Python versions License

Description

Substrate uses a lightweight and efficient encoding and decoding program to optimize how data is sent and received over the network. The program used to serialize and deserialize data is called the SCALE codec, with SCALE being an acronym for Simple Concatenated Aggregate Little-Endian.

Documentation

https://polkascan.github.io/py-scale-codec/

Installation

pip install scalecodec

Code examples

from scalecodec.types import ScaleBytes, Bool, String, U32, U8, U16, Struct, Vec, Compact, Tuple, Enum

# encode a Vec<u16>
obj = Vec(U16).new()
value = [1, 2]
data = obj.encode(value)

# Define and decode a Struct
scale_obj = Struct(test=U8, test2=Tuple(U8, U8)).new()
value = scale_obj.decode(ScaleBytes("0x020105"))

# Define and encode an Enum
scale_obj = Enum(
    Bool=Bool(), 
    Number=U32, 
    Complex=Struct(data=String(), version=Compact(U8)), 
    None_=None
).new()
value = {'Bool': True}

data = scale_obj.encode(value)

Examples of different types

Type Description Example SCALE decoding value SCALE encoded value
bool Boolean values are encoded using the least significant bit of a single byte. True 0x01
u16 Basic integers are encoded using a fixed-width little-endian (LE) format. 42 0x2a00
Compact A "compact" or general integer encoding is sufficient for encoding large integers (up to 2**536) and is more efficient at encoding most values than the fixed-width version. (Though for single-byte values, the fixed-width integer is never worse.) 0 0x00
1 0x04
42 0xa8
69 0x1501
100000000000000 0x0b00407a10f35a
Vec A collection of same-typed values is encoded, prefixed with a compact encoding of the number of items, followed by each item's encoding concatenated in turn. [4, 8, 15, 16, 23, 42] 0x18040008000f00100017002a00
BitVec A sequence of bools, represented in a more space efficient bit format 0b00000010_01111101 0x287d02
str,Bytes, String Strings are Vectors of bytes (Vec<u8>) containing a valid UTF8 sequence. "Test" 0x1054657374
b"Test" 0x1054657374
[84, 101, 115, 116] 0x1054657374
[u8; 4] Fixed sized array of in this case an u8 b"babe" 0x62616265
"0x62616265" 0x62616265
[98, 97, 98, 101] 0x62616265
AccountId An SS58 formatted representation of an account. See also the SS58 util functions "5GDyPHLVHcQYPTWfygtPY eogQjyZy7J9fsi4brPhgEFq4pcv" 0xb80269ec500e458a630846b99105c397 ee574125823d6f4388e9c7572e115c05
Enum Example: enum IntOrBool { Int(u8), Bool(bool),} A fixed number of variants, each mutually exclusive and potentially implying a further value or series of values. Encoded as the first byte identifying the index of the variant that the value is. Any further bytes are used to encode any data that the variant implies. Thus, no more than 256 variants are supported. {'Int': 8} 0x002a
{'Bool': True} 0x0101
Struct Example: struct Motion { pub votes: Vec<AccountId>, pub id: u32 } For structures, the values are named, but that is irrelevant for the encoding (names are ignored - only order matters). All containers store elements consecutively. The order of the elements is not fixed, depends on the container, and cannot be relied on at decoding. This implicitly means that decoding some byte-array into a specified structure that enforces an order and then re-encoding it could result in a different byte array than the original that was decoded. {"votes": ["5GDyPHLVHcQYPTWfygtPYeo gQjyZy7J9fsi4brPhgEFq4pcv"], "id": 4} 0x04b80269ec500e458a630846b99105c397ee57 4125823d6f4388e9c7572e115c0504000000

License

https://github.com/polkascan/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

scalecodec-2.0.0a1.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

scalecodec-2.0.0a1-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file scalecodec-2.0.0a1.tar.gz.

File metadata

  • Download URL: scalecodec-2.0.0a1.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for scalecodec-2.0.0a1.tar.gz
Algorithm Hash digest
SHA256 9cd1ec6008fb6658b42b22a2b90299791ece036eaf11945da8ce99198ee753a8
MD5 f2c06ff0def30e9de542d4e356fb6fd5
BLAKE2b-256 9310c08787d26bcbc4d54b921272579b12bf2c7820e091447e7e0794b55db264

See more details on using hashes here.

File details

Details for the file scalecodec-2.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: scalecodec-2.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for scalecodec-2.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ed2c12c2e8cf5a1bc145654a7bef600fb7e85ca5cf069f29c3d5f1c7c501ade
MD5 ace7d0c5cde84cff4b44ac6c145fe6de
BLAKE2b-256 04be8dbdfb2ff63f45afbc4c9ee68758e6276afab8fbf6f487e86bb64749fec5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page